check-installed 1.0.0 → 2.0.1

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 CHANGED
@@ -10,10 +10,20 @@ $ npm i check-installed --save-dev
10
10
 
11
11
  ## Usage
12
12
  - Run this before npm scripts to make sure that the environment is set up correctly
13
- - It checks that npm packages and engines (node, dotnet, anything that responds to `--version`) are installed and are the correct version
13
+ - It checks that engines and node modules are installed and are the correct versions
14
14
  - Version are checked with `semver.satisfies()`, so you can use any valid semver range
15
15
  - See [semver](https://www.npmjs.com/package/semver) for more info
16
16
 
17
+ ### Engine Check
18
+ - Compares the installed version of an engine to the version specified in `package.json`
19
+ - Engines can be things like node, dotnet, or anything that responds to `--version`
20
+
21
+ ### Node Module Check
22
+ - Compares the installed version of a node module to the version specified in `package.json`
23
+ - Versions for modules that have `latest` are not checked, only that they are installed
24
+ - Optional node module dependencies are only checked if they are installed
25
+ - Git URL versions are skipped
26
+
17
27
  ## Example
18
28
  In `package.json`:
19
29
  ```json
@@ -24,11 +34,7 @@ In `package.json`:
24
34
  },
25
35
  "scripts": {
26
36
  "check": "check-installed",
27
- "start": "npm run check && node index.js",
28
- "test": "npm run check && mocha"
29
- },
30
- "dependencies": {
31
- "semver": "^7.5.4"
37
+ "start": "npm run check && node index.js"
32
38
  }
33
39
  ```
34
40
 
@@ -42,5 +48,17 @@ In `package.json`:
42
48
  #### `--help`
43
49
  Show help (skips check)
44
50
 
51
+ #### `--skip-engines`
52
+ Skip checking engines
53
+
54
+ #### `--skip-modules`
55
+ Skip checking node modules
56
+
57
+ #### `--show-success`
58
+ Show success message on successful checks
59
+
45
60
  #### `--show-engines`
46
61
  Show installed engines on successful check
62
+
63
+ ### `--show-modules`
64
+ Show installed node modules on successful check
package/index.js CHANGED
@@ -1,19 +1,6 @@
1
+ const { HELP, OPTIONS } = require('./lib/constants')
1
2
  const { checkEngines } = require('./lib/checkEngines')
2
-
3
- const HELP = `
4
- Usage: check-installed [options]
5
-
6
- Options:
7
- --help Show help (skips check)
8
- --show-engines Show installed engines on successful check
9
-
10
- See https://www.npmjs.com/package/check-installed
11
- `.trim()
12
-
13
- const OPTIONS = {
14
- help: false,
15
- showEngines: false
16
- }
3
+ const { checkModules } = require('./lib/checkModules')
17
4
 
18
5
  /**
19
6
  * Get options from CLI arguments
@@ -39,6 +26,7 @@ function getOptions (args) {
39
26
 
40
27
  /**
41
28
  * check-installed CLI entry point
29
+ * @param {string[]} argv - CLI arguments
42
30
  */
43
31
  async function cli (argv = process.argv) {
44
32
  const args = argv.slice(2)
@@ -49,7 +37,13 @@ async function cli (argv = process.argv) {
49
37
  return
50
38
  }
51
39
 
52
- await checkEngines(options)
40
+ if (!options.skipEngines) {
41
+ await checkEngines(options)
42
+ }
43
+
44
+ if (!options.skipModules) {
45
+ await checkModules(options)
46
+ }
53
47
  }
54
48
 
55
49
  exports.cli = cli
@@ -4,10 +4,11 @@ const semver = require('semver')
4
4
 
5
5
  /**
6
6
  * Get the version of an engine
7
- * @param {string} engine - The name of the engine to get version for
7
+ * @param {string} name - The name of the engine to get version for
8
+ * @returns {Promise<string>} - The version of the engine
8
9
  */
9
- async function getEngineVersion (engine) {
10
- const spawned = spawn(engine, ['--version'])
10
+ async function getEngineVersion (name) {
11
+ const spawned = spawn(name, ['--version'])
11
12
 
12
13
  return new Promise((resolve) => {
13
14
  let result = ''
@@ -32,39 +33,45 @@ async function getEngineVersion (engine) {
32
33
 
33
34
  /**
34
35
  * Checks that the given engine is installed and satisfies version (throws if not)
35
- * @param {string} engine - The name of the engine to check
36
+ * @param {string} name - The name of the engine to check
36
37
  * @param {string} version - The version to check against
37
- * @returns {Promise<[string, string]>} - The [engine, version] installed and checked successfully
38
+ * @returns {Promise<[string, string, string]>} - The [name, version, installed] installed and checked successfully
38
39
  */
39
- async function checkEngineVersion (engine, version) {
40
- const installed = await getEngineVersion(engine, version)
40
+ async function checkEngineVersion (name, version) {
41
+ const installed = await getEngineVersion(name, version)
41
42
 
42
43
  if (!semver.satisfies(installed, version)) {
43
- throw new Error(`Engine check failed for ${engine}: installed ${installed}, expected ${version}`)
44
+ throw new Error(`check-installed engine failed for ${name}: installed ${installed}, expected ${version}`)
44
45
  }
45
46
 
46
- return [engine, installed]
47
+ return [name, version, installed]
47
48
  }
48
49
 
49
50
  /**
50
51
  * Checks engines in `package.json` are installed
51
- * @param {boolean} showInstalled - Whether to show installed engines
52
+ * @param {typeof import('./constants').OPTIONS} options - Options for the CLI
52
53
  */
53
- async function checkEngines ({
54
- showEngines = false
55
- } = {}) {
54
+ async function checkEngines (options) {
55
+ const { showSuccess, showEngines } = options
56
56
  const json = require(resolve(process.cwd(), 'package.json'))
57
57
 
58
+ // Check engines
58
59
  const engines = await Promise.all(
59
60
  Object.entries(json.engines || {})
60
- .map(([engine, version]) => checkEngineVersion(engine, version))
61
+ .map(([name, version]) => checkEngineVersion(name, version))
61
62
  )
62
63
 
64
+ if (showSuccess) {
65
+ console.log('check-installed engines successful')
66
+ }
67
+
63
68
  if (showEngines) {
64
- for (const [engine, version] of engines) {
65
- console.log(`${engine}: ${version}`)
69
+ for (const [name, version, installed] of engines) {
70
+ console.log(`${name}: ${installed} (${version})`)
66
71
  }
67
72
  }
68
73
  }
69
74
 
70
- exports.checkEngines = checkEngines
75
+ module.exports = {
76
+ checkEngines
77
+ }
@@ -0,0 +1,85 @@
1
+ const { resolve } = require('path')
2
+ const fs = require('fs')
3
+ const semver = require('semver')
4
+
5
+ /**
6
+ * Regex used to extract the real name and version from an npm alias
7
+ */
8
+ const NPM_ALIAS_REGEX = /npm:(.+)@(.+)/
9
+
10
+ /**
11
+ * Checks that the given node module is installed and satisfies version (throws if not)
12
+ * @param {string} name - The name of the node module to check
13
+ * @param {string} version - The version to check against
14
+ * @returns {[string, string, string, string]} - The [name, version, aliased, installed] installed and checked successfully
15
+ */
16
+ function checkModuleVersion (name, version, optional) {
17
+ // Update with npm alias if needed ("my-react": "npm:react@^18.2.0")
18
+ let aliased = ''
19
+ if (NPM_ALIAS_REGEX.test(version)) {
20
+ const [, realName, realVersion] = version.match(NPM_ALIAS_REGEX)
21
+
22
+ aliased = `${realName} `
23
+ console.log('realnamel', realName)
24
+ version = realVersion
25
+ }
26
+
27
+ // Ignore git URLs version
28
+ if (/^(git.*:\/\/|github:|gitlab:|bitbucket:|gist:)/.test(version)) {
29
+ return [name, version, aliased, 'skipped']
30
+ }
31
+
32
+ const directoryPath = `node_modules/${name}`
33
+ const jsonPath = `${directoryPath}/package.json`
34
+
35
+ // Check if module is installed and contains a `package.json` (throw if not optional)
36
+ if (!fs.existsSync(directoryPath) || !fs.existsSync(jsonPath)) {
37
+ if (optional) {
38
+ return [name, version, aliased, 'none']
39
+ } else {
40
+ throw new Error(`check-installed module failed for ${name}: installed none, expected ${aliased}${version}`)
41
+ }
42
+ }
43
+
44
+ const json = require(resolve(process.cwd(), jsonPath))
45
+ const installed = json.version
46
+
47
+ if (version !== 'latest' && !semver.satisfies(json.version, version)) {
48
+ throw new Error(`check-installed module failed for ${name}: installed ${installed}, expected ${aliased}${version}`)
49
+ }
50
+
51
+ return [name, version, aliased, installed]
52
+ }
53
+
54
+ /**
55
+ * Checks node modules in `package.json` are installed
56
+ * @param {typeof import('./constants').OPTIONS} options - Options for the CLI
57
+ */
58
+ async function checkModules (options) {
59
+ const { showSuccess, showModules } = options
60
+ const json = require(resolve(process.cwd(), 'package.json'))
61
+
62
+ // Check modules
63
+ const modules = [
64
+ ...Object.entries(json.dependencies || {})
65
+ .map(([name, version]) => checkModuleVersion(name, version, false)),
66
+ ...Object.entries(json.devDependencies || {})
67
+ .map(([name, version]) => checkModuleVersion(name, version, false)),
68
+ ...Object.entries(json.optionalDependencies || {})
69
+ .map(([name, version]) => checkModuleVersion(name, version, true))
70
+ ]
71
+
72
+ if (showSuccess) {
73
+ console.log('check-installed modules successful')
74
+ }
75
+
76
+ if (showModules) {
77
+ for (const [name, version, aliased, installed] of modules) {
78
+ console.log(`${name}: ${installed} (${aliased}${version})`)
79
+ }
80
+ }
81
+ }
82
+
83
+ module.exports = {
84
+ checkModules
85
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Help text for the CLI
3
+ */
4
+ const HELP = `
5
+ Usage: check-installed [options]
6
+
7
+ Options:
8
+ --help Show help (skips check)
9
+ --skip-engines Skip checking engines
10
+ --skip-modules Skip checking node modules
11
+ --show-success Show success message on successful checks
12
+ --show-engines Show installed engines on successful check
13
+ --show-modules Show installed node modules on successful check
14
+
15
+ See https://www.npmjs.com/package/check-installed
16
+ `.trim()
17
+
18
+ /**
19
+ * Default options for the CLI
20
+ */
21
+ const OPTIONS = {
22
+ help: false,
23
+ skipEngines: false,
24
+ skipModules: false,
25
+ showSuccess: false,
26
+ showEngines: false,
27
+ showModules: false
28
+ }
29
+
30
+ module.exports = {
31
+ HELP,
32
+ OPTIONS
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "check-installed",
3
- "version": "1.0.0",
3
+ "version": "2.0.1",
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 --show-engines",
35
+ "check": "./bin/check-installed",
36
36
  "lint": "standard",
37
37
  "lint:fix": "npm run lint -- --fix",
38
38
  "test": "npm run check",