Skip to content

Commit

Permalink
Support scripts in bin/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 17, 2022
1 parent 40ddf65 commit 9fc2e9e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
1 change: 1 addition & 0 deletions test/bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../..')('../guarded.mjs', 16, 0);

0 comments on commit 9fc2e9e

Please sign in to comment.