check-installed 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -22
- package/lib/checkEngines.js +5 -0
- package/lib/checkModules.js +5 -0
- package/lib/cli.js +3 -23
- package/lib/getOptions.js +42 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,58 +10,97 @@ Minimal Node package to check that dependencies are installed ✅
|
|
|
10
10
|
$ npm i check-installed --save-dev
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
-
|
|
13
|
+
## Checks
|
|
14
|
+
### Engines Check
|
|
15
|
+
- Compares the installed version of engines to the versions specified in `package.json`
|
|
16
|
+
- Engines can be things like node, dotnet, or anything that responds to `--version`
|
|
16
17
|
- Version are checked with `semver.satisfies()`, so you can use any valid semver range
|
|
17
18
|
- See [semver](https://www.npmjs.com/package/semver) for more info
|
|
18
|
-
|
|
19
|
-
### Engine Check
|
|
20
|
-
- Compares the installed version of an engine to the version specified in `package.json`
|
|
21
|
-
- Engines can be things like node, dotnet, or anything that responds to `--version`
|
|
22
|
-
|
|
23
|
-
### Node Module Check
|
|
24
|
-
- Compares the installed version of a node module to the version specified in `package.json`
|
|
25
|
-
- Versions for modules that have `latest` are not checked, only that they are installed
|
|
26
|
-
- Optional node module dependencies are only checked if they are installed
|
|
27
|
-
- Git URL versions are skipped
|
|
28
|
-
|
|
29
|
-
## Example
|
|
30
|
-
In `package.json`:
|
|
31
19
|
```json
|
|
32
20
|
"engines": {
|
|
33
21
|
"node": ">=12.x",
|
|
34
22
|
"npm": ">=6",
|
|
35
23
|
"dotnet": "^6.0.410"
|
|
36
24
|
},
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Modules Check
|
|
28
|
+
- Compares the installed versions of node modules to the versions specified in `package.json`
|
|
29
|
+
- Versions for modules that have `latest` are not checked, only that they are installed
|
|
30
|
+
- Optional node module dependencies are only checked if they are installed
|
|
31
|
+
- Git URL versions are skipped
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### CLI
|
|
36
|
+
Run `check-installed` before npm scripts or directly to ensure that the environment is set up correctly.
|
|
37
|
+
If any check fails, the process will throw and exit with a non-zero exit code.
|
|
38
|
+
|
|
39
|
+
#### NPM Scripts
|
|
40
|
+
You can add a check script and run with `npm run check` or call it from other commands:
|
|
41
|
+
```json
|
|
37
42
|
"scripts": {
|
|
38
43
|
"check": "check-installed",
|
|
39
44
|
"start": "npm run check && node index.js",
|
|
40
45
|
"test": "npm run check && jest"
|
|
41
46
|
}
|
|
42
47
|
```
|
|
48
|
+
This repo actually uses `check-installed` directly 🤯.
|
|
43
49
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
```
|
|
48
|
-
|
|
50
|
+
#### Command Line
|
|
51
|
+
```bash
|
|
52
|
+
npx check-installed
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### API
|
|
56
|
+
You can call the checks directly from node programatically if needed like:
|
|
57
|
+
```js
|
|
58
|
+
const { checkEngines, checkModules } = require('check-installed')
|
|
59
|
+
const json = require('./package.json')
|
|
60
|
+
|
|
61
|
+
await checkEngines(json)
|
|
62
|
+
await checkModules(json)
|
|
63
|
+
await checkEngines(json, { showEngines: true })
|
|
64
|
+
await checkModules(json, { showSuccess: true })
|
|
49
65
|
```
|
|
66
|
+
Each check returns a `Promise<void>` that throws if any check fails.
|
|
67
|
+
They are typed to accept the entire `options` object, but only use options they need.
|
|
68
|
+
|
|
69
|
+
### Options
|
|
70
|
+
- All options are optional and will be defaulted
|
|
71
|
+
- CLI formats to change default values:
|
|
72
|
+
- `boolean`:
|
|
73
|
+
- `true`: `--option-name` or `--option-name=true`
|
|
74
|
+
- `false`: `--option-name=false` or `--option-name=` or `--option-name=anythingelse`
|
|
75
|
+
- `string`: `--option-name=value`
|
|
76
|
+
- Options for `checkEngines` and `checkModules` are the same as CLI options but `camelCased`
|
|
50
77
|
|
|
51
78
|
#### `--help`
|
|
52
79
|
Show help (skips check)
|
|
53
80
|
|
|
81
|
+
*Type: `boolean`, Default: `false`*
|
|
82
|
+
|
|
54
83
|
#### `--skip-engines`
|
|
55
84
|
Skip checking engines
|
|
56
85
|
|
|
86
|
+
*Type: `boolean`, Default: `false`*
|
|
87
|
+
|
|
57
88
|
#### `--skip-modules`
|
|
58
89
|
Skip checking node modules
|
|
59
90
|
|
|
91
|
+
*Type: `boolean`, Default: `false`*
|
|
92
|
+
|
|
60
93
|
#### `--show-success`
|
|
61
94
|
Show success message on successful checks
|
|
62
95
|
|
|
96
|
+
*Type: `boolean`, Default: `false`*
|
|
97
|
+
|
|
63
98
|
#### `--show-engines`
|
|
64
99
|
Show installed engines on successful check
|
|
65
100
|
|
|
66
|
-
|
|
101
|
+
*Type: `boolean`, Default: `false`*
|
|
102
|
+
|
|
103
|
+
#### `--show-modules`
|
|
67
104
|
Show installed node modules on successful check
|
|
105
|
+
|
|
106
|
+
*Type: `boolean`, Default: `false`*
|
package/lib/checkEngines.js
CHANGED
|
@@ -53,6 +53,7 @@ async function checkEngineVersion (name, version) {
|
|
|
53
53
|
* Checks engines in `package.json` are installed
|
|
54
54
|
* @param {object} json - The `package.json` JSON object
|
|
55
55
|
* @param {Partial<typeof import('./constants').OPTIONS>} options - Options for the CLI
|
|
56
|
+
* @returns {Promise<void>} - Resolves when all engines are checked (throws on check fail)
|
|
56
57
|
*/
|
|
57
58
|
async function checkEngines (json = {}, options) {
|
|
58
59
|
const { showSuccess, showEngines } = { ...OPTIONS, ...options }
|
|
@@ -72,6 +73,10 @@ async function checkEngines (json = {}, options) {
|
|
|
72
73
|
console.log(`${name}: ${installed} (${version})`)
|
|
73
74
|
}
|
|
74
75
|
}
|
|
76
|
+
|
|
77
|
+
if (showSuccess || showEngines) {
|
|
78
|
+
console.log('')
|
|
79
|
+
}
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
module.exports = {
|
package/lib/checkModules.js
CHANGED
|
@@ -56,6 +56,7 @@ function checkModuleVersion (name, version, optional) {
|
|
|
56
56
|
* Checks node modules in `package.json` are installed
|
|
57
57
|
* @param {object} json - The `package.json` JSON object
|
|
58
58
|
* @param {Partial<typeof import('./constants').OPTIONS>} options - Options for the CLI
|
|
59
|
+
* @returns {Promise<void>} - Resolves when all modules are checked (throws on check fail)
|
|
59
60
|
*/
|
|
60
61
|
async function checkModules (json = {}, options) {
|
|
61
62
|
const { showSuccess, showModules } = { ...OPTIONS, ...options }
|
|
@@ -79,6 +80,10 @@ async function checkModules (json = {}, options) {
|
|
|
79
80
|
console.log(`${name}: ${installed} (${aliased}${version})`)
|
|
80
81
|
}
|
|
81
82
|
}
|
|
83
|
+
|
|
84
|
+
if (showSuccess || showModules) {
|
|
85
|
+
console.log('')
|
|
86
|
+
}
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
module.exports = {
|
package/lib/cli.js
CHANGED
|
@@ -1,33 +1,13 @@
|
|
|
1
1
|
const { resolve } = require('path')
|
|
2
|
-
const { HELP
|
|
2
|
+
const { HELP } = require('./constants')
|
|
3
|
+
const { getOptions } = require('./getOptions')
|
|
3
4
|
const { checkEngines } = require('./checkEngines')
|
|
4
5
|
const { checkModules } = require('./checkModules')
|
|
5
6
|
|
|
6
|
-
/**
|
|
7
|
-
* Get options from CLI arguments
|
|
8
|
-
* @param {string[]} args - CLI arguments
|
|
9
|
-
* @returns {typeof OPTIONS} - Options for the CLI with defaults
|
|
10
|
-
*/
|
|
11
|
-
function getOptions (args) {
|
|
12
|
-
const options = {}
|
|
13
|
-
|
|
14
|
-
for (const arg of args) {
|
|
15
|
-
if (arg.startsWith('--')) {
|
|
16
|
-
const [key, value] = arg.slice(2).split('=')
|
|
17
|
-
const keyCamelCase = key.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
|
|
18
|
-
|
|
19
|
-
if (OPTIONS[keyCamelCase] != null) {
|
|
20
|
-
options[keyCamelCase] = value ?? true
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return options
|
|
26
|
-
}
|
|
27
|
-
|
|
28
7
|
/**
|
|
29
8
|
* check-installed CLI entry point
|
|
30
9
|
* @param {string[]} argv - CLI arguments
|
|
10
|
+
* @returns {Promise<void>} - Promise that resolves when the CLI is done (throws on check fail)
|
|
31
11
|
*/
|
|
32
12
|
async function cli (argv = process.argv) {
|
|
33
13
|
const json = require(resolve(process.cwd(), 'package.json'))
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const { OPTIONS } = require('./constants')
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get check-installed options from CLI arguments
|
|
5
|
+
* @param {string[]} args - CLI arguments
|
|
6
|
+
* @returns {typeof OPTIONS} - Options for the CLI with defaults
|
|
7
|
+
*/
|
|
8
|
+
function getOptions (args = []) {
|
|
9
|
+
const options = {}
|
|
10
|
+
|
|
11
|
+
for (const arg of args) {
|
|
12
|
+
if (arg.startsWith('--')) {
|
|
13
|
+
const [key, value] = arg.slice(2).split('=')
|
|
14
|
+
const keyCamelCase = key.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
|
|
15
|
+
const option = OPTIONS[keyCamelCase]
|
|
16
|
+
|
|
17
|
+
// Only set if the option is defined in `OPTIONS`
|
|
18
|
+
if (option !== undefined) {
|
|
19
|
+
let newValue
|
|
20
|
+
|
|
21
|
+
// If boolean, `true` only if no value or value is "true"
|
|
22
|
+
// Otherwise, only if value provided
|
|
23
|
+
if (typeof option === 'boolean') {
|
|
24
|
+
newValue = value == null ? true : value === 'true'
|
|
25
|
+
} else if (value != null) {
|
|
26
|
+
newValue = value
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Set only if valid and not already default
|
|
30
|
+
if (newValue !== undefined && newValue !== option) {
|
|
31
|
+
options[keyCamelCase] = newValue
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return options
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
getOptions
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "check-installed",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Minimal Node package to check that dependencies are installed ✅",
|
|
5
5
|
"author": "Phillip Lanclos <check-installed@flippidippi.com>",
|
|
6
6
|
"license": "Unlicense",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"npm": ">=6"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"check": "./bin/check-installed",
|
|
35
|
+
"check": "./bin/check-installed --show-engines",
|
|
36
36
|
"lint": "standard",
|
|
37
37
|
"lint:fix": "npm run lint -- --fix",
|
|
38
38
|
"test": "npm run check && jest",
|