UNPKG

2.22 kBJavaScriptView Raw
1const { logInfo } = require('./log');
2const { getPackageJson, getPackageJsonAnuxSettings, shell, getArg, resolveFile, waitForAnyKeyToEnd } = require('./utils');
3
4function getArgs(args) {
5 const isDev = getArg(args, 'dev', false);
6 const nonInteractive = getArg(args, 'non-interactive', false);
7 const useNodemon = getArg(args, 'use-nodemon', false);
8 args = args.filter(arg => !['dev', 'non-interactive', 'use-nodemon'].includes(arg.toLowerCase())); // remove all known args
9
10 return { isDev, nonInteractive, useNodemon };
11}
12
13async function removeDistFolder() {
14 await shell('rm -rf dist');
15}
16
17module.exports = function anuxBuild(args) {
18 const { isDev, nonInteractive, useNodemon } = getArgs(args);
19 let buildProcess = null;
20
21 const endBuild = async () => {
22 if (!nonInteractive) { logInfo('Shutting down build watcher...'); }
23 await buildProcess.kill();
24 };
25
26 const promise = (async () => {
27 const { name, version } = getPackageJson({ throwErrorIfNotFound: true });
28 const { build: { clearBeforeCompile } } = getPackageJsonAnuxSettings({ build: { clearBeforeCompile: true } });
29
30 logInfo(`Building the ${isDev ? 'development' : 'production'} version of ${name} v${version}...`);
31 if (clearBeforeCompile) {
32 logInfo('Clearing dist folder...');
33 await removeDistFolder();
34 logInfo('Cleared, continuing build process...');
35 }
36 const configFile = await resolveFile('configs/webpack.config.js');
37 const buildCommand = `npx webpack --config ${configFile} ${isDev ? '--mode development --watch' : '--mode production'} ${useNodemon ? '--use-nodemon' : ''}`;
38 buildProcess = shell(buildCommand);
39 buildProcess.catch(({ exitCode, stdout, stderr }) => {
40 exitCode = exitCode == null ? 0 : exitCode;
41 if (exitCode !== 0) {
42 stderr = stderr === '' ? stdout : stderr;
43 // eslint-disable-next-line no-console
44 console.error(stderr);
45 }
46 });
47 if (isDev) {
48 if (!nonInteractive) { await waitForAnyKeyToEnd(endBuild); }
49 } else {
50 await buildProcess;
51 logInfo('Building completed successfully.');
52 }
53 })();
54 promise.kill = endBuild;
55 return promise;
56};