vue-vben-admin/build/script/preview.ts

64 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import chalk from 'chalk';
import Koa from 'koa';
import inquirer from 'inquirer';
2020-10-14 22:57:52 +08:00
// import { sh } from 'tasksfile';
2020-09-28 20:19:10 +08:00
import staticServer from 'koa-static';
import portfinder from 'portfinder';
import { resolve } from 'path';
import viteConfig from '../../vite.config';
2020-10-14 22:57:52 +08:00
import { getIPAddress, run } from '../utils';
2020-09-28 20:19:10 +08:00
const BUILD = 1;
const NO_BUILD = 2;
// start server
2020-09-28 20:19:10 +08:00
const startApp = () => {
const port = 9680;
portfinder.basePort = port;
const app = new Koa();
app.use(staticServer(resolve(process.cwd(), viteConfig.outDir || 'dist')));
portfinder.getPort(async (err, port) => {
if (err) {
throw err;
} else {
app.listen(port, function () {
const empty = ' ';
const common = `The preview program is already running:
- LOCAL: http://localhost:${port}/
- NETWORK: http://${getIPAddress()}:${port}/
`;
console.log(chalk.cyan('\n' + empty + common));
});
}
});
};
export const runPreview = async () => {
2020-09-28 20:19:10 +08:00
const prompt = inquirer.prompt({
type: 'list',
message: 'Please select a preview method',
name: 'type',
choices: [
{
name: 'Preview after packaging',
value: BUILD,
},
{
name: `No packaging, preview directly (need to have dist file after packaging)`,
value: NO_BUILD,
},
],
});
const { type } = await prompt;
if (type === BUILD) {
2020-10-14 22:57:52 +08:00
await run('npm', ['run', 'build']);
// await sh('npm run build', {
// async: true,
// nopipe: true,
// });
2020-09-28 20:19:10 +08:00
}
startApp();
};