Skip to content

Commit

Permalink
fix: create restructure map
Browse files Browse the repository at this point in the history
This gives us a map over root paths and their file paths.
  • Loading branch information
sQVe committed Feb 24, 2020
1 parent bc177c1 commit 4090fdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -41,7 +41,7 @@ const printHelp = (exitCode: number) => {

const parseArgs = (
args: any[]
): { options: Partial<Options>; paths: string[] } =>
): { options: Partial<Options>; rootPaths: string[] } =>
args.reduce(
(acc, arg) => {
switch (arg) {
Expand All @@ -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<Options> =
cosmiconfigSync("destiny").search()?.config ?? {};
const { options, paths } = parseArgs(args);
const { options, rootPaths } = parseArgs(args);

const mergedOptions: Options = {
...defaultOptions,
Expand All @@ -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") {
Expand Down
10 changes: 6 additions & 4 deletions src/index/formatFileStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
25 changes: 18 additions & 7 deletions src/index/getFilePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/**/*.*"));
}
Expand All @@ -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;

0 comments on commit 4090fdd

Please sign in to comment.