This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·191 lines (149 loc) · 4.6 KB
/
cli.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node --experimental-worker
Error.stackTraceLimit = Infinity;
const path = require('path');
const arg = require('arg');
const chalk = require('chalk');
const _debug = require('debug');
const fs = require('promise-fs');
const parseTagfile = require('tag-parser');
const {visualizeSyntaxError} = require('./lib/syntax-error');
const pattern = require('./lib/patterns');
// NOTE: don't import anything before this line that
// uses the `tag:*` namespaces.
_debug.names.push(/^tag$/, /^tag:echo$/);
const debug = _debug('tag');
let debugv = () => {};
const helpText = chalk`
{bold tag} - yet another task runner
{dim $} tag -v [{underline task} ...] [@{underline tag} ...] [{underline NAME}=[{underline value}] ...]
{dim $} tag --help
OPTIONS
--help shows this help message
--version, -V shows the version string
--verbose, -v verbose output
--tagfile, -F {underline filename} the filename to read as the Tagfile
(defaults to './Tagfile')
@{underline tag_name} enables a tag by name
(can be specified multiple times)
{underline NAME}=[{underline value}] sets a variable
(can be specified multiple times)
`;
const args = arg({
'--help': Boolean,
'--verbose': Boolean,
'-v': '--verbose',
'--version': Boolean,
'-V': '--version',
'--tagfile': String,
'-F': '--tagfile'
});
if (args['--help']) {
console.error(helpText);
process.exit(2);
}
if (args['--version']) {
console.error(require('./package.json').version);
process.exit(2);
}
if (args['--verbose']) {
_debug.names.push(/^tag:.+$/);
debugv = _debug('tag:cli');
}
args['--tagfile'] = args['--tagfile'] || './Tagfile';
async function main() {
debugv('arguments: %O', args);
const namespace = {
TAGPATH: {
type: 'variable',
value: [{literal: [
path.join(__dirname, 'lib/stdlib/%.tag.js'),
path.join(__dirname, 'lib/stdlib/%.tag'),
'./%.tag',
'./%.tag.js',
'./%',
'./node_modules/%/index.tag',
'./node_modules/%/index.tag.js',
'./node_modules/%.tag',
'./node_modules/%.tag.js',
'./node_modules/%'
].join(':')}]
}
};
const tasks = [];
for (const arg of args._) {
let variable;
if (arg[0] === '@') {
const name = arg.substring(1);
if (!pattern.tag.test(name)) {
throw new Error(`invalid tag format: ${name}`);
}
if (namespace[name] && namespace[name].type !== 'tag') {
throw new Error(`attempting to enable non-tag '${name}' which is of type '${namespace[name].type}'`);
}
namespace[name] = {
type: 'tag',
enabled: true
};
} else if ((variable = pattern.variableAssignment.exec(arg))) {
const name = variable[1];
if (namespace[name] && namespace[name].type !== 'variable') {
throw new Error(`attempting to enable non-variable '${name}' which is of type '${namespace[name].type}'`);
}
namespace[name] = {
type: 'variable',
value: [{literal: variable[2]}]
};
} else {
tasks.push(arg);
}
}
if (tasks.length === 0) {
tasks.push('all');
}
tasks.forEach(task => {
if (!pattern.task.test(task)) {
throw new Error(`task name is invalid: ${task}`);
}
});
debugv('tasks to run:', ...tasks);
await fs.access(args['--tagfile'], fs.constants.R_OK);
const resolvedPath = path.resolve(args['--tagfile']);
debugv('correct access to Tagfile:', resolvedPath);
const buf = await fs.readFile(resolvedPath);
debugv('read Tagfile completely: %d bytes', buf.length);
const contents = buf.toString('utf-8');
debugv('initial namespace (before plugin): %O', namespace);
try {
const {HostContext} = require('./lib/context');
const {parseStatement} = require('./lib/parse');
const tagfile = parseTagfile(contents);
debugv('tagfile: %O', tagfile);
const contexts = [];
const hostCtx = new HostContext({namespace});
contexts.push(hostCtx);
for (const statement of tagfile) {
// Linters are annoying sometimes.
// The await is REQUIRED. Do not remove it.
// eslint-disable-next-line no-await-in-loop
await Promise.all(contexts.map(ctx => parseStatement(ctx, statement)));
}
} catch (error) {
if (!error.filename || !error.source) {
error.filename = resolvedPath;
error.source = contents;
}
throw error;
}
}
main()
.catch(error => {
debug('fatal error: %s', args['--verbose'] ? error.stack : error.message.replace(/^[^ ]\s+/, ''));
visualizeSyntaxError(debug, error);
process.exit(1);
})
.catch(error => {
// Something went wrong internall.
// Fail-safe error report here.
console.error(error.stack);
process.exit(10);
});