This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (95 loc) · 2.82 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
/* eslint-disable import/no-dynamic-require, global-require */
const path = require('path');
const fs = require('fs');
const minimist = require('minimist');
const findUp = require('find-up');
const chalk = require('chalk');
const showHelp = require('./lib/help');
// generic error handling, we don't need to handle them anywhere else
const handleError = require('./lib/handle-error');
process.on('unhandledRejection', handleError);
process.on('uncaughtException', handleError);
// process user input
const argv = minimist(process.argv.slice(2), {
boolean: ['version', 'help', 'quiet'],
string: ['oh'],
alias: { help: 'h', version: 'v', quiet: 'q' },
default: {
help: false,
quiet: false,
},
});
// find the oh.js
// eslint-disable-next-line no-nested-ternary
const ohFilePath = argv.oh
? fs.existsSync(argv.oh) ? argv.oh : null
: findUp.sync('oh.js');
// if it doesn't exist, error and stop
if (argv.oh && !ohFilePath) {
showHelp({
message: chalk.red(`${argv.oh} could not be found.`),
});
process.exit(1);
}
// if it doesn't exist, error and stop
if (!ohFilePath) {
const pkg = require('./package.json');
showHelp({
message: chalk.red(
`No task manifest could be found.
See ${chalk.green(pkg.homepage)} to find out more.`
),
});
process.exit(1);
}
// get the list of tasks defined in the manifest
const userTasks = require(ohFilePath);
// if we have the help flag or no tasks, show help
if (argv.help || !argv._.length) {
showHelp({ userTasks, ohFilePath });
process.exit(argv.help ? 0 : 1);
}
// if we have an unkown task/s, show help
const unknownTasks = argv._.filter(_ => !userTasks[_]);
if (unknownTasks.length) {
showHelp({
userTasks,
ohFilePath,
message: unknownTasks
.map(_ => `${chalk.red(_)} ${chalk.dim('is not a task.')}`)
.join('\n'),
});
process.exit(0);
}
// down to business...
// set cwd to wherever oh.js is
process.chdir(path.dirname(path.resolve(ohFilePath)));
// get the bits we needs from the args
const tasksToRun = argv._;
const userArgs = Object.keys(argv).reduce(
(result, arg) => {
if (
['_', 'h', 'help', 'v', 'version', 'q', 'quiet', 'oh'].some(
_ => _ === arg
)
) {
return result;
}
return Object.assign(result, { [arg]: argv[arg] });
},
{}
);
// turn the functions exported by oh.js into OhTasks
const runTask = require('./lib/runTask');
runTask.addTasks({
userTasks,
userArgs,
quiet: argv.quiet,
});
runTask('__before', { silent: true })
.then(() =>
tasksToRun.reduce(
(allTasks, taskToRun) => allTasks.then(() => runTask(taskToRun)),
Promise.resolve()
))
.then(() => runTask('__after', { silent: true }));