Skip to content

Commit

Permalink
fix: handle edge cases where linked file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 19, 2020
1 parent 8a4ea3e commit 3a139cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/index/generateTrees/toFractalTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { hasCycle } from "./toFractalTree/hasCycle";
import { RootOption } from "../shared/RootOption";
import { isLinkedFile, linkedFileToOriginal } from "./shared/isLinkedFile";
import chalk from "chalk";
import { fileWithoutExtension } from "../shared/fileWithoutExtension";
import { isTestFile } from "./shared/isTestFile";

export function toFractalTree(graph: Graph, entryPoints: string[]) {
const tree: RootOption["tree"] = {};
Expand Down Expand Up @@ -130,6 +132,8 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) {
});
}

const treeKeys = Object.keys(tree);

if (linkedFiles.size > 0) {
// const globalTests = [];

Expand All @@ -141,7 +145,30 @@ export function toFractalTree(graph: Graph, entryPoints: string[]) {
path.basename(sourceFile)
);
// source file will either be in the current dir or one up
const sourceFilePath = tree[sourceFile] || tree[oneDirUp];
let sourceFilePath = tree[sourceFile] || tree[oneDirUp];

// sometimes the test is add.test.jsx and the source is add.test.js
// so we have to do a linear search to find source key
// we could probably optimize this if needed by doing some work in the fn above
if (!sourceFilePath) {
const sourceFileWithoutFileExtension = fileWithoutExtension(sourceFile);
for (const key of treeKeys) {
if (path.basename(key).startsWith(sourceFileWithoutFileExtension)) {
sourceFilePath = tree[key];
break;
}
}
}

if (!sourceFilePath && isTestFile(linkedFile)) {
// could not link by filename
// so the backup is linking by first relative import
const [firstRelativeImport] = graph[linkedFile];
if (firstRelativeImport) {
sourceFilePath = tree[firstRelativeImport];
}
}

if (!sourceFilePath) {
logger.warn(
`could not find source file that is linked to ${chalk.blueBright(
Expand Down
4 changes: 4 additions & 0 deletions src/index/shared/fileWithoutExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import path from "path";

export const fileWithoutExtension = (f: string) =>
path.basename(f, path.extname(f));

0 comments on commit 3a139cd

Please sign in to comment.