how to change "npm scripts" directory - npm

"scripts": {
...
...
"puppeteer": "cd test/UI-tests/src && set BROWSER=chrome && mocha test.js"
}
I want to wrap all the commands in "puppeteer", so once I type npm run puppeteer, it can set the browser and run the test.js file.
The way I write it does not work.

Related

package.json npm node scripts not minifying css; not excluding files

Here are my package.json scripts...
"scripts": {
"build:sass": "sass --no-source-map src/assets/scss:dist/assets/css",
"build:js": "uglifyjs-folder ./src/assets/js -o ./dist/assets/js/main.js",
"copy:assets": "copyfiles -e .src/assets/scss/**/* -e .src/assets/js/**/* -u 1 ./src/assets/**/* dist",
"copy:html": "copyfiles -u 1 ./src/*.html dist",
"copy": "npm-run-all --parallel copy:*",
"watch:assets": "onchange \"/src/assets/**/*\" -- npm run copy:assets",
"watch:html": "onchange \"src/*.html\" -- npm run copy:html",
"watch:sass": "sass --no-source-map --watch src/assets/scss:dist/assets/css",
"watch": "npm-run-all --parallel watch:*",
"serve": "browser-sync start --server dist --files dist",
"start": "npm-run-all copy --parallel watch serve",
"build": "npm-run-all copy:html build:*",
"postbuild": "postcss dist/assets/css/*.css -u autoprefixer cssnano -r --no-map"
}
For this line, I want to exclude the sass and js directories since they are handled by other scripts, but the exclusion isn't working, its still copying all the files in those directories.
"copy:assets": "copyfiles -e .src/assets/scss/**/* -e .src/assets/js/**/* -u 1 ./src/assets/**/* dist",
And for this line, the css file is updated but it is not minified
"watch:sass": "sass --no-source-map --watch src/assets/scss:dist/assets/css",
The build script works. The issues above happen with the start script.
Any help would be greatly appreciated.
Well I was able to find a work around, sort of. Here is what I did.
I installed the recursive-copy npm package.
npm i recursive-copy --save-dev
I created a copyfolder.js file in my utils folder with the following contents.
import copy from 'recursive-copy';
import { argv } from 'node:process';
const args = process.argv;
const src = args[2];
const dest = args[3];
var options = {overwrite: true,};
try {
const results = await copy(src, dest, options);
console.info('Copied ' + results.length + ' files');
} catch (error) {
console.error('Copy failed: ' + error);
}
then i put the following script in my package.json file. Notice it has the arguments that are passed to the copyfolder.js script which are the source and destination directories. Instead of trying to copy all folders with exclusions as I stated in the op, I created a script for each folder I wanted copied. For example I would use it for a Vendors or local bootstrap folder perhaps. My scss/css, images, and js folders are handled by the minifyers/compilers. The copyfolder is used for any other content I want to put in my dist or public builds.
"copyfolder:utils": "node utils/copyfolder.js ./src/assets/test ./dist/assets/test"
I also put a script in the package.json file that uses the npm onchange package to run the above copyfolder:utils script when anything in the source folder changes.
"watch:utils": "onchange \"src/assets/test/**/*\" -- npm run copyfolder:utils"

DRY way to change working directory for npm scripts

I'm just trying out NPM as my build system for a small project and I'd like to ask if there is a clean and easy to maintain way to change the working directory for the build scripts. My first thought was something like
"scripts": {
"cd:workdir": "cd src/path/to/my/work/dir/",
"task:1": "npm run cd:workdir && command1",
"task:2": "npm run cd:workdir && command2",
[...]
}
But it seams command* is executed in a different process then npm run cd:workdir, so this doesn't work.
The only working way I found so far is:
"scripts": {
"task:1": "cd src/path/to/my/work/dir/ && command1",
"task:2": "cd src/path/to/my/work/dir/ && command2",
[...]
}
But there must be a better way to do this to keep it DRY and better maintainable. Thanks!
I think you can use child_process.exec to execute some commands from a javascript file:
// task.js
const { exec } = require('child_process');
exec('command', {
cwd: 'src/path/to/my/work/dir/'
}, (error, stdout, stderr) => {
// handle error, stderr...
console.log(stdout)
});
and execute that javascript file using node in npm script: "task:1": "node task.js"

How to modify npm scripts in package.json for Windows [duplicate]

issue:
in script:
we want to check env. variable {dev/test/mock} and do following script run based on it.
if $mock is true the run script start-mock else go on reach real test server
scenario 1:
we added commands aggregated in package.json script section
e.g. : "test": "export NODE_ENV=dev; grunt", [on linux]
which is "test": "(SET NODE_ENV=dev) & (grunt)", [on win32]
scenario 2:
could be bat/sh script sitting in package and we called them out from package.json
scenario 3: (permanent solution)
not sure if its already available out there
something like
get arguments from script section: to give flexibility and freedom to end user.
e.g. : "test": "solution.env NODE_ENV=dev; solution grunt"
where we can have script to process (input with process.platform) out put depends on OS.
"start-pm2": "if \"%MOCK%\" == \"true\" ( npm run mock & pm2 start process.json --env test ) else ( pm2 start process.json )", [windows] for linux if.. fi
Use: run-script-os
For example:
// from pacakge.json
"scripts": {
// ...
"dist": "run-script-os",
"dist:win32": "tar -C dist -cvzf %npm_package_name%-%npm_package_version%.tgz .",
"dist:linux": "tar -C dist -cvzf $npm_package_name-$npm_package_version.tgz ."
},
Lets consider implementation of 3-th solution like e.g.
package.json
"scripts": {
"command" : "node bin/command.js"
}
bin/command.js
const spawn = require("child_process").spawn
const platform = require("os").platform()
const cmd = /^win/.test(platform)
? `${process.cwd()}\\bin\\command.bat`
: `${process.cwd()}/bin/command.sh`
spawn(cmd, [], { stdio: "inherit" }).on("exit", code => process.exit(code))
depends on environments script will execute command.bat or command.sh
You will need to implement solution 3.
You can use cross-env package that does it for you.

How add variable npm run build in package.json

I have 6 projects in an Angular workspace and I have to build each. Instead of write six lines in my package.json for each projet, for example :
"build_a":" npm run build a"
"buiild_b": "npm run build b"
I would like to create only one line like this :
"build_app": "npm run build name="aaa""
How I can do it ?
you could rely on environment variables in order to discover such names.
however it depends on which operating system you're using on how to define env variables.
"scripts":{
"build:a":"cross-env NAME=a npm run build",
"build:b":"cross-env NAME=b npm run build",
"build:c":"cross-env NAME=c npm run build",
"build":"browserify src/main.js -o build.js"
}
You would end up with a script section more or less like this.
Finally I found the solution using a node.js script: build-subproject.js.
const { exec } = require('child_process');
const args = process.argv.slice(2).join(' ');
console.log(`RUNNING build with args: ${args}`);
exec(
`ng build ${args} && cd dist/${args} && npm pack `,
(error, stdout) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.info(`stdout: ${stdout}`);
}
);
In package.json,
"build-subproject": "node ./build-subproject.js",
Then run , npm run build-subproject my-project-name

Using a package.json variable in npm script

I'm using npm run to build a javascript file through browserify. Before building, I would like it to create a directory in my build folder, named after the version listed in the package.json. Here is a trimmed example of my package.json:
{
"name": "My App",
"version": "0.0.0-pre-alpha",
"description": "App desc",
"main": "index.js",
"dependencies": {
"browserify" : "*",
}
"scripts": {
"prebuild": "mkdir -p build/$npm_package_version",
"browserify" : "browserify ./src/index.js ./build/$npm_package_version/js/myapp-$npm_package_version.js",
"build" : "npm run prebuild && npm run browserify"
}
}
The code executed in prebuild is:
mkdir -p build/$npm_package_version
But I want it to execute
mkdir -p build/0.0.0-pre-alpha
What am I doing wrong?
Update:
Turns out you can't use arguments with mkdir in a script. So i ended up using the mkdirp npm module.
Old post:
For others looking for an answer: Turns out when you are working in windows the correct way to use the variables is
%npm_package_version%
So the final code should look like:
"prebuild": "mkdir -p build/%npm_package_version%"