Rename file with NPM

故事扮演 提交于 2019-12-23 09:34:35

问题


Is there a way to rename a single file in npm scripts? 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. 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.

My ideal solution is something I can include in the package.json, as a one line command, e.g. rename old-file-name new-file-name


回答1:


Sure. npm script can run any node js file you want.

For example:

require('fs').rename(oldPath,newPath)

More Info:

  • https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback
  • https://docs.npmjs.com/misc/scripts



回答2:


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. For renaming, you can use the sub-package cash-mv to rename a specific file.




回答3:


Configure the scripts section of your package.json as follows:

"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 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!



来源:https://stackoverflow.com/questions/35258871/rename-file-with-npm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!