admin管理员组

文章数量:1279043

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 mand 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 mand, e.g. rename old-file-name new-file-name

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 mand 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 mand, e.g. rename old-file-name new-file-name

Share Improve this question edited Feb 8, 2016 at 6:40 MartyIX 28.7k32 gold badges139 silver badges217 bronze badges asked Feb 7, 2016 at 20:42 JRJurmanJRJurman 1,7282 gold badges19 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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

For example:

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

More Info:

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

Turns out the npm library cash offers several basic mand line utilities that can be run as a single line mand from a package.json. For renaming, you can use the sub-package cash-mv to rename a specific file.

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 mand:

npm run copy-and-rename

On successful pletion you should see the following logged to the console after the file has been copied and renamed:

File successfully renamed!

本文标签: javascriptRename file with NPMStack Overflow