diff --git a/README.md b/README.md index 96fd777..00763a2 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ OPTIONS -h, --help Output usage information -w, --write Restructure and edit folders and files -S, --avoid-single-file Flag to indicate if single files in folders should be avoided + --ignore [path] Ignore file(s) / folder(s) by a file path or a glob pattern. ``` Dry run which will output what the resulting file structure will look like: diff --git a/src/index.ts b/src/index.ts index 588798c..580e56e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ const { argv } = process; export type Config = { help: boolean; include: string[]; + ignore: string[]; version: boolean; write: boolean; avoidSingleFile: boolean; @@ -22,6 +23,7 @@ export type Config = { const defaultConfig: Config = { help: false, include: [], + ignore: [], version: false, write: false, avoidSingleFile: false, @@ -47,6 +49,11 @@ const printHelp = (exitCode: number) => { description: "Flag to indicate if single files in folders should be avoided", }, + { + flags: ["--ignore [path]"], + description: + "Ignore file(s) / folder(s) by a file path or a glob pattern", + }, ]); return process.exit(exitCode); @@ -77,6 +84,11 @@ const parseArgs = (args: string[]) => { case "--avoid-single-file": cliConfig.avoidSingleFile = true; break; + case "--ignore": + const nextOptionIdx = args.findIndex(x => x.startsWith("-")); + + cliConfig.ignore = [...(cliConfig.ignore ?? []), ...(args.splice(0, nextOptionIdx) ?? [])]; + break; default: { if (fs.existsSync(arg) || glob.hasMagic(arg)) { cliConfig.include = [...(cliConfig.include ?? []), arg]; @@ -107,7 +119,10 @@ export const run = async (args: string[]) => { if (mergedConfig.version) return printVersion(); if (mergedConfig.include.length === 0) return printHelp(1); - const restructureMap = getRestructureMap(mergedConfig.include); + const restructureMap = getRestructureMap( + mergedConfig.include, + mergedConfig.ignore + ); const filesToEdit = Object.values(restructureMap).flat(); if (filesToEdit.length === 0) { diff --git a/src/index/getFilePaths.ts b/src/index/getFilePaths.ts index 0a04fe5..acb365f 100644 --- a/src/index/getFilePaths.ts +++ b/src/index/getFilePaths.ts @@ -6,8 +6,8 @@ import logger from "../shared/logger"; const isDirectory = (filePath: string) => fs.lstatSync(filePath).isDirectory(); const isFile = (filePath: string) => fs.lstatSync(filePath).isFile(); -export const globSearch = (pattern: string) => { - const matches = glob.sync(pattern); +export const globSearch = (pattern: string, ignore: string[]) => { + const matches = glob.sync(pattern, { ignore }); const files = matches.filter(match => isFile(match)); if (files.length === 0) { @@ -17,7 +17,7 @@ export const globSearch = (pattern: string) => { return files; }; -export const getFilePaths = (rootPath: string) => { +export const getFilePaths = (rootPath: string, ignore: string[]) => { const filePaths: string[] = []; const paths = [rootPath]; @@ -27,7 +27,7 @@ export const getFilePaths = (rootPath: string) => { const isGlobPattern = glob.hasMagic(filePath); if (isGlobPattern) { - filePaths.push(...globSearch(filePath)); + filePaths.push(...globSearch(filePath, ignore)); continue; } @@ -50,11 +50,11 @@ export const getFilePaths = (rootPath: string) => { }; /** Get a restructure map with rootPath keys and filePaths values. */ -export const getRestructureMap = (rootPaths: string[]) => +export const getRestructureMap = (rootPaths: string[], ignore: string[]) => rootPaths.reduce<{ [key: string]: string[] }>( (acc, rootPath) => ({ ...acc, - [rootPath]: getFilePaths(rootPath), + [rootPath]: getFilePaths(rootPath, ignore), }), {} );