condition Script in package.json - node.js

I have two questions:
How I can do this npm run script --production instead npm run script --env=production
How I want to pass as an argument to another script something like, if you want to create database on development you just run npm run dev --development --create-database or if you wanna drop development database npm run dev --development --drop-database, this is because I want to add one script to handle create or drop database but if you are doing this in production database, it will throw you a warning..
This is what I have until now
"dev": "NODE_ENV=development nodemon src/serve.js",
"start": "NODE_ENV=production src/serve.js",
"test": "NODE_ENV=test jest --testTimeout=10000 --runInBand --detectOpenHandles",
"db:create": "NODE_ENV=$npm_config_env npm run db:condition",
"db:drop": "NODE_ENV=$npm_config_env npx sequelize-cli db:drop",
"db:condition": "if [[ ${NODE_ENV} == \"production\" ]]; then npm run warning; else npm run db:reset; fi",
"db:reset": "npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all",
"warning": "echo \"You can't do this on production\""
If you can see I only add condition for "db:create".

Related

Concatenating command line arguments and flags in NPM package.json scripts

I am looking for a way to do something like this:
package.json
[...]
"scripts": {
"test": "cd ./apps/my-awesome-app && npx cypress run",
"test:watch": "npm run test && --headed",
[...]
so then at the command line, running
npm run test:watch
will result in the following commands being executed:
cd ./apps/my-awesome-app
npx cypress run --headed
However, this is not working as expected. Is there a way to achieve this without repeating the whole "test"-string?

Publish NPM package from inside docker container

I am desperately trying to publish my NPM package to our NPM repo.
I keep getting an error stating that my working directory is not clean and I can't get my head around it.
This is my Dockerfile:
FROM node:12
ARG VERSION
COPY .npmrc /root/.npmrc
COPY .gitconfig /root/.gitconfig
COPY .git-credentials /root/.git-credentials
WORKDIR /home/node/app/
COPY package.json package.json
RUN npm install
COPY . .
RUN npm run release:testless -- ${VERSION}
package.json:
"scripts": {
"prepare": "npm run prepare:util",
"prepare:util": "npm explore vl-ui-util -- npm run install:copy",
"test": "wct -l chrome,firefox --npm",
"release": "npm run release:prepare && np",
"release:prepare": "npm run release:prepare:build",
"release:prepare:build": "npm run build",
"release:prepare:commit": "git add -f vl-map.js && git commit --amend --no-edit && git pull",
"release:testless": "npm run release:prepare && np --yolo",
"demo": "npm run dev",
"dev": "concurrently \"npm:bundle:watch\" \"http-server\"",
"build": "npm run bundle:build",
"bundle:watch": "rollup --config rollup.config.js --watch",
"bundle:build": "rollup --config rollup.config.js"
}
This results in:
npm ERR! Git working directory not clean.
I solved it by cloning inside the container instead of letting Bamboo do the clone and copy all my source files into the container.

How to set host environment variables inside npm scripts(package.json)

Imagine I have an environment variable
export NODE_ENV=production
when I do
echo $NODE_ENV //--> shows production which is correct
Problem:
Inside my package.json
scripts: {
...
"build": "export REACT_APP_NODE_ENV=${NODE_ENV:-development};
npm run build-css && react-scripts build",
...
}
Now when I do npm run build REACT_APP_NODE_ENV is getting set to development...but it should have been production as NODE_ENV is present.
If I do
scripts: {
...
"build": "export REACT_APP_NODE_ENV=production;
npm run build-css && react-scripts build",
...
}
It works correctly as expected i.e. all scripts access the REACT_APP_NODE_ENV with expected value that is production.
Goal
I wish to avoid hardcoding in my package.json
How can I set REACT_APP_NODE_ENV with value ${NODE_ENV}
"build": "export REACT_APP_NODE_ENV=${NODE_ENV};
npm run build-css && react-scripts build",
You probably want to ensure that this is cross-platform which will save you some headaches later on.
That problem has already been solved in the npm package cross-var.
Then, assuming you've already exported NODE_ENV, you use it this way:
"scripts": {
"build": "REACT_APP_NODE_ENV=${NODE_ENV}"
}

In npm how to pass env variable to another script command?

I have 2 commands and another 2 commands that invoke the first two but with different env variables.
but setting env variable doesn't work.
"build": "NODE_ENV=production npm run build:client && npm run build:server",
"build:tests": "NODE_ENV=tests npm run build:client && npm run build:server",
"build:client": "webpack --config webpack.conf.js",
"build:server": "webpack --config webpack.conf.js",
but sadly, inside conf.prod.js i can't access node_env because it was written inside another script (1 "above" it).
Thanks

Npm scripts doesn't work the way I want

see below:
scripts": {
"build": "node_modules/.bin/babel sercer/src --out-dir server/dist ",
"build:watch": "node_modules/.bin/babel server/src --out-dir server/dist --watch",
"start:server": "node ./node_modules/nodemon/bin/nodemon.js ./server/dist/app.js",
"dev" : "(npm run build:watch) && (npm run start:server)"
}
you know, both of them can work well when I run npm run xxx , but when i conbian them like npm run dev does,the last one will not taking effect.what wrong with my script?
You could try
"dev" : "npm run build:watch && npm run start:server"
you can use the post- and pre- scripts that will be called before and after that script.
eg :
"build": "npm run build:css && npm run build:js",
"prebuild:js": "npm run lint"
In the above example build will execute both build:css and build:js - but not before running the lint task.

Resources