Skip to content

Commit

Permalink
fix: don't overwrite existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 19, 2020
1 parent 54bd159 commit 9a2c6c4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"--runInBand",
"end-to-end",
"-t",
"linked-files"
"dir-and-file-same-name"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
Expand Down
21 changes: 20 additions & 1 deletion src/index/formatFileStructure/moveFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ export async function moveFiles(
) {
const git = Git(parentFolder);
const isFolderGitTracked = await git.checkIsRepo();
for (const [oldPath, newPath] of Object.entries(tree)) {
const entries = Object.entries(tree);
const fileAlreadyExistsEntries = [] as typeof entries;
while (entries.length || fileAlreadyExistsEntries.length) {
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const [oldPath, newPath] = entries.length
? entries.pop()!
: fileAlreadyExistsEntries.pop()!;
/* eslint-enable */
// skip globals
if (oldPath.includes("..")) continue;

Expand All @@ -33,6 +40,18 @@ export async function moveFiles(
const newDirname = path.dirname(newAbsolutePath);
fs.ensureDirSync(newDirname);

if (fs.existsSync(newAbsolutePath)) {
if (entries.length) {
// try moving this file later after the other files have been moved
fileAlreadyExistsEntries.push([oldPath, newPath]);
} else {
logger.warn(
`not moving "${oldAbsolutePath}" to "${newAbsolutePath}" because "${newAbsolutePath}" already exists`
);
}
continue;
}

const shouldGitMv =
isFolderGitTracked && (await isFileGitTracked(git, oldAbsolutePath));

Expand Down
22 changes: 22 additions & 0 deletions tests/__snapshots__/end-to-end.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ import \\"@testing-library/jest-dom/extend-expect\\";
"
`;
exports[`end-to-end --avoid-single-file dir-and-file-same-name 1`] = `
"dir-and-file-same-name
├── cost-of-living
│ └── cost-of-living.js
└── cost-of-living.js"
`;
exports[`end-to-end --avoid-single-file dir-and-file-same-name: dir-and-file-same-name/cost-of-living.js 1`] = `"require('./cost-of-living')"`;
exports[`end-to-end --avoid-single-file dir-and-file-same-name: dir-and-file-same-name/cost-of-living/cost-of-living.js 1`] = `""`;
exports[`end-to-end --avoid-single-file duplicate-imports 1`] = `
"duplicate-imports
├── index
Expand Down Expand Up @@ -992,6 +1003,17 @@ import \\"@testing-library/jest-dom/extend-expect\\";
"
`;
exports[`end-to-end dir-and-file-same-name 1`] = `
"dir-and-file-same-name
├── cost-of-living
│ └── cost-of-living.js
└── cost-of-living.js"
`;
exports[`end-to-end dir-and-file-same-name: dir-and-file-same-name/cost-of-living.js 1`] = `"require('./cost-of-living/cost-of-living')"`;
exports[`end-to-end dir-and-file-same-name: dir-and-file-same-name/cost-of-living/cost-of-living.js 1`] = `""`;
exports[`end-to-end duplicate-imports 1`] = `
"duplicate-imports
├── index
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../cost-of-living')

0 comments on commit 9a2c6c4

Please sign in to comment.