npm script shortcuts

Use npm scripts for frequently used commands

You can use the npm scripts to run bash scripts, and unleash the unlimited power of scripting.

You don’t have to memorize all your commands and their parameters.

If you frequently use certain commands, consider adding them to your package.json file as a script. For instance, if you want to use sfdx command to deploy the content of your force-app folder, you can do this simply:

json
"scripts" : {
    "deploy:all": "sf project deploy start  --source-dir force-app -w"
}

Now to execute your sfdx command you can type the following command in terminal:

bash
npm run deploy:all

Check the next page to see another examples

You can use the npm scripts to run bash scripts, and unleash the unlimited power of scripting.

json
"scripts": {
    "hello": "bash hello.sh"
}

Create a file ‘hello.sh’ with the script (in this case it should belong to the root folder of your project):

bash
#!/bin/bash

echo "Hello World"

Now to execute your bash script you can type the following command in terminal:

bash
npm run hello

Another useful example is to run the Anonymous Apex. Let me show you how this could be used to enable debug mode without using UI.

json
"scripts": {
    "debug-mode": "sfdx apex:execute -f  \"debugMode.apex\""
}

Create an apex script in file ‘debugMode.apex’

apex
update new User(Id = UserInfo.getUserId(), UserPreferencesUserDebugModePref = true);

Now to execute your sfdx command you can type the following command in terminal:

bash
npm run debug-mode

Don’t forget, also to check what the npm scripts that are already existing in your package.json file.

There are probably some useful commands already created there.

npm script shortcuts — screenshot 5

I would also recommend checking a great article about npm scripts by Philippe Ozil (link in the comment)

Do you have any favourite npm scripts that boost your productivity? Share them in the comments!

Also, don’t forget to follow our page to get other examples and tips!

Text and code were extracted from the original slide. Plain-text version of the whole catalog