-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdefault.js
More file actions
173 lines (151 loc) · 5.91 KB
/
Copy pathdefault.js
File metadata and controls
173 lines (151 loc) · 5.91 KB
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
const {Command, flags} = require('@oclif/command');
const chalk = require('chalk');
const moduleFormats = require('../lib/module-format').formats;
const numericOption = require('../lib/numeric-option');
const shell = require('../lib/shell.js');
class LeiaCommand extends Command {
static id = 'leia';
static description = 'Cleverly converts markdown files into mocha cli tests';
static usage = `<files> <patterns> \
[--cleanup-header=<cleanup-headers>] \
[--debug] \
[--help] \
[--ignore=<patterns>] \
[--module-format=<auto|commonjs|esm>] \
[--retry=<count>] \
[--setup-header=<setup-headers>] \
[--test-header=<test-headers>] \
[--shell=<bash|cmd|powershell|pwsh|sh|zsh>] \
[--stdin] \
[--timeout=<seconds>] \
[--version]`;
static strict = false;
static examples = [
'leia README.md',
'leia README.md "examples/**/*.md" --retry 6 --test-header Tizzestin',
'leia "examples/*.md" --ignore BUTNOTYOU.md test --stdin --timeout 5',
'leia README.md --shell cmd',
'leia README.md --module-format esm',
];
static args = [{name: 'tests', description: 'files or patterns to scan for test'}];
static flags = {
// Plugin commands and placeholder --debug for use with @lando/argv
'debug': flags.boolean({description: 'shows debug output'}),
'help': flags.help({description: 'shows help'}),
'version': flags.version({description: 'shows version info', char: 'v'}),
// Setup header
'setup-header': flags.string({
char: 's',
description: 'considers these h2 sections as setup commands',
multiple: true,
default: ['Start', 'Setup', 'This is the dawning'],
}),
// Test header
'test-header': flags.string({
char: 't',
description: 'considers these h2 sections as tests',
multiple: true,
default: ['Test', 'Validat', 'Verif'],
}),
// Cleanup header
'cleanup-header': flags.string({
char: 'c',
description: 'considers these h2 sections as cleanup commands',
multiple: true,
default: ['Clean', 'Tear', 'Burn'],
}),
// Additional options
'ignore': flags.string({
char: 'i',
description: 'files or patterns to ignore',
multiple: true,
}),
'module-format': flags.string({
default: 'auto',
description: 'generates CommonJS or ESM harnesses, autodetected by default',
options: moduleFormats,
}),
'retry': flags.string({
char: 'r',
description: 'non-negative number of times to retry each test',
default: 1,
parse: numericOption.retry,
}),
'shell': flags.string({
default: shell().binary,
description: 'runs tests with given shell, autodetected by default',
options: ['bash', 'cmd', 'powershell', 'pwsh', 'sh', 'zsh'],
}),
'stdin': flags.boolean({
description: 'attachs stdin when the test is run',
}),
'timeout': flags.string({
default: 1800,
description: `non-negative whole seconds before tests time out (max ${numericOption.MAX_TIMEOUT_SECONDS})`,
parse: numericOption.timeout,
}),
// Legacy flags that still work if you pass them in but are no longer shown in help or documented
//
// @NOTE: --spawn is the default/only option now so the inclusion below is just so existing leia usage out in the
// wild doesnt start erroring on an upgrade
'spawn': flags.boolean({hidden: true}),
'split-file': flags.boolean({hidden: true}),
}
// Override warn for chalkability of string input
warn(input, options) {
if (typeof input === 'string') input = chalk.yellow(input);
super.warn(input, options);
}
// Override warn for chalkability of string input
error(input, options) {
if (typeof input === 'string') input = chalk.red(input);
super.error(input, options);
}
// The stuff that runs
async run() {
const invocationCwd = process.cwd();
// Get modules and stuff
// @NOTE: we do this here so we dont need to load big things like lodash just to show the CLI
const _ = require('lodash');
const debug = require('debug')('leia:cli');
debug('starting default command execution');
// Grab all teh things
const {args, argv, flags} = this.parse(LeiaCommand);
// Set args.files to argv
args.tests = argv;
// If source is nill then show help and throw error
// @NOTE: this doesnt feel like exactly right usage but it was better than the default
if (_.isEmpty(args.tests)) this._help();
// Combine our args and options into a parsed and camelCase-keyed object
const options = _(_.toPairs(_.merge({}, flags, args)))
.map((pair) => ([_.camelCase(pair[0]), pair[1]]))
.fromPairs()
.value();
// make sure we split any headers that need to be split
['setupHeader', 'testHeader', 'cleanupHeader'].forEach((header) => {
if (options[header].length === 1) options[header] = options[header][0].split(',');
});
// Summon leia to do the things
const Leia = require('./../lib/leia');
const leia = new Leia();
options.moduleFormat = leia.resolveModuleFormat(options.moduleFormat, invocationCwd);
// Some advanced kenny loggins
debug('leia parsed args and flags into options: %o', options);
// Combine all patterns and search for the things
const files = leia.find(options.tests, options.ignore);
debug('detected possible test source files: %o', files.join(', '));
// Combine our args and options
const sources = leia.parse(files, options);
debug('detected valid test sources %o', _.map(sources, 'file').join(', '));
// Generate test files from parsed data and return list of generated files
const tests = leia.generate(sources);
debug('generated leia tests to %o', tests.join(', '));
// Get the test runner and execute
const runner = await leia.runAsync(tests, options);
runner.run((failures) => {
debug('tests completed with %o failures', failures);
process.exitCode = failures ? 1 : 0;
});
}
}
module.exports = LeiaCommand;