Using angular-jsdoc I am using this command to generate my docs
node .\node_modules\jsdoc\jsdoc.js app -c .\node_modules\angular-jsdoc\common\conf.json -d docs -t .\node_modules\angular-jsdoc\angular-template\
I don't prefer typing such long command every-time I update my doc.
I would like to know some shortcut so that I can avoid typing or copy/pasting this command.
As the command indicates node is being used for execution. I would suggest using npm 's functionality to create project based commands.
This can be done as follows:
Create a package.json file in your master directory (using npm init).
Add the below json object in package.json file
"scripts": {
"gendoc":"node .\node_modules\jsdoc\jsdoc.js app -c .\node_modules\angular-jsdoc\common\conf.json -d docs -t .\node_modules\angular-jsdoc\angular-template\"
}
This will create a gendoc command for your project which can be executed from the command line as follows:
npm run gendoc
Now you don't need to type entire command, just enter the above command, npm will search for the scripts object in package.json and execute the script whose key is gendoc.
Related
I have the following scripts in my package.json file
"docker-build": "docker build -t mouchin/my-image-name .",
"docker-push": "docker push mouchin/my-image-name:latest",
"deploy-server": "ssh root#myserverip 'docker pull mouchin/my-image-name:latest'",
"deploy": "npm run docker-build && npm run docker-push && npm run deploy-server"
Problem is that i want to hide
mouchin/my-image-name and root#myserverip
Using some sort of env, maybe saving my variables in .env.prod , but i dont know if i can read the variables saved there directly into package.json
You can use environment variables in your rpm scripts just as you would if you execute the command on the command line (for example $SSH_HOST). However those variables will need to be set directly in the shell that executes the nom script.
Now in order to get the environment variables from an env file loaded, you have to do so manually. For example using a snippet like this:
if [ ! -f .env ]
then
export $(cat .env | xargs)
fi
Source
To execute this before any other script, you could use the built-in lifecylce scripts of npm.
Perhaps, you also want to change the snippet code to load one or the other .env file in case you have one for production and one for development. You will probably be able to use the environment variable NODE_ENV for this, as it is used in most setups, however this last step really depends on your build setup.
I am using Git Bash terminal on my Windows 10 machine. I have below versions of node and npm
$ node -v
v12.16.1
$ npm -v
6.13.4
However while running the command $ npx create-react-app my-app I am getting the below error
'create-react-app' is not recognized as an internal or external command,
operable program or batch file.
I have added the create-react-app location till npm in PATH variable and checked its present using the echo command.
Also I am able to run the above npx command in cmd but not in Git Bash. Is there anything different I have to do run it?
PATH : /c/Users/SAURABH/AppData/Roaming/npm
I am able to run the create-react-app by doing the below though:
C:/Users/SAURABH/AppData/Roaming/npm/create-react-app one-hello-world
Not sure why npx is not working
EDIT: I solved by adding the below to PATH
C:\Users\SAURABH\AppData\Roaming\npm\node_modules
But the above path has the create-react-app folder and not the batch file. It should point to some batch file right?
I have same issue on my mac, start using yarn create react-app my-app . This solution works for me.
You should try this command with cmd or PowerShell. it's a bug with GitBash.
I can solve this error with cmd.
I'm trying to manage the configuration of a react project on windows, it was previously running on mac. I'm using yarn build. inside the package.json scripts>build was configured as "rm-rf deployment/static;react-scripts build && mv build deployment/static". since the rm-rf and mv commands are for Linux, I tried using rmdir/del and move instead.. but it doesn't seem to work. I'm getting the error: Parameter format not correct - "static".
Windows Solution (cmd.exe)
The equivalent of that build script running via Command Prompt or PowerShell on Windows is:
"scripts": {
"build": "rd /s/q \"deployment/static\" 2> nul & react-scripts build && md \"deployment/static\" && move \"build\" \"deployment/static\""
}
Explanation:
The rm-rf equivalent for Windows (cmd.exe) is rd /s/q
The mv equivalent for Windows (cmd.exe) is move
All directory paths have been wrapped in escaped double quotes \"...\". For example;
deployment/static has been rewritten as \"deployment/static\".
Although escaped double quotes are not entirely necessary in this scenario, it's good practice and necessary to do this when paths may include spaces or punctuation characters.
The semi-colon ; has been replaced with the single & operator to ensure the react-script build part runs regardless of whether the initial rd /s/q ... command fails or succeeds.
The following error message would be printed to the console when using rd to delete a folder/path which may not exist:
The system cannot find the path specified
To prevent this error message from potentially being printed to the console we redirect the error message to NUL using the 2> nul part.
The md \"deployment/static\" part utilizes Windows md command to make the static directory - which is very similar to the mkdir command in bash.
Note: The above syntax will fail on nix based operating systems such as macOS and Linux.
Cross Platform Solution (Windows/Linux/macOS...)
To achieve a cross platform solution, (i.e. one which runs successfully on Windows, Linux, and macOS), I suggest writing two Nodejs utility scripts to substitute the rm -rf and mv bash commands. These two Nodejs scripts can then be invoked via your npm-script.
The following steps describe how this can be achieved.
Install shelljs which provides portable Unix shell commands for Nodejs. To do this, cd to your project directory an run the following command:
npm i -D shelljs
Create a new Nodejs script named rm.js with the following content:
rm.js
const shell = require('shelljs');
const args = process.argv.slice(2);
const dir = args[0];
shell.rm('-rf', dir);
Save this file in the root of your project directory, at the same level as where your projects package.json is stored.
Create a another Nodejs script named mv.js with the following content:
mv.js
const shell = require('shelljs');
const args = process.argv.slice(2);
const src = args[0];
const dest = args[1];
// Check src path has been provided and is valid
if (!src || !shell.test('-d', src)) {
console.log('\x1b[31m\x1b[40mERR!\x1b[0m src path cannot be found: %s', src);
process.exit(1);
}
// Check dest path has been provided.
if (!dest) {
console.log('\x1b[31m\x1b[40mERR!\x1b[0m dest path must be provided:');
process.exit(1);
}
// Make dest directory if necessary.
shell.mkdir('-p', dest);
// Move the file.
shell.mv(src, dest);
Also save this file in the root of your project directory, at the same level as where your projects package.json is stored.
Then configure your build script in package.json as follows:
"scripts": {
"build": "node rm \"deployment/static\" & react-scripts build && node mv \"build\" \"deployment/static\""
}
Note
The two utility scripts rm.js and mv.js are invoked in the npm-script named build via the parts reading; node rm ... and node mv ... respectively.
If you decide to store these two scripts in a different folder instead of the projects root directory, (as suggested in steps 2 and 3 previously), then you'll need to change the paths to the files. For example; if they were both saved in a folder named scripts which is located in the root of your project directory then your build script would be changed to:
"scripts": {
"build": "node scripts/rm \"deployment/static\" & react-scripts build && node scripts/mv \"build\" \"deployment/static\""
}
Edit / Update:
An alternative cross-platform solution, (which wasn't available when originally posting this answer), is to utilize the shx package, which is described as:
shx is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts
Run the following command to install shx:
npm i -D shx
Then change your build script in package.json as follows:
"scripts": {
"build": "shx rm -rf deployment/static & react-scripts build && shx mv build deployment/static"
}
I use rimraf for this very reason.
Install it globally
npm i -g rimraf
and update your script as follows
"rimraf deployment/static;react-scripts build && mv build deployment/static"
The shx package should work very well; they even show rm -rf in the topmost package.json demo
Some of my coworkers use rimraf which is specifically the rm -rf unix command for node
for Windows:
in client folder
},
"scripts": {
"build-win": "react-scripts build && move build ..\\server\\public",
},
that will move folder to server folder
I am having trouble getting Jenkins to use the "npm" command from the "Execute shell" (under the "Build" step).
I am attempting to set up a new Jenkins instance (a copy from a previous one). I just imported the old jobs and am now getting the various services that these jobs depend on (e.g. maven, nodejs, ansible etc.) installed.
I am having trouble getting nodejs and npm, in particular, set up.
When I type the command "npm install" on the command line, I get the following:
uws#9.14.0 install /var/lib/jenkins/workspace . . .
. . .
Binary is fine
added 1282 packages in 36.424s
When I then attempt to run the same command using the Jenkins execute shell, I get the following:
+ npm install
/tmp/jenkins7750702649955218109.sh: line 2: npm: command not found
Build step 'Execute shell' marked build as failure
Why would this command be accessible to me from the command line but not to Jenkins?
Some things I have checked:
-The path to "node" and "npm" are both on the path. At least "shortcuts" are on the path (this worked in the previous Jenkins instance).
-The node and npm binaries have root:root ownership, but their security settings are (currently) 755 (or -rwxr-xr-x).
-I have the nodejs plugin installed. Not sure of how to use it, but its settings match those on the previous Jenkins instance. Both instances use the execute shell (as opposed to any special Jenkins tool) to run the "npm" commands, whilst only the old version worked.
So, what else should I be checking that I am not? What does Jenkins require to access the "npm" command?
Try this below options in Jenkins
Option 1:
Option 2:
I'm trying to figure out how o update a single snapshot file. In the docs it says just add the -t and I presume the file name but thats not working for me.
Example, in terminal I use.
jest -u -t test/js/tests/components/myTestApp.test.js
Any ideas why this would not work. I also added it as a string.
According to doc https://facebook.github.io/jest/docs/en/cli.html -t option is searching not for file name, but for specific spec name. They provided an example
jest -u -t="ColorPicker"
where "ColorPicker" is name of the spec, given in describe or test blocks.
In order to update particular snapshot, you can pass name as an argument to your script test command.
In package.json
"test": "jest --verbose"
In command line
npm test "<spec name>" -- -u
OR
npm test "<spec name>" -- --updateSnapshot
this works
jest /path/to/file --updateSnapshot
If you run Jest via npm test, you can use arguments by inserting a -- between npm test and the Jest arguments - path & snapshot flag:
npm test -- path/to/file -u