UNPKG

1.64 kBJavaScriptView Raw
1const chalk = require('chalk');
2const { logInfo } = require('./log');
3const path = require('path');
4const { getPackageJson, getPackageJsonAnuxSettings, shell, resolveFile, waitForAnyKeyToEnd } = require('./utils');
5const fs = require('fs');
6const rootPath = process.cwd();
7
8async function removeDistFolder() {
9 await shell('rm -rf dist');
10}
11
12module.exports = async function anuxHarness() {
13 const { name, version } = getPackageJson({ throwErrorIfNotFound: true });
14 const { build: { clearBeforeCompile } } = getPackageJsonAnuxSettings({ build: { clearBeforeCompile: true } });
15
16 if (!fs.existsSync(path.resolve(rootPath, './harnesses.ts'))) throw new Error('There is no harnesses.ts file in the root of the project.');
17
18 logInfo(`Starting harness for ${name} v${version}...`);
19 logInfo(chalk`{black.bgYellowBright Press any key to end.}`);
20 if (clearBeforeCompile) {
21 logInfo('Clearing dist folder...');
22 await removeDistFolder();
23 logInfo('Cleared, continuing harness creation...');
24 }
25 const configFile = await resolveFile('configs/harness.webpack.config.js');
26 const buildCommand = `npx webpack-dev-server --config ${configFile} --mode development`;
27 const buildProcess = shell(buildCommand);
28 buildProcess.catch(({ exitCode, stdout, stderr }) => {
29 exitCode = exitCode == null ? 0 : exitCode;
30 if (exitCode !== 0) {
31 stderr = stderr === '' ? stdout : stderr;
32 // eslint-disable-next-line no-console
33 console.error(stderr);
34 }
35 });
36 await waitForAnyKeyToEnd(async () => {
37 logInfo('Shutting down harness...');
38 await buildProcess.kill();
39 });
40};