Skip to content

Commit

Permalink
Now able to copy a file to a directory, without providing the filename
Browse files Browse the repository at this point in the history
  • Loading branch information
paras20xx committed Jan 26, 2023
1 parent ea722c3 commit 6971763
Show file tree
Hide file tree
Showing 10 changed files with 1,543 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ Sample file: [copy-files-from-to.cjson](test/advanced-usage/copy-files-from-to.c
"to": "public/assets"
},

// Copy the file at the mentioned path into the target directory
{
"from": "src/app/images/favicon.ico",
"to": "public/"
},

// Copy the files matching the "glob" pattern (matching files, along with the their folder structure go into the "to" directory)
{
"from": "assets/**/*.jpg",
Expand Down
16 changes: 15 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ var main = function (params) {
}
return unixify(path.join(configFileSourceDirectory, from));
}()),
to: unixify(path.join(configFileSourceDirectory, to)),
to: (
(to.charAt(to.length - 1) === '/') ?
unixify(path.join(configFileSourceDirectory, to)) + '/' :
unixify(path.join(configFileSourceDirectory, to))
),
toFlat: toFlat,
removeSourceMappingURL: removeSourceMappingURL,
minify: minify
Expand Down Expand Up @@ -379,6 +383,16 @@ var main = function (params) {
return arr;
}());

copyFiles = copyFiles.map((copyFile) => {
if (
copyFile.to.charAt(copyFile.to.length - 1) === '/' &&
!isGlob(copyFile.intendedFrom)
) {
copyFile.to = path.join(copyFile.to, path.basename(copyFile.from));
}
return copyFile;
});

var writeContents = function (copyFile, options, cb) {
var to = copyFile.to,
intendedFrom = copyFile.intendedFrom;
Expand Down
1 change: 1 addition & 0 deletions test/test-copy-files-to-a-directory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target-dir/
20 changes: 20 additions & 0 deletions test/test-copy-files-to-a-directory/copy-files-from-to.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"copyFiles": [
{
"from": "source-dir/file1.txt",
"to": "target-dir/"
},
{
"from": "source-dir/file2.*",
"to": "target-dir/"
},
{
"from": "source-dir/*.md",
"to": "target-dir/"
},
{
"from": "https://raw.githubusercontent.com/webextensions/console-panel/v1.0.3/src/console-panel.js",
"to": "target-dir/remote/"
}
]
}
1,457 changes: 1,457 additions & 0 deletions test/test-copy-files-to-a-directory/expected-output/console-panel.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/test-copy-files-to-a-directory/source-dir/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file1.txt
1 change: 1 addition & 0 deletions test/test-copy-files-to-a-directory/source-dir/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file2.txt
1 change: 1 addition & 0 deletions test/test-copy-files-to-a-directory/source-dir/readme1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readme1.md
1 change: 1 addition & 0 deletions test/test-copy-files-to-a-directory/source-dir/readme2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readme2.md
40 changes: 40 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,45 @@ describe('package', function() {
}
);
});

it('should be able to copy files to a directory', function (done) {
const testDir = path.join(__dirname, 'test-copy-files-to-a-directory');
const cwdToUse = testDir;

rimrafSync(path.join(cwdToUse, 'target-dir'));

const
file1TxtSource = path.join(testDir, 'source-dir', 'file1.txt'),
file2TxtSource = path.join(testDir, 'source-dir', 'file2.txt'),
readme1MdSource = path.join(testDir, 'source-dir', 'readme1.md'),
readme2MdSource = path.join(testDir, 'source-dir', 'readme2.md'),
webFileSource = path.join(testDir, 'expected-output', 'console-panel.js');

const
file1TxtTarget = path.join(testDir, 'target-dir', 'file1.txt'),
file2TxtTarget = path.join(testDir, 'target-dir', 'file2.txt'),
readme1MdTarget = path.join(testDir, 'target-dir', 'readme1.md'),
readme2MdTarget = path.join(testDir, 'target-dir', 'readme2.md'),
webFileTarget = path.join(testDir, 'target-dir', 'remote', 'console-panel.js');

shell.exec(
path.join(__dirname, '..', 'index.js'),
{
silent: true,
cwd: cwdToUse
},
function (exitCode, stdout, stderr) { // eslint-disable-line no-unused-vars
expect(file(file1TxtTarget)).to.equal(file(file1TxtSource));
expect(file(file2TxtTarget)).to.equal(file(file2TxtSource));

expect(file(readme1MdTarget)).to.equal(file(readme1MdSource));
expect(file(readme2MdTarget)).to.equal(file(readme2MdSource));

expect(file(webFileTarget)).to.equal(file(webFileSource));

done();
}
);
});
});
});

0 comments on commit 6971763

Please sign in to comment.