Skip to content

Commit

Permalink
Create script to run a command iff it's available on the PATH
Browse files Browse the repository at this point in the history
... and use it to run actionlint and ShellCheck.
  • Loading branch information
ericcornelissen committed Oct 11, 2023
1 parent 4f03fd8 commit abaae3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ rules:
- error
- allowedLicenses:
- MIT
- MIT-0
- MPL-2.0
numericOnlyVariation: false
jsdoc/empty-tags:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@
"format:check": "npm run _prettier -- --check",
"fuzz": "node script/fuzz.js",
"license-check": "licensee --errors-only",
"lint": "npm run lint:js && npm run lint:json && npm run lint:md && npm run lint:yml",
"lint:ci": "actionlint",
"lint": "npm run lint:ci && npm run lint:js && npm run lint:json && npm run lint:md && npm run lint:sh && npm run lint:yml",
"lint:ci": "node script/maybe-run.js actionlint",
"lint:js": "npm run _eslint -- --ext .js,.cjs",
"lint:json": "npm run _eslint -- --ext .json,.jsonc",
"lint:md": "markdownlint --dot --ignore-path .gitignore .",
"lint:sh": "shellcheck script/hooks/*.sh script/hooks/pre-*",
"lint:sh": "node script/maybe-run.js shellcheck script/hooks/*.sh script/hooks/pre-*",
"lint:yml": "npm run _eslint -- --ext .yml",
"mutation": "npm run mutation:unit && npm run mutation:integration",
"mutation:integration": "stryker run stryker.integration.config.js",
Expand Down
7 changes: 7 additions & 0 deletions script/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export function isWindows() {
return os.platform() === "win32";
}

export function spawnSync(cmd, argv) {
return cp.spawnSync(cmd, argv, {
cwd: projectRoot,
stdio: "inherit",
});
}

export function npm(argv) {
return cp.spawn(npmCmd, argv, {
cwd: projectRoot,
Expand Down
31 changes: 31 additions & 0 deletions script/maybe-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @overview Run a tool if it's present on the PATH, otherwise output a warning.
* @license MIT-0
*/

import process from "node:process";

import which from "which";

import { common } from "./_.js";

if (common.argv.length === 0) {
console.error("Provide a command to try and execute.");
process.exit(1);
}

const cmd = common.argv[0];
const args = common.argv.slice(1);

try {
which.sync(cmd);
} catch (_) {
console.warn(
`Command '${cmd}' not found, it will not be run.`,
"Install it to make this warning go away.",
);
process.exit(0);
}

const { status } = common.spawnSync(cmd, args);
process.exit(status);

0 comments on commit abaae3d

Please sign in to comment.