JRJurman
JRJurman

Reputation: 1728

Rename file with NPM

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

Upvotes: 7

Views: 7386

Answers (3)

Mohammad Ayoub Khan
Mohammad Ayoub Khan

Reputation: 2970

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!

Upvotes: 0

JRJurman
JRJurman

Reputation: 1728

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.

Upvotes: 4

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24600

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

For example:

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

More Info:

Upvotes: 4

Related Questions