Skip to content

Commit

Permalink
feat(cli): add option to define ignored folders
Browse files Browse the repository at this point in the history
-i, --ignore will overwrite the default ignored folders and
lets the user choose which folders to crawl
  • Loading branch information
tim-richter committed Aug 9, 2024
1 parent 6dc4dc8 commit 46d8137
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ spinne scan -o console

- `-d, --directory <path>`: scan from a different directory
- `-o, --output <format>`: define the output format
- `-i, --ignore <path>`: define ignored folders. this is set to a reasonable default, but if
you need more control over the scanned `.tsx` files, you might need to use this. paths will gets matched
against all subpaths. You can define multiple via `-i fixtures dist`, or `-i fixtures -i dist`
4 changes: 2 additions & 2 deletions src/features/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export function scanAST({ code, filePath }: ScanArgs): ScanResult {
return report;
}

export const makeReport = async (cwd: string) => {
const files = scan(cwd);
export const makeReport = async (cwd: string, ignore: Array<string>) => {
const files = scan(cwd, ignore);
getLogger().info(`Found ${files.length} files`);

const report: Array<ScanResult> = [];
Expand Down
14 changes: 9 additions & 5 deletions src/features/report/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import { fdir } from 'fdir';

const DEFAULT_GLOBS = ['**/!(*.test|*.spec).@(tsx)'];

export const scan = (crawlFrom: string) => {
export const scan = (crawlFrom: string, ignore: Array<string>) => {
const globs = DEFAULT_GLOBS;

// eslint-disable-next-line new-cap
const files = new fdir()
.glob(...globs)
.withRelativePaths()
.exclude(
(dirName) =>
.exclude((dirName) => {
if (ignore && ignore.length > 0)
return ignore.some((val) => dirName.startsWith(val));

return (
dirName.startsWith('node_modules') ||
dirName.startsWith('.') ||
dirName.startsWith('dist'),
)
dirName.startsWith('dist')
);
})
.crawl(crawlFrom)
.sync();

Expand Down
2 changes: 1 addition & 1 deletion src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const scan = async (
) => {
try {
initLogger('info', options.output !== 'console');
const report = await makeReport(options.directory);
const report = await makeReport(options.directory, options.ignore);

if (options.output === 'file') {
await fs.writeJSON(join(options.directory, 'scan-data.json'), report);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Return = {
output: 'file' | 'console';
d: string;
directory: string;
ignore: Array<string>;
i: Array<string>;
};
};

Expand All @@ -30,6 +32,12 @@ export const createProgram = (): Return => {
choices: ['file', 'console'],
default: 'file',
},
ignore: {
alias: 'i',
describe: 'Ignore folders/files',
type: 'array',
default: [],
}
},
handler: (_) => {},
};
Expand Down
9 changes: 9 additions & 0 deletions test/integration/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ it('should output directly to console if output=console was used', async () => {
`"[{"components":[{"name":"Button","importInfo":{"imported":"Button","local":"Button","moduleName":"my-library","importType":"ImportSpecifier"},"props":[{"name":"variant","value":"blue"}],"propsSpread":false,"location":{"start":{"line":6,"column":7},"end":{"line":6,"column":13}}},{"name":"Button","importInfo":{"imported":"Button","local":"Button","moduleName":"my-library","importType":"ImportSpecifier"},"props":[{"name":"variant","value":"blue"}],"propsSpread":true,"location":{"start":{"line":7,"column":7},"end":{"line":7,"column":13}}}],"filePath":"fixtures/simple/src/Button.tsx"}]"`,
);
});

it('should allow to ignore additional directories', async () => {
const { stderr, exitCode } = await execa({
reject: false,
})`node ${cli} scan -o console -i fixtures`;

expect(exitCode).toEqual(1);
expect(stderr).toMatchInlineSnapshot(`"No files found to scan"`);
});

0 comments on commit 46d8137

Please sign in to comment.