From 5d48340592df5fad8a02e901d7c87915f9933747 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Fri, 13 Mar 2020 16:50:17 -0400 Subject: [PATCH 1/4] feat: add --ignore property/flag --- README.md | 1 + src/index.ts | 11 ++++++++++- src/index/getFilePaths.ts | 12 ++++++------ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 96fd777..c187603 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} .gitignore like file path expressions to ignore ``` Dry run which will output what the resulting file structure will look like: diff --git a/src/index.ts b/src/index.ts index 66e798e..b0e981e 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, @@ -44,6 +46,7 @@ const printHelp = (exitCode: number) => { -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} .gitignore like file path expressions to ignore ` ); @@ -75,6 +78,9 @@ const parseArgs = (args: string[]) => { case "--avoid-single-file": cliConfig.avoidSingleFile = true; break; + case "--ignore": + cliConfig.ignore = [...(cliConfig.ignore ?? []), args.shift() ?? ""]; + break; default: { if (fs.existsSync(arg) || glob.hasMagic(arg)) { cliConfig.include = [...(cliConfig.include ?? []), arg]; @@ -105,7 +111,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), }), {} ); From b47a51d180a62caa56c3eb982b4c07673fd359ed Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sun, 15 Mar 2020 14:48:04 -0400 Subject: [PATCH 2/4] Update README.md Co-Authored-By: Anatole Lucet <35486736+AnatoleLucet@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c187603..00763a2 100644 --- a/README.md +++ b/README.md @@ -68,7 +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} .gitignore like file path expressions to ignore + --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: From 69110200543d39c548e2b8edd428f530cbd2eb23 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sun, 15 Mar 2020 14:48:17 -0400 Subject: [PATCH 3/4] Update src/index.ts Co-Authored-By: Anatole Lucet <35486736+AnatoleLucet@users.noreply.github.com> --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b0e981e..8b1aa8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,7 @@ const printHelp = (exitCode: number) => { -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} .gitignore like file path expressions to ignore + --ignore [path] Ignore file(s) / folder(s) by a file path or a glob pattern. ` ); From 533ced91323ec0fffdd1f78bf2cbf98c5d8ba66e Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sun, 15 Mar 2020 14:48:24 -0400 Subject: [PATCH 4/4] Update src/index.ts Co-Authored-By: Anatole Lucet <35486736+AnatoleLucet@users.noreply.github.com> --- src/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8b1aa8f..741800f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,7 +79,9 @@ const parseArgs = (args: string[]) => { cliConfig.avoidSingleFile = true; break; case "--ignore": - cliConfig.ignore = [...(cliConfig.ignore ?? []), args.shift() ?? ""]; + const nextOptionIdx = args.findIndex(x => x.startsWith("-")); + + cliConfig.ignore = [...(cliConfig.ignore ?? []), ...(args.splice(0, nextOptionIdx) ?? [])]; break; default: { if (fs.existsSync(arg) || glob.hasMagic(arg)) {