check-node-engine 1.0.0 → 1.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 CHANGED
@@ -22,7 +22,7 @@ Compare the required node engine to the last older LTS node version.
22
22
 
23
23
  ### Methods
24
24
 
25
- * ``` (packageJsonPath?: string) => Promise<void> ``` compare node versions
25
+ * ``` (source?: string | tPackageType) => Promise<void> ``` compare node versions
26
26
 
27
27
  ### Command line options
28
28
 
package/bin/cmd.js CHANGED
@@ -24,9 +24,9 @@
24
24
 
25
25
  if (0 < ARGS.length && ARGS.includes("--get-lts")) {
26
26
 
27
- const getOlderCurrentOfficialLTSVersion = require("../lib/cjs/getOlderCurrentOfficialLTSVersion.js");
27
+ const getCachedOlderCurrentOfficialLTSVersion = require("../lib/cjs/utils/getCachedOlderCurrentOfficialLTSVersion.js").default;
28
28
 
29
- (getOlderCurrentOfficialLTSVersion.default ?? getOlderCurrentOfficialLTSVersion)().then((lts) => {
29
+ getCachedOlderCurrentOfficialLTSVersion().then((lts) => {
30
30
 
31
31
  (0, console).log(lts);
32
32
 
@@ -1 +1,2 @@
1
- export default function checkVersionNode(packageJsonPath?: string): Promise<void>;
1
+ export type tPackageType = Record<string, object | string | number | boolean>;
2
+ export default function checkVersionNode(source?: string | tPackageType): Promise<void>;
@@ -7,26 +7,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.default = checkVersionNode;
8
8
  // natives
9
9
  const node_path_1 = require("node:path");
10
+ const promises_1 = require("node:fs/promises");
10
11
  // locals
11
- const isFile_1 = __importDefault(require("./utils/isFile"));
12
- const readPackageEngineNode_1 = __importDefault(require("./readPackageEngineNode"));
13
- const getOlderCurrentOfficialLTSVersion_1 = __importDefault(require("./getOlderCurrentOfficialLTSVersion"));
14
- const compareVersions_1 = __importDefault(require("./utils/compareVersions"));
12
+ const checkFile_1 = __importDefault(require("./utils/checkFile"));
13
+ const isPlainObject_1 = __importDefault(require("./utils/isPlainObject"));
14
+ const getEngineNode_1 = __importDefault(require("./utils/getEngineNode"));
15
+ const getCachedOlderCurrentOfficialLTSVersion_1 = __importDefault(require("./utils/getCachedOlderCurrentOfficialLTSVersion"));
15
16
  const minimumVersionFromEngine_1 = __importDefault(require("./utils/minimumVersionFromEngine"));
17
+ const compareVersions_1 = __importDefault(require("./utils/compareVersions"));
16
18
  // module
17
- function checkVersionNode(packageJsonPath = (0, node_path_1.join)(process.cwd(), "package.json")) {
18
- return (0, isFile_1.default)(packageJsonPath).then((isPackageAFile) => {
19
- if (!isPackageAFile) {
20
- throw new Error("this data is not a file: \"" + packageJsonPath + "\"");
21
- }
22
- }).then(() => {
23
- return (0, readPackageEngineNode_1.default)(packageJsonPath).then((engineNode) => {
24
- return (0, getOlderCurrentOfficialLTSVersion_1.default)().then((olderCurrentOfficialLTS) => {
25
- const engineMinimum = (0, minimumVersionFromEngine_1.default)(engineNode);
26
- if (0 > (0, compareVersions_1.default)(engineMinimum, olderCurrentOfficialLTS)) {
27
- throw new Error("engines.node \"" + engineNode + "\" (minimum " + engineMinimum + ") is lower than the older current official LTS Node.js (\"" + olderCurrentOfficialLTS + "\")");
28
- }
19
+ function checkVersionNode(source = (0, node_path_1.join)(process.cwd(), "package.json")) {
20
+ return Promise.resolve().then(() => {
21
+ if ("string" === typeof source) {
22
+ return (0, checkFile_1.default)(source).then(() => {
23
+ return (0, promises_1.readFile)(source, "utf-8");
24
+ }).then((content) => {
25
+ return JSON.parse(content);
29
26
  });
27
+ }
28
+ else if ((0, isPlainObject_1.default)(source)) {
29
+ return source;
30
+ }
31
+ else {
32
+ throw new TypeError("\"source\" parameter is not a string or a package type");
33
+ }
34
+ }).then((packageData) => {
35
+ const engineNode = (0, getEngineNode_1.default)(packageData);
36
+ return (0, getCachedOlderCurrentOfficialLTSVersion_1.default)().then((olderCurrentOfficialLTS) => {
37
+ const engineMinimum = (0, minimumVersionFromEngine_1.default)(engineNode);
38
+ if (0 > (0, compareVersions_1.default)(engineMinimum, olderCurrentOfficialLTS)) {
39
+ throw new Error("engines.node \"" + engineNode + "\" (minimum " + engineMinimum + ") is lower than the older current official LTS Node.js (\"" + olderCurrentOfficialLTS + "\")");
40
+ }
30
41
  });
31
42
  });
32
43
  }
@@ -0,0 +1,13 @@
1
+ export type Release = {
2
+ "version": string;
3
+ "date": string;
4
+ "lts": boolean;
5
+ "files": string[];
6
+ "npm": string;
7
+ "v8": string;
8
+ "uv": string;
9
+ "zlib": string;
10
+ "openssl": string;
11
+ "modules": string;
12
+ "security": boolean;
13
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ export default class LTSCache {
2
+ private constructor();
3
+ static cleanupExpired(): Promise<void>;
4
+ static readFresh(): Promise<string | null>;
5
+ static write(version: string): Promise<void>;
6
+ private static _getCacheDir;
7
+ private static _formatFilename;
8
+ private static _parseFilename;
9
+ private static _isExpired;
10
+ private static _listCacheFiles;
11
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ // deps
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ // natives
5
+ const node_path_1 = require("node:path");
6
+ const node_os_1 = require("node:os");
7
+ const promises_1 = require("node:fs/promises");
8
+ // consts
9
+ const CACHE_DIR_NAME = "check-node-engine";
10
+ const CACHE_TTL_MS = 15 * 60 * 1000;
11
+ const CACHE_FILENAME_PATTERN = /^(\d{4}-\d{2}-\d{2}T)(\d{2})-(\d{2})-(\d{2})-(\d{3})Z$/;
12
+ // module
13
+ // eslint-disable-next-line @typescript-eslint/no-extraneous-class
14
+ class LTSCache {
15
+ constructor() {
16
+ throw new Error("LTSCache is a static class");
17
+ }
18
+ static cleanupExpired() {
19
+ return LTSCache._listCacheFiles().then((files) => {
20
+ const now = new Date();
21
+ const dir = LTSCache._getCacheDir();
22
+ return Promise.all(files.map((file) => {
23
+ const date = LTSCache._parseFilename(file);
24
+ if (null === date || !LTSCache._isExpired(date, now)) {
25
+ return Promise.resolve();
26
+ }
27
+ return (0, promises_1.unlink)((0, node_path_1.join)(dir, file)).then(() => {
28
+ // nothing to do here
29
+ }).catch(() => {
30
+ // nothing to do here
31
+ });
32
+ })).then(() => {
33
+ // nothing to do here
34
+ });
35
+ }).catch(() => {
36
+ // nothing to do here
37
+ });
38
+ }
39
+ static readFresh() {
40
+ return LTSCache._listCacheFiles().then((files) => {
41
+ const now = new Date();
42
+ const fresh = [];
43
+ files.forEach((file) => {
44
+ const date = LTSCache._parseFilename(file);
45
+ if (null === date || LTSCache._isExpired(date, now)) {
46
+ return;
47
+ }
48
+ fresh.push({
49
+ "name": file,
50
+ "date": date
51
+ });
52
+ });
53
+ fresh.sort((a, b) => {
54
+ return b.date.getTime() - a.date.getTime();
55
+ });
56
+ if (0 === fresh.length) {
57
+ return Promise.resolve(null);
58
+ }
59
+ return (0, promises_1.readFile)((0, node_path_1.join)(LTSCache._getCacheDir(), fresh[0].name), "utf-8").then((content) => {
60
+ const trimmed = content.trim();
61
+ return "" === trimmed ? null : trimmed;
62
+ }).catch(() => {
63
+ return null;
64
+ });
65
+ }).catch(() => {
66
+ return null;
67
+ });
68
+ }
69
+ static write(version) {
70
+ const dir = LTSCache._getCacheDir();
71
+ const file = (0, node_path_1.join)(dir, LTSCache._formatFilename(new Date()));
72
+ return (0, promises_1.mkdir)(dir, {
73
+ "recursive": true
74
+ }).then(() => {
75
+ return (0, promises_1.writeFile)(file, version, "utf-8");
76
+ }).catch(() => {
77
+ // nothing to do here
78
+ });
79
+ }
80
+ static _getCacheDir() {
81
+ return (0, node_path_1.join)((0, node_os_1.homedir)(), CACHE_DIR_NAME);
82
+ }
83
+ static _formatFilename(date) {
84
+ return date.toISOString().replaceAll(":", "-").replace(".", "-");
85
+ }
86
+ static _parseFilename(name) {
87
+ const match = CACHE_FILENAME_PATTERN.exec(name);
88
+ if (null === match) {
89
+ return null;
90
+ }
91
+ const parsed = new Date(match[1] + match[2] + ":" + match[3] + ":" + match[4] + "." + match[5] + "Z");
92
+ if (Number.isNaN(parsed.getTime())) {
93
+ return null;
94
+ }
95
+ return parsed;
96
+ }
97
+ static _isExpired(date, now) {
98
+ return CACHE_TTL_MS < now.getTime() - date.getTime();
99
+ }
100
+ static _listCacheFiles() {
101
+ return (0, promises_1.readdir)(LTSCache._getCacheDir(), {
102
+ "withFileTypes": true
103
+ }).then((entries) => {
104
+ return entries.filter((entry) => {
105
+ return entry.isFile();
106
+ }).map((entry) => {
107
+ return entry.name;
108
+ });
109
+ });
110
+ }
111
+ }
112
+ exports.default = LTSCache;
@@ -0,0 +1 @@
1
+ export default function checkFile(file: string): Promise<void>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ // deps
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.default = checkFile;
8
+ // locals
9
+ const isFile_1 = __importDefault(require("./isFile"));
10
+ // module
11
+ function checkFile(file) {
12
+ return (0, isFile_1.default)(file).then((isItAFile) => {
13
+ if (!isItAFile) {
14
+ throw new Error("\"file\" parameter (" + file + ") is not a valid file");
15
+ }
16
+ });
17
+ }
@@ -0,0 +1 @@
1
+ export default function getCachedOlderCurrentOfficialLTSVersion(): Promise<string>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ // deps
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.default = getCachedOlderCurrentOfficialLTSVersion;
8
+ // locals
9
+ const LTSCache_1 = __importDefault(require("./LTSCache"));
10
+ const getOlderCurrentOfficialLTSVersion_1 = __importDefault(require("./getOlderCurrentOfficialLTSVersion"));
11
+ // module
12
+ function getCachedOlderCurrentOfficialLTSVersion() {
13
+ return LTSCache_1.default.cleanupExpired().then(() => {
14
+ return LTSCache_1.default.readFresh();
15
+ }).then((cached) => {
16
+ if (null !== cached) {
17
+ return cached;
18
+ }
19
+ return (0, getOlderCurrentOfficialLTSVersion_1.default)().then((version) => {
20
+ return LTSCache_1.default.write(version).then(() => {
21
+ return version;
22
+ });
23
+ });
24
+ });
25
+ }
@@ -0,0 +1,2 @@
1
+ import type { tPackageType } from "../checkVersionNode";
2
+ export default function getEngineNode(packageData: tPackageType): string;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // types & interfaces
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.default = getEngineNode;
5
+ // module
6
+ function getEngineNode(packageData) {
7
+ if ("object" !== typeof packageData || !("engines" in packageData)) {
8
+ throw new Error("package.json must contain an \"engines\" field");
9
+ }
10
+ const { engines } = packageData;
11
+ if ("object" !== typeof engines || !("node" in engines)) {
12
+ throw new Error("package.json \"engines\" must contain a \"node\" field");
13
+ }
14
+ const { "node": engineNode } = engines;
15
+ if ("string" !== typeof engineNode || "" === engineNode.trim()) {
16
+ throw new Error("package.json \"engines.node\" must be a non-empty string");
17
+ }
18
+ return engineNode;
19
+ }
@@ -10,7 +10,7 @@ const node_https_1 = require("node:https");
10
10
  // externals
11
11
  const semver_1 = __importDefault(require("semver"));
12
12
  // locals
13
- const compareVersions_1 = __importDefault(require("./utils/compareVersions"));
13
+ const compareVersions_1 = __importDefault(require("./compareVersions"));
14
14
  // consts
15
15
  const NODEJS_DIST_INDEX_URL = "https://nodejs.org/dist/index.json";
16
16
  const LTS_LINE_MAX_AGE_MONTHS = 18;
@@ -47,18 +47,12 @@ function getOlderCurrentOfficialLTSVersion() {
47
47
  const major = String(coerced.major);
48
48
  const current = latestByMajor.get(major);
49
49
  if (!current || 0 < (0, compareVersions_1.default)(release.version, current.version)) {
50
- latestByMajor.set(major, {
51
- "date": release.date,
52
- "lts": release.lts,
53
- "version": release.version
54
- });
50
+ latestByMajor.set(major, { ...release });
55
51
  }
56
52
  });
57
- const activeLTSLines = [...latestByMajor.values()]
58
- .filter((release) => {
53
+ const activeLTSLines = [...latestByMajor.values()].filter((release) => {
59
54
  return cutoff <= new Date(release.date);
60
- })
61
- .sort((a, b) => {
55
+ }).sort((a, b) => {
62
56
  return (0, compareVersions_1.default)(b.version, a.version);
63
57
  });
64
58
  if (2 > activeLTSLines.length) {
@@ -0,0 +1 @@
1
+ export default function isPlainObject(value: unknown): boolean;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ // module
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.default = isPlainObject;
5
+ function isPlainObject(value) {
6
+ if ("object" !== typeof value || null === value) {
7
+ return false;
8
+ }
9
+ return Object.getPrototypeOf(value) === Object.getPrototypeOf({});
10
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "check-node-engine",
4
- "version": "1.0.0",
4
+ "version": "1.2.0",
5
5
  "description": "Compare the required node engine to the last older LTS node version.",
6
6
 
7
7
  "type": "commonjs",
@@ -22,7 +22,7 @@
22
22
  "lint-bin": "npx eslint --config .eslintrc-bin.js --ext .js,.cjs,.mjs ./bin/**/*",
23
23
  "lint-back": "npx eslint --config .eslintrc-back.js --ext .ts,.cts,.mts ./lib/src/**/*",
24
24
  "lint-tests": "npx eslint --config .eslintrc-tests.js --ext .js,.cjs,.mjs ./test/**/*",
25
- "lint": "npm run lint-bin && npm run lint-back && npm run lint-tests",
25
+ "lint": "npm run lint-bin && npm run build && npm run lint-back && npm run lint-tests",
26
26
 
27
27
  "clean-back": "npx rimraf ./lib/cjs",
28
28
  "clean": "npm run clean-back",
@@ -48,23 +48,23 @@
48
48
  "lib/cjs"
49
49
  ],
50
50
  "engines": {
51
- "node": ">=22.22.3"
51
+ "node": ">=22.23.2"
52
52
  },
53
53
 
54
54
  "dependencies": {
55
- "semver": "7.8.1"
55
+ "semver": "7.8.5"
56
56
  },
57
57
  "devDependencies": {
58
- "@types/node": "25.9.1",
59
- "@types/semver": "7.7.1",
60
- "check-version-modules": "2.4.0",
58
+ "@types/node": "26.2.0",
59
+ "@types/semver": "7.8.0",
60
+ "check-version-modules": "2.5.0",
61
61
  "eslint-plugin-personnallinter": "git+ssh://git@github.com/Psychopoulet/eslint-plugin-personnallinter",
62
62
  "husky": "9.1.7",
63
- "mocha": "11.7.6",
63
+ "mocha": "11.8.0",
64
64
  "nyc": "18.0.0",
65
65
  "rimraf": "6.1.3",
66
66
  "typescript": "5.9.3",
67
- "used-deps-analyzer": "0.3.0"
67
+ "used-deps-analyzer": "0.4.0"
68
68
  },
69
69
  "optionalDependencies": {
70
70
  "colors": "1.4.0"
@@ -1 +0,0 @@
1
- export default function readPackageEngineNode(packageJsonPath: string): Promise<string>;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- // deps
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.default = readPackageEngineNode;
5
- // natives
6
- const promises_1 = require("node:fs/promises");
7
- // module
8
- function readPackageEngineNode(packageJsonPath) {
9
- return (0, promises_1.access)(packageJsonPath).catch(() => {
10
- throw new Error("package.json not found at \"" + packageJsonPath + "\"");
11
- }).then(() => {
12
- return (0, promises_1.readFile)(packageJsonPath, "utf8");
13
- }).then((content) => {
14
- let packageData = null;
15
- try {
16
- packageData = JSON.parse(content);
17
- }
18
- catch (e) {
19
- throw new Error("package.json is not valid JSON : " + (e instanceof Error ? e.message : String(e)));
20
- }
21
- if ("object" !== typeof packageData || !("engines" in packageData)) {
22
- throw new Error("package.json must contain an \"engines\" field");
23
- }
24
- const { engines } = packageData;
25
- if ("object" !== typeof engines || !("node" in engines)) {
26
- throw new Error("package.json \"engines\" must contain a \"node\" field");
27
- }
28
- const { "node": engineNode } = engines;
29
- if ("string" !== typeof engineNode || "" === engineNode.trim()) {
30
- throw new Error("package.json \"engines.node\" must be a non-empty string");
31
- }
32
- return engineNode;
33
- });
34
- }