-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
395 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"pre-push": "jest" | ||
"pre-push": "run-p check test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +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 ( | ||
rootDirFiles: string[][], | ||
filesToEdit: string[] | ||
filePaths: string[], | ||
rootOptions: RootOption[] | ||
) => { | ||
const unusedFiles: string[] = []; | ||
const rootOptions: RootOption[] = []; | ||
|
||
for (const startingFiles of rootDirFiles) { | ||
if (startingFiles.length <= 1) { | ||
continue; | ||
} | ||
|
||
logger.info("Generating a graph and fractal tree."); | ||
|
||
const { graph, files, useForwardSlash, parentFolder } = buildGraph( | ||
startingFiles | ||
); | ||
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!"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 25 additions & 17 deletions
42
src/index/formatFileStructure/fixImports/makeImportPath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
import path from "path"; | ||
|
||
function getExtensionFromImport(relativePath: string) { | ||
const ext = path.extname(relativePath); | ||
const includeExtension = [".js", ".jsx", ".ts", ".tsx"].includes(ext); | ||
return includeExtension ? ext : undefined; | ||
} | ||
|
||
export const makeImportPath = ( | ||
p1: string, | ||
p2: string, | ||
fromPath: string, | ||
toPath: string, | ||
useForwardSlashes: boolean | ||
) => { | ||
const fullPath = path.relative(path.dirname(p1), p2); | ||
const ext = path.extname(fullPath); | ||
let newImport = path.join( | ||
path.dirname(fullPath), | ||
path.basename( | ||
fullPath, | ||
[".js", ".jsx", ".ts", ".tsx"].includes(ext) ? ext : undefined | ||
) | ||
); | ||
const fromDirectory = path.dirname(fromPath); | ||
const relativePath = path.relative(fromDirectory, toPath); | ||
|
||
const relativeDirectory = path.dirname(relativePath); | ||
const ext = getExtensionFromImport(relativePath); | ||
const fileName = path.basename(relativePath, ext); | ||
|
||
let newImport = path.join(relativeDirectory, fileName); | ||
|
||
if (!newImport.startsWith(".")) { | ||
// Ensures relative imports. | ||
const notRelative = !newImport.startsWith("."); | ||
if (notRelative) { | ||
newImport = "./" + newImport; | ||
} | ||
|
||
if (useForwardSlashes) { | ||
// Replace \ with /. | ||
newImport = newImport.replace(/\\/g, "/"); | ||
} else { | ||
// Replace / and \ with \\. | ||
// Replace / and \ with \\. | ||
if (!useForwardSlashes) { | ||
newImport = newImport.replace(/\/|\\+/g, "\\\\"); | ||
} | ||
// Replace \ with /. | ||
else { | ||
newImport = newImport.replace(/\\/g, "/"); | ||
} | ||
|
||
return newImport; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import chalk from "chalk"; | ||
|
||
import logger from "../shared/logger"; | ||
import printTree from "./generateTrees/printTree"; | ||
import { RootOption as Root } from "./shared/RootOption"; | ||
import { buildGraph } from "./generateTrees/buildGraph"; | ||
import { findEntryPoints } from "./generateTrees/findEntryPoints"; | ||
import { toFractalTree } from "./generateTrees/toFractalTree"; | ||
|
||
export function generateTrees(restructureMap: { [key: string]: string[] }) { | ||
return Object.entries(restructureMap).reduce<Root[]>( | ||
(rootOptions, [rootPath, filePaths]) => { | ||
if (filePaths.length <= 1) return rootOptions; | ||
|
||
logger.info(`Generating tree for: ${rootPath}`); | ||
|
||
const { graph, files, useForwardSlash, parentFolder } = buildGraph( | ||
filePaths | ||
); | ||
const tree = toFractalTree(graph, findEntryPoints(graph)); | ||
|
||
const usedFilePaths = new Set(Object.entries(graph).flat(2)); | ||
const unusedFiles = files.filter( | ||
filePath => !usedFilePaths.has(filePath) | ||
); | ||
|
||
logger.log(chalk.bold.blue(rootPath)); | ||
printTree(Object.values(tree)); | ||
if (unusedFiles.length > 0) { | ||
logger.warn( | ||
`Found ${unusedFiles.length} unused files:` + | ||
"\n" + | ||
unusedFiles.join("\n") | ||
); | ||
} | ||
|
||
rootOptions.push({ | ||
parentFolder, | ||
tree, | ||
useForwardSlash, | ||
}); | ||
|
||
return rootOptions; | ||
}, | ||
[] | ||
); | ||
} |
Oops, something went wrong.