From 9cfa9f23bdf2c0b1a40e219673709cb7fb08d327 Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 11:20:07 +1100 Subject: [PATCH 01/17] removes duplicate unused files from logger.warn --- src/index/formatFileStructure.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index/formatFileStructure.ts b/src/index/formatFileStructure.ts index b8ebbb5..02566a1 100644 --- a/src/index/formatFileStructure.ts +++ b/src/index/formatFileStructure.ts @@ -53,9 +53,7 @@ export const formatFileStructure = async ( logger.warn( "Unused files:" + "\n" + - [...unusedFiles, ...unusedFiles] - .map(file => " ".repeat(8) + file) - .join("\n") + [...unusedFiles].map(file => " ".repeat(8) + file).join("\n") ); } From 6c272c1127f4be361052be9950a3e27ddd268d91 Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 18:59:22 +1100 Subject: [PATCH 02/17] enhances code readability and adds TSDoc to "removeEmptyFolders" --- .../formatFileStructure/removeEmptyFolders.ts | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index e10af68..01795f7 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -1,18 +1,27 @@ import fs from "fs"; import path from "path"; -export const removeEmptyFolders = (p: string) => { - const files = fs.readdirSync(p); - if (!files) { - fs.rmdirSync(p); - } - for (const file of files) { - const fullPath = path.join(p, file); - if (fs.lstatSync(fullPath).isDirectory()) { - removeEmptyFolders(fullPath); - if (!fs.readdirSync(fullPath).length) { - fs.rmdirSync(fullPath); - } - } +/** + * @summary + * Recursively removes all empty folders. + * + * @param directory the full path of a directory + * + * @example + * removeEmptyFolders('/home/user/documents/project/src') + */ +export function removeEmptyFolders(directory: string): void { + const files = fs.readdirSync(directory); + if (!files) return fs.rmdirSync(directory); + + for (const base of files) { + const fullPath = path.resolve(directory, base); + const isDirectory = fs.lstatSync(fullPath).isDirectory(); + + if (!isDirectory) continue; + + removeEmptyFolders(fullPath); + const isEmpty = !fs.readdirSync(fullPath).length; + if (isEmpty) fs.rmdirSync(fullPath); } -}; +} From 1c4c53fd38156aeadd0aea82726e1d43d315eca5 Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 19:05:49 +1100 Subject: [PATCH 03/17] adds whitespace and a full stop to "removeEmptyFolders" --- src/index/formatFileStructure/removeEmptyFolders.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 01795f7..3a55014 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -5,7 +5,7 @@ import path from "path"; * @summary * Recursively removes all empty folders. * - * @param directory the full path of a directory + * @param directory the full path of a directory. * * @example * removeEmptyFolders('/home/user/documents/project/src') @@ -16,11 +16,12 @@ export function removeEmptyFolders(directory: string): void { for (const base of files) { const fullPath = path.resolve(directory, base); - const isDirectory = fs.lstatSync(fullPath).isDirectory(); + const isDirectory = fs.lstatSync(fullPath).isDirectory(); if (!isDirectory) continue; removeEmptyFolders(fullPath); + const isEmpty = !fs.readdirSync(fullPath).length; if (isEmpty) fs.rmdirSync(fullPath); } From efdcb4b0f273217a045d3048dc44a587c283687b Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 09:49:01 +0100 Subject: [PATCH 04/17] refactor: remove unnecessary unusedFiles spread --- src/index/formatFileStructure.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index/formatFileStructure.ts b/src/index/formatFileStructure.ts index 02566a1..ddf3f91 100644 --- a/src/index/formatFileStructure.ts +++ b/src/index/formatFileStructure.ts @@ -49,11 +49,11 @@ export const formatFileStructure = async ( removeEmptyFolders(parentFolder); } - if (unusedFiles.length) { + if (unusedFiles.length > 0) { logger.warn( - "Unused files:" + + `Found ${unusedFiles.length} unused files:` + "\n" + - [...unusedFiles].map(file => " ".repeat(8) + file).join("\n") + unusedFiles.map(file => " ".repeat(8) + file).join("\n") ); } From b03df8700e1b01af29f3c3b58c7677405fc1fc4d Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 19:55:27 +1100 Subject: [PATCH 05/17] Update src/index/formatFileStructure/removeEmptyFolders.ts clearer purpose of empty array boolean check. Co-Authored-By: Oskar Grunning --- src/index/formatFileStructure/removeEmptyFolders.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 3a55014..bbc0df8 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -22,7 +22,7 @@ export function removeEmptyFolders(directory: string): void { removeEmptyFolders(fullPath); - const isEmpty = !fs.readdirSync(fullPath).length; + const isEmpty = fs.readdirSync(fullPath).length === 0; if (isEmpty) fs.rmdirSync(fullPath); } } From b205294296d9eed4144ef8c523410e6b659b21e4 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 09:56:40 +0100 Subject: [PATCH 06/17] fix: remove leftover log --- src/index.ts | 1 - src/index/formatFileStructure.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index bb82887..669584e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -102,7 +102,6 @@ const getFilePaths = (paths: string[], detectRoots: boolean) => { ) ); detectRoots = false; - console.log(paths); } else { paths.push(path.join(filePath, "/**/*.*")); } diff --git a/src/index/formatFileStructure.ts b/src/index/formatFileStructure.ts index ddf3f91..fab4cfa 100644 --- a/src/index/formatFileStructure.ts +++ b/src/index/formatFileStructure.ts @@ -57,5 +57,5 @@ export const formatFileStructure = async ( ); } - logger.info("Successfully restructured!"); + logger.info("Restructure was successful!"); }; From 614355e0824e7d9d4e5685040963f187f2eb0e1a Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 19:57:41 +1100 Subject: [PATCH 07/17] adds clarity for empty array check --- src/index/formatFileStructure/removeEmptyFolders.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 3a55014..8d907af 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -5,8 +5,6 @@ import path from "path"; * @summary * Recursively removes all empty folders. * - * @param directory the full path of a directory. - * * @example * removeEmptyFolders('/home/user/documents/project/src') */ @@ -22,7 +20,7 @@ export function removeEmptyFolders(directory: string): void { removeEmptyFolders(fullPath); - const isEmpty = !fs.readdirSync(fullPath).length; + const isEmpty = fs.readdirSync(fullPath).length === 0; if (isEmpty) fs.rmdirSync(fullPath); } } From 82bd83513f5881fe43cb6e6604ffb03b4cdefa9c Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 20:02:20 +1100 Subject: [PATCH 08/17] renames "base" to "filePath" for clarity. --- src/index/formatFileStructure/removeEmptyFolders.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 8d907af..90bb204 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -12,8 +12,8 @@ export function removeEmptyFolders(directory: string): void { const files = fs.readdirSync(directory); if (!files) return fs.rmdirSync(directory); - for (const base of files) { - const fullPath = path.resolve(directory, base); + for (const filePath of files) { + const fullPath = path.resolve(directory, filePath); const isDirectory = fs.lstatSync(fullPath).isDirectory(); if (!isDirectory) continue; From e0b69143cc98baeaf4a161a204c1d0d703e56544 Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 20:19:21 +1100 Subject: [PATCH 09/17] removes @example from TSDoc --- src/index/formatFileStructure/removeEmptyFolders.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 90bb204..7ecb067 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -4,9 +4,6 @@ import path from "path"; /** * @summary * Recursively removes all empty folders. - * - * @example - * removeEmptyFolders('/home/user/documents/project/src') */ export function removeEmptyFolders(directory: string): void { const files = fs.readdirSync(directory); From f18b9f7469aee88c986bdd5fbacee2d7ee1500b6 Mon Sep 17 00:00:00 2001 From: Wayne Van Son Date: Thu, 20 Feb 2020 20:37:30 +1100 Subject: [PATCH 10/17] removes the `@summary` from comment --- src/index/formatFileStructure/removeEmptyFolders.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/index/formatFileStructure/removeEmptyFolders.ts b/src/index/formatFileStructure/removeEmptyFolders.ts index 7ecb067..29f5896 100644 --- a/src/index/formatFileStructure/removeEmptyFolders.ts +++ b/src/index/formatFileStructure/removeEmptyFolders.ts @@ -1,10 +1,7 @@ import fs from "fs"; import path from "path"; -/** - * @summary - * Recursively removes all empty folders. - */ +/** Recursively removes all empty folders. */ export function removeEmptyFolders(directory: string): void { const files = fs.readdirSync(directory); if (!files) return fs.rmdirSync(directory); From b6d6d4136b4ca0ce9d75c15db2a2da83e86edf07 Mon Sep 17 00:00:00 2001 From: Anatole Lucet <35486736+AnatoleLucet@users.noreply.github.com> Date: Thu, 20 Feb 2020 11:08:38 +0100 Subject: [PATCH 11/17] adding ci badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9d7579f..a15a132 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Prettier for File Structures [![npm version](https://badge.fury.io/js/destiny.svg)](https://badge.fury.io/js/destiny) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/benawad/destiny/issues) +[![](https://github.com/benawad/destiny/workflows/ci/badge.svg)](https://github.com/benawad/destiny/actions?query=workflow%3Aci) --- From d8cd62bde4f862a973fba1e48aeb42bbe4045aab Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 11:43:40 +0100 Subject: [PATCH 12/17] fix: separate getFilePaths into module This also refactors and simplifies its logic. --- src/getFilePaths.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 57 +++-------------------------------------- 2 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 src/getFilePaths.ts diff --git a/src/getFilePaths.ts b/src/getFilePaths.ts new file mode 100644 index 0000000..fb2e9c1 --- /dev/null +++ b/src/getFilePaths.ts @@ -0,0 +1,62 @@ +import glob from "glob"; +import path from "path"; +import { existsSync, lstatSync, readdirSync } from "fs-extra"; + +import logger from "./shared/logger"; + +const isDirectory = (filePath: string) => lstatSync(filePath).isDirectory(); +const isFile = (filePath: string) => lstatSync(filePath).isFile(); + +const globSearch = (pattern: string) => { + const matches = glob.sync(pattern); + const files = matches.filter(match => isFile(match)); + + if (files.length === 0) { + logger.error("Could not find any files for: " + pattern, 1); + } + + return files; +}; + +/** Recursively get all file paths. */ +export const getFilePaths = ( + paths: string[], + options = { detectRoots: false } +) => { + const files: string[][] = []; + + while (paths.length > 0) { + const filePath = paths.pop(); + + if (filePath == null || filePath.length === 0) continue; + + const isGlobPattern = glob.hasMagic(filePath); + if (isGlobPattern) { + files.push(globSearch(filePath)); + continue; + } + + if (existsSync(filePath)) { + if (isFile(filePath)) { + files.push([filePath]); + } else if (isDirectory(filePath)) { + if (options.detectRoots) { + paths.push( + ...readdirSync(path.resolve(filePath)).map(x => + path.join(filePath, x) + ) + ); + options.detectRoots = false; + } else { + paths.push(path.join(filePath, "/**/*.*")); + } + } + } else { + logger.error(`Unable to resolve the path: ${filePath}`); + } + } + + return files; +}; + +export default getFilePaths; diff --git a/src/index.ts b/src/index.ts index 669584e..292cdb1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,10 @@ import chalk from "chalk"; -import glob from "glob"; -import { existsSync, lstatSync, readdirSync } from "fs-extra"; import path from "path"; +import getFilePaths from "./getFilePaths"; +import logger from "./shared/logger"; import { formatFileStructure } from "./index/formatFileStructure"; import { version } from "../package.json"; -import logger from "./shared/logger"; const { argv } = process; const defaults = { @@ -66,56 +65,6 @@ const parseArgs = (args: any[]): ParsedArgs => return acc; }, defaults); -const getFilePaths = (paths: string[], detectRoots: boolean) => { - const files: string[][] = []; - - while (paths.length > 0) { - const filePath = paths.pop(); - - if (!filePath) continue; - if (glob.hasMagic(filePath)) { - const globFiles = glob.sync(filePath); - - if (globFiles.length === 0) { - logger.error("Could not find any files for: " + filePath, 1); - } - files.push( - globFiles.filter(x => { - const isFile = lstatSync(x).isFile(); - - if (!isFile) { - logger.warn(`Skipping non file: ${x}`); - } - return isFile; - }) - ); - } else if (!existsSync(filePath)) { - logger.error(`Unable to resolve the path: ${filePath}`); - } else { - const stats = lstatSync(filePath); - - if (stats.isDirectory()) { - if (detectRoots) { - paths.push( - ...readdirSync(path.resolve(filePath)).map(x => - path.join(filePath, x) - ) - ); - detectRoots = false; - } else { - paths.push(path.join(filePath, "/**/*.*")); - } - } else if (stats.isFile()) { - files.push([filePath]); - } else { - logger.warn(`Skipping: ${filePath}`); - } - } - } - - return files; -}; - export const run = async (args: any[]) => { const { options, paths } = parseArgs(args); @@ -125,7 +74,7 @@ export const run = async (args: any[]) => { logger.info("Resolving files."); - const filesToRestructure = getFilePaths(paths, options.detectRoots); + const filesToRestructure = getFilePaths(paths, options); const filesToEdit = filesToRestructure.flat(); if (filesToRestructure.length === 0) { From c0e4619bf90e8eead235523c19719ddb646d86da Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 11:46:14 +0100 Subject: [PATCH 13/17] chore: restructure src --- src/index.ts | 2 +- src/{ => index}/getFilePaths.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => index}/getFilePaths.ts (97%) diff --git a/src/index.ts b/src/index.ts index 292cdb1..e89d144 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; import path from "path"; -import getFilePaths from "./getFilePaths"; +import getFilePaths from "./index/getFilePaths"; import logger from "./shared/logger"; import { formatFileStructure } from "./index/formatFileStructure"; import { version } from "../package.json"; diff --git a/src/getFilePaths.ts b/src/index/getFilePaths.ts similarity index 97% rename from src/getFilePaths.ts rename to src/index/getFilePaths.ts index fb2e9c1..a461902 100644 --- a/src/getFilePaths.ts +++ b/src/index/getFilePaths.ts @@ -2,7 +2,7 @@ import glob from "glob"; import path from "path"; import { existsSync, lstatSync, readdirSync } from "fs-extra"; -import logger from "./shared/logger"; +import logger from "../shared/logger"; const isDirectory = (filePath: string) => lstatSync(filePath).isDirectory(); const isFile = (filePath: string) => lstatSync(filePath).isFile(); From 6d8262b8af901624e2967d08f53b33e035632686 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 11:59:14 +0100 Subject: [PATCH 14/17] fix: repair detectRoots option --- src/index/getFilePaths.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index/getFilePaths.ts b/src/index/getFilePaths.ts index a461902..0e23889 100644 --- a/src/index/getFilePaths.ts +++ b/src/index/getFilePaths.ts @@ -41,11 +41,11 @@ export const getFilePaths = ( files.push([filePath]); } else if (isDirectory(filePath)) { if (options.detectRoots) { - paths.push( - ...readdirSync(path.resolve(filePath)).map(x => - path.join(filePath, x) - ) - ); + const childDirectories = readdirSync(path.resolve(filePath)) + .map(x => path.join(filePath, x)) + .filter(x => isDirectory(x)); + + paths.push(...childDirectories); options.detectRoots = false; } else { paths.push(path.join(filePath, "/**/*.*")); From be0a7534b1174f9944b1da1021c1b463179680e4 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 18:26:07 +0100 Subject: [PATCH 15/17] chore: add config dependencies --- package.json | 5 +++-- yarn.lock | 36 ++---------------------------------- 2 files changed, 5 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 12311c7..0c66a6a 100644 --- a/package.json +++ b/package.json @@ -56,15 +56,16 @@ "rollup-plugin-add-shebang": "^0.3.0", "rollup-plugin-babel": "^4.3.0", "rollup-plugin-terser": "^5.2.0", + "semantic-release": "^17.0.4", "tree-node-cli": "^1.2.5", - "typescript": "^3.7.5", - "semantic-release": "^17.0.4" + "typescript": "^3.7.5" }, "license": "MIT", "dependencies": { "@babel/runtime": "^7.8.4", "chalk": "^3.0.0", "core-js": "^3.6.4", + "cosmiconfig": "^6.0.0", "fs-extra": "^8.1.0", "glob": "^7.1.6", "resolve": "^1.15.1", diff --git a/yarn.lock b/yarn.lock index bd61d54..3e45383 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2650,7 +2650,7 @@ debug@^3.1.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debuglog@*, debuglog@^1.0.1: +debuglog@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI= @@ -3949,7 +3949,7 @@ import-local@^3.0.2: pkg-dir "^4.2.0" resolve-cwd "^3.0.0" -imurmurhash@*, imurmurhash@^0.1.4: +imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= @@ -5253,11 +5253,6 @@ lockfile@^1.0.4: dependencies: signal-exit "^3.0.2" -lodash._baseindexof@*: - version "3.1.0" - resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c" - integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw= - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -5266,33 +5261,11 @@ lodash._baseuniq@~4.6.0: lodash._createset "~4.0.0" lodash._root "~3.0.0" -lodash._bindcallback@*: - version "3.0.1" - resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" - integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4= - -lodash._cacheindexof@*: - version "3.0.2" - resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92" - integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI= - -lodash._createcache@*: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093" - integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM= - dependencies: - lodash._getnative "^3.0.0" - lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY= -lodash._getnative@*, lodash._getnative@^3.0.0: - version "3.9.1" - resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" - integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U= - lodash._root@~3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692" @@ -5333,11 +5306,6 @@ lodash.isstring@^4.0.1: resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451" integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE= -lodash.restparam@*: - version "3.6.1" - resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" - integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" From 759ff1d34d76bc2e03486e055615bf04ace7e583 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 18:28:08 +0100 Subject: [PATCH 16/17] feat: add support for config --- src/index.ts | 87 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/src/index.ts b/src/index.ts index e89d144..80f2557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import chalk from "chalk"; -import path from "path"; +import { cosmiconfigSync } from "cosmiconfig"; +import { existsSync, lstatSync, readdirSync } from "fs-extra"; import getFilePaths from "./index/getFilePaths"; import logger from "./shared/logger"; @@ -7,18 +8,17 @@ import { formatFileStructure } from "./index/formatFileStructure"; import { version } from "../package.json"; const { argv } = process; -const defaults = { - options: { - help: false, - version: false, - detectRoots: false, - }, - paths: [], + +type Options = { + detectRoots: boolean; + help: boolean; + version: boolean; }; -type ParsedArgs = { - options: { help: boolean; version: boolean; detectRoots: boolean }; - paths: string[]; +const defaultOptions: Options = { + detectRoots: false, + help: false, + version: false, }; const printVersion = () => console.log("v" + version); @@ -43,38 +43,51 @@ const printHelp = (exitCode: number) => { return process.exit(exitCode); }; -const parseArgs = (args: any[]): ParsedArgs => - args.reduce((acc, arg) => { - switch (arg) { - case "-h": - case "--help": - acc.options.help = true; - break; - case "-V": - case "--version": - acc.options.version = true; - break; - case "-dr": - case "--detect-roots": - acc.options.detectRoots = true; - break; - default: - acc.paths.push(arg); - } - - return acc; - }, defaults); - -export const run = async (args: any[]) => { +const parseArgs = ( + args: any[] +): { options: Partial; paths: string[] } => + args.reduce( + (acc, arg) => { + switch (arg) { + case "-h": + case "--help": + acc.options.help = true; + break; + case "-V": + case "--version": + acc.options.version = true; + break; + case "-dr": + case "--detect-roots": + acc.options.detectRoots = true; + break; + default: + acc.paths.push(arg); + } + + return acc; + }, + { options: {}, paths: [] } + ); + +export const run = async (args: string[]) => { + const config: Partial = + cosmiconfigSync("destiny").search()?.config ?? {}; const { options, paths } = parseArgs(args); - if (options.help) return printHelp(0); - if (options.version) return printVersion(); + const mergedOptions: Options = { + ...defaultOptions, + ...config, + ...options, + }; + + if (mergedOptions.help) return printHelp(0); + if (mergedOptions.version) return printVersion(); if (paths.length === 0) return printHelp(1); logger.info("Resolving files."); - const filesToRestructure = getFilePaths(paths, options); + const filesToRestructure = getFilePaths(paths, mergedOptions.detectRoots); const filesToEdit = filesToRestructure.flat(); if (filesToRestructure.length === 0) { From 5cc1270cdcd2bcf5ae0306964678bc3b5ed341c0 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Thu, 20 Feb 2020 18:46:33 +0100 Subject: [PATCH 17/17] fix: repair getFilePaths options --- src/index.ts | 4 ++-- src/index/getFilePaths.ts | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 80f2557..bdb2d11 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { version } from "../package.json"; const { argv } = process; -type Options = { +export type Options = { detectRoots: boolean; help: boolean; version: boolean; @@ -87,7 +87,7 @@ export const run = async (args: string[]) => { logger.info("Resolving files."); - const filesToRestructure = getFilePaths(paths, mergedOptions.detectRoots); + const filesToRestructure = getFilePaths(paths, mergedOptions); const filesToEdit = filesToRestructure.flat(); if (filesToRestructure.length === 0) { diff --git a/src/index/getFilePaths.ts b/src/index/getFilePaths.ts index 0e23889..e22e113 100644 --- a/src/index/getFilePaths.ts +++ b/src/index/getFilePaths.ts @@ -3,6 +3,7 @@ import path from "path"; import { existsSync, lstatSync, readdirSync } from "fs-extra"; import logger from "../shared/logger"; +import { Options } from "../index"; const isDirectory = (filePath: string) => lstatSync(filePath).isDirectory(); const isFile = (filePath: string) => lstatSync(filePath).isFile(); @@ -19,10 +20,8 @@ const globSearch = (pattern: string) => { }; /** Recursively get all file paths. */ -export const getFilePaths = ( - paths: string[], - options = { detectRoots: false } -) => { +export const getFilePaths = (paths: string[], options: Options) => { + let { detectRoots } = options; const files: string[][] = []; while (paths.length > 0) { @@ -40,13 +39,13 @@ export const getFilePaths = ( if (isFile(filePath)) { files.push([filePath]); } else if (isDirectory(filePath)) { - if (options.detectRoots) { + if (detectRoots) { const childDirectories = readdirSync(path.resolve(filePath)) .map(x => path.join(filePath, x)) .filter(x => isDirectory(x)); paths.push(...childDirectories); - options.detectRoots = false; + detectRoots = false; } else { paths.push(path.join(filePath, "/**/*.*")); }