-
Notifications
You must be signed in to change notification settings - Fork 208
/
betools.js
executable file
·225 lines (214 loc) · 9.62 KB
/
betools.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! /usr/bin/env node
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
"use strict"
const yargs = require("yargs");
var path = require("path");
var child_process = require("child_process");
yargs.strict(true)
.wrap(Math.min(150, yargs.terminalWidth()))
.version("2.0.0")
.usage("Bentley Scripts Utility\n\n These scripts consist of several standard npm tasks an app may want to make use of.")
.command("test", "Run mocha tests on the current repository",
function (yargs) {
return yargs.options({
"packageRoot": {
describe: "The root of the package to locate the Mocha bin. Default is 'process.cwd()'."
},
"testDir": {
describe: "The location of the test directory containing .js files. Default is './lib/test'."
},
"mochaOpts": {
describe: "Adds the provided options file to mocha using --opts. See mocha docs for priority order, https://mochajs.org/api/module-lib_cli_options.html#.loadMochaOpts."
},
"timeout": {
describe: "Overrides the default timeout passed to Mocha. Default is 999999."
},
"grep": {
describe: "Add the grep pattern to Mocha."
},
"offline": {
describe: "If set to 'mock', "
},
"watch": {
describe: "Adds the --watch and --inline-diffs parameters to the Mocha command."
},
"debug": {
describe: "Adds the --inspect=9229 and --debug-brk parameters to the Mocha command."
},
"defineWindow": {
describe: "Adds the `--require jsdom-global/register` to the Mocha command. Use if a window is needed for compilation."
},
"invert": {
describe: "Adds the --invert option to Mocha, only if '--grep' is provided too."
}
})
},
(argv) => { testCommand(argv) })
.command("test-tsnode", "Run the ts-node version of the Mocha tests (NOTE: This may fail when running code coverage due to a behavior within Istanbul. If this occurs, you should run the script directly with Node)",
function (yargs) {
return yargs.options({
"packageRoot": {
describe: "The root of the package to locate the Mocha bin"
},
"testDir": {
describe: "The location of the test directory"
},
"watch": {
describe: "Adds the --watch and --inline-diffs parameters to the Mocha command"
},
"debug": {
describe: "Adds the --inspect=9229 and --debug-brk parameters to the Mocha command"
},
"tscPaths": {
describe: "Adds the --require tsconfig-paths/register arguments to the Mocha command"
}
})
},
(argv) => { testTsNodeCommand(argv) })
.command("docs", "Generate TypeDoc documentation by using the provided parameters to pass to TypeDoc. Supports generating html TypeScript documentation as well as a json representation of the documentation.",
function (yargs) {
return yargs.options({
"source": {
describe: "Specify the TypeScript source directory"
},
"out": {
describe: "Specify the directory of the html output"
},
"json": {
describe: "Specify the directory and filename of the json output"
},
"baseUrl": {
describe: "Specify a baseUrl to resolve modules"
},
"includes": {
describe: "Specify a baseUrl to resolve modules"
},
"excludes": {
describe: "Specify a directory, filename, or pattern to be excluded"
},
"excludeGlob": {
describe: "Specify a directory, filename, or pattern to be excluded"
},
"tsIndexFile": {
describe: "The barrel file containing the module documentation. This file is copied to the output folder for parsing."
},
"onlyJson": {
describe: "Specify a baseUrl to resolve modules"
}
})
},
(argv) => { docsCommand(argv) })
.command("extract", "Extract sample code from test files in a specific directory",
function (yargs) {
return yargs.options({
"extractFrom": {
describe: "The path at which the sample code files are located"
},
"out": {
describe: "The path at which to output the selected code"
},
"fileExt": {
describe: "The extension of the files to include"
},
"recursive": {
alias: "r",
describe: "Recursively search subdirectories from"
}
})
},
(argv) => { extractCommand(argv) })
.command("extract-api", "Extracts the API of the Typescript library starting from an entry file with a default presets. Powered by @microsoft/api-extractor (https://api-extractor.com)",
function (yargs) {
return yargs.options({
"entry": {
describe: "The main Typescript entry point for the library which is compiled to the 'main' field in the package.json"
},
"ignoreMissingTags": {
describe: "Turns off the 'ae-missing-release-tag' option which returns an error when a missing release tag is detected"
}
})
},
(argv) => { extractApiCommand(argv) })
.command("pseudolocalize", "Pseudo-localizes an english localization JSON file.",
function (yargs) {
return yargs.options({
"englishDir": {
describe: "The path to the English localization folder. Default is `./public/locales/en`"
},
"out": {
describe: "The output path to put the pseudo-localized files. Default is `./public/locales/en-pseudo`"
}
})
},
(argv) => { pseudolocalizeCommand(argv) })
.help()
.argv;
function testCommand(options) {
const rootOpt = options.packageRoot ? ["--packageRoot", options.packageRoot] : [];
const testDirOpt = options.testDir ? ["--testDir", options.testDir] : [];
// NOTE: We use `mochaOpts` as the cli arg instead of `opts` because of conflicts on the commander options object
const optionsOpt = options.mochaOpts ? ["--opts", options.mochaOpts] : [];
const timeoutOpt = options.timeout ? ["--timeout", options.timeout] : [];
const grepOpt = options.grep ? ["--grep", `\"${options.grep}\"`] : [];
const offlineOpt = options.offline ? ["--offline", options.offline] : [];
const watchOpt = options.watch ? ["--watch"] : [];
const debugOpt = options.debug ? ["--debug"] : [];
const windowOpt = options.defineWindow ? ["--defineWindow"] : [];
const invertOpt = options.invert ? ["--invert"] : [];
exec(["node", path.resolve(__dirname, "../scripts/test.js"),
...rootOpt, ...testDirOpt, ...optionsOpt, ...timeoutOpt, ...grepOpt,
...offlineOpt, ...watchOpt, ...debugOpt, ...windowOpt, ...invertOpt]);
}
function testTsNodeCommand(options) {
const rootOpt = options.packageRoot ? ["--packageRoot", options.packageRoot] : [];
const testDirOpt = options.testDir ? ["--testDir", options.testDir] : [];
const watchOpt = options.watch ? ["--watch"] : [];
const debugOpt = options.debug ? ["--debug"] : [];
const tscPathsOpt = options.tscPaths ? ["--tscPaths"] : [];
exec(["node", path.resolve(__dirname, "../scripts/test-tsnode.js"), ...rootOpt, ...testDirOpt, ...watchOpt, ...debugOpt, ...tscPathsOpt]);
}
function docsCommand(options) {
const sourceOpt = options.source ? ["--source", options.source] : [];
const outOpt = options.out ? ["--out", options.out] : [];
const jsonOpt = options.json ? ["--json", options.json] : [];
const baseUrlOpt = options.baseUrl ? ["--baseUrl", options.baseUrl] : [];
const includesOpt = options.includes ? ["--includes", options.includes] : [];
const excludesOpt = options.excludes ? ["--excludes", options.excludes] : [];
const excludesGlobOpt = options.excludes ? ["--excludeGlob", options.excludeGlob] : [];
const indexFileOpt = options.tsIndexFile ? ["--tsIndexFile", options.tsIndexFile] : [];
const onlyJsonOpt = options.onlyJson ? ["--onlyJson"] : [];
exec(["node", path.resolve(__dirname, "../scripts/docs.js"),
...sourceOpt, ...outOpt, ...jsonOpt, ...baseUrlOpt, ...includesOpt,
...excludesOpt, ...excludesGlobOpt, ...indexFileOpt, ...onlyJsonOpt]);
}
function extractCommand(options) {
const extractOpt = options.extractFrom ? ["--extractFrom", options.extractFrom] : [];
const outOpt = options.out ? ["--out", options.out] : [];
const fileExt = options.fileExt ? ["--fileExt", options.fileExt] : [];
const recursive = options.recursive ? ["--recursive"] : [];
exec(["node", path.resolve(__dirname, "../scripts/extract.js"), ...extractOpt, ...outOpt, ...fileExt, ...recursive]);
}
function extractApiCommand(options) {
const entryOpt = options.entry ? ["--entry", options.entry] : [];
const ignoreTagsOpt = options.ignoreMissingTags ? ["--ignoreMissingTags"] : [];
exec(["node", path.resolve(__dirname, "../scripts/extract-api.js"), ...entryOpt, ...ignoreTagsOpt]);
}
function pseudolocalizeCommand(options) {
const englishDir = options.englishDir ? ["--englishDir", options.englishDir] : [];
const outOpt = options.out ? ["--out", options.out] : [];
exec(["node", path.resolve(__dirname, "../scripts/pseudolocalize"), ...englishDir, ...outOpt]);
}
function exec(cmd) {
console.log("Running command:");
console.log(cmd.join(" "));
try {
return child_process.execSync(cmd.join(" "), { encoding: "utf8", stdio: 'inherit' });
} catch (error) {
if (error.status)
process.exit(error.status);
throw error;
}
}