Skip to content

Commit

Permalink
add notify option & fix opts defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
SassNinja committed May 25, 2020
1 parent 3c06a31 commit 5e90597
Show file tree
Hide file tree
Showing 4 changed files with 1,882 additions and 2,090 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Have you ever been in the situation you were facing an issue that was simply cau

Are you tired of telling people to (re)install their node modules when they ask you why something is not working?

This lightweight, zero-dependency package might be the perfect solution for you! It (re)installs node modules whenever the target package file has changed!
This lightweight package might be the perfect solution for you! It (re)installs node modules whenever the target package file has changed!

## Installation

Expand Down Expand Up @@ -61,6 +61,14 @@ installer(options);

## Options

| Option | Type | Default |
| ---------- | ------- | -------------- |
| manager | string | 'npm' |
| file | string | 'package.json' |
| install | boolean | true |
| updateHash | boolean | true |
| notify | boolean | false |

### manager

The manager option defines the package manager that gets used to (re)install the node modules. By default it's `npm` but you can use whatever you like (as long as it supports the `install` command).
Expand Down Expand Up @@ -96,7 +104,15 @@ 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
reinstall-node-modules --update false
```

### notify

The notify option defines if a [notification](https://github.com/mikaelbr/node-notifier) gets sent in case the target package file has changed. This is in particular useful if you have disabled `install` to gently inform your colleagues they should (re)install their node modules.

```
reinstall-node-modules --notify true
```

## Config
Expand Down
19 changes: 14 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ const compareHash = require('./compare-hash');
const getFilePath = require('./get-file-path');
const install = require('./install');
const getConfigFile = require('./get-config-file');
const notifier = require('node-notifier');

const argv = getArgv(process.argv.slice(2));

const defaultOptions = {
file: argv.file || 'package.json',
manager: argv.manager || 'npm',
install: argv.install || true,
updateHash: argv.update || true
file: argv.file || 'package.json',
install: argv.install !== false,
updateHash: argv.update !== false,
notify: argv.notify === true,
};

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

getFilePath(opts.file)
.then(filePath => compareHash(filePath, opts.updateHash === true))
.then((filePath) => compareHash(filePath, opts.updateHash === true))
.then(({ hasChanged, fileName }) => {
if (hasChanged === true) {
if (opts.install !== false) {
Expand All @@ -30,11 +32,18 @@ function run(options) {
} else {
console.log(`\n\x1b[31m${fileName} has changed – you should (re)install node modules\x1b[0m\n`);
}
if (opts.notify) {
notifier.notify({
title: `${fileName} has changed`,
message: 'you should (re)install node modules',
timeout: 8,
});
}
} else {
console.log(`\n\x1b[32m${fileName} has not changed – no need to do anything\x1b[0m`);
}
})
.catch(err => {
.catch((err) => {
console.error(err);
});
}
Expand Down
Loading

0 comments on commit 5e90597

Please sign in to comment.