preview.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import chalk from 'chalk';
  2. import Koa from 'koa';
  3. // import inquirer from 'inquirer';
  4. import staticServer from 'koa-static';
  5. import portfinder from 'portfinder';
  6. import { resolve } from 'path';
  7. import viteConfig from '../../vite.config';
  8. import { getIPAddress } from '../utils';
  9. // import { runBuild } from './postBuild';
  10. // const BUILD = 1;
  11. // const NO_BUILD = 2;
  12. // start server
  13. const startApp = () => {
  14. const port = 9680;
  15. portfinder.basePort = port;
  16. const app = new Koa();
  17. app.use(staticServer(resolve(process.cwd(), viteConfig.outDir || 'dist')));
  18. portfinder.getPort(async (err, port) => {
  19. if (err) {
  20. throw err;
  21. } else {
  22. app.listen(port, function () {
  23. const empty = ' ';
  24. const common = `The preview program is already running:
  25. - LOCAL: http://localhost:${port}/
  26. - NETWORK: http://${getIPAddress()}:${port}/
  27. `;
  28. console.log(chalk.cyan('\n' + empty + common));
  29. });
  30. }
  31. });
  32. };
  33. // export const runPreview = async () => {
  34. // // const prompt = inquirer.prompt({
  35. // // type: 'list',
  36. // // message: 'Please select a preview method',
  37. // // name: 'type',
  38. // // choices: [
  39. // // {
  40. // // name: 'Preview after packaging',
  41. // // value: BUILD,
  42. // // },
  43. // // {
  44. // // name: `No packaging, preview directly (need to have dist file after packaging)`,
  45. // // value: NO_BUILD,
  46. // // },
  47. // // ],
  48. // // });
  49. // const { type } = await prompt;
  50. // if (type === BUILD) {
  51. // await runBuild(true);
  52. // }
  53. // };
  54. startApp();