build.ts 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* eslint-disable no-console */

import ora from 'ora';
import path from 'path';
import {spawn} from 'child_process';
import {writeFileSync} from 'fs';

const next = require.resolve('next/dist/bin/next');
export const projectRoot = path.dirname(require.resolve('@visualdl/core'));

P
Peter Pan 已提交
11
export default function (action: 'build' | 'export', ...args: string[]): Promise<number> {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    return new Promise((resolve, reject) => {
        const capitalizedAction = action.replace(/^./, w => w.toUpperCase());

        const spinner = ora(`${capitalizedAction} in process...`).start();

        const log = path.join(process.cwd(), `${action}.log`);
        writeFileSync(log, '', {flag: 'w'});

        const p = spawn(next, [action, ...args], {
            cwd: projectRoot,
            env: {
                ...process.env,
                NODE_ENV: 'production'
            }
        });

        p.stdout.on('data', data => writeFileSync(log, data, {flag: 'a'}));
        p.stderr.on('data', data => writeFileSync(log, data, {flag: 'a'}));

        p.on('close', code => {
            if (code) {
                spinner.fail(`${capitalizedAction} failed!`);
                console.error(`Please refer to ${log} for more detail.`);
                reject(code);
            } else {
                spinner.succeed(`${capitalizedAction} complete!`);
                resolve(code);
            }
        });
    });
}