Adding a delay to npm's concurrently - npm

I am using NPM's concurrently to run:
concurrently "npm run watch-sass" "npm run watch"
"scripts": {
"test": "nps test",
"watch-sass": "multi-sass -w -d static/css/pages -o static/css/pages -n -d static/css/common -o static/css/common -n -d static/css/components -o static/css/components",
"watch": "nps webpack.build.development.watch",
...
},
However, as soon as watch-sass completes a single file, watch runs and the files do not have a chance to be included in the build.
I need to insert some kind of delay after 'watch-sass' so that 'watch' will not activate until either (1) a delay everytime, or (2) a way to know when ALL scss files have been compiled.

Related

Is there a way to get the name of the npm script passed to the command specified by that script?

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

Output to command line within npm scripts?

I have a script in my package.json with the following code:
"scripts": {
"build": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css",
"watch": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css --watch"
}
It works fine. but how do I get it to output text to the command line, something like
script ran at {{ date(now) }}
In other words, I want to see a notification when a script has run.
Cross Platform (Windows/Linux/macOS...)
You can do the following to log the date/time when the script begins its task:
"build": "node -e \"console.log('script started at: %s', Date())\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
Explanation:
The part on the left side of the && operator that reads;
node -e \"console.log('script started at: %s', Date())\"
utilizes Node.js command line option -e to evaluate the inline JavaScript.
The inline script utilizes JavaScript's console.log(...) and Date().
The commands on the right side of the && operator are whatever command(s) you want to run, in your scenario it's the postcss command.
Variations:
To color the log you could add some ANSI/VT100 Control sequences. For instance:
"build": "node -e \"console.log('%sscript started at: %s%s', '\\x1b[42;30m', Date(), '\\x1b[0m' )\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
To log when the npm script completed instead of started you can switch the order of the commands. For example:
"build": "postcss tailwind/tailwind.css -o css/cenic.tailwind.css && node -e \"console.log('script completed at: %s', Date())\""
Nix platforms only (Linux/MacOS...)
If you only want a solution that runs on *nix platforms, then you can do the following instead:
"build": "echo \"script started at: $(date)\" && postcss tailwind/tailwind.css -o css/cenic.tailwind.css"
Explanation:
The part on the left side of the && operator that reads:
echo \"script started at: $(date)\"
utilizes the shells echo command to print the message to the command line.
Command substitution, i.e. the $(...) part, is utilized to obtain the output from the shells date command.
The commands on the right side of the && operator are whatever command(s) you want to run, in your scenario it's the postcss command.
If you want to apply visual styling to the echo command please refer to my answer here

A valid environment variable to satisfy env:PRISMA_SECRET could not be found

When I try to generate a Prisma token, it keeps giving me the following error, even though I have the PRISMA_SECRET configured in config/dev.env.:
A valid environment variable to satisfy the declaration!
'env:PRISMA_SECRET' could not be found.
My config/dev.env is configured as following:
PRISMA_ENDPOINT=http://local:4466/
PRISMA_SECRET=somesecret
JWT_SECRET=somesecret
And following is how I start npm run dev:
"dev": "env-cmd ./config/dev.env nodemon src/index.js --ext js,graphql --exec babel-node",
When I console.log(process.env.PRISMA_SECRET), it shows the variable properly so I'm not sure what's going on. I'm tried restarting Docker and re-deploying Prisma, but to no avail. Any help would be appreciated.
You need to add an -f flag, it's to do with an update to env-cmd
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js --exec babel-node -e js,graphql",

Generating sourcemaps with npm scripts using node-sass and postcss autoprefixer

Is it possible to generate fully working sourcemaps using node-sass and postcss autoprefixer when piping output from one to another? I currently have the following in package.json:
"scripts": {
"sass": "node-sass sass/app.scss --source-map true --source-map-embed true",
"postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
"css": "npm run sass -s | npm run postcss:autoprefixer -s > css/app.css"
}
This produces a semi-working inline sourcemap, but the links to original files are incorrect, so clicking on them in Chrome devtools won't load them (it seems like they are processed as relative links and then referenced from the css folder). I tried to fix this by adding the --source-map-contents true option to node-sass, but then autoprefixer bugs out, I suspect because it doesn't like the line length of the dataUri.
Ideally I'd prefer to output a separate .map file anyway, but setting the node-sass option to --source-map css/app.css.map doesn't write anything out, presumably because only the css is output to stdout.
Replacing source-map with source-map-root and a filesystem URL seems to do the trick:
"scripts": {
"sass": "node-sass sass/app.scss --source-map-root file://${PWD}/ --source-map-embed true",
"postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
"css": "npm run sass -s | npm run postcss:autoprefixer -s > css/app.css"
}
Would still be good to know if it was possible to output separate .map files though!
Update:
Below is the new package.json using exorcist as recommended by RyanZim's comment:
"scripts": {
"sass": "node-sass sass/app.scss --source-map-root file://${PWD}/ --source-map-embed true",
"postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
"css": "npm run sass -s | npm run postcss:autoprefixer -s | exorcist css/app.css.map > css/app.css"
}
Try:
sass input.scss:output.css

npm tslint don't give error

Hey all got a issue with npm and tslint I was hoping you could help me with.
Ok here comes my situation and code:
package.json
"scripts": {
"lint": "tslint -c tslint.json 'src/app/**/*.ts'",
"pretest": "npm run lint ",
"test": "echo 'No test are made'",
...
},
When I run command npm test this is the output:
input terminal
$ npm test
output terminal
> keoom#1.0.0 pretest c:\Users\Hjorth\Documents\github\keoom-angular
> npm run lint
> keoom#1.0.0 lint c:\Users\Hjorth\Documents\github\keoom-angular
> tslint -c tslint.json 'src/app/**/*.ts'
> keoom#1.0.0 test c:\Users\Hjorth\Documents\github\keoom-angular
> echo 'No test are made'
'No test are made'
If I only run command tslint -c tslint.json 'src/app/**/*.ts' I on the other hand see the linting error.
input terminal
$ tslint -c tslint.json 'src/app/**/*.ts'
output terminal
src/app/app.component.ts[1, 27]: " should be '
So as you can see there is a linting error in my code, but if I am not running the script directly It will not show. What I am expecting is this:
When I run npm test the script pretest will run, and that script will run the script lint, it will then exit with exit 0 before test script is run as it will find the linting error.
Anyone that can be of assistance.
It's the quotes around the file spec in the tslint command that are the problem:
"lint": "tslint -c tslint.json 'src/app/**/*.ts'"
^ ^
If you remove those, you should see the expected error reported by tslint.