How to reset -g parameter for "npm install" in hooks scripts? - javascript

I have a following project structure:
install.js:
var path = require('path'),
exec = require('child_process').exec;
exec('npm install', {cwd: path.join(__dirname, './some_modules')});
package.json:
"scripts": {
"install": "node install.js"
},
"dependencies": {
"gulp": "3.8.10"
},
And have some dependencies in some_modules/package.json.
In case installation locally we get the expected result:
But in case installation globally (with -g parameter) we have following broken structure:
Question: How to get rid of the influence -g parameter for install.js -> exec('npm install') ?
Try it here: https://github.com/MishaMykhalyuk/npm-i-with-g-and-hooks (npm install -g git+https://github.com/MishaMykhalyuk/npm-i-with-g-and-hooks.git).

Related

Format argument value before passing to a yarn/npm script

I have a storybook start script which I want to run for some specific folder:
"storybook": "start-storybook -p 6006 -s ./src"
This loads all stories from src folder. As the amount of stories becomes larger, I want to run stories only from some of subfolders:
start-storybook -p 6006 -s ./src/components/CommonComponents
start-storybook -p 6006 -s ./src/components/DashboardComponents
How can I format argument value dynamically in order to start storybook like this below?
$ yarn storybook CommonComponents
And it would turn into:
start-storybook -p 6006 -s ./src/components/CommonComponents
storybook task could be a script, and then inside the script you parse the arguments, and call start-storybook
Create a task in package.json (e.q run-storybook) and set it to execute the custom script:
"run-storybook": yarn ./path/to/script.js
#!/bin/env node
// script.js
const { spawn } = require('child_process')
const args = process.argv.slice(2)
const port = args[1]
const component = args[3]
const path = `./src/components/${component}`
spawn('yarn', ['start-storybook', '-p', port, '-s', path], {
stdio: 'inherit',
shell: true
})
Then you can call: yarn run-storybook -p 600 -s yourcomponent
Note: make sure the script is executable: chmod +x /path/to/script.js.

Lerna: how can i get successfully published packages?

I'm having monorepo with lerna and demo web-site.
I want to publish changed packages and automatically update it in demo from 1 command like npm run release.
package.json:
...
"scripts": {
"release": "node ./release.js",
...
}
release.js
const { spawnSync } = require('child_process');
const cmd = `npx lerna publish`;
const updatedPackages = spawnSync(cmd, { stdio: 'inherit', shell: true }); // there i want get list of successfully published packages.
udpdateDemo(updatedPackages); // custom function, which get list of packages and update it for demo

npm argument results in "Insufficient number of arguments or no entry found"

I try to pass a custom flag from npm script to my webpack config but it results in following error. The logs
Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
ERROR in Entry module not found: Error: Can't resolve '--no-dist' in 'C:\Users\user\gitroot\MyProject\sharepoint'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! my-project#4.0.0 dev: `webpack --mode development -- --no-dist`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the my-project#4.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
package.json
...
"scripts": {
"dev": "webpack --mode development -- --no-dist",
"dev:dist": "webpack --mode development",
"build": "webpack --mode production"
},
...
webpack.config.js
let username = process.env.USERNAME;
if (process.env.npm_config_user !== undefined && process.env.npm_config_user !== "") {
username = process.env.npm_config_user;
}
console.log("username", username);
console.log("process.argv.slice(2)", process.argv.slice(2));
const no_dist = process.argv.slice(2).indexOf("--no-dist") > -1;
console.log("no_dist", no_dist);
Tests
npm run dev:dist
This works without errors, it gets bundled and distributed without problems. Gives the following output:
username user
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
npm run dev:dist --user test
Also works and gives the following output:
username test
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
npm run dev
Here it gets interesting, I try to run the dev script which has a --no-dist flag. Output:
username user
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true
As you can see, no_dist boolean is set to true, which is the wanted behaviour. But I get the following error:
Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
npm run dev --user test
Same behaviour as test 3. The arguments are passed to the webpack.config.js but result in the same error.
username test
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true
Am I missing something here?
As #squgeim mentioned, webpack does not support flags and environment variables should be used. This pointed me in the right direction: Environment variables
I changed my package.json and webpack.config.js file like this:
package.json
"scripts": {
"dev": "webpack --mode development --env.dist=false",
"dev:dist": "webpack --mode development",
"build": "webpack --mode production"
},
--env.dist=false adds dist to the environment variables.
webpack.config.js
There is one change that you will have to make to your webpack config. Typically, module.exports points to the configuration object. To use the env variable, you must convert module.exports to a function.
module.exports = env => {
const no_dist = (env && env.dist === "false");
return {
//webpack configuration
}
This seems to be the correct way to do it. Thanks again #squgeim to point me in the right direction!
I just got the same error yesturday,and i found out in my case it is due to the ES6 syntax error.I used module.export instead of module.exports in webpack.config.js.
I don't think webpack supports custom cli flags in it's configurations.
The idiomatic approach is to use environment variables to pass custom arguments to your configurations.
"dev:dist": "DIST=true webpack --mode development"
And access it like:
if (process.env.DIST === 'true') {
}

how to get my parameters using yargs

I start my koa app with yarn:
yarn start --some=123
The content of package.json is:
"start": "cross-env NODE_ENV=develop nodemon --watch src --exec npm run start:babel -L",
My file structure:
package.json
src
-- main.js
-- config.js
-- (some other files...)
My config.js file:
import { argv } from 'yargs';
console.log(argv);
Will print:
{ _: [], help: false, version: false, '$0': 'src/main' }
How can I get the parameters I want? should I change the script in package.json file?

Using wildcard to run multiple scripts for npm run test

I my package.json I have
"scripts": {
"test": "node tests/*-test.js"
}
And I have a-test.js and b-test.js in the tests folder, which I can verify by running ls tests/*-test.js.
However, npm run test is only executing a-test.js. How can I execute all *-test.js scripts? Explicitly listing them is not an option, since I will have more than 2 to run in the future.
You could use a task manager such as grunt or gulp, or a simple script that execute those scripts:
test.js:
require('./test/a-test.js')
require('./test/b-test.js')
package.json
"scripts": {
"test": "node test.js"
}
You could also use the include-all module for automating these for you https://www.npmjs.com/package/include-all
Example using includeAll:
const path = require('path');
const includeAll = require('include-all');
const controller = includeAll({
dirname: path.join(__dirname, 'test'),
filter: /(.+test)\.js$/,
});

Categories

Resources