Skip to content

Commit

Permalink
fix: use generateTrees and refactor formatFileStructure
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 24, 2020
1 parent e0366d6 commit fb48957
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 61 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import chalk from "chalk";
import { cosmiconfigSync } from "cosmiconfig";

import getFilePaths, { getRestructureMap } from "./index/getFilePaths";
import logger from "./shared/logger";
import { formatFileStructure } from "./index/formatFileStructure";
import { generateTrees } from "./index/generateTrees";
import { version } from "../package.json";
import getRestructureMap from "./index/getFilePaths";

const { argv } = process;

Expand Down Expand Up @@ -87,7 +88,8 @@ export const run = async (args: string[]) => {
return;
}

await formatFileStructure(restructureMap, filesToEdit);
const rootOptions = generateTrees(restructureMap);
await formatFileStructure(filesToEdit, rootOptions);
};

if (process.env.NODE_ENV !== "test") {
Expand Down
48 changes: 3 additions & 45 deletions src/index/formatFileStructure.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,21 @@
import { buildGraph } from "./formatFileStructure/buildGraph";
import { findEntryPoints } from "./formatFileStructure/findEntryPoints";
import { moveFiles } from "./formatFileStructure/moveFiles";
import { toFractalTree } from "./formatFileStructure/toFractalTree";
import { removeEmptyFolders } from "./formatFileStructure/removeEmptyFolders";
import { fixImports } from "./formatFileStructure/fixImports";
import { RootOption } from "./shared/RootOption";
import logger from "../shared/logger";

export const formatFileStructure = async (
restructureMap: { [key: string]: string[] },
filesToEdit: string[]
filePaths: string[],
rootOptions: RootOption[]
) => {
const unusedFiles: string[] = [];
const rootOptions: RootOption[] = [];

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(
filePaths
);
const tree = toFractalTree(graph, findEntryPoints(graph));
const usedFiles = new Set(Object.entries(graph).flat(2));

rootOptions.push({
tree,
useForwardSlash,
parentFolder,
});

files.forEach(file => {
if (!usedFiles.has(file)) {
unusedFiles.push(file);
}
});
}

logger.info("Fixing imports.");
await fixImports(filesToEdit, rootOptions);
fixImports(filePaths, rootOptions);

for (const { tree, parentFolder } of rootOptions) {
logger.info("Moving files.");
await moveFiles(tree, parentFolder);
removeEmptyFolders(parentFolder);
}

if (unusedFiles.length > 0) {
logger.warn(
`Found ${unusedFiles.length} unused files:` +
"\n" +
unusedFiles.map(file => " ".repeat(8) + file).join("\n")
);
}

logger.info("Restructure was successful!");
};
21 changes: 10 additions & 11 deletions src/index/formatFileStructure/fixImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ const getNewImportPath = (
return makeImportPath(newFilePath, absImportPath, lastUseForwardSlash);
};

export const fixImports = (files: string[], rootOptions: RootOption[]) => {
for (const file of files) {
const imports = findEdges(file);
if (!imports.length) {
continue;
}
export const fixImports = (filePaths: string[], rootOptions: RootOption[]) => {
for (const filePath of filePaths) {
const imports = findEdges(filePath);

if (!imports.length) continue;

const basedir = path.dirname(file);
const newFilePath = getNewFilePath(file, rootOptions);
const basedir = path.dirname(filePath);
const newFilePath = getNewFilePath(filePath, rootOptions);
const ogText = readFileSync(filePath).toString();

const ogText = readFileSync(file).toString();
// deep copy
let newText = ogText.repeat(1);
for (const imp of imports) {
const absPath = customResolve(imp[1], basedir);
Expand All @@ -60,8 +58,9 @@ export const fixImports = (files: string[], rootOptions: RootOption[]) => {
.replace(`"${imp[1]}"`, `"${newImportText}"`);
}
}

if (newText !== ogText) {
writeFileSync(file, newText);
writeFileSync(filePath, newText);
}
}
};
6 changes: 3 additions & 3 deletions src/index/shared/RootOption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface RootOption {
export type RootOption = {
parentFolder: string;
tree: Record<string, string>;
useForwardSlash: boolean;
parentFolder: string;
}
};

1 comment on commit fb48957

@xendaya
Copy link

@xendaya xendaya commented on fb48957 Jun 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RandomTree(), PectinateTree(), BalancedTree() and StarTree()

Please sign in to comment.