How to create a custom command to replace `gulp` or` npm run`?

How can I write my own terminal commands for an NPM project that would replace the gulp command without relying on npm run ?

My project contains the following gulp and http-server user commands ...

 ## COMMANDS I CURRENTLY HAVE | Command | Task | |-----------------------|-----------------------------| | npm run boom | "Builds then watches files" | | npm run boom -- build | "Builds the assets files" | | npm run launch | "Starts a local server" | 

This is because of the scripts in the package.json file ...

 /// package.json { "scripts": { "boom": "gulp", "launch": "http-server -o" }, "devDependencies": { "gulp": "^3.9.0", "gulp-sass": "^2.1.0", "http-server": "^0.8.5" } } 

Here is what I really would like my user commands to be ...

 ## COMMANDS I WANT TO HAVE | Command | Task | |-------------|-----------------------------| | boom | "Builds then watches files" | | boom build | "Builds the assets files" | | boom launch | "Starts a local server" | 

EDIT

Can I add a debug version of the script in NPM? ” Is not the same as me. I already defined the scripts in my package.json file, this is how I ended up with my current commands. I want to write my own project commands / scripts that do the same thing as what I listed, without entering npm run .

Example

  • I have
    • npm run boom team
  • What is he doing?
    • runs the gulp command
  • What command do I want in the project instead, which does the same thing?
    • boom team
+6
source share
1 answer

I usually do this by creating a small CLI using commander , then adding a bin entry to package.json pointing to the CLI.

  { ... "author": "", "license": "ISC", "bin":{ "boom": "boom-cli.js" ... } 

Then run either npm install -g or npm link -g from the project root.

If I remember correctly, npm link -g creates a symbolic link between your package and wherever globally installed npm packages are stored, which is preferable for me, because I like to change often.

This is a good resource on this topic.

+3
source

All Articles