@rowanmanning/node-versions 1.0.5 → 2.0.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 CHANGED
@@ -46,20 +46,20 @@ An array of strings (`string[]`) that contains every released versions of Node.j
46
46
  A function to list the Node.js versions that a `package.json` file claims to support. This function has the following signature:
47
47
 
48
48
  ```ts
49
- (path: string, options?: object) => Promise<string[]>
49
+ (packageJson: PackageJson, options?: object) => string[]
50
50
  ```
51
51
 
52
- The `path` argument is required and must be an accessible folder that contains a `package.json` file _or_ the path to a `package.json` file itself.
52
+ `PackageJson` must be a JavaScript object that is a valid `package.json` or `package-lock.json` file. We recommend using [@rowanmanning/package-json](../package-json#readme) or [@npmcli/package-json](https://github.com/npm/package-json#readme).
53
53
 
54
54
  The `options` argument is optional and can be used to change the way the method works. The following options are available:
55
55
 
56
56
  * `majorsOnly`: A `boolean` option defaulting to `false`. If this is set to `true` then the returned versions will have any minor/patch versions removed. E.g. `20.0.0` becomes `20`
57
57
 
58
58
  ```js
59
- const versions = await getPackageNodeVersions('/path/to/my/repo');
59
+ const versions = getPackageNodeVersions(require('./package.json'));
60
60
  // ["v22.5.1", "v20.16.0", "v18.20.4", ...]
61
61
 
62
- const versions = await getPackageNodeVersions('/path/to/my/repo', { majorsOnly: true });
62
+ const versions = await getPackageNodeVersions(require('./package.json'), { majorsOnly: true });
63
63
  // ["22", "20", "18", ...]
64
64
  ```
65
65
 
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { AnyPackageLock, PackageJson } from '@rowanmanning/package-json';
2
+
1
3
  declare module '@rowanmanning/node-versions' {
2
4
  export interface Options {
3
5
  majorsOnly?: boolean | undefined;
@@ -11,7 +13,7 @@ declare module '@rowanmanning/node-versions' {
11
13
  ): string[];
12
14
 
13
15
  export function getPackageNodeVersions(
14
- path: string,
16
+ packageJson: Partial<PackageJson> | Partial<AnyPackageLock>,
15
17
  options?: Options | undefined
16
- ): Promise<string[]>;
18
+ ): string[];
17
19
  }
package/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const { loadPackage } = require('./lib/load-package');
4
3
  const possibleNodeVersions = require('./data/versions.json');
5
4
  const semver = require('semver');
6
5
 
@@ -38,10 +37,19 @@ exports.getEnginesNodeVersions = function getEnginesNodeVersions(engines, option
38
37
  };
39
38
 
40
39
  /** @type {nodeVersions['getPackageNodeVersions']} */
41
- exports.getPackageNodeVersions = async function getPackageNodeVersions(path, options) {
42
- const packageJson = await loadPackage(path);
43
- if (typeof packageJson.engines?.node !== 'string') {
40
+ exports.getPackageNodeVersions = function getPackageNodeVersions(packageJson, options) {
41
+ /** @type {any} */
42
+ let engines;
43
+
44
+ // We can only extract engines data from a v2+ lockfile
45
+ if (typeof packageJson?.lockfileVersion === 'number' && packageJson.lockfileVersion > 1) {
46
+ engines = packageJson?.packages?.['']?.engines?.node;
47
+ } else {
48
+ engines = packageJson?.engines?.node;
49
+ }
50
+
51
+ if (typeof engines !== 'string') {
44
52
  return [];
45
53
  }
46
- return exports.getEnginesNodeVersions(packageJson.engines.node, options);
54
+ return exports.getEnginesNodeVersions(engines, options);
47
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rowanmanning/node-versions",
3
- "version": "1.0.5",
3
+ "version": "2.0.0",
4
4
  "description": "Get the Node.js versions that a repo says it supports",
5
5
  "keywords": [],
6
6
  "author": "Rowan Manning (https://rowanmanning.com/)",
@@ -22,7 +22,9 @@
22
22
  "main": "index.js",
23
23
  "types": "index.d.ts",
24
24
  "dependencies": {
25
- "@npmcli/package-json": "^5.2.0",
26
25
  "semver": "^7.6.3"
26
+ },
27
+ "devDependencies": {
28
+ "@rowanmanning/package-json": "^1.0.0"
27
29
  }
28
30
  }
@@ -1,29 +0,0 @@
1
- 'use strict';
2
-
3
- const PackageJson = require('@npmcli/package-json');
4
-
5
- /**
6
- * @param {string} path
7
- * @returns {Promise<PackageJson.Content>}
8
- */
9
- exports.loadPackage = async function loadPackage(path) {
10
- try {
11
- const pkg = await PackageJson.load(path);
12
- return pkg.content;
13
- } catch (/** @type {any} */ cause) {
14
- if (cause.code === 'ENOENT') {
15
- throw Object.assign(new Error(`No package.json file found at ${path}`, { cause }), {
16
- code: 'PACKAGE_JSON_MISSING'
17
- });
18
- }
19
- if (cause.code === 'EJSONPARSE') {
20
- throw Object.assign(
21
- new Error(`Invalid package.json file found at ${path}`, { cause }),
22
- {
23
- code: 'PACKAGE_JSON_INVALID'
24
- }
25
- );
26
- }
27
- throw cause;
28
- }
29
- };