running windows Comman_Prompt command through package.json scripts - node.js

I need to run in command line (cmd)
npm run build
and after that, I need to run
xcopy C:\fileOne C:\fileTwo
BUT, I would like to run only one command and to execute both of those above.
So I thought maybe my package.json should look like this:
"scripts": {
"build": "react-scripts build",
"copy": "xcopy C:\path\firstFile C:\path\secondFile",
"zack": "npm run build && npm run copy",
},
based on this idea:
"scripts": {
"a1": "command1",
"a2": "command2",
"zack": "npm run a1 && npm run a2",
},
and then I could run in command line:
npm run zack
but I'm not managing to make it happen
(the reason why I'm doing this, is: I want to change source code in SubliteText 3 (HTML JS CSS) and automatically to send (copy-paste) in Eclipse (in some other project) )
I want to do this:
My main problem is how to put local directory path inside the string in package.json file.

Instead of writing commands directly inside the package.json you should write a script file that handles it for you.
Something like
var fs = require('fs');
fs.createReadStream('PATH_TO_FILE_ONE').pipe(fs.createWriteStream('PATH_TO_NEW_FILE'));
save the above script as something like afterBuild.js
and in your package.json just do zack as npm run build && node afterBuild.js

I installed npc module:
npm install ncp -g
npm install ncp --save
then I created a file: afterBuild.js and I inserted this code inside:
var ncp = require("ncp");
ncp("C:/directory/file.txt", "C:/destination/directory/file.txt", callback);
function callback(){} //empty function, I don't need it for the start
and in package.json i wrote:
"scripts": {
"build": "react-scripts build",
"zack": "npm run build && node afterBuild.js"
},
and I run it form command line like:
npm run zack

Related

How to call Terminal Commands in a TEXT File?

I really need help on this, couldn't find much examples on google.
For example:
I have this web project. Inside this project there is, app.js, views/home.ejs, some npm packages and etc...
This project, we are not allowed to type "npm install" "npm start" to run our project or we get a zero. The teacher will only type 1 line in terminal which is "run app.py" for python or "run js app" to run our code, will not install packages on localhost.
Wants us to make a textfile to automatically install all packages in the background and run the application automatically? How would I do that?
For example in text file:
inside TXT.FILE {
#1 run "npm install express" in terminal
#2 run "npm install body-parser" in terminal
#3 run "node app.js" in terminal
#4 also run "ls"
}
Basically just call terminal commands in a text file. A text file that will automatically execute them in order.
You have two options.
Add this into your package.json file
...
"scripts": {
...
"start": "npm install express && npm install body-parser && node app.js && ls"
},
Now you can use npm start to run all these commands at one go.
Add a bash script in your project directory.
The file should be named your-script-name.sh.
Inside add
#!/bin/bash
npm install express && npm install body-parser && node app.js && ls
You can run the script using ./your-script-name.sh in your terminal.
"scripts": {
...
"start": "npm install express && npm install body-parser && node app.js && ls"
},
Yes this works very well, tested it! Thank you.
This calls terminal commands under 1 line of code!

package.json: what is the difference between & and &&?

Title pretty much says it all, but I'd also like to know if these commands work or behave differently depending upon the OS.
example1:
"scripts": {
"build": "babel -d serverbuild ./server",
"exe": "node ./serverbuild/index.js",
"start": "npm run build && npm run exe"
}
example2:
"scripts": {
"build": "babel -d serverbuild ./server",
"exe": "node ./serverbuild/index.js",
"start": "npm run build & npm run exe"
}
Given these examples portions of a package.json, what would be the difference between npm run start?
When using &&, the first command will run, and if it does not error, the second command will run. It's like a logical AND.
Using &, however, will run a command in the background. So in your second package.json, npm run build will start running in the background and then npm run exe will run as well regardless of what happens to the first command.

How to run npm test after npm start in bash script

In my docker file, I want to run test script inside it after app is up in Docker dev version. Problem is when I created a bash script and add like that, test script is not working.
Here is my package json file:
"scripts": {
"build": "./node_modules/babel-cli/bin/babel.js src --out-dir lib --copy-files lib --plugins transform-react-jsx --presets es2015",
"bundle": "./node_modules/browserify/bin/cmd.js lib/client.js -o public/js/bundle.js",
"start": "npm run build && npm run bundle && node lib/server.js",
"test": "mocha ./src/*.test.js"
},
and here is bash script file
#!/bin/bash
npm start
npm run start
npm test
npm run test
Please let me know how to fix it, thanks much.
#!/bin/bash
npm run build
npm run bundle
forever start ./lib/server.js
npm test
I've found solution that I need to install forever or pm2 first.

Pass npm script command line arguments to a specific script inside it

I have a scenario where I have to run three npm script to achieve some result in my application. I combined them in one npm script. This is my package.json:
"scripts": {
"setup": "npm install && npm run some-script && npm install",
"some-script": "gulp some-other-script"
}
what I would like to do is to pass arguments to setup script from command line which will be passed further to the some-script script.
If I run npm run script -- --abc=123 the arguments are added at the end of the script but I would like to pass it to specific script (in this case to npm run some-script). I also tried to rewrite script definition like this:
"setup": "npm install && npm run some-script -- --sample=sample&& npm install" but with no luck.
I'm aware of shell functions (described here: Sending command line arguments to npm script and here https://github.com/npm/npm/issues/9627) but I need a solution which will work cross-platform.
Is there a way to do that?
Ok I found a work around.
1st goal was to be able to use some script with arguments, and cascade this to the calls of other scripts :
npm run main -- --arg1
"main": "npm run script1 && npm run script2"
Problem with this approach is that the cascading would only be done by adding the arg passed to npm run main at the END of the line "npm run script1 && npm run script2". I could find no way to pass it to the 1st element : npm run script1
Here is the work around I found :
1st you need to add in package.json:
"config": {
"arg1": "ARG_VALUE"
},
Then in your script you can add it to the call like this :
"main": "npm run script_no_arg1 && npm run scrip1 -- --%npm_package_config_arg1% && npm run script2 -- --%npm_package_config_component% && npm run script_no_arg2"
Finally, You don't need to call it with any arg : npm run main
But you need to modify ARG_VALUE before launc the script :)
Last thing : in my case I was calling gulp tasks :
"cleardown": "rimraf build lib",
"inline-build-templates": "gulp inline-build-templates",
"step1": "npm run cleardown && npm run inline-build-templates -- --%npm_package_config_arg1%",
It works you can get the argument into the gulp task that way :
gulp.task('inline-build-templates', function() {
let component = process.argv[3];
component = component.split('--')[1];
console.log('RUNNING GULP TASK : inline-build-templates for component ' + component);
// whatever task gulp
});
Hope it helps.

Run npm from subdirectories on Heroku

I have a project which contains 2 subprojects:
First is the API
Second is the client
And both of these projects have their own dependencies mapped in their own packages.json files, which is placed in each subdir.
So the question is how to run npm install from sub directories on heroku?
I tried putting something like this in the main npm file
"scripts": {
"postinstall": "cd my_subdir; npm install"
}
But it doesn't work, showing can't cd to my_subdir
Utilize npm's --prefix option:
"scripts": {
"postinstall": "npm install --prefix ./my_subdir"
}

Resources