节点NCP副本而不在命令行模式重写(无撞)
GNU副本具有-n
标志不覆盖,像这样复制:
cp -n config.ini.dist config.ini
有没有一种方式来获得the npm package ncp在命令行上相同的功能?
我的目标是复制一个分布式的配置文件的NPM包:
"scripts": {
"prepare": "ncp config.dist.ini config.ini -n",
...
然而,它不知道-n
标志,并覆盖现有文件。
如果向下滚动您提供的文件链接的页面,你会发现撞选项设置为false,会做你问什么。
options.clobber - 布尔=真。如果设置为false,NCP将不会覆盖已经存在的目标文件。
对于命令行使用,它似乎从看他们的源代码,该CLI不公开“撞”选项的CLI。幸运的是,它很容易这个自己用自己的小工具修复:
//./bin/ncp
#!/usr/env node
const ncp = require('ncp').ncp;
ncp.limit = 16;
// note: also add error handling. I'm also being explicit here, you can
// use their bin file for a more extensive example: https://github/AvianFlu/ncp/blob/master/bin/ncp
const source = process.argv[2];
const destination = process.argv[3];
// note you can get fancier and read the options in from the CLI if you want, I'm just assuming you know what you want to always use here.
ncp(source, destination, { clobber: false }, function (err) {
if (err) {
return console.error(err);
process.exit(1);
}
console.log('done!');
process.exit(1):
});
然后在你的package.json你可以做
"scripts": { "prepare": "./bin/ncp config.ini.dist config.ini"}}
希望帮助。