Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Feb 26, 2020
2 parents 44ade7b + a75b68a commit 559d195
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Prettier for File Structures

[![npm version](https://badge.fury.io/js/destiny.svg)](https://badge.fury.io/js/destiny)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/benawad/destiny/issues)
[![](https://github.com/benawad/destiny/workflows/ci/badge.svg)](https://github.com/benawad/destiny/actions?query=workflow%3Aci)
[![](https://github.com/benawad/destiny/workflows/ci/badge.svg)](https://github.com/benawad/destiny/actions?query=workflow%3Aci) [![Join the chat at https://gitter.im/destiny-dev/community](https://badges.gitter.im/destiny-dev/community.svg)](https://gitter.im/destiny-dev/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

---

Expand Down
7 changes: 7 additions & 0 deletions src/index/formatFileStructure/fixImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import { makeImportPath } from "./fixImports/makeImportPath";
import { customResolve } 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 @@ -50,6 +51,12 @@ export const fixImports = (filePaths: string[], rootOptions: RootOption[]) => {
let newText = ogText.repeat(1);
for (const imp of imports) {
const absPath = customResolve(imp[1], basedir);

if (absPath == null) {
logger.error(`Cannot find import ${imp[1]}`);
continue;
}

const newImportText = getNewImportPath(absPath, newFilePath, rootOptions);

if (newImportText) {
Expand Down
8 changes: 8 additions & 0 deletions src/index/generateTrees/buildGraph.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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 { addEdgeToGraph } from "./buildGraph/addEdge";
import logger from "../../shared/logger";

export function buildGraph(files: string[]) {
const parentFolder = findSharedParent(files);
Expand All @@ -12,6 +14,7 @@ export function buildGraph(files: string[]) {
const totalFiles: string[] = [];
let numForwardSlashes = 0;
let numBackSlashes = 0;

for (let file of files) {
if (file === ".git") {
continue;
Expand All @@ -34,6 +37,11 @@ export function buildGraph(files: string[]) {

const pathWithExtension = customResolve(edge[1], path.dirname(edge[0]));

if (pathWithExtension == null) {
logger.error(`Cannot find import ${edge[1]}`);
return;
}

const end = path.relative(parentFolder, pathWithExtension);

addEdgeToGraph([start, end], graph);
Expand Down
1 change: 1 addition & 0 deletions src/index/generateTrees/buildGraph/addEdge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export function addEdgeToGraph([start, end]: [string, string], graph: Graph) {
if (!(start in graph)) {
graph[start] = [];
}
if (graph[start].includes(end)) return;
graph[start].push(end);
}
20 changes: 16 additions & 4 deletions src/index/shared/customResolve.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import resolve from "resolve";

const extensions = [
".js",
".json",
".jsx",
".sass",
".scss",
".svg",
".ts",
".tsx",
];

/** 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"],
});
try {
return resolve.sync(id, { basedir, extensions });
} catch (err) {
return null;
}
};
28 changes: 28 additions & 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 Expand Up @@ -336,6 +337,33 @@ import \\"@testing-library/jest-dom/extend-expect\\";
"
`;
exports[`end-to-end duplicate-imports 1`] = `
"duplicate-imports
├── index
│ ├── routes
│ │ └── home.js
│ └── routes.js
└── index.js"
`;
exports[`end-to-end duplicate-imports: duplicate-imports/index.js 1`] = `
"/* eslint-disable*/
import { five } from \\"./index/routes\\";
import { five } from \\"./index/routes\\";
"
`;
exports[`end-to-end duplicate-imports: duplicate-imports/index/routes.js 1`] = `
"export * from \\"./routes/home\\";
"
`;
exports[`end-to-end duplicate-imports: duplicate-imports/index/routes/home.js 1`] = `
"export const five = 5;
"
`;
exports[`end-to-end duplicates 1`] = `
"duplicates
├── index
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
1 change: 1 addition & 0 deletions tests/fixtures/duplicate-imports/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const five = 5;
4 changes: 4 additions & 0 deletions tests/fixtures/duplicate-imports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable*/

import { five } from "./routes";
import { five } from "./routes";
1 change: 1 addition & 0 deletions tests/fixtures/duplicate-imports/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./home";

0 comments on commit 559d195

Please sign in to comment.