UNPKG

1.58 kBPlain TextView Raw
1import debug from "debug";
2import fsExtra from "fs-extra";
3
4import { task } from "../internal/core/config/config-env";
5import { BuidlerError } from "../internal/core/errors";
6import { ERRORS } from "../internal/core/errors-list";
7import { runScriptWithBuidler } from "../internal/util/scripts-runner";
8
9import { TASK_COMPILE, TASK_RUN } from "./task-names";
10
11export default function () {
12 const log = debug("buidler:core:tasks:run");
13
14 task(TASK_RUN, "Runs a user-defined script after compiling the project")
15 .addPositionalParam(
16 "script",
17 "A js file to be run within buidler's environment"
18 )
19 .addFlag("noCompile", "Don't compile before running this task")
20 .setAction(
21 async (
22 { script, noCompile }: { script: string; noCompile: boolean },
23 { run, buidlerArguments }
24 ) => {
25 if (!(await fsExtra.pathExists(script))) {
26 throw new BuidlerError(ERRORS.BUILTIN_TASKS.RUN_FILE_NOT_FOUND, {
27 script,
28 });
29 }
30
31 if (!noCompile) {
32 await run(TASK_COMPILE);
33 }
34
35 log(
36 `Running script ${script} in a subprocess so we can wait for it to complete`
37 );
38
39 try {
40 process.exitCode = await runScriptWithBuidler(
41 buidlerArguments,
42 script
43 );
44 } catch (error) {
45 throw new BuidlerError(
46 ERRORS.BUILTIN_TASKS.RUN_SCRIPT_ERROR,
47 {
48 script,
49 error: error.message,
50 },
51 error
52 );
53 }
54 }
55 );
56}