In an NPM project, I'd like to have a commit for each build version. This will allow me to go back to the current build version, fix a bug, without having to go through all the QA of a new version.
We can commit using npm scripts like this (see this answer):
package.json
"scripts": {
"git": "git add . && git commit -m",
}
Then invoke the script by running:
npm run git -- "Message of the commit"
I'd like to automate it to run after npm run build. For this purpose we can create a new command.
package.json
"scripts": {
"buildAndCommit": "npm run build && git add . && git commit -m",
}
This could be run using npm run buildAndCommit -- "commit for a new build"
The only thing left is that I'd like to identify this commit as one that could be linked to a commit. Is it possible to start the message automatically with "BUILD -" and to add to that the unique message which is passed in the command line? Something like:
package.json
"scripts": {
"buildAndCommit": "npm run build && git add . && git commit -'Build' + $uniqueMessageFromTheCommandLine`",
}
If it is not possible to template the string in package.json, how could I achieve it using a command line script? (Powershell is my command line tool).
Running on *nix platforms
On a *nix platform npm utilizes sh by default to execute the npm script(s). In this scenario you can simply use a shell function and reference the git message argument passed via the CLI using the $1 positional parameter.
Your npm script would be redefined like this:
"scripts": {
"build": "...",
"buildAndCommit": "func() { npm run build && git add . && git commit -m \"BUILD - $1\"; }; func"
}
Cross platform
Unfortunately, via Windows Powershell the solution is not quite as simple and terse.
When using Powershell npm utilizes cmd by default to execute npm script(s). Likewise npm utilizes cmd by default via other Windows consoles too, such as Command Prompt.
One way to achieve your requirement is to invoke a node.js via your npm script. The following provides two different methods that are essentially the same. Either will run successfully cross-platform (in your case via Powershell).
Method A - Using a separate node.js script
Create the following node.js script. Let's name the file script.js and save it in the root of the project directory, i.e. in the same directory where package.json resides.
script.js
const execSync = require('child_process').execSync;
const mssg = 'BUILD - ' + process.argv[2];
execSync('npm run build && git add . && git commit -m \"' + mssg + '\"', { stdio:[0, 1, 2] });
Explanation
The node.js builtin process.argv captures the argument at index two, i.e. the git commit message, that was provided via the CLI. The git commit message is concatenated with with the substring BUILD - to form the desired commit message. The resultant string is assigned to the variable mssg.
We then utilize the node.js builtin execSync() to execute your given npm script. As you can see, the value of the mssg variable is used as the git commit message.
The stdio option is utilized to ensure the correct configuration of the pipes, i.e. stdin, stdout, 'stderr', are established between the parent and child process.
Define your npm script named buildAndCommit as follows:
package.json
"scripts": {
"build": "...",
"buildAndCommit": "node script"
}
Above node invokes script.js.
Method B - Inline the node.js script in npm script
Alternatively, the aforementioned node.js script (i.e. script.js) can be provided inline in your npm script instead - therefore negating the use of a separate .js file.
package.json
"scripts": {
"build": "...",
"buildAndCommit": "node -e \"const mssg = 'BUILD - ' + process.argv[1]; require('child_process').execSync('npm run build && git add . && git commit -m \\\"' + mssg + '\\\"', { stdio:[0, 1, 2] })\""
}
This utilizes the same code from Method A albeit it slightly refactored. The notable differences are:
The nodejs command line option -e is utilized to evaluate the inline JavaScript.
process.argv this time will capture the argument, i.e. the git commit message, at index one in the array of arguments.
Additional escaping of the double quotes is necessary, i.e. \\\"
Running the npm script
Using either Method A or Method B run the command via your CLI as desired: For instance:
$ npm run buildAndCommit -- "commit for a new build"
This will produce the following git commit message:
BUILD - commit for a new build
Related
How can a postinstall script be restricted to run only on macOS?
I have a shell script inside my React native library and it needs to be started when the npm install has completed.
This works great with postinstall but the problem is that Windows can't execute the shell script.
"scripts": {
"postinstall": "./iospatch.sh"
},
I need a way to limit that, to only run on macOS.
I tried with this library but it didn't work for my case
https://www.npmjs.com/package/cross-os
For cross-platform consider redefining your npm script as follows. This ensures that the shell script (.sh) is run only, via the postinstall npm script, when installing your package on macOS.
"scripts": {
"postinstall": "node -e \"process.platform === 'darwin' && require('child_process').spawn('sh', ['./iospatch.sh'], { stdio: 'inherit'})\""
}
Explanation:
The node -e \"...\" part utilizes the Node.js command line option -e to evaluate the inline JavaScript as follows:
process.platform === 'darwin' utilizes the process.platform property to identify the operating system platform. If it equals darwin then it's macOS.
The last part on the right-hand side of the && operator;
require('child_process').spawn('sh', ['./iospatch.sh'], { stdio: 'inherit'})
is executed only if the expression on the left-hand side of the && operator is true, i.e. it only runs if the platform is macOS.
That part of the code essentially utilizes the child_process.spawn() method to invoke your .sh file. The stdio option is set to inherit to configure the pipes for stdin, stdout, stderr in the child process.
Also note the command passed to child_process.spawn() is sh and the argument is the file path to the shell script, i.e. ['./iospatch.sh']. We do this to avoid having to set file permissions on macOS so that it can execute iospatch.sh.
I tried various solutions for this question but not able to pass 2 test cases amongst 4. Please help me crackdown this problem
Perform the below mentioned steps by creating package.json file named npm_commands in the maxbot directory
Create a file named index.js.
Write a js code in index.js file to create a string named myVar and value as node package manager
and convert it into uppercase.
NOTE: Please use console.log to display the output of myVar in index.js file
Configure scripts in package.json
(a) to check the versions of npm and node by using npm run release | tee output1.txt
(b) to execute index.js by using npm run build | tee output.txt
npm init - to initialize new package
give npm_commands as package name
give index.js as start file
Create new file named as index.js
declare : let myVar = "node package manager"
print var in uppercase: console.log(myVar.toUpperCase());
Add these scripts in package.json
to check npm and node version: "release": "npm -v && node -v",
to execute index.js file: "build": "node index.js"
Save and then run these commands to get the results:
npm run release | tee output1.txt
npm run build | tee output.txt
Step 1: run the command to initialize new package
npm init
Step 2: Command will start package initialization process
Set the package name:
package name: (challange) npm_commands
Step3: Press enter till time it ask for "entry point", by default it set to index.js verify and press enter key. Until the initialization process ends.
Step 4: Go to editor project explorer add create new file index.js, and add following code in it.
let myVar = 'node package manager';
console.log(myVar.toUpperCase());
Step 5: Add following lines into script block of package.json file
"release" : "npm -v && node -v",
"build" : "node index.js",
Script block looks like as below after adding above lines
"scripts": {
"release" : "npm -v && node -v",
"build" : "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Step 6: Now open the command prompt and run the following commands
npm run release | tee output1.txt
npm run build | tee output.txt
Above commands create the output1.txt, and output.txt files in project explorer containing respective commands output.
Step 7: That's it! now run the test and check your FS_SCORE for 100% and submit the test.
With npm or yarn, is it possible for the script specified by an npm script to know the name of the npm script itself? For example:
"scripts": {
"foo": "echo Original command: $0",
"bar": "echo Original command: $0"
}
I'd like the result of those two scripts to be something like:
Original command: yarn run foo
Original command: yarn run bar
But all I actually get is: Original command: /bin/sh.
And in case it makes a difference, it's just the name of the script I need, not the yarn run part, so output like Original command: foo would be fine.
NPM adds the npm_lifecycle_event environment variable. It's similar to package.json vars.
*Nix (Linux, macOS, ... )
On *nix platforms npm utilizes sh as the default shell for running npm scripts, therefore your scripts can be defined as:
"scripts": {
"foo": "echo The script run was: $npm_lifecycle_event",
"bar": "echo The script run was: $npm_lifecycle_event"
}
Note: The dollar prefix $ to reference the variable.
Windows:
On Windows npm utilizes cmd.exe as the default shell for running npm scripts, therefore your scripts can be defined as:
"scripts": {
"foo": "echo The script run was: %npm_lifecycle_event%",
"bar": "echo The script run was: %npm_lifecycle_event%"
}
Note: The leading and trailing percentage sign % used to reference the variable.
Cross-platform (Linux, macOS, Windows, ... )
For cross-platform you can either:
Utilize cross-var to enable a single syntax, i.e. using the dollar sign prefix $ as per the *nix syntax.
Or, utilize the node.js command line option -p to evaluate and print the result of the following inline JavaScript:
"scripts": {
"foo": "node -e \"console.log('The script run was:', process.env.npm_lifecycle_event)\"",
"bar": "node -e \"console.log('The script run was:', process.env.npm_lifecycle_event)\""
}
Note In this example we:
Access the npm_lifecycle_event environment variable using the node.js process.env property.
Utilize console.log (instead of echo) to print the result to stdout
I'm using commander.js command like this ./index.js --project mono --type item --title newInvoice --comments 'Creates an invoice' --write, Now I'm using the command through npm run item newInvoice by setting some options in package.json file like this
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"snapshot": "node --max-old-space-size=10240 ./scripts/snapshot.js",
"item": "./index.js --project mono --type item --title",
"config": "./index.js --project mono --type config --title"
}
But whenever I'm trying to get the --write option with npm using npm run item newInvoice --write it's showing undefined for --write
Source Code:
#!/usr/bin/env node
const fs = require('fs');
const program = require('commander');
require('colors');
program
.version('0.1.0')
.option('--project [project]', 'Specifies the project name', 'mono')
.option('--type [type]', 'Type of code to generate, either "item" or "config"', /^(config|item)$/, 'config')
.option('--title [title]', 'Title of the item or config', 'untitled')
.option('--comments [comments]', 'Configs: describe the config', '#todo description/comments')
.option('--write', 'Write the source code to a new file in the expected path')
.option('--read', 'To see what would be written the source code to a new file in the expected path')
.parse(process.argv);
console.log(program.write, program.read); //=> undefined undefined
Can anyone help me how to use commander js command with npm?
When you run your npm run command you need to utilize the special -- option to demarcate the end of any option(s) that may belong to the npm run command itself (e.g. --silent), and the beginning of the argument(s) that are to be passed to the end of the npm script.
Run the following command instead:
npm run item -- newInvoice --write
Given the aforementioned command and the command currently defined your npm script named item they essentially form the following compound command prior to execution:
./index.js --project mono --type item --title newInvoice --write
^ ^
The npm run documentation states the following:
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script.
and it's usage syntax is defined in the Synopsis section as:
npm run-script <command> [--silent] [-- <args>...]
^^
Note: It's not possible to add the -- option to the npm script itself.
I want to to define a scripts entry in my package.json to simplify building for several environments.
In the script execution I need to replace $1 (or whatever syntax I need for a placeholder) by a parameter I would pass to npm run build-script like for example --env=prod or even simpler, --prod. How can I do that? Other questions and answers I found here didn't help me solve the problem.
"scripts": {
"build-for": "ng build --output-path=../../web/admin-v2 --env=$1 --base-href=\"/admin-v2/\""
}
I often resort to creating a utility node script for this kind of scenario and invoke it via the scripts section of package.json.
build-for.js
var nodeCLI = require('shelljs-nodecli');
var env = '--env=foo'; // <-- Default env flag/value when no arg provided.
if (process.argv.indexOf('--prod') !== -1) {
env = '--env=prod';
}
// Check for other possible env values
if (process.argv.indexOf('--quux') !== -1) {
env = '--env=quux';
}
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
build-for.js utilizes nodes process.argv to ascertain the argument/flag passed via the CLI and then invokes the ng command (the one currently defined in your package.json) using shelljs-nodecli.
npm i -D shelljs-nodecli
Lets assume that build-for.js is saved to a hidden folder named .scripts in your projects root directory; then your scripts section of package.json will be defined as follows:
package.json
{
...
"scripts": {
"build-for": "node ./.scripts/build-for"
},
...
}
Running the script
Invoke the npm script by running:
npm run build-for -- --prod
Note the special -- before the argument --prod must be included as explained here
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:
Given the logic curently in the build-for.js - when no arguments are passed, for example:
npm run build-for
...the env flag will be set to --env=foo
Running the following:
npm run build-for -- --quux
...will result in the env flag will be set to --env=quux
Caveat
I haven't fully tested build-for.js, so you may find that you don't need to escape the double-quotes in this part '--base-href=\"/admin-v2/\"' of the following command (nodeCLI may handle that for you.):
// Run the ng build command
nodeCLI.exec('ng', 'build', '--output-path=../../web/admin-v2', env, '--base-href=\"/admin-v2/\"');
Not exactly what you're looking for but you can use environment variables & provide them inline:
package.json script:
"<script>": "echo ${ENV1} ${ENV2}"
run like so:
ENV1=a ENV2=b npm run <script>
$ npm run <script>
a b