@fynjs/run 1.1.0 → 1.1.1
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/bin/qrun.js +6 -1
- package/bin/xrun.js +1 -1
- package/dist/cli/check-global.d.ts +1 -0
- package/{cli → dist/cli}/check-global.js +3 -3
- package/dist/cli/ck.d.ts +1 -0
- package/dist/cli/cli-options.d.ts +80 -0
- package/dist/cli/cli-options.js +95 -0
- package/dist/cli/config.d.ts +7 -0
- package/dist/cli/config.js +7 -0
- package/dist/cli/env.d.ts +17 -0
- package/dist/cli/env.js +31 -0
- package/dist/cli/npm-loader.d.ts +2 -0
- package/dist/cli/npm-loader.js +43 -0
- package/dist/cli/parse-cmd-args.d.ts +34 -0
- package/dist/cli/parse-cmd-args.js +199 -0
- package/dist/cli/provider-packages.d.ts +28 -0
- package/dist/cli/provider-packages.js +62 -0
- package/dist/cli/search-up-task-file.d.ts +8 -0
- package/dist/cli/search-up-task-file.js +79 -0
- package/dist/cli/task-file.d.ts +40 -0
- package/dist/cli/task-file.js +177 -0
- package/dist/cli/ts-runner.d.ts +2 -0
- package/dist/cli/ts-runner.js +60 -0
- package/dist/cli/usage.d.ts +2 -0
- package/dist/cli/wrap-process.d.ts +10 -0
- package/dist/cli/wrap-process.js +26 -0
- package/dist/cli/xrun-main.d.ts +119 -0
- package/dist/cli/xrun-main.js +417 -0
- package/dist/cli/xrun.d.ts +2 -0
- package/{cli → dist/cli}/xrun.js +0 -1
- package/dist/lib/chalk.d.ts +2 -0
- package/{lib → dist/lib}/chalk.js +0 -1
- package/dist/lib/cli-context.d.ts +97 -0
- package/dist/lib/cli-context.js +129 -0
- package/dist/lib/defaults.d.ts +30 -0
- package/{lib → dist/lib}/defaults.js +13 -16
- package/dist/lib/gen-xqid.d.ts +2 -0
- package/dist/lib/gen-xqid.js +8 -0
- package/dist/lib/index.d.ts +3 -0
- package/{lib → dist/lib}/index.js +0 -3
- package/dist/lib/logger.d.ts +2 -0
- package/dist/lib/logger.js +95 -0
- package/dist/lib/my-pkg.d.ts +4 -0
- package/dist/lib/my-pkg.js +42 -0
- package/dist/lib/ns-order.d.ts +7 -0
- package/dist/lib/ns-order.js +62 -0
- package/dist/lib/print-tasks/index.d.ts +2 -0
- package/dist/lib/print-tasks/index.js +100 -0
- package/dist/lib/reporters/console.d.ts +20 -0
- package/dist/lib/reporters/console.js +119 -0
- package/dist/lib/stringify.d.ts +2 -0
- package/dist/lib/stringify.js +19 -0
- package/dist/lib/util/parse-array.d.ts +2 -0
- package/dist/lib/util/parse-array.js +5 -0
- package/dist/lib/util/task-options.d.ts +37 -0
- package/dist/lib/util/task-options.js +115 -0
- package/dist/lib/util/update-env.d.ts +1 -0
- package/dist/lib/util/update-env.js +18 -0
- package/dist/lib/xqitem.d.ts +22 -0
- package/dist/lib/xqitem.js +57 -0
- package/dist/lib/xqtor.d.ts +57 -0
- package/dist/lib/xqtor.js +704 -0
- package/dist/lib/xqtree.d.ts +11 -0
- package/dist/lib/xqtree.js +33 -0
- package/dist/lib/xrun-instance.d.ts +9 -0
- package/dist/lib/xrun-instance.js +27 -0
- package/dist/lib/xrun.d.ts +55 -0
- package/dist/lib/xrun.js +317 -0
- package/dist/lib/xtask-spec.d.ts +10 -0
- package/dist/lib/xtask-spec.js +57 -0
- package/dist/lib/xtasks.d.ts +25 -0
- package/dist/lib/xtasks.js +119 -0
- package/package.json +39 -15
- package/cli/cli-options.js +0 -98
- package/cli/config.js +0 -8
- package/cli/env.js +0 -33
- package/cli/npm-loader.js +0 -56
- package/cli/parse-cmd-args.js +0 -156
- package/cli/provider-packages.js +0 -73
- package/cli/search-up-task-file.js +0 -92
- package/cli/task-file.js +0 -192
- package/cli/ts-runner.js +0 -62
- package/cli/wrap-process.js +0 -27
- package/cli/xrun-main.js +0 -425
- package/lib/cli-context.js +0 -145
- package/lib/gen-xqid.js +0 -10
- package/lib/logger.js +0 -105
- package/lib/ns-order.js +0 -76
- package/lib/print-tasks/index.js +0 -105
- package/lib/reporters/console.js +0 -133
- package/lib/stringify.js +0 -16
- package/lib/util/parse-array.js +0 -5
- package/lib/util/update-env.js +0 -16
- package/lib/xqitem.js +0 -70
- package/lib/xqtor.js +0 -814
- package/lib/xqtree.js +0 -41
- package/lib/xrun-instance.js +0 -33
- package/lib/xrun.d.ts +0 -89
- package/lib/xrun.js +0 -344
- package/lib/xtask-spec.js +0 -63
- package/lib/xtasks.js +0 -137
- /package/{cli → dist/cli}/ck.js +0 -0
- /package/{cli → dist/cli}/usage.js +0 -0
package/bin/qrun.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
process.env.XRUN_QUIET = "1";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
//
|
|
5
|
+
// dynamic import - a static `import "./xrun.js"` is hoisted and its module body (which ends in
|
|
6
|
+
// a top level `await xrun()`) runs BEFORE the assignment above, so xrun never sees XRUN_QUIET.
|
|
7
|
+
//
|
|
8
|
+
await import("./xrun.js");
|
package/bin/xrun.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function checkGlobal(): boolean;
|
package/dist/cli/ck.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "chalker";
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
cwd: {
|
|
3
|
+
alias: string;
|
|
4
|
+
desc: string;
|
|
5
|
+
args: string;
|
|
6
|
+
requireArg: boolean;
|
|
7
|
+
};
|
|
8
|
+
dir: {
|
|
9
|
+
alias: string;
|
|
10
|
+
desc: string;
|
|
11
|
+
args: string;
|
|
12
|
+
requireArg: boolean;
|
|
13
|
+
};
|
|
14
|
+
npm: {
|
|
15
|
+
alias: string;
|
|
16
|
+
desc: string;
|
|
17
|
+
args: string;
|
|
18
|
+
argDefault: string;
|
|
19
|
+
};
|
|
20
|
+
nmbin: {
|
|
21
|
+
alias: string;
|
|
22
|
+
desc: string;
|
|
23
|
+
args: string;
|
|
24
|
+
argDefault: string;
|
|
25
|
+
};
|
|
26
|
+
list: {
|
|
27
|
+
alias: string;
|
|
28
|
+
desc: string;
|
|
29
|
+
args: string;
|
|
30
|
+
argDefault: string;
|
|
31
|
+
};
|
|
32
|
+
full: {
|
|
33
|
+
type: string;
|
|
34
|
+
alias: string;
|
|
35
|
+
desc: string;
|
|
36
|
+
counting: number;
|
|
37
|
+
};
|
|
38
|
+
ns: {
|
|
39
|
+
alias: string;
|
|
40
|
+
desc: string;
|
|
41
|
+
args: string;
|
|
42
|
+
};
|
|
43
|
+
soe: {
|
|
44
|
+
alias: string;
|
|
45
|
+
desc: string;
|
|
46
|
+
args: string;
|
|
47
|
+
argDefault: string;
|
|
48
|
+
customTypes: {
|
|
49
|
+
enum: (v: any) => any;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
quiet: {
|
|
53
|
+
alias: string;
|
|
54
|
+
desc: string;
|
|
55
|
+
args: string;
|
|
56
|
+
argDefault: string;
|
|
57
|
+
};
|
|
58
|
+
serial: {
|
|
59
|
+
alias: string[];
|
|
60
|
+
desc: string;
|
|
61
|
+
args: string;
|
|
62
|
+
argDefault: string;
|
|
63
|
+
};
|
|
64
|
+
concurrent: {
|
|
65
|
+
alias: string[];
|
|
66
|
+
desc: string;
|
|
67
|
+
args: string;
|
|
68
|
+
argDefault: string;
|
|
69
|
+
};
|
|
70
|
+
env: {
|
|
71
|
+
desc: string;
|
|
72
|
+
args: string;
|
|
73
|
+
};
|
|
74
|
+
require: {
|
|
75
|
+
alias: string;
|
|
76
|
+
desc: string;
|
|
77
|
+
args: string;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
export default _default;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import chalk from "../lib/chalk.js";
|
|
3
|
+
import myPkg from "../lib/my-pkg.js";
|
|
4
|
+
import config from "./config.js";
|
|
5
|
+
export default {
|
|
6
|
+
cwd: {
|
|
7
|
+
alias: "w",
|
|
8
|
+
desc: `Set ${myPkg.name}'s ${chalk.magenta("CWD")}`,
|
|
9
|
+
args: "<path string>",
|
|
10
|
+
requireArg: true
|
|
11
|
+
},
|
|
12
|
+
dir: {
|
|
13
|
+
alias: "d",
|
|
14
|
+
desc: `Set dir to look for ${chalk.green(config.taskFile)} (default is ${chalk.magenta("CWD")})`,
|
|
15
|
+
args: "<path string>",
|
|
16
|
+
requireArg: true
|
|
17
|
+
},
|
|
18
|
+
npm: {
|
|
19
|
+
alias: "n",
|
|
20
|
+
desc: `load npm scripts into namespace ${chalk.magenta("npm")} (--no-npm to disable)`,
|
|
21
|
+
args: "[ boolean]",
|
|
22
|
+
argDefault: "true"
|
|
23
|
+
},
|
|
24
|
+
nmbin: {
|
|
25
|
+
alias: "b",
|
|
26
|
+
desc: `add ${chalk.magenta("CWD/node_modules/.bin")} to ${chalk.blue("PATH")}`,
|
|
27
|
+
args: "[ boolean]",
|
|
28
|
+
argDefault: "true"
|
|
29
|
+
},
|
|
30
|
+
list: {
|
|
31
|
+
alias: "l",
|
|
32
|
+
desc: "List tasks names from list of comma separated namespaces (default is all namespaces)",
|
|
33
|
+
args: "[namespaces string]",
|
|
34
|
+
argDefault: ""
|
|
35
|
+
},
|
|
36
|
+
full: {
|
|
37
|
+
type: "count",
|
|
38
|
+
alias: "f",
|
|
39
|
+
desc: "--list show tasks names with namespace",
|
|
40
|
+
counting: Infinity
|
|
41
|
+
},
|
|
42
|
+
ns: {
|
|
43
|
+
alias: "m",
|
|
44
|
+
desc: "List all namespaces",
|
|
45
|
+
args: "[ boolean]"
|
|
46
|
+
},
|
|
47
|
+
soe: {
|
|
48
|
+
alias: "e",
|
|
49
|
+
desc: `Stop on errors - one of: no, soft, full`,
|
|
50
|
+
args: "[mode enum]",
|
|
51
|
+
argDefault: "full",
|
|
52
|
+
customTypes: {
|
|
53
|
+
enum: v => {
|
|
54
|
+
/* istanbul ignore next */
|
|
55
|
+
if (v === undefined) {
|
|
56
|
+
return "full";
|
|
57
|
+
}
|
|
58
|
+
/* istanbul ignore next */
|
|
59
|
+
if (!v || v === "no") {
|
|
60
|
+
/* istanbul ignore next */
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
assert(v === "soft" || v === "full", `option soe value must be one of: no, soft, full`);
|
|
64
|
+
return v;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
quiet: {
|
|
69
|
+
alias: "q",
|
|
70
|
+
desc: "Do not output any logs",
|
|
71
|
+
args: "[ boolean]",
|
|
72
|
+
argDefault: "false"
|
|
73
|
+
},
|
|
74
|
+
serial: {
|
|
75
|
+
alias: ["s", "x", "ser"],
|
|
76
|
+
desc: "Execute tasks from command line serially",
|
|
77
|
+
args: "[ boolean]",
|
|
78
|
+
argDefault: "false"
|
|
79
|
+
},
|
|
80
|
+
concurrent: {
|
|
81
|
+
alias: ["c", "conc"],
|
|
82
|
+
desc: "Execute tasks from command line concurrently (default)",
|
|
83
|
+
args: "[ boolean]",
|
|
84
|
+
argDefault: "true"
|
|
85
|
+
},
|
|
86
|
+
env: {
|
|
87
|
+
desc: "Set env. ie: --env NODE_ENV=development FOO=BAR -.",
|
|
88
|
+
args: "[env string...]"
|
|
89
|
+
},
|
|
90
|
+
require: {
|
|
91
|
+
alias: "r",
|
|
92
|
+
desc: `require module for tasks instead of loading ${config.taskFile}. require from path is CWD`,
|
|
93
|
+
args: "[modules string...]"
|
|
94
|
+
}
|
|
95
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import myPkg from "../lib/my-pkg.js";
|
|
2
|
+
export default {
|
|
3
|
+
taskFile: "xrun-tasks.js",
|
|
4
|
+
taskFileExt: ["js", "cjs", "ts", "mts", "cts", "mjs"],
|
|
5
|
+
search: ["xrun-tasks", "xrun", "xclap.", "clapfile.", "clap.", "gulpfile."],
|
|
6
|
+
getPkgOpt: pkg => ["xclap", "xrun", myPkg.name].find(f => pkg.hasOwnProperty(f))
|
|
7
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare const lib: {
|
|
2
|
+
container: any;
|
|
3
|
+
xrunTaskFile: string;
|
|
4
|
+
xrunPackagePath: string;
|
|
5
|
+
xrunId: string;
|
|
6
|
+
forceColor: string;
|
|
7
|
+
xrunCwd: string;
|
|
8
|
+
xrunVersion: string;
|
|
9
|
+
xrunBinDir: string;
|
|
10
|
+
xrunNodeBin: string;
|
|
11
|
+
xrunQuiet: string;
|
|
12
|
+
get(key: any): any;
|
|
13
|
+
set(key: any, val: any): void;
|
|
14
|
+
has(key: any): any;
|
|
15
|
+
del(key: any): void;
|
|
16
|
+
};
|
|
17
|
+
export default lib;
|
package/dist/cli/env.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import WrapProcess from "./wrap-process.js";
|
|
3
|
+
const lib = {
|
|
4
|
+
container: WrapProcess.env,
|
|
5
|
+
xrunTaskFile: "XRUN_TASKFILE",
|
|
6
|
+
xrunPackagePath: "XRUN_PACKAGE_PATH",
|
|
7
|
+
xrunId: "XRUN_ID",
|
|
8
|
+
forceColor: "FORCE_COLOR",
|
|
9
|
+
xrunCwd: "XRUN_CWD",
|
|
10
|
+
xrunVersion: "XRUN_VERSION",
|
|
11
|
+
xrunBinDir: "XRUN_BIN_DIR",
|
|
12
|
+
xrunNodeBin: "XRUN_NODE_BIN",
|
|
13
|
+
xrunQuiet: "XRUN_QUIET",
|
|
14
|
+
get(key) {
|
|
15
|
+
assert(key, `env.get invalid key: ${key}`);
|
|
16
|
+
return lib.container[key];
|
|
17
|
+
},
|
|
18
|
+
set(key, val) {
|
|
19
|
+
assert(key, `env.set invalid key: ${key}`);
|
|
20
|
+
lib.container[key] = `${val}`;
|
|
21
|
+
},
|
|
22
|
+
has(key) {
|
|
23
|
+
assert(key, `env.has invalid key: ${key}`);
|
|
24
|
+
return lib.container.hasOwnProperty(key);
|
|
25
|
+
},
|
|
26
|
+
del(key) {
|
|
27
|
+
assert(key, `env.del invalid key: ${key}`);
|
|
28
|
+
delete lib.container[key];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export default lib;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import logger from "../lib/logger.js";
|
|
2
|
+
import chalk from "../lib/chalk.js";
|
|
3
|
+
import { readPackageUpSync } from "read-package-up";
|
|
4
|
+
import myPkg from "../lib/my-pkg.js";
|
|
5
|
+
import config from "./config.js";
|
|
6
|
+
import env from "./env.js";
|
|
7
|
+
export default (xrun, options) => {
|
|
8
|
+
const readPkg = readPackageUpSync();
|
|
9
|
+
if (!readPkg) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const Pkg = readPkg.packageJson;
|
|
13
|
+
const pkgName = chalk.magenta(readPkg.path.replace(process.cwd(), "."));
|
|
14
|
+
if (Pkg.scripts && options.npm !== false) {
|
|
15
|
+
const scripts = {};
|
|
16
|
+
for (const k in Pkg.scripts) {
|
|
17
|
+
if (!k.startsWith("pre") && !k.startsWith("post")) {
|
|
18
|
+
const pre = `pre${k}`;
|
|
19
|
+
const post = `post${k}`;
|
|
20
|
+
scripts[k] = xrun.serial(Pkg.scripts.hasOwnProperty(pre) && pre, xrun.exec(Pkg.scripts[k], "npm"), Pkg.scripts.hasOwnProperty(post) && post);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
scripts[k] = xrun.exec(Pkg.scripts[k], "npm");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
xrun.load("npm", scripts);
|
|
27
|
+
/* istanbul ignore else */
|
|
28
|
+
if (env.get(env.xrunPackagePath) !== readPkg.path) {
|
|
29
|
+
logger.log(`Loaded npm scripts from ${pkgName} into namespace ${chalk.magenta("npm")}`);
|
|
30
|
+
}
|
|
31
|
+
env.set(env.xrunPackagePath, readPkg.path);
|
|
32
|
+
}
|
|
33
|
+
const pkgOptField = config.getPkgOpt(Pkg);
|
|
34
|
+
const pkgConfig = pkgOptField && Pkg[pkgOptField];
|
|
35
|
+
if (pkgConfig) {
|
|
36
|
+
const tasks = Object.assign({}, pkgConfig.tasks);
|
|
37
|
+
/* istanbul ignore else */
|
|
38
|
+
if (Object.keys(tasks).length > 0) {
|
|
39
|
+
xrun.load("pkg", tasks);
|
|
40
|
+
logger.log(`Loaded ${myPkg.name} tasks from ${pkgName} into namespace ${chalk.magenta("pkg")}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} SearchResult
|
|
3
|
+
* @property {boolean} found - Whether a task file was found
|
|
4
|
+
* @property {boolean} foundPkg - Whether a package.json was found
|
|
5
|
+
* @property {string} [xrunFile] - Path to the found task file
|
|
6
|
+
* @property {string} dir - Directory where the file was found
|
|
7
|
+
*/
|
|
8
|
+
import { updateCwd, searchTaskFile, loadTaskFile, processTasks } from "./task-file.js";
|
|
9
|
+
/**
|
|
10
|
+
* Parse command line arguments
|
|
11
|
+
* @param {string[]} argv - Command line arguments
|
|
12
|
+
* @param {number} start - Index to start parsing from
|
|
13
|
+
* @returns {ParseResult} Parsed arguments and tasks
|
|
14
|
+
*/
|
|
15
|
+
declare function parseArgs(argv: any, start: any): Promise<{
|
|
16
|
+
opts: Record<string, import("@fynjs/cli-args").OptionValue>;
|
|
17
|
+
tasks: any[];
|
|
18
|
+
parsed: any;
|
|
19
|
+
searchResult: any;
|
|
20
|
+
} | {
|
|
21
|
+
opts: Record<string, import("@fynjs/cli-args").OptionValue>;
|
|
22
|
+
tasks: string[];
|
|
23
|
+
parsed: import("@fynjs/cli-args").ParseResult;
|
|
24
|
+
searchResult: any;
|
|
25
|
+
}>;
|
|
26
|
+
export { parseArgs, updateCwd, searchTaskFile, loadTaskFile, processTasks };
|
|
27
|
+
declare const _default: {
|
|
28
|
+
parseArgs: typeof parseArgs;
|
|
29
|
+
updateCwd: typeof updateCwd;
|
|
30
|
+
searchTaskFile: typeof searchTaskFile;
|
|
31
|
+
loadTaskFile: typeof loadTaskFile;
|
|
32
|
+
processTasks: typeof processTasks;
|
|
33
|
+
};
|
|
34
|
+
export default _default;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} SearchResult
|
|
3
|
+
* @property {boolean} found - Whether a task file was found
|
|
4
|
+
* @property {boolean} foundPkg - Whether a package.json was found
|
|
5
|
+
* @property {string} [xrunFile] - Path to the found task file
|
|
6
|
+
* @property {string} dir - Directory where the file was found
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} ParseOptions
|
|
10
|
+
* @property {string} [cwd] - Current working directory
|
|
11
|
+
* @property {string} [dir] - Directory to search in
|
|
12
|
+
* @property {string[]} [require] - Modules to require
|
|
13
|
+
* @property {boolean} [npm] - Whether to load npm tasks
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {Object} ParseResult
|
|
17
|
+
* @property {string[]} tasks - List of tasks to run
|
|
18
|
+
* @property {Object} parsed - Parsed command line arguments
|
|
19
|
+
* @property {SearchResult} searchResult - Result of searching for task files
|
|
20
|
+
*/
|
|
21
|
+
import Path from "path";
|
|
22
|
+
import cliOptions from "./cli-options.js";
|
|
23
|
+
import { NixClap } from "@fynjs/cli-args";
|
|
24
|
+
import xsh from "xsh";
|
|
25
|
+
import usage from "./usage.js";
|
|
26
|
+
import logger from "../lib/logger.js";
|
|
27
|
+
import myPkg from "../lib/my-pkg.js";
|
|
28
|
+
import ck from "./ck.js";
|
|
29
|
+
import config from "./config.js";
|
|
30
|
+
import env from "./env.js";
|
|
31
|
+
import fs from "fs";
|
|
32
|
+
import { updateCwd, searchTaskFile, loadTaskFile, processTasks, loadTasks } from "./task-file.js";
|
|
33
|
+
import { loadProviderPackages } from "./provider-packages.js";
|
|
34
|
+
import WrapProcess from "./wrap-process.js";
|
|
35
|
+
import ownInstance from "../lib/xrun-instance.js";
|
|
36
|
+
import { getTaskOptionSpec } from "../lib/util/task-options.js";
|
|
37
|
+
/**
|
|
38
|
+
* Read and parse package.json from a directory
|
|
39
|
+
* @param {string} dir - Directory containing package.json
|
|
40
|
+
* @returns {Object} Parsed package.json contents
|
|
41
|
+
*/
|
|
42
|
+
function readPackageJson(dir) {
|
|
43
|
+
try {
|
|
44
|
+
const pkgData = fs.readFileSync(Path.join(dir, "package.json"), "utf-8");
|
|
45
|
+
return JSON.parse(pkgData);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
/* istanbul ignore next */
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parse command line arguments
|
|
54
|
+
* @param {string[]} argv - Command line arguments
|
|
55
|
+
* @param {number} start - Index to start parsing from
|
|
56
|
+
* @returns {ParseResult} Parsed arguments and tasks
|
|
57
|
+
*/
|
|
58
|
+
async function parseArgs(argv, start) {
|
|
59
|
+
const nc = new NixClap({
|
|
60
|
+
allowUnknownCommand: true, // Allow task names as commands
|
|
61
|
+
allowUnknownOption: true, // Allow task-specific options
|
|
62
|
+
/* istanbul ignore next */
|
|
63
|
+
exit: code => {
|
|
64
|
+
WrapProcess.exit(code);
|
|
65
|
+
},
|
|
66
|
+
handlers: {
|
|
67
|
+
/* istanbul ignore next */
|
|
68
|
+
exit: code => {
|
|
69
|
+
WrapProcess.exit(code);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
.version(myPkg.version)
|
|
74
|
+
.usage(usage)
|
|
75
|
+
.init(cliOptions, {} // no xrun commands
|
|
76
|
+
);
|
|
77
|
+
// Parse all arguments at once - @fynjs/cli-args handles command/option separation
|
|
78
|
+
let parsed = nc.parse(argv, start);
|
|
79
|
+
let opts = parsed.command.opts;
|
|
80
|
+
const myDir = xsh.pathCwd.replace(Path.dirname(import.meta.dirname), ".");
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
if (env.get(env.xrunVersion) !== myPkg.version || env.get(env.xrunBinDir) !== myDir) {
|
|
83
|
+
logger.log(ck `<green>${myPkg.name}</> version ${myPkg.version} at <magenta>${myDir}</>`);
|
|
84
|
+
}
|
|
85
|
+
/* istanbul ignore next */
|
|
86
|
+
if (env.get(env.xrunNodeBin) !== process.execPath) {
|
|
87
|
+
logger.log(ck `<green>node.js</> version ${process.version} at <magenta>${process.execPath}</>`);
|
|
88
|
+
}
|
|
89
|
+
env.set(env.xrunVersion, myPkg.version);
|
|
90
|
+
env.set(env.xrunBinDir, myDir);
|
|
91
|
+
env.set(env.xrunNodeBin, process.execPath);
|
|
92
|
+
// don't search if user has explicitly set CWD
|
|
93
|
+
const search = !opts.cwd;
|
|
94
|
+
const saveCwd = env.get(env.xrunCwd);
|
|
95
|
+
opts.cwd = updateCwd(opts.cwd);
|
|
96
|
+
let searchResult = {};
|
|
97
|
+
/* istanbul ignore next */
|
|
98
|
+
if (!opts.require) {
|
|
99
|
+
searchResult = searchTaskFile(search, opts);
|
|
100
|
+
}
|
|
101
|
+
const Pkg = readPackageJson(opts.cwd);
|
|
102
|
+
const pkgOptField = config.getPkgOpt(Pkg);
|
|
103
|
+
let pkgConfig = {};
|
|
104
|
+
if (pkgOptField) {
|
|
105
|
+
pkgConfig = Object.assign(pkgConfig, Pkg[pkgOptField]);
|
|
106
|
+
delete pkgConfig.cwd; // not allow pkg config to override cwd
|
|
107
|
+
delete pkgConfig.tasks;
|
|
108
|
+
// nc.applyConfig(pkgConfig, parsed);
|
|
109
|
+
const pkgName = ck `<magenta>./package.json</>`;
|
|
110
|
+
logger.log(ck `Applied <green>${pkgOptField}</> options from ${pkgName}`);
|
|
111
|
+
}
|
|
112
|
+
const loaded = await loadTasks(opts, searchResult);
|
|
113
|
+
// user has no tasks or explicitly enable searching for provider modules
|
|
114
|
+
if (loaded === false || pkgConfig.loadProviderModules) {
|
|
115
|
+
loadProviderPackages(Pkg, saveCwd, opts);
|
|
116
|
+
}
|
|
117
|
+
// Register loaded tasks into subcommand definitions for proper argument & option parsing
|
|
118
|
+
const subCommands = {};
|
|
119
|
+
if (ownInstance.xrun?._tasks) {
|
|
120
|
+
const names = ownInstance.xrun._tasks.names();
|
|
121
|
+
const fullNames = ownInstance.xrun._tasks.fullNames();
|
|
122
|
+
const allTaskNames = new Set([...names, ...fullNames]);
|
|
123
|
+
for (const name of allTaskNames) {
|
|
124
|
+
try {
|
|
125
|
+
const lookupRes = ownInstance.xrun._tasks.lookup(name);
|
|
126
|
+
const taskSpec = getTaskOptionSpec(lookupRes?.item);
|
|
127
|
+
if (Object.keys(taskSpec.options).length > 0 ||
|
|
128
|
+
Object.keys(taskSpec.commands).length > 0) {
|
|
129
|
+
subCommands[name] = {
|
|
130
|
+
desc: lookupRes?.item?.desc || "",
|
|
131
|
+
options: taskSpec.options,
|
|
132
|
+
subCommands: taskSpec.commands,
|
|
133
|
+
commands: taskSpec.commands,
|
|
134
|
+
...(taskSpec.allowUnknownOption !== undefined
|
|
135
|
+
? { allowUnknownOption: taskSpec.allowUnknownOption }
|
|
136
|
+
: {})
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// ignore lookup errors for invalid task names
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (Object.keys(subCommands).length > 0) {
|
|
146
|
+
const nc2 = new NixClap({
|
|
147
|
+
allowUnknownCommand: true,
|
|
148
|
+
allowUnknownOption: true,
|
|
149
|
+
/* istanbul ignore next */
|
|
150
|
+
exit: code => {
|
|
151
|
+
WrapProcess.exit(code);
|
|
152
|
+
},
|
|
153
|
+
handlers: {
|
|
154
|
+
/* istanbul ignore next */
|
|
155
|
+
exit: code => {
|
|
156
|
+
WrapProcess.exit(code);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
.version(myPkg.version)
|
|
161
|
+
.usage(usage);
|
|
162
|
+
try {
|
|
163
|
+
nc2.init(cliOptions, subCommands);
|
|
164
|
+
parsed = nc2.parse(argv, start);
|
|
165
|
+
const savedCwd = opts.cwd;
|
|
166
|
+
opts = parsed.command.opts;
|
|
167
|
+
opts.cwd = savedCwd;
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
logger.log(ck `<red>ERROR</>: ${err.message}`);
|
|
171
|
+
WrapProcess.exit(1);
|
|
172
|
+
return {
|
|
173
|
+
opts,
|
|
174
|
+
tasks: [],
|
|
175
|
+
parsed: {
|
|
176
|
+
command: { opts, subCmdNodes: {} },
|
|
177
|
+
errorNodes: [{ errors: [err] }]
|
|
178
|
+
},
|
|
179
|
+
searchResult
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Extract tasks from commands
|
|
184
|
+
const tasks = Object.keys(parsed.command.subCmdNodes);
|
|
185
|
+
return {
|
|
186
|
+
opts,
|
|
187
|
+
tasks,
|
|
188
|
+
parsed,
|
|
189
|
+
searchResult
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
export { parseArgs, updateCwd, searchTaskFile, loadTaskFile, processTasks };
|
|
193
|
+
export default {
|
|
194
|
+
parseArgs,
|
|
195
|
+
updateCwd,
|
|
196
|
+
searchTaskFile,
|
|
197
|
+
loadTaskFile,
|
|
198
|
+
processTasks
|
|
199
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search for provider packages in the node_modules directory.
|
|
3
|
+
*
|
|
4
|
+
* When does xrun load provider packages?
|
|
5
|
+
*
|
|
6
|
+
* 1. When the user has no tasks loaded
|
|
7
|
+
* 2. When the user sets the `loadProviderModules` option in their package.json under the `xrun` key
|
|
8
|
+
*
|
|
9
|
+
* What's considered a provider package?
|
|
10
|
+
*
|
|
11
|
+
* 1. The package sets the `xrunProvider` config in its package.json
|
|
12
|
+
* 2. The package has @fynjs/run listed in its dependencies
|
|
13
|
+
*
|
|
14
|
+
* What module does xrun require from the provider package to look for tasks?
|
|
15
|
+
*
|
|
16
|
+
* 1. The provider package sets `module` prop under the `xrunProvider` config in its package.json
|
|
17
|
+
* 2. Otherwise, xrun will require the provider package using its name
|
|
18
|
+
*
|
|
19
|
+
* How does xrun load tasks from the provider package?
|
|
20
|
+
*
|
|
21
|
+
* 1. If the provider module has a `loadTasks` export function, it will be used to load tasks
|
|
22
|
+
*
|
|
23
|
+
* @param {*} userPkg - the user's package.json object, typically the one in CWD
|
|
24
|
+
* @param {*} saveCwd - the CWD when xrun started, before xrun updated it to user specified CWD
|
|
25
|
+
* @param {*} opts - the command line options
|
|
26
|
+
*/
|
|
27
|
+
declare function loadProviderPackages(userPkg: any, saveCwd: any, opts: any): void;
|
|
28
|
+
export { loadProviderPackages };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { makeOptionalRequire } from "optional-require";
|
|
2
|
+
import { processTasks } from "./task-file.js";
|
|
3
|
+
import WrapProcess from "./wrap-process.js";
|
|
4
|
+
import myPkg from "../lib/my-pkg.js";
|
|
5
|
+
import path from "path";
|
|
6
|
+
/**
|
|
7
|
+
* Search for provider packages in the node_modules directory.
|
|
8
|
+
*
|
|
9
|
+
* When does xrun load provider packages?
|
|
10
|
+
*
|
|
11
|
+
* 1. When the user has no tasks loaded
|
|
12
|
+
* 2. When the user sets the `loadProviderModules` option in their package.json under the `xrun` key
|
|
13
|
+
*
|
|
14
|
+
* What's considered a provider package?
|
|
15
|
+
*
|
|
16
|
+
* 1. The package sets the `xrunProvider` config in its package.json
|
|
17
|
+
* 2. The package has @fynjs/run listed in its dependencies
|
|
18
|
+
*
|
|
19
|
+
* What module does xrun require from the provider package to look for tasks?
|
|
20
|
+
*
|
|
21
|
+
* 1. The provider package sets `module` prop under the `xrunProvider` config in its package.json
|
|
22
|
+
* 2. Otherwise, xrun will require the provider package using its name
|
|
23
|
+
*
|
|
24
|
+
* How does xrun load tasks from the provider package?
|
|
25
|
+
*
|
|
26
|
+
* 1. If the provider module has a `loadTasks` export function, it will be used to load tasks
|
|
27
|
+
*
|
|
28
|
+
* @param {*} userPkg - the user's package.json object, typically the one in CWD
|
|
29
|
+
* @param {*} saveCwd - the CWD when xrun started, before xrun updated it to user specified CWD
|
|
30
|
+
* @param {*} opts - the command line options
|
|
31
|
+
*/
|
|
32
|
+
function loadProviderPackages(userPkg, saveCwd, opts) {
|
|
33
|
+
const optionalRequire = makeOptionalRequire(opts.cwd || WrapProcess.cwd());
|
|
34
|
+
const providers = Object.keys(Object.assign({}, userPkg.optionalDependencies, userPkg.devDependencies, userPkg.dependencies));
|
|
35
|
+
providers.forEach(pkgName => {
|
|
36
|
+
const providerPath = optionalRequire.resolvePath(pkgName);
|
|
37
|
+
const pkgJsonPath = providerPath &&
|
|
38
|
+
path.join(providerPath.substring(0, pkgName.length + providerPath.indexOf(pkgName)), `package.json`);
|
|
39
|
+
const providerPkg = pkgJsonPath && optionalRequire(pkgJsonPath);
|
|
40
|
+
if (!providerPkg) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const provider = providerPkg.xrunProvider;
|
|
44
|
+
// accept both the current name (@fynjs/run) and the legacy name (@xarc/run) so
|
|
45
|
+
// providers published before the rename are still detected
|
|
46
|
+
const XRUN_DEP_NAMES = [myPkg.name, "@xarc/run"];
|
|
47
|
+
if (!provider && !XRUN_DEP_NAMES.some(n => providerPkg.dependencies?.[n])) {
|
|
48
|
+
// module is not marked as a provider and doesn't have xrun as dep, assume not
|
|
49
|
+
// a provider
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// module looks like a provider and user does not have tasks loaded, continue
|
|
53
|
+
// to see if module exports `loadTasks`
|
|
54
|
+
const req = (provider?.module && `/${provider.module}`) || "";
|
|
55
|
+
const providerMod = optionalRequire(`${pkgName}${req}`);
|
|
56
|
+
if (providerMod?.loadTasks) {
|
|
57
|
+
const loadMsg = saveCwd !== opts.cwd ? `provider package ${pkgName}` : "";
|
|
58
|
+
processTasks(providerMod.loadTasks, loadMsg);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export { loadProviderPackages };
|