Let’s say our contrived project directory is structured as follows:
.
├── Gruntfile.js
├── assets
│ └── zip
│ ├── file1.zip
│ ├── file2.zip
│ └── file3.zip
├── node_modules
│ └── ...
└── package.json
Then consider the following Gruntfile.js example that utilizes grunt-contrib-copy:
Gruntfile.js
module.exports = function(grunt) {
const target = grunt.option('target') || 'foobar'; // default dir is `foobar`.
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.initConfig({
copy: {
zips: {
files: [{
expand: true,
cwd: 'assets/zip',
src: '*.zip',
dest: 'projects/' + target
}]
}
}
});
grunt.registerTask('copy-zips', [ 'copy:zips' ]);
};
The output via the grunt
command.
If we
cd
to the project directory and run the followinggrunt
command:grunt copy-zips
the following folders (
projects/foobar
) are created in the root of the project directory.Results Example A
. ├── ... └── projects └── foobar ├── file1.zip ├── file2.zip └── file3.zip
Note the default
foobar
folder has been created containing a copy of the*.zip
files. The folder was namedfoobar
because we DID NOT provide an argument via the command line.Next, If we
cd
to the project directory again and run the following grunt command:grunt copy-zips --target=my-folder-name
the following folders (
projects/my-folder-name
) are created in the root of the project directory.Results Example B
. ├── ... └── projects └── my-folder-name ├── file1.zip ├── file2.zip └── file3.zip
Note This time the
my-folder-name
folder has been created containing a copy of the*.zip
files because we provided the--target=my-folder-name
argument/option.
The output via the npm
command.
Firstly let’s configure the scripts
section in the projects package.json as follows:
package,json
{
...
"scripts": {
"copy-zips": "grunt copy-zips"
},
...
}
If we
cd
to the project directory and run the followingnpm
command:npm run copy-zips
we yield the same results as shown in the aforementioned Results Example A section.
Next, If we cd to the project directory again and run the following
npm
command:npm run copy-zips -- --target=my-folder-name
we yield the same results as shown in the aforementioned Results Example B section.
Note: The additional
--
option betweennpm run copy-zips
and the argument--target=my-folder-name
.The npm docs describe the
--
option as follows:As of [email protected], you can use custom arguments when executing scripts. The special option
--
is used bygetopt
to delimit the end of the options. npm will pass all the arguments after the--
directly to your script:
Additional note:
If you don’t want to create the my-folder-name
folder inside the projects
folder, and instead you want to create the my-folder-name
folder in the root of the project directory, then you can redefine the dest
property in the copy
task as follows:
Gruntfile.js
// ...
grunt.initConfig({
copy: {
zips: {
files: [{
expand: true,
cwd: 'assets/zip',
src: '*.zip',
dest: target // <-----
}]
}
}
});
// ...
Running the same command:
npm run copy-zips -- --target=my-folder-name
yields the following result:
.
├── ...
└── my-folder-name
├── file1.zip
├── file2.zip
└── file3.zip
CLICK HERE to find out more related problems solutions.