Skip to content

Commit 788561e

Browse files
authored
feat: add --git-changed option to pass the changed files according to git (#142)
1 parent 025de54 commit 788561e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

bin/cli.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @fileoverview The CLI that is executed from a terminal.
44
* Acts as an interface to the JS API
55
*/
6+
import {exec} from 'node:child_process';
67
import path from 'node:path';
78
import process from 'node:process';
89
import {glob} from 'glob';
@@ -54,7 +55,8 @@ const cli = meow(
5455
${chalk.bold('--debug, -d')} Show debug logs
5556
${chalk.bold('--ci, -C')} Only output to stdout once, when linting is finished
5657
${chalk.bold('--stdin')} Read an SVG from stdin
57-
${chalk.bold('--summary')} Print the summary at the end (default)`,
58+
${chalk.bold('--summary')} Print the summary at the end (default)
59+
${chalk.bold('--git-changed')} Only lint files that have changed according to git`,
5860
{
5961
importMeta: import.meta,
6062
flags: {
@@ -73,6 +75,33 @@ process.on('exit', () => {
7375
gui.finish();
7476
});
7577

78+
/**
79+
* Load files based on input and/or git changed files
80+
* @param {string} input CLI input files
81+
* @param {boolean} gitChanged Whether to load changed files from git
82+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths
83+
*/
84+
const loadInputFiles = (input, gitChanged) =>
85+
new Promise((resolve, reject) => {
86+
const files = input ?? [];
87+
if (gitChanged) {
88+
exec(
89+
"git diff --cached --name-only --diff-filter=ACM '**/*.svg'",
90+
(error, stdout) => {
91+
if (error) {
92+
reject(error);
93+
return;
94+
}
95+
96+
const allFiles = [...files, ...stdout.split('\n').filter(Boolean)];
97+
resolve(allFiles);
98+
},
99+
);
100+
} else {
101+
resolve(files);
102+
}
103+
});
104+
76105
/** CLI main function */
77106
// eslint-disable-next-line unicorn/prefer-top-level-await
78107
(async function () {
@@ -145,7 +174,8 @@ process.on('exit', () => {
145174
// Lint all the CLI specified files
146175
const ignore = configObject.ignore || [];
147176
delete configObject.ignore; // Remove ignore from config to avoid passing it to SVGLint
148-
const files = cli.input
177+
const inputFiles = await loadInputFiles(cli.input, cli.flags.gitChanged);
178+
const files = inputFiles
149179
.flatMap((v) => glob.sync(v, {ignore}))
150180
.map((v) => path.resolve(process.cwd(), v));
151181

0 commit comments

Comments
 (0)