Blame view

build/script/preview.ts 1.53 KB
陈文彬 authored
1
2
import chalk from 'chalk';
import Koa from 'koa';
vben authored
3
// import inquirer from 'inquirer';
陈文彬 authored
4
5
6
7
import staticServer from 'koa-static';
import portfinder from 'portfinder';
import { resolve } from 'path';
import viteConfig from '../../vite.config';
8
import { getIPAddress } from '../utils';
vben authored
9
// import { runBuild } from './postBuild';
陈文彬 authored
10
vben authored
11
12
// const BUILD = 1;
// const NO_BUILD = 2;
陈文彬 authored
13
14
// start server
陈文彬 authored
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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));
      });
    }
  });
};
vben authored
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// export const runPreview = async () => {
//   // 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) {
//     await runBuild(true);
//   }
// };
startApp();