From 9a2c6c4bed1798eaa57c90383bb803bc9f7fb9d5 Mon Sep 17 00:00:00 2001 From: Ben Awad Date: Tue, 18 Aug 2020 21:46:21 -0500 Subject: [PATCH] fix: don't overwrite existing files --- .vscode/launch.json | 2 +- src/index/formatFileStructure/moveFiles.ts | 21 +++++++++++++++++- tests/__snapshots__/end-to-end.test.ts.snap | 22 +++++++++++++++++++ .../dir-and-file-same-name/cost-of-living.js | 0 .../cost-of-living/index.js | 1 + 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/dir-and-file-same-name/cost-of-living.js create mode 100644 tests/fixtures/dir-and-file-same-name/cost-of-living/index.js diff --git a/.vscode/launch.json b/.vscode/launch.json index 851db88..db0911c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "--runInBand", "end-to-end", "-t", - "linked-files" + "dir-and-file-same-name" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen" diff --git a/src/index/formatFileStructure/moveFiles.ts b/src/index/formatFileStructure/moveFiles.ts index 6bd4915..1ade391 100644 --- a/src/index/formatFileStructure/moveFiles.ts +++ b/src/index/formatFileStructure/moveFiles.ts @@ -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; @@ -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)); diff --git a/tests/__snapshots__/end-to-end.test.ts.snap b/tests/__snapshots__/end-to-end.test.ts.snap index addc10b..c2b3c30 100644 --- a/tests/__snapshots__/end-to-end.test.ts.snap +++ b/tests/__snapshots__/end-to-end.test.ts.snap @@ -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 @@ -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 diff --git a/tests/fixtures/dir-and-file-same-name/cost-of-living.js b/tests/fixtures/dir-and-file-same-name/cost-of-living.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/dir-and-file-same-name/cost-of-living/index.js b/tests/fixtures/dir-and-file-same-name/cost-of-living/index.js new file mode 100644 index 0000000..0d90dc6 --- /dev/null +++ b/tests/fixtures/dir-and-file-same-name/cost-of-living/index.js @@ -0,0 +1 @@ +require('../cost-of-living') \ No newline at end of file