@5minds/processcube_docflow
Version:
Create webpage to view process documentation
77 lines (65 loc) • 2.28 kB
JavaScript
const { execSync } = require('child_process');
const path = require('path');
const version = require('./package.json').version;
const NEXT_PATH = path.join(__dirname, 'node_modules', '.bin', 'next');
const HTTP_SERVER_PATH = path.join(__dirname, 'node_modules', '.bin', 'http-server');
const RELATIVE_CWD = path.relative(__dirname, process.cwd());
const OUT_DIR = process.env.OUT_DIR ?? '.static';
const RELATIVE_OUT_DIR = path.join(RELATIVE_CWD, OUT_DIR);
const prefixedLog = (...args) => console.log('[@5minds/processcube_docflow]\t', ...args);
function build() {
prefixedLog(`Version:\t${version}`);
prefixedLog('Building DocFlow application...');
try {
execSync(`${NEXT_PATH} build`, {
cwd: __dirname,
env: {
...process.env,
CWD: RELATIVE_CWD,
OUT_DIR: RELATIVE_OUT_DIR,
},
});
} catch (error) {
console.error('[@5minds/processcube_docflow]\t', 'Error building DocFlow application:', error);
process.exit(1);
}
prefixedLog('DocFlow application built successfully.');
prefixedLog();
prefixedLog('You can start the app by running:');
if (OUT_DIR === '.static') {
prefixedLog('\tdocflow serve');
prefixedLog();
prefixedLog('or with custom options:');
}
prefixedLog(`\tdocflow serve \${PWD}/${OUT_DIR} -p 3000 -c-1`);
}
function serve() {
let args = process.argv.slice(3).join(' ');
if (args.length === 0) {
args = `${path.join(process.cwd(), OUT_DIR)} -p 3000 -c-1`;
}
const httpServerCommand = `${HTTP_SERVER_PATH} ${args}`;
prefixedLog(`Serving DocFlow application with command: ${httpServerCommand}\n`);
try {
execSync(httpServerCommand, {
cwd: __dirname,
stdio: 'inherit',
});
} catch (error) {
console.error('[@5minds/processcube_docflow]\t', 'Error serving DocFlow application:', error);
process.exit(1);
}
}
if (process.argv.includes('serve')) {
serve();
} else if (process.argv.includes('build')) {
build();
} else {
prefixedLog('Usage: docflow [build|serve]');
prefixedLog();
prefixedLog('Environment variables:');
prefixedLog('\tOUT_DIR: The output directory for the build. Default: .static');
prefixedLog('\tBASE_PATH: The base path for the app. Default: /');
process.exit(1);
}