Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

My project contains the following Gulp and http-server custom 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 its 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 would actually like my custom 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 script to NPM?" is not the same as what I am asking. I've already defined scripts in my package.json file, which is how I ended up with my current commands. What I want is to write my own project commands/scripts that do the same things as what I have listed, without having to type npm run.

Example

  • I Currently Have
    • npm run boom command
  • What Does it Do?
    • runs gulp command
  • What Command Do I Want Available in the Project Instead, That Does the Same Thing?
    • boom command
like image 906
SuitAndCape Avatar asked Jan 22 '16 22:01

SuitAndCape


Video Answer


1 Answers

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

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

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

If I recall correctly, npm link -g creates a symlink between your package and wherever your globally installed npm packages are kept which is preferable for me since I like to change things frequently.

This is a good resource on the topic.

like image 198
leesio Avatar answered Oct 12 '22 23:10

leesio