hereby 1.0.2 → 1.0.5

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
@@ -1,6 +1,8 @@
1
1
  # hereby
2
2
 
3
- [![npm version](https://badge.fury.io/js/hereby.svg)](https://badge.fury.io/js/hereby)
3
+ [![npm](https://img.shields.io/npm/v/hereby.svg)](https://npmjs.com/package/hereby)
4
+ [![node](https://img.shields.io/node/v/hereby.svg)](https://nodejs.org)
5
+ [![install size](https://packagephobia.com/badge?p=hereby)](https://packagephobia.com/result?p=hereby)
4
6
  [![ci](https://github.com/jakebailey/hereby/actions/workflows/ci.yml/badge.svg)](https://github.com/jakebailey/hereby/actions/workflows/ci.yml)
5
7
  [![codecov](https://codecov.io/gh/jakebailey/hereby/branch/main/graph/badge.svg?token=YL2Z1uk5dh)](https://codecov.io/gh/jakebailey/hereby)
6
8
 
@@ -8,7 +10,10 @@
8
10
 
9
11
  # Herebyfile.mjs
10
12
 
11
- Tasks are defined in `Herebyfile.mjs`, like:
13
+ Tasks are defined in `Herebyfile.mjs`. Exported tasks are available to run
14
+ at the CLI, with support for `export default`.
15
+
16
+ For example:
12
17
 
13
18
  ```js
14
19
  import { execa } from "execa";
package/dist/cli/index.js CHANGED
@@ -4,71 +4,72 @@ import { formatTasks } from "./formatTasks.js";
4
4
  import { findHerebyfile, loadHerebyfile } from "./loadHerebyfile.js";
5
5
  import { getUsage, parseArgs } from "./parseArgs.js";
6
6
  import { reexec } from "./reexec.js";
7
- import { runTasksWithCLIRunner } from "./runner.js";
7
+ import { CLIRunner } from "./runner.js";
8
8
  import { ExitCodeError, simplifyPath, UserError } from "./utils.js";
9
- export async function main(system) {
9
+ export async function main(d) {
10
10
  try {
11
- await mainWorker(system);
11
+ await mainWorker(d);
12
12
  }
13
13
  catch (e) {
14
14
  if (e instanceof ExitCodeError) {
15
- system.process.exitCode = e.exitCode;
15
+ d.setExitCode(e.exitCode);
16
16
  }
17
17
  else if (e instanceof UserError) {
18
- system.error(`${chalk.red("Error")}: ${e.message}`);
19
- system.process.exitCode = 1;
18
+ d.error(`${chalk.red("Error")}: ${e.message}`);
19
+ d.setExitCode(1);
20
20
  }
21
21
  else {
22
22
  throw e;
23
23
  }
24
24
  }
25
25
  }
26
- async function mainWorker(system) {
27
- const args = parseArgs(system.process.argv.slice(2));
26
+ async function mainWorker(d) {
27
+ const args = parseArgs(d.argv.slice(2));
28
28
  if (args.help) {
29
- system.log(getUsage());
29
+ d.log(getUsage());
30
30
  return;
31
31
  }
32
- let herebyfilePath = args.herebyfile ?? (await findHerebyfile(system.process.cwd()));
33
- herebyfilePath = path.resolve(system.process.cwd(), herebyfilePath);
34
- if (await reexec(system, herebyfilePath)) {
32
+ let herebyfilePath = args.herebyfile ?? (await findHerebyfile(d.cwd()));
33
+ herebyfilePath = path.resolve(d.cwd(), herebyfilePath);
34
+ if (await reexec(d, herebyfilePath)) {
35
35
  return;
36
36
  }
37
- system.process.chdir(path.dirname(herebyfilePath));
37
+ d.chdir(path.dirname(herebyfilePath));
38
+ d.log(`Using ${simplifyPath(herebyfilePath)}`);
38
39
  const herebyfile = await loadHerebyfile(herebyfilePath);
39
40
  if (args.printTasks) {
40
- system.log(formatTasks(herebyfile.tasks, herebyfile.defaultTask));
41
+ d.log(formatTasks(herebyfile.tasks, herebyfile.defaultTask));
41
42
  return;
42
43
  }
44
+ const tasks = selectTasks(herebyfile, args.run);
45
+ try {
46
+ const runner = new CLIRunner(d);
47
+ await runner.runTasks(...tasks);
48
+ }
49
+ catch (e) {
50
+ // We will have already printed some message here.
51
+ // Set the error code and let the process run to completion,
52
+ // so we don't end up with an unflushed output.
53
+ throw new ExitCodeError(1, e);
54
+ }
55
+ }
56
+ export function selectTasks(herebyfile, taskNames) {
43
57
  const allTasks = new Map();
44
58
  for (const task of herebyfile.tasks) {
45
59
  allTasks.set(task.options.name, task);
46
60
  }
47
- let tasks;
48
- if (args.run && args.run.length > 0) {
49
- tasks = args.run.map((name) => {
61
+ if (taskNames && taskNames.length > 0) {
62
+ return taskNames.map((name) => {
50
63
  const task = allTasks.get(name);
51
64
  if (!task) {
52
- throw new UserError(`Task ${name} does not exist or is not exported in the Herebyfile.`);
65
+ throw new UserError(`Task "${name}" does not exist or is not exported in the Herebyfile.`);
53
66
  }
54
67
  return task;
55
68
  });
56
69
  }
57
- else {
58
- if (!herebyfile.defaultTask) {
59
- throw new UserError("No default task defined; please specify a task name.");
60
- }
61
- tasks = [herebyfile.defaultTask];
62
- }
63
- system.log(`Using ${simplifyPath(herebyfilePath)}`);
64
- try {
65
- await runTasksWithCLIRunner(system, ...tasks);
66
- }
67
- catch {
68
- // We will have already printed some message here.
69
- // Set the error code and let the process run to completion,
70
- // so we don't end up with an unflushed output.
71
- throw new ExitCodeError(1);
70
+ if (!herebyfile.defaultTask) {
71
+ throw new UserError("No default task defined; please specify a task name.");
72
72
  }
73
+ return [herebyfile.defaultTask];
73
74
  }
74
75
  //# sourceMappingURL=index.js.map
@@ -1,8 +1,7 @@
1
1
  import chalk from "chalk";
2
- import foregroundChild from "foreground-child";
3
- import { resolve } from "import-meta-resolve";
4
2
  import { fileURLToPath, pathToFileURL } from "url";
5
- export async function reexec(system, herebyfilePath) {
3
+ import { UserError } from "./utils.js";
4
+ export async function reexec(d, herebyfilePath) {
6
5
  // If hereby is installed globally, but run against a Herebyfile in some
7
6
  // other package, that Herebyfile's import will resolve to a different
8
7
  // installation of the hereby package. There's no guarantee that the two
@@ -15,17 +14,23 @@ export async function reexec(system, herebyfilePath) {
15
14
  // TODO: Rather than spawning a child process, perhaps we could instead
16
15
  // import the CLI from the other version and run it.
17
16
  const thisCLI = await resolveToPath("hereby/cli", new URL(import.meta.url));
18
- const otherCLI = await resolveToPath("hereby/cli", pathToFileURL(herebyfilePath));
17
+ let otherCLI;
18
+ try {
19
+ otherCLI = await resolveToPath("hereby/cli", pathToFileURL(herebyfilePath));
20
+ }
21
+ catch {
22
+ throw new UserError("Unable to find hereby; ensure hereby is installed in your package.");
23
+ }
19
24
  if (thisCLI === otherCLI) {
20
25
  return false;
21
26
  }
22
27
  // TODO: If this turns out to be common, remove this warning.
23
- system.error(`${chalk.yellow("Warning")}: re-running hereby as imported by the Herebyfile.`);
24
- const args = [...system.process.execArgv, otherCLI, ...system.process.argv.slice(2)];
25
- foregroundChild(system.process.execPath, args);
28
+ d.error(`${chalk.yellow("Warning")}: re-running hereby as imported by the Herebyfile.`);
29
+ const args = [...d.execArgv, otherCLI, ...d.argv.slice(2)];
30
+ d.foregroundChild(d.execPath, args);
26
31
  return true;
27
- }
28
- async function resolveToPath(specifier, url) {
29
- return fileURLToPath(new URL(await resolve(specifier, url.toString())));
32
+ async function resolveToPath(specifier, url) {
33
+ return fileURLToPath(new URL(await d.resolve(specifier, url.toString())));
34
+ }
30
35
  }
31
36
  //# sourceMappingURL=reexec.js.map
@@ -1,38 +1,33 @@
1
1
  import assert from "assert";
2
2
  import chalk from "chalk";
3
- import prettyMilliseconds from "pretty-ms";
4
3
  import { Runner } from "../runner.js";
5
- export function runTasksWithCLIRunner(system, ...tasks) {
6
- return new CLIRunner({ system, concurrency: system.numCPUs }).runTasks(...tasks);
7
- }
8
- class CLIRunner extends Runner {
9
- constructor(options) {
10
- super(options);
4
+ export class CLIRunner extends Runner {
5
+ constructor(d) {
6
+ super({ concurrency: d.numCPUs });
11
7
  this._errored = false;
12
8
  this._startTimes = new WeakMap();
13
- this._system = options.system;
9
+ this._d = d;
14
10
  }
15
11
  onTaskStart(task) {
16
12
  this._startTimes.set(task, Date.now());
17
13
  if (this._errored) {
18
14
  return; // Skip logging.
19
15
  }
20
- this._system.log(`Starting ${chalk.blue(task.options.name)}`);
16
+ this._d.log(`Starting ${chalk.blue(task.options.name)}`);
21
17
  }
22
18
  onTaskFinish(task) {
23
19
  if (this._errored) {
24
20
  return; // Skip logging.
25
21
  }
26
22
  const took = Date.now() - checkDefined(this._startTimes.get(task));
27
- this._system.log(`Finished ${chalk.green(task.options.name)} in ${prettyMilliseconds(took)}`);
23
+ this._d.log(`Finished ${chalk.green(task.options.name)} in ${this._d.prettyMilliseconds(took)}`);
28
24
  }
29
25
  onTaskError(task, e) {
30
26
  if (this._errored) {
31
27
  return; // Skip logging.
32
28
  }
33
29
  this._errored = true;
34
- this._system.error(`Error in ${chalk.red(task.options.name)}`);
35
- this._system.error(`${e}`);
30
+ this._d.error(`Error in ${chalk.red(task.options.name)}\n${e}`);
36
31
  }
37
32
  }
38
33
  function checkDefined(value) {
package/dist/cli/utils.js CHANGED
@@ -31,20 +31,34 @@ export class UserError extends Error {
31
31
  * without logging anything.
32
32
  */
33
33
  export class ExitCodeError {
34
- constructor(exitCode) {
34
+ constructor(exitCode, reason) {
35
35
  this.exitCode = exitCode;
36
+ this.reason = reason;
36
37
  }
37
38
  }
38
- export function createSystem(process) {
39
+ /* eslint-disable no-restricted-globals */
40
+ export async function real() {
41
+ const { default: foregroundChild } = await import("foreground-child");
42
+ const { resolve } = await import("import-meta-resolve");
43
+ const { default: prettyMilliseconds } = await import("pretty-ms");
39
44
  return {
40
- log(message) {
41
- process.stdout.write(message + "\n");
45
+ log: console.log,
46
+ error: console.error,
47
+ // eslint-disable-next-line @typescript-eslint/unbound-method
48
+ cwd: process.cwd,
49
+ // eslint-disable-next-line @typescript-eslint/unbound-method
50
+ chdir: process.chdir,
51
+ argv: process.argv,
52
+ execArgv: process.execArgv,
53
+ execPath: process.execPath,
54
+ setExitCode: (code) => {
55
+ process.exitCode = code;
42
56
  },
43
- error(message) {
44
- process.stderr.write(message + "\n");
45
- },
46
- process,
47
57
  numCPUs: os.cpus().length,
58
+ foregroundChild,
59
+ resolve,
60
+ prettyMilliseconds,
48
61
  };
49
62
  }
63
+ /* eslint-enable no-restricted-globals */
50
64
  //# sourceMappingURL=utils.js.map
package/dist/cli.js CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
  import { main } from "./cli/index.js";
3
- import { createSystem } from "./cli/utils.js";
4
- // eslint-disable-next-line no-restricted-globals
5
- await main(createSystem(process));
3
+ import { real } from "./cli/utils.js";
4
+ await main(await real());
6
5
  //# sourceMappingURL=cli.js.map
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ export interface TaskOptions {
24
24
  * A hereby Task. To get an instance, call `test`.
25
25
  */
26
26
  export declare class Task {
27
+ private _options;
27
28
  private constructor();
28
29
  }
29
30
  /**
package/dist/index.js CHANGED
@@ -3,6 +3,28 @@
3
3
  */
4
4
  export class Task {
5
5
  constructor(options) {
6
+ // Runtime typecheck; consumers of hereby may not have enabled
7
+ // typechecking, so this is helpful.
8
+ if (typeof options.name !== "string") {
9
+ throw new Error("Task name is not a string.");
10
+ }
11
+ if (typeof options.description !== "string" && typeof options.description !== "undefined") {
12
+ throw new Error("Task description is not a string or undefined.");
13
+ }
14
+ if (!Array.isArray(options.dependencies) && typeof options.dependencies !== "undefined") {
15
+ throw new Error("Task dependencies is not an array or undefined.");
16
+ }
17
+ if (options.dependencies) {
18
+ for (const dep of options.dependencies) {
19
+ if (!(dep instanceof Task)) {
20
+ throw new Error("Task dependency is not a task.");
21
+ }
22
+ }
23
+ }
24
+ if (typeof options.run !== "function" && typeof options.run !== "undefined") {
25
+ throw new Error("Task run is not a function or undefined.");
26
+ }
27
+ // Non-type checks.
6
28
  if (!options.name) {
7
29
  throw new Error("Task name must not be empty.");
8
30
  }
@@ -12,7 +34,11 @@ export class Task {
12
34
  if (!options.dependencies?.length && !options.run) {
13
35
  throw new Error("Task must have at run function or dependencies.");
14
36
  }
15
- this.options = options;
37
+ this._options = options;
38
+ }
39
+ /* @internal */
40
+ get options() {
41
+ return this._options;
16
42
  }
17
43
  /* @internal */
18
44
  static create(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hereby",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "A simple task runner",
5
5
  "repository": "github:jakebailey/hereby",
6
6
  "type": "module",
@@ -35,8 +35,7 @@
35
35
  "LICENSE",
36
36
  "./dist/**/*.js",
37
37
  "!**/__tests__/**",
38
- "./dist/index.d.ts",
39
- "!./dist/testUtils.js"
38
+ "./dist/index.d.ts"
40
39
  ],
41
40
  "dependencies": {
42
41
  "chalk": "^5.0.1",
@@ -57,12 +56,10 @@
57
56
  "@typescript-eslint/parser": "^5.33.1",
58
57
  "ava": "^4.3.1",
59
58
  "c8": "^7.12.0",
60
- "cross-env": "^7.0.3",
61
59
  "eslint": "^8.22.0",
62
60
  "eslint-config-prettier": "^8.5.0",
63
61
  "eslint-plugin-ava": "^13.2.0",
64
62
  "eslint-plugin-simple-import-sort": "^7.0.0",
65
- "esmock": "^1.9.4",
66
63
  "execa": "^6.1.0",
67
64
  "moq.ts": "^9.0.2",
68
65
  "prettier": "^2.7.1",
@@ -91,8 +88,6 @@
91
88
  "compile": false
92
89
  },
93
90
  "environmentVariables": {
94
- "NODE_OPTIONS": "--loader=esmock",
95
- "NODE_NO_WARNINGS": "1",
96
91
  "FORCE_COLOR": "0"
97
92
  }
98
93
  },