简体   繁体   English

使用NPM重命名文件

[英]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: 更多信息:

Turns out the npm library cash offers several basic command line utilities that can be run as a single line command from a package.json. 结果是npm库现金提供了几个基本的命令行实用程序,可以作为package.json中的单行命令运行。 For renaming, you can use the sub-package cash-mv to rename a specific file. 对于重命名,您可以使用子包cash-mv重命名特定文件。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM