flecks/packages/fleck/build/commands.js

107 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-01-16 00:28:20 -06:00
const {stat, unlink} = require('fs/promises');
const {join} = require('path');
2022-02-25 04:58:08 -06:00
2024-01-22 12:11:23 -06:00
// eslint-disable-next-line import/no-extraneous-dependencies
2024-01-22 09:16:07 -06:00
const {commands: coreCommands} = require('@flecks/build/build/commands');
2024-01-16 00:28:20 -06:00
const {D} = require('@flecks/core');
2024-01-22 09:16:07 -06:00
const {glob} = require('@flecks/core/server');
2024-01-16 00:28:20 -06:00
const chokidar = require('chokidar');
const clearModule = require('clear-module');
const Mocha = require('mocha');
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(),
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 = {
options: [
2024-01-24 00:26:52 -06:00
program.createOption('-d, --no-production', 'dev build'),
program.createOption('-w, --watch', 'watch for changes'),
program.createOption('-v, --verbose', 'verbose output'),
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'),
2022-02-25 04:58:08 -06:00
action: async (opts) => {
const {
watch,
} = opts;
2022-03-24 09:44:37 -05:00
const {build} = coreCommands(program, flecks);
2024-01-16 00:28:20 -06:00
const child = await build.action(undefined, opts);
2024-01-23 09:06:00 -06:00
const testPaths = await glob(join(FLECKS_CORE_ROOT, 'test/**/*.js'));
2022-02-25 04:58:08 -06:00
if (0 === testPaths.length) {
// eslint-disable-next-line no-console
console.log('No fleck tests found.');
2022-03-24 09:44:37 -05:00
return child;
2022-02-25 04:58:08 -06:00
}
2022-02-28 05:16:24 -06:00
const testLocation = join(FLECKS_CORE_ROOT, 'dist', 'test.js');
2022-02-25 04:58:08 -06:00
if (watch) {
await unlink(testLocation);
}
debug('Testing...', opts);
// eslint-disable-next-line no-constant-condition
while (true) {
try {
// eslint-disable-next-line no-await-in-loop
await stat(testLocation);
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
}
}
2022-03-22 01:02:02 -05:00
const runMocha = async () => {
const mocha = new Mocha();
mocha.ui('bdd');
clearModule(testLocation);
mocha.addFile(testLocation);
mocha.loadFiles();
return new Promise((r, e) => {
mocha.run((code) => {
if (!code) {
r();
return;
}
const error = new Error('Tests failed');
error.code = code;
e(error);
});
});
2022-02-25 04:58:08 -06:00
};
2024-01-16 00:28:20 -06:00
require('@flecks/core/build/stub')(flecks.stubs);
2022-02-25 04:58:08 -06:00
if (!watch) {
await new Promise((resolve, reject) => {
child.on('exit', (code) => {
if (code !== 0) {
reject(code);
return;
}
resolve();
});
child.on('error', reject);
});
2022-06-28 05:20:29 -05:00
await runMocha();
return 0;
2022-02-25 04:58:08 -06:00
}
2022-03-24 09:44:37 -05:00
chokidar.watch(testLocation)
.on('all', async () => {
2023-11-30 21:41:42 -06:00
await new Promise((resolve) => {
setTimeout(resolve, 50);
});
2022-03-24 09:44:37 -05:00
runMocha();
});
2022-02-25 04:58:08 -06:00
return new Promise(() => {});
},
};
return commands;
};