Skip to content

Commit

Permalink
Update maybe-run.js script (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcornelissen authored Oct 21, 2023
1 parent 376a77d commit c97c3c8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ rules:
- error
- allowedLicenses:
- MIT
- MIT-0
- MPL-2.0
numericOnlyVariation: false
jsdoc/empty-tags:
Expand Down Expand Up @@ -755,6 +754,7 @@ ignorePatterns:
- _reports/
- .temp/
- node_modules/
- script/maybe-run.js
- index.cjs
- index.d.cts
- testing.cjs
Expand Down
7 changes: 0 additions & 7 deletions script/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ 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
95 changes: 77 additions & 18 deletions script/maybe-run.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,94 @@
/**
* @overview Run a tool if it's present on the PATH, otherwise output a warning.
* If used in a CI context, the tool will be run without any checks.
* @license MIT-0
* # Maybe Run JS
*
* Run a tool if it is present on the PATH, otherwise output a warning.
*
* Retrieved from (and updates available at):
* https://gist.github.com/ericcornelissen/6253b63db8c6f6ec6a58cb4d36c8e989
*
* ## Installation
*
* This script is intended to be vendored:
*
* - Add `is-ci` as a development dependency (if it is not already) using
* `npm install --save-dev is-ci`.
* - Create a new file in your project, e.g. `script/maybe-run.js`, and copy
* this code into it.
* - Use as `$ node script/maybe-run.js`.
*
* ## Description
*
* A helper script for Node.js projects to aid in continuously running optional
* tooling (without errors if it is missing). When used it will run the given
* command if and only if it is available on the current system, outputting a
* warning when the command is not found. In a continuous integration context
* the script will still exit with a non-zero exit code if the command is not
* found.
*
* The target command will be executed in the working directory where this
* script is invoked. That is, when running `node path/to/maybe-run.js CMD`, the
* command will be invoked in the directory where `node` is being invoked.
*
* For instructions on how to use this script run `node maybe-run.js` (without
* any further arguments).
*
* ## License
*
* MIT No Attribution
*
* Copyright 2023 Eric Cornelissen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import cp from "node:child_process";
import process from "node:process";

import isCI from "is-ci";
import which from "which";

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

if (common.argv.length === 0) {
console.error("Provide a command to try and execute.");
if (process.argv.length < 3) {
console.info(
"Provide a command to try and execute.",
"\n",
"\nUsage:",
"\n node path/to/maybe-run.js [CMD] [ARGS...]",
);
process.exit(1);
}

const cmd = common.argv[0];
const args = common.argv.slice(1);
const cmd = process.argv[2];
const args = process.argv.slice(3 /*, end */);
const { status } = cp.spawnSync(cmd, args, {
cwd: "./",
stdio: "inherit",
});

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

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

0 comments on commit c97c3c8

Please sign in to comment.