flecks/packages/fleck/build/commands.js

149 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-02-05 17:08:26 -06:00
const {access} = require('fs/promises');
2024-02-11 21:03:06 -06:00
const {join} = require('path');
2022-02-25 04:58:08 -06:00
const {hook: coreCommands} = require('@flecks/build/build/hooks/@flecks/build.commands');
2024-02-05 17:08:26 -06:00
const {rimraf} = require('@flecks/build/src/server');
const D = require('@flecks/core/build/debug');
2024-02-11 21:03:06 -06:00
const {glob, pipesink, processCode} = require('@flecks/core/src/server');
2024-01-16 00:28:20 -06:00
const Mocha = require('mocha');
2024-02-04 12:26:13 -06:00
const {watchParallelRun} = require('mocha/lib/cli/watch-run');
2022-02-25 04:58:08 -06:00
2024-01-22 09:16:07 -06:00
const debug = D('@flecks/build.commands');
2022-02-25 04:58:08 -06:00
const {
2022-02-28 05:16:24 -06:00
FLECKS_CORE_ROOT = process.cwd(),
2024-02-11 21:03:06 -06:00
TERM,
2022-02-25 04:58:08 -06:00
} = process.env;
2024-01-16 00:28:20 -06:00
module.exports = (program, flecks) => {
2022-02-25 04:58:08 -06:00
const commands = {};
commands.test = {
2024-02-06 08:14:12 -06:00
args: [
program.createArgument('[only]', 'only run a specific test'),
],
2022-02-25 04:58:08 -06:00
options: [
2024-01-24 00:26:52 -06:00
program.createOption('-d, --no-production', 'dev build'),
program.createOption('-p, --platform [platforms...]', 'platforms to test'),
2024-02-05 17:08:26 -06:00
program.createOption('-t, --timeout <ms>', 'timeout').default(2000),
2024-01-24 00:26:52 -06:00
program.createOption('-v, --verbose', 'verbose output'),
2024-02-10 00:03:39 -06:00
program.createOption('-w, --watch', 'watch for changes'),
2022-02-25 04:58:08 -06:00
],
2024-01-24 00:55:34 -06:00
description: [
'Run tests.',
'',
'The options are passed along to the `build` command.',
].join('\n'),
2024-02-06 08:14:12 -06:00
action: async (only, opts) => {
2022-02-25 04:58:08 -06:00
const {
2024-02-10 00:03:39 -06:00
platform: platforms,
2024-02-07 21:01:02 -06:00
production,
2024-02-05 17:08:26 -06:00
timeout,
2022-02-25 04:58:08 -06:00
watch,
} = opts;
2022-03-24 09:44:37 -05:00
const {build} = coreCommands(program, flecks);
2024-02-13 06:39:31 -06:00
// Check for work.
const [env, argv] = [{}, {mode: production ? 'production' : 'development'}];
if (platforms) {
process.env.FLECKS_CORE_TEST_PLATFORMS = JSON.stringify(platforms);
}
2024-02-13 06:39:31 -06:00
const filename = await flecks.resolveBuildConfig('test.webpack.config.js', '@flecks/build');
const config = {test: await require(filename)(env, argv, flecks)};
await flecks.configureBuilds(config, env, argv);
2024-02-14 02:48:11 -06:00
if (!config.test.entry) {
2024-02-13 06:39:31 -06:00
return undefined;
}
2024-02-11 21:03:06 -06:00
// Remove the previous test(s).
2024-02-05 17:08:26 -06:00
await rimraf(join(FLECKS_CORE_ROOT, 'dist', 'test'));
2024-02-04 12:26:13 -06:00
// Kick off building the test and wait for the file to exist.
2024-02-11 21:03:06 -06:00
const child = await build.action(
2024-02-10 00:03:39 -06:00
'test',
{
2024-02-11 21:03:06 -06:00
env: {
FLECKS_CORE_TEST_PLATFORMS: platforms && JSON.stringify(platforms),
2024-02-11 21:03:06 -06:00
FORCE_COLOR: 'dumb' !== TERM,
},
2024-02-10 00:03:39 -06:00
production,
2024-02-11 21:03:06 -06:00
stdio: watch ? 'inherit' : 'pipe',
2024-02-10 00:03:39 -06:00
watch,
},
);
2024-02-11 21:03:06 -06:00
if (!watch) {
const stdout = pipesink(child.stdout);
if (0 !== await processCode(child)) {
const buffer = await stdout;
if (!process.stdout.write(buffer)) {
await new Promise((resolve, reject) => {
process.stdout.on('error', reject);
process.stdout.on('drain', resolve);
});
}
program.error('\nbuilding tests failed!\n');
}
}
2022-02-25 04:58:08 -06:00
debug('Testing...', opts);
2024-02-11 21:03:06 -06:00
while (watch) {
2022-02-25 04:58:08 -06:00
try {
// eslint-disable-next-line no-await-in-loop
2024-02-05 17:08:26 -06:00
await access(join(FLECKS_CORE_ROOT, 'dist', 'test'));
2022-02-25 04:58:08 -06:00
break;
}
catch (error) {
// eslint-disable-next-line no-await-in-loop
2023-11-30 21:41:42 -06:00
await new Promise((resolve) => {
setTimeout(resolve, 50);
});
2022-02-25 04:58:08 -06:00
}
}
2024-02-11 21:03:06 -06:00
let files = await glob(join(FLECKS_CORE_ROOT, 'dist', 'test', '**', '*.js'));
if (0 === files.length) {
return undefined;
}
if (only) {
const index = files.indexOf(join(FLECKS_CORE_ROOT, 'dist', only));
if (-1 !== index) {
files = [files[index]];
}
else {
throw new Error(`Test '${only}' does not exist!`);
}
}
2024-02-04 12:26:13 -06:00
// Magic.
2024-01-30 12:55:17 -06:00
require('@flecks/core/build/resolve')(
2024-01-26 06:43:03 -06:00
{
alias: flecks.resolver.aliases,
fallback: flecks.resolver.fallbacks,
},
flecks.stubs,
);
2024-02-05 17:08:26 -06:00
const mocha = new Mocha({parallel: true, timeout});
2024-02-04 12:26:13 -06:00
mocha.ui('bdd');
if (watch) {
2024-02-05 17:08:26 -06:00
watchParallelRun(
mocha,
{
watchFiles: files,
},
{
file: files,
spec: [],
},
);
2024-02-04 12:26:13 -06:00
return new Promise(() => {});
2022-02-25 04:58:08 -06:00
}
2024-02-05 17:08:26 -06:00
mocha.files = files;
2024-02-04 12:26:13 -06:00
return new Promise((r, e) => {
mocha.run((code) => {
if (!code) {
r();
return;
}
const error = new Error('Tests failed');
error.code = code;
e(error);
2022-03-24 09:44:37 -05:00
});
2024-02-04 12:26:13 -06:00
});
2022-02-25 04:58:08 -06:00
},
};
return commands;
};