Skip to content

Commit

Permalink
make get-argv smarter, remove dry and add install/updateHash
Browse files Browse the repository at this point in the history
get-argv now converts true/false strings into boolean (as you would expect)
  • Loading branch information
SassNinja committed May 22, 2020
1 parent 97c8d4d commit 4a5e2a4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,22 @@ If you wanna e.g. use `package-lock.json` instead of `package.json` you only nee
reinstall-node-modules --file package-lock.json
```

### dry
### install

The dry option disables an actual (re)install but only logs whether the target package file has changed.
The install option defines if an actual (re)install is executed.
So to disable it you only need to set it `false`

```
reinstall-node-modules --dry
reinstall-node-modules --install false
```

### updateHash

The updateHash option defines if a \*.hash file is written to disk.
Be careful with disabling this because your target package file will be considered as changed then every time.

```
reinstall-node-modules --updateHash false
```

## Config
Expand Down
19 changes: 12 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ const argv = getArgv(process.argv.slice(2));
const defaultOptions = {
file: argv.file || 'package.json',
manager: argv.manager || 'npm',
dry: argv.dry || 'false'
install: argv.install || true,
updateHash: argv.update || true
};

function run(options) {
const opts = Object.assign({}, defaultOptions, getConfigFile(), options);

getFilePath(opts.file)
.then(filePath => compareHash(filePath, opts.dry !== 'true'))
.then(filePath => compareHash(filePath, opts.updateHash === true))
.then(({ hasChanged, fileName }) => {
if (hasChanged === true) {
try {
console.log(`\n\x1b[31m${fileName} has changed – (re)installing node modules now\x1b[0m\n`);
install(opts);
} catch (err) {
console.error(err);
if (opts.install !== false) {
try {
console.log(`\n\x1b[31m${fileName} has changed – (re)installing node modules now\x1b[0m\n`);
install(opts);
} catch (err) {
console.error(err);
}
} else {
console.log(`\n\x1b[31m${fileName} has changed – you should (re)install node modules\x1b[0m\n`);
}
} else {
console.log(`\n\x1b[32m${fileName} has not changed – no need to do anything\x1b[0m`);
Expand Down
2 changes: 1 addition & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { execSync } = require('child_process');
* @param {Object} opts installer options
*/
function install(opts) {
if (opts.dry !== 'true') {
if (opts.install !== false) {
execSync(opts.manager === 'npm' && opts.file.endsWith('package-lock.json') ? 'npm ci' : `${opts.manager} install`, { stdio: 'inherit' });
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/utils/get-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ function getArgv(argvRaw) {
if (arg.startsWith('--')) {
const splitted = arg.split('=');
const key = splitted[0].slice(2);
let val = 'true';
let val = true;

if (splitted.length > 1) {
val = splitted[1];
} else if (argvRaw[idx + 1] && !argvRaw[idx + 1].startsWith('--')) {
val = argvRaw[idx + 1];
}

if (val === 'true') {
val = true;
} else if (val === 'false') {
val = false;
}

argv[key] = val;
}
});
Expand Down
16 changes: 13 additions & 3 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ describe('main', () => {
install({ manager: 'npm', file: 'package-lock.json' });
expect(execSync.mock.calls[0][0]).toBe('npm ci');
});
it('should not install if running dry', () => {
install({ dry: 'true' });
it('should repect install option', () => {
install({ install: false });
expect(execSync.mock.calls.length).toBe(0);
});
});
Expand Down Expand Up @@ -109,7 +109,17 @@ describe('utils', () => {
it('should be true if only --key is set without val', () => {
const argv = getArgv(['--production']);

expect(argv).toEqual({ production: 'true' });
expect(argv).toEqual({ production: true });
});
it('should be typeof boolean if command line argument is "true"', () => {
const argv = getArgv(['--production=true']);

expect(argv).toEqual({ production: true });
});
it('should be typeof boolean if command line argument is "false"', () => {
const argv = getArgv(['--production=false']);

expect(argv).toEqual({ production: false });
});
});
});

0 comments on commit 4a5e2a4

Please sign in to comment.