From 6e5f67808a9f118abc1d32263f98218cc88a1174 Mon Sep 17 00:00:00 2001 From: dpilafian Date: Mon, 7 Mar 2022 00:50:00 -0800 Subject: [PATCH] Support flat mode for copying a single file #100 --- index.js | 4 ++++ test.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/index.js b/index.js index d2eb105..4abb713 100644 --- a/index.js +++ b/index.js @@ -106,6 +106,10 @@ const preprocessDestinationPath = ({entry, destination, options}) => { return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath), path.relative(entry.pattern.originalPath, entry.path)); } + if (!entry.pattern.isDirectory && options.flat) { + return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath)); + } + return path.join(options.cwd, destination, path.relative(options.cwd, entry.path)); }; diff --git a/test.js b/test.js index f470c95..4c0c01a 100644 --- a/test.js +++ b/test.js @@ -247,6 +247,25 @@ test('flatten directory tree', async t => { ); }); +test('flatten single file', async t => { + fs.mkdirSync(t.context.tmp); + fs.mkdirSync(path.join(t.context.tmp, 'source')); + fs.writeFileSync( + path.join(t.context.tmp, 'source/bar.js'), + 'console.log("bar");', + ); + + await cpy('source/bar.js', 'destination', { + cwd: t.context.tmp, + flat: true, + }); + + t.is( + read(t.context.tmp, 'source/bar.js'), + read(t.context.tmp, 'destination/bar.js'), + ); +}); + // TODO: Enable again when ESM supports mocking. // eslint-disable-next-line ava/no-skip-test test.skip('cp-file errors are CpyErrors', async t => {