[英]Rename file with NPM
Is there a way to rename a single file in npm scripts? 有没有办法在npm脚本中重命名单个文件? I want to prepare files for distribution, but I need the built files to be named differently than they are in the source...
我想准备分发文件,但我需要建立的文件的命名方式与它们在源代码中的不同...
I have tried orn, but that only seems to work on the command line, not as an npm script. 我尝试过orn,但这似乎只能在命令行上运行,而不是作为npm脚本。 I'm specifically looking to just add a cross-platform dependency to do my project, rather than writing my own javascript script to copy files over.
我特别想要添加一个跨平台的依赖项来完成我的项目,而不是编写我自己的javascript脚本来复制文件。
My ideal solution is something I can include in the package.json
, as a one line command, eg rename old-file-name new-file-name
我理想的解决方案是我可以在
package.json
包含的内容,作为一行命令,例如rename old-file-name new-file-name
Sure. 当然。
npm script
can run any node js file you want. npm script
可以运行你想要的任何节点js文件。
For example: 例如:
require('fs').rename(oldPath,newPath)
More Info: 更多信息:
Configure the scripts section of your package.json as follows: 配置package.json的脚本部分,如下所示:
"scripts": {
"rename": "node -e \"require('fs').rename('C:/abc.text', 'C:/xyz.text', function(err) { if (err) console.log(err); console.log('File renamed!') })\""
},
Then run the following npm command: 然后运行以下npm命令:
npm run copy-and-rename
On successful completion you should see the following logged to the console after the file has been copied and renamed: 成功完成后,您应该在复制和重命名文件后看到以下内容登录到控制台:
File successfully renamed!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.