Skip to content

Commit 248febe

Browse files
authored
feat(cli): add --filter option for filtering files (#66)
1 parent 6006d07 commit 248febe

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

packages/cli/index.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class Project {
5555

5656
async init(
5757
// @ts-expect-error
58-
clack: typeof import('@clack/prompts')
58+
clack: typeof import('@clack/prompts'),
59+
filesFilter: Set<string>
5960
) {
6061
this.configFile = ts.findConfigFile(path.dirname(this.tsconfig), ts.sys.fileExists, 'tsslint.config.ts');
6162

@@ -98,7 +99,18 @@ class Project {
9899
return this;
99100
}
100101

101-
clack.log.info(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${gray(`(${this.fileNames.length})`)}`);
102+
const originalFileNamesLength = this.fileNames.length;
103+
104+
if (filesFilter.size) {
105+
this.fileNames = this.fileNames.filter(f => filesFilter.has(f));
106+
if (!this.fileNames.length) {
107+
clack.log.warn(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${gray('(No files left after filter)')}`);
108+
return this;
109+
}
110+
}
111+
112+
const filteredLengthDiff = originalFileNamesLength - this.fileNames.length;
113+
clack.log.info(`${label} ${path.relative(process.cwd(), this.tsconfig)} ${gray(`(${this.fileNames.length}${filteredLengthDiff ? `, skipped ${filteredLengthDiff}` : ''})`)}`);
102114

103115
if (!process.argv.includes('--force')) {
104116
this.cache = cache.loadCache(this.tsconfig, this.configFile, ts.sys.createHash);
@@ -303,8 +315,31 @@ class Project {
303315
}
304316
}
305317

318+
// get filter glob option
319+
let filterSet: Set<string> = new Set();
320+
321+
const filterArgIndex = process.argv.findIndex(arg => arg === '--filter');
322+
if (filterArgIndex !== -1) {
323+
for (let i = filterArgIndex + 1; i < process.argv.length; i++) {
324+
const filterGlob = process.argv[i];
325+
if (!filterGlob || filterGlob.startsWith('-')) {
326+
clack.log.error(red(`Missing argument for --filter.`));
327+
process.exit(1);
328+
}
329+
330+
const fileNames = glob.sync(filterGlob).map(f => path.resolve(f));
331+
for (const fileName of fileNames)
332+
filterSet.add(fileName);
333+
}
334+
335+
if (!filterSet.size) {
336+
clack.log.error(red(`No files found after --filter files.`));
337+
process.exit(1);
338+
}
339+
}
340+
306341
for (const [tsconfig, languages] of tsconfigAndLanguages) {
307-
projects.push(await new Project(tsconfig, languages).init(clack));
342+
projects.push(await new Project(tsconfig, languages).init(clack, filterSet));
308343
}
309344

310345
spinner?.start();

0 commit comments

Comments
 (0)