Skip to content

Commit e6f5cc5

Browse files
authored
feat(cli): add --config and --[no-]write options (#58)
1 parent 33b4987 commit e6f5cc5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

expf.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"write": false
3+
}

packages/cli/bin/expf.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { argv } from 'node:process';
44
const { values, positionals } = parseArgs({
55
args: argv,
66
allowPositionals: true,
7+
allowNegative: true,
78
options: {
89
help: {
910
type: 'boolean'
@@ -37,6 +38,15 @@ const { values, positionals } = parseArgs({
3738
overrides: {
3839
type: 'string',
3940
short: 'o'
41+
},
42+
43+
config: {
44+
type: 'string',
45+
short: 'c'
46+
},
47+
48+
write: {
49+
type: 'boolean'
4050
}
4151
}
4252
});

packages/cli/load.mjs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function help (opts = {}) {
1616
--test=@expressjs/perf-load-example
1717
--node=lts_latest
1818
--overrides='{"express":"latest"}'
19+
--config=./expf.config.json
20+
--[no-]write
1921
`
2022
}
2123

@@ -25,13 +27,36 @@ export default function main (_opts = {}) {
2527
return;
2628
}
2729
return new Promise(async (resolve, reject) => {
30+
const cwd = normalize(join(import.meta.dirname, '..', '..'));
31+
32+
let conf = {};
33+
try {
34+
conf = (await import(join(cwd, _opts.config || 'expf.config.json'), {
35+
with: {
36+
type: 'json'
37+
}
38+
})).default;
39+
} catch (err) {
40+
// Only throw if config was explicitly passed, not if we failed to load the default file
41+
if (_opts.config) {
42+
throw new Error('Failed to load config file', {
43+
cause: err
44+
});
45+
}
46+
// Warn when a config file was found but was not loadable
47+
if (err.code !== 'ERR_MODULE_NOT_FOUND') {
48+
process.emitWarning(err);
49+
}
50+
}
51+
2852
const opts = {
29-
cwd: normalize(join(import.meta.dirname, '..', '..')),
53+
cwd,
3054
repo: 'https://github.com/expressjs/perf-wg.git',
3155
repoRef: 'master',
3256
runner: '@expressjs/perf-runner-docker',
3357
test: '@expressjs/perf-load-example',
3458
node: 'lts_latest',
59+
...conf,
3560
..._opts
3661
};
3762

@@ -82,10 +107,14 @@ export default function main (_opts = {}) {
82107
signal: ac.signal
83108
});
84109

85-
const outputFile = join(dirname(import.meta.resolve(opts.test).replace(/^file:/, '')), 'results', 'result-' + Date.now() + '.json');
86-
await mkdir(dirname(outputFile), { recursive: true });
87-
await writeFile(outputFile, JSON.stringify(results, null, 2));
88-
console.log(`written to: ${outputFile}`);
110+
if (opts.write !== false) {
111+
const outputFile = join(dirname(import.meta.resolve(opts.test).replace(/^file:/, '')), 'results', 'result-' + Date.now() + '.json');
112+
await mkdir(dirname(outputFile), { recursive: true });
113+
await writeFile(outputFile, JSON.stringify(results, null, 2));
114+
console.log(`written to: ${outputFile}`);
115+
} else {
116+
console.log(results);
117+
}
89118
} catch (e) {
90119
console.error(e);
91120
}

0 commit comments

Comments
 (0)