From 954e9dbacb7515bd1e358723086e8aeff6d7ca7b Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Mon, 24 Feb 2020 21:38:41 -0500 Subject: [PATCH] fix: don't fail if can't find file and add json support --- src/index/formatFileStructure/fixImports.ts | 7 +++++- src/index/generateTrees/buildGraph.ts | 8 ++++++- src/index/shared/customResolve.ts | 26 +++++++++++++++++---- tests/__snapshots__/end-to-end.test.ts.snap | 1 + tests/fixtures/cra/App.js | 1 + 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/index/formatFileStructure/fixImports.ts b/src/index/formatFileStructure/fixImports.ts index 29201d4..c010d3a 100644 --- a/src/index/formatFileStructure/fixImports.ts +++ b/src/index/formatFileStructure/fixImports.ts @@ -2,8 +2,9 @@ import { readFileSync, writeFileSync } from "fs-extra"; import { findEdges } from "../shared/findEdges"; import path from "path"; import { makeImportPath } from "./fixImports/makeImportPath"; -import { customResolve } from "../shared/customResolve"; +import { customResolve, canResolve } from "../shared/customResolve"; import { RootOption } from "../shared/RootOption"; +import logger from "../../shared/logger"; const getNewFilePath = (file: string, rootOptions: RootOption[]) => { for (const { tree, parentFolder } of rootOptions) { @@ -49,6 +50,10 @@ export const fixImports = (filePaths: string[], rootOptions: RootOption[]) => { let newText = ogText.repeat(1); for (const imp of imports) { + if (!canResolve(imp[1], basedir)) { + logger.error(`Cannot find import ${imp[1]}`); + continue; + } const absPath = customResolve(imp[1], basedir); const newImportText = getNewImportPath(absPath, newFilePath, rootOptions); diff --git a/src/index/generateTrees/buildGraph.ts b/src/index/generateTrees/buildGraph.ts index a3040fe..80c3055 100644 --- a/src/index/generateTrees/buildGraph.ts +++ b/src/index/generateTrees/buildGraph.ts @@ -2,8 +2,9 @@ import path from "path"; import { findEdges } from "../shared/findEdges"; import { Graph, OldGraph } from "./shared/Graph"; import { findSharedParent } from "./shared/findSharedParent"; -import { customResolve } from "../shared/customResolve"; +import { customResolve, canResolve } from "../shared/customResolve"; import { addEdgeToGraph } from "./buildGraph/addEdge"; +import logger from "../../shared/logger"; export function buildGraph(files: string[]) { const parentFolder = findSharedParent(files); @@ -26,6 +27,11 @@ export function buildGraph(files: string[]) { } totalFiles.push(start); findEdges(file).forEach(edge => { + if (!canResolve(edge[1], path.dirname(edge[0]))) { + logger.error(`Cannot find import ${edge[1]}`); + return; + } + if (edge[1].includes("/")) { numForwardSlashes++; } else if (edge[1].includes("\\")) { diff --git a/src/index/shared/customResolve.ts b/src/index/shared/customResolve.ts index e7bbd48..68d733c 100644 --- a/src/index/shared/customResolve.ts +++ b/src/index/shared/customResolve.ts @@ -1,9 +1,27 @@ import resolve from "resolve"; +const extensions = [ + ".js", + ".jsx", + ".json", + ".sass", + ".scss", + ".svg", + ".ts", + ".tsx", +]; + +/** Checks if the file exists */ +export const canResolve = (id: string, basedir: string) => { + try { + resolve.sync(id, { basedir, extensions }); + return true; + } catch (err) { + return false; + } +}; + /** Resolve with a list of predefined extensions. */ export const customResolve = (id: string, basedir: string) => { - return resolve.sync(id, { - basedir, - extensions: [".js", ".jsx", ".sass", ".scss", ".svg", ".ts", ".tsx"], - }); + return resolve.sync(id, { basedir, extensions }); }; diff --git a/tests/__snapshots__/end-to-end.test.ts.snap b/tests/__snapshots__/end-to-end.test.ts.snap index 29c409a..8b6de26 100644 --- a/tests/__snapshots__/end-to-end.test.ts.snap +++ b/tests/__snapshots__/end-to-end.test.ts.snap @@ -58,6 +58,7 @@ exports[`end-to-end cra: cra/index/App.js 1`] = ` "import React from \\"react\\"; import logo from \\"./App/logo.svg\\"; import \\"./App/App.css\\"; +import \\"./non-existent-file\\"; function App() { return ( diff --git a/tests/fixtures/cra/App.js b/tests/fixtures/cra/App.js index 43d2f26..6e64d66 100644 --- a/tests/fixtures/cra/App.js +++ b/tests/fixtures/cra/App.js @@ -1,6 +1,7 @@ import React from "react"; import logo from "./logo.svg"; import "./App.css"; +import "./non-existent-file"; function App() { return (