From 8ebbf59cb3c6b40c46009e3dcacc4fceb462bfb3 Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Tue, 28 May 2019 13:57:16 -0400 Subject: [PATCH] Copy file into directory (#11) * Copy file into directory * Add test and check if file exists before stating * Format --- packages/io/__tests__/io.test.ts | 18 ++++++++++++++++++ packages/io/src/io.ts | 3 +++ 2 files changed, 21 insertions(+) diff --git a/packages/io/__tests__/io.test.ts b/packages/io/__tests__/io.test.ts index 95218250..045bb4dd 100644 --- a/packages/io/__tests__/io.test.ts +++ b/packages/io/__tests__/io.test.ts @@ -34,6 +34,24 @@ describe('cp', () => { ) }) + it('copies file into directory', async () => { + const root: string = path.join( + path.join(__dirname, '_temp'), + 'cp_file_to_directory' + ) + const sourceFile: string = path.join(root, 'cp_source') + const targetDirectory: string = path.join(root, 'cp_target') + const targetFile: string = path.join(targetDirectory, 'cp_source') + await io.mkdirP(targetDirectory) + await fs.writeFile(sourceFile, 'test file content') + + await io.cp(sourceFile, targetDirectory, {recursive: false, force: true}) + + expect(await fs.readFile(targetFile, {encoding: 'utf8'})).toBe( + 'test file content' + ) + }) + it('try copying to existing file with -n', async () => { const root: string = path.join(getTestTemp(), 'cp_to_existing') const sourceFile: string = path.join(root, 'cp_source') diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index 25811854..17740cda 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -267,6 +267,9 @@ async function move( await copyDirectoryContents(source, dest, force, moveOptions.deleteOriginal) } else { + if ((await ioUtil.exists(dest)) && (await ioUtil.isDirectory(dest))) { + dest = path.join(dest, path.basename(source)) + } if (force) { await ioUtil.copyFile(source, dest) } else {