From 9fc2e9e94bc162c0b2214244df9201254a8bdb2c Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Sun, 17 Jul 2022 18:58:43 +0200 Subject: [PATCH] Support scripts in `bin/` folder --- README.md | 8 ++++++-- index.js | 7 ++++++- package.json | 2 +- test/bin/run.js | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 test/bin/run.js diff --git a/README.md b/README.md index 371b757..645de41 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,13 @@ require('version-guard')('./path/to/file/to/run', 14, 18); * **minMajor** - the lowest major Node.js version that should be allowed to run the file * **[minMinor]** - the lowest minor version of `minMajor` that should be allowed to run the file -Returns no data. Throws when configured incorrectly. Logs when failing silently. +On supported versions imports and runs `filePath` using the [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) (supporting both ESM and CJS modules). -`filePath` gets imported using the [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import), thus supporting both ESM and CJS modules. +On non-supported versions, fails silently with an error message. + +Apart from checking current node version this command also looks up the main project's `package.json` and checks that the `engines.node` in it mentions the same version number as is sent to this command. To ensure that maintainers doesn't forget to update one of the two and thus the two diverging. + +## Notes This project itself is a CJS project as the entire point is to work on incredibly old node.js versions. diff --git a/index.js b/index.js index 93438b7..04e8903 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,12 @@ var versionGuard = function (filePath, minMajor, minMinor) { } var mainPath = path.dirname(mainFile); - var pkgPath = path.resolve(mainPath, './package.json'); + var pkgPath = path.resolve( + mainPath, + (mainPath.slice(-4) === '/bin' || mainPath.slice(-4) === '\\bin') + ? '../package.json' + : './package.json' + ); /** @type {{ [key: string]: any } | undefined} */ var pkgJson; diff --git a/package.json b/package.json index 49999f0..b692522 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "clean": "run-p clean:*", "prepare": "husky install", "prepublishOnly": "run-s build", - "test-ci": "node test/run.js", + "test-ci": "node test/run.js && node test/bin/run.js", "test": "run-s check test-ci" }, "keywords": [], diff --git a/test/bin/run.js b/test/bin/run.js new file mode 100644 index 0000000..8013cc1 --- /dev/null +++ b/test/bin/run.js @@ -0,0 +1 @@ +require('../..')('../guarded.mjs', 16, 0);