From 4090fdd90191c87f834bfc7f14651c1d5e5b09c4 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Mon, 24 Feb 2020 14:22:05 +0100 Subject: [PATCH] fix: create restructure map This gives us a map over root paths and their file paths. --- src/index.ts | 20 ++++++++++---------- src/index/formatFileStructure.ts | 10 ++++++---- src/index/getFilePaths.ts | 25 ++++++++++++++++++------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/index.ts b/src/index.ts index 348d588..e666ec8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; import { cosmiconfigSync } from "cosmiconfig"; -import getFilePaths from "./index/getFilePaths"; +import getFilePaths, { getRestructureMap } from "./index/getFilePaths"; import logger from "./shared/logger"; import { formatFileStructure } from "./index/formatFileStructure"; import { version } from "../package.json"; @@ -41,7 +41,7 @@ const printHelp = (exitCode: number) => { const parseArgs = ( args: any[] -): { options: Partial; paths: string[] } => +): { options: Partial; rootPaths: string[] } => args.reduce( (acc, arg) => { switch (arg) { @@ -54,18 +54,18 @@ const parseArgs = ( acc.options.version = true; break; default: - acc.paths.push(arg); + acc.rootPaths.push(arg); } return acc; }, - { options: {}, paths: [] } + { options: {}, rootPaths: [] } ); export const run = async (args: string[]) => { const config: Partial = cosmiconfigSync("destiny").search()?.config ?? {}; - const { options, paths } = parseArgs(args); + const { options, rootPaths } = parseArgs(args); const mergedOptions: Options = { ...defaultOptions, @@ -75,19 +75,19 @@ export const run = async (args: string[]) => { if (mergedOptions.help) return printHelp(0); if (mergedOptions.version) return printVersion(); - if (paths.length === 0) return printHelp(1); + if (rootPaths.length === 0) return printHelp(1); logger.info("Resolving files."); - const filesToRestructure = getFilePaths(paths); - const filesToEdit = filesToRestructure.flat(); + const restructureMap = getRestructureMap(rootPaths); + const filesToEdit = Object.values(restructureMap).flat(); - if (filesToRestructure.length === 0) { + if (filesToEdit.length === 0) { logger.error("Could not find any files to restructure", 1); return; } - await formatFileStructure(filesToRestructure, filesToEdit); + await formatFileStructure(restructureMap, filesToEdit); }; if (process.env.NODE_ENV !== "test") { diff --git a/src/index/formatFileStructure.ts b/src/index/formatFileStructure.ts index fab4cfa..ce3ecaf 100644 --- a/src/index/formatFileStructure.ts +++ b/src/index/formatFileStructure.ts @@ -8,21 +8,23 @@ import { RootOption } from "./shared/RootOption"; import logger from "../shared/logger"; export const formatFileStructure = async ( - rootDirFiles: string[][], + restructureMap: { [key: string]: string[] }, filesToEdit: string[] ) => { const unusedFiles: string[] = []; const rootOptions: RootOption[] = []; - for (const startingFiles of rootDirFiles) { - if (startingFiles.length <= 1) { + for (const rootPath in restructureMap) { + const filePaths = restructureMap[rootPath]; + + if (filePaths.length <= 1) { continue; } logger.info("Generating a graph and fractal tree."); const { graph, files, useForwardSlash, parentFolder } = buildGraph( - startingFiles + filePaths ); const tree = toFractalTree(graph, findEntryPoints(graph)); const usedFiles = new Set(Object.entries(graph).flat(2)); diff --git a/src/index/getFilePaths.ts b/src/index/getFilePaths.ts index 19ab567..3abb72e 100644 --- a/src/index/getFilePaths.ts +++ b/src/index/getFilePaths.ts @@ -19,23 +19,24 @@ const globSearch = (pattern: string) => { }; /** Recursively get all file paths. */ -export const getFilePaths = (paths: string[]) => { - const files: string[][] = []; +const getFilePaths = (rootPath: string) => { + const filePaths: string[] = []; + const paths = [rootPath]; while (paths.length > 0) { - const filePath = paths.pop(); + const filePath = paths.shift(); if (filePath == null || filePath.length === 0) continue; const isGlobPattern = glob.hasMagic(filePath); if (isGlobPattern) { - files.push(globSearch(filePath)); + filePaths.push(...globSearch(filePath)); continue; } if (existsSync(filePath)) { if (isFile(filePath)) { - files.push([filePath]); + filePaths.push(filePath); } else if (isDirectory(filePath)) { paths.push(path.join(filePath, "/**/*.*")); } @@ -44,7 +45,17 @@ export const getFilePaths = (paths: string[]) => { } } - return files; + return filePaths; }; -export default getFilePaths; +/** Get a restructure map with rootPath keys and filePaths values. */ +export const getRestructureMap = (rootPaths: string[]) => + rootPaths.reduce<{ [key: string]: string[] }>( + (acc, rootPath) => ({ + ...acc, + [rootPath]: getFilePaths(rootPath), + }), + {} + ); + +export default getRestructureMap;