Skip to content

Commit

Permalink
fix: don't fail if can't find file and add json support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpza committed Feb 25, 2020
1 parent 44ade7b commit 954e9db
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/index/formatFileStructure/fixImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion src/index/generateTrees/buildGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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("\\")) {
Expand Down
26 changes: 22 additions & 4 deletions src/index/shared/customResolve.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
1 change: 1 addition & 0 deletions tests/__snapshots__/end-to-end.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/cra/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import "./non-existent-file";

function App() {
return (
Expand Down

0 comments on commit 954e9db

Please sign in to comment.