-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (128 loc) · 4.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import path from 'path';
import proc from 'process';
import { shell } from '@tunnckocore/execa';
/**
* Collecting tasks/scripts from various places such as `scripts.config.js`
* or even all the defined ones from `package.json`'s field `scripts`.
* You can also pass `scripts.extends` and pass local javascript file
* or some npm module which in turn can be either CJS or ESM written.
*
* @name scripts
* @param {Array} [argv] array of string, defaults to `process.argv.slice(2)`
* @param {object} [options] optioanl settings, like `cwd` and `manager`, also can pass `tasks` from here
* @returns {Promise} if object is resolved, then it's all the collected tasks/scripts from configs,
* if array, then it's array of [execa][] results.
* @public
*/
export default async function monora(cliArgv, options) {
const argv = Array.isArray(cliArgv) ? cliArgv : proc.argv.slice(2);
const { cwd, tasks, manager, nonStrictBehavior, env } = Object.assign(
{ cwd: proc.cwd() },
options,
);
const pkgPath = resolve(cwd, 'package.json');
const pkg = await import(pkgPath);
const config = await loadConfig(cwd, manager);
let scripts = Object.assign({}, pkg.scripts, pkg.monora, tasks, config);
const cfgPresets = scripts.extends || scripts.preset || scripts.presets;
if (cfgPresets) {
const presetPath = cfgPresets.startsWith('.')
? resolve(cwd, cfgPresets)
: cfgPresets;
scripts = Object.assign({}, scripts, await import(presetPath));
}
// List all available scripts/tasks
// if not one given, e.g. running `$ scripts`
if (argv.length === 0) {
return scripts;
}
const commands = normalizer(scripts, argv, nonStrictBehavior);
return shell(commands, { env, stdio: 'inherit' });
}
function resolve(...args) {
return path.resolve(path.join(...args));
}
async function loadConfig(cwd, mngr = 'yarn') {
const config =
(await tryCatch(() => import(resolve(cwd, 'scripts.config.js')))) ||
(await tryCatch(() => import(resolve(cwd, `${mngr}.scripts.js`)))) ||
(await tryCatch(() => import(resolve('..', '..', 'scripts.config.js')))) ||
(await tryCatch(() => import(resolve('..', '..', `${mngr}.scripts.js`))));
return config;
}
async function tryCatch(fn) {
let val = null;
try {
val = await fn();
} catch (err) {
return null;
}
return val;
}
function normalizer(scripts, argv, nonStrictBehavior) {
const name = argv.shift();
const pre = scripts[`pre${name}`];
const cmd = scripts[name];
const post = scripts[`post${name}`];
const stringify = (x) => [x].concat(argv).join(' ');
/**
* Pretty handy. The hooks works both for the package.json scripts
* and for installed bin executables.
* For example: you have installed eslint.
* Run `monora eslint` and it will run `preeslint` hook task,
* the eslint cli, and `posteslint` task hook (e.g. defined in npm.scripts.js)
*/
return (
[pre, cmd || stringify(name), post]
.filter(Boolean)
.reduce(function reducer(acc, script) {
if (typeof script === 'string') {
return acc.concat(script);
}
if (Array.isArray(script)) {
return script.reduce(reducer, acc);
}
if (typeof script === 'function') {
const result = script(scripts, argv);
if (typeof result === 'string') {
return acc.concat(result);
}
if (Array.isArray(result)) {
return result.reduce(reducer, acc);
}
}
return acc;
}, [])
.filter(Boolean)
// append argv/args & flags
.map((x, i, arr) => {
const shouldPrep = nonStrictBehavior === false && i === arr.length - 1;
if ((cmd && nonStrictBehavior) || shouldPrep) {
return stringify(x);
}
return x;
})
);
}
// A bit more complex and featureful
// .reduce(function reducer(promise, script) {
// return promise.then(async (acc) => {
// if (typeof script === 'string') {
// return shell(script, options);
// }
// if (Array.isArray(script)) {
// return script.reduce(
// (x, val) => reducer(x, val),
// Promise.resolve(acc),
// );
// }
// if (typeof script === 'function') {
// const result = await script(scripts, argv);
// if (typeof result === 'string') {
// return shell(result, options);
// }
// return reducer(Promise.resolve(acc), result);
// }
// return acc;
// });
// }, Promise.resolve([]));