flecks/packages/fleck/src/server/commands.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-02-25 04:58:08 -06:00
import {stat, unlink} from 'fs/promises';
import {join} from 'path';
import chokidar from 'chokidar';
import D from 'debug';
import glob from 'glob';
import {
commands as coreCommands,
spawnWith,
} from '@flecks/core/server';
const debug = D('@flecks/core/commands');
const {
2022-02-28 05:16:24 -06:00
FLECKS_CORE_ROOT = process.cwd(),
2022-02-25 04:58:08 -06:00
} = process.env;
export default (program, flecks) => {
const commands = {};
commands.test = {
options: [
['-d, --no-production', 'dev build'],
['-w, --watch', 'watch for changes'],
['-v, --verbose', 'verbose output'],
],
description: 'run tests',
action: async (opts) => {
const {
watch,
} = opts;
2022-02-28 05:16:24 -06:00
const testPaths = glob.sync(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.');
return 0;
}
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);
}
const {build} = coreCommands(program, flecks);
const child = build.action(undefined, opts);
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
await new Promise((resolve) => setTimeout(resolve, 50));
}
}
const spawnMocha = () => {
const localEnv = {};
const spawnArgs = [
'--colors',
'--reporter', 'min',
testLocation,
];
return spawnWith('mocha', localEnv, spawnArgs);
};
if (!watch) {
await new Promise((resolve, reject) => {
child.on('exit', (code) => {
if (code !== 0) {
reject(code);
return;
}
resolve();
});
child.on('error', reject);
});
return spawnMocha();
}
let tester;
chokidar.watch(testLocation)
.on('all', () => {
if (tester) {
tester.kill();
}
tester = spawnMocha();
});
return new Promise(() => {});
},
};
return commands;
};