mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-26 08:36:19 +08:00
33
build/script/preview.ts
Normal file
33
build/script/preview.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import chalk from 'chalk';
|
||||
import Koa from 'koa';
|
||||
// import inquirer from 'inquirer';
|
||||
import staticServer from 'koa-static';
|
||||
import portfinder from 'portfinder';
|
||||
import { resolve } from 'path';
|
||||
import { getIPAddress } from '../utils';
|
||||
|
||||
// start server
|
||||
const startApp = () => {
|
||||
const port = 9680;
|
||||
portfinder.basePort = port;
|
||||
const app = new Koa();
|
||||
|
||||
app.use(staticServer(resolve(process.cwd(), '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));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
startApp();
|
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { networkInterfaces } from 'os';
|
||||
import dotenv from 'dotenv';
|
||||
import chalk from 'chalk';
|
||||
// import execa from 'execa';
|
||||
@@ -10,6 +11,25 @@ export const isFunction = (arg: unknown): arg is (...args: any[]) => any =>
|
||||
export const isRegExp = (arg: unknown): arg is RegExp =>
|
||||
Object.prototype.toString.call(arg) === '[object RegExp]';
|
||||
|
||||
/**
|
||||
* get client ip address
|
||||
*/
|
||||
export function getIPAddress() {
|
||||
let interfaces = networkInterfaces();
|
||||
for (let devName in interfaces) {
|
||||
let iFace = interfaces[devName];
|
||||
if (!iFace) return;
|
||||
for (let i = 0; i < iFace.length; i++) {
|
||||
let alias = iFace[i];
|
||||
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
|
||||
return alias.address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export function isDevFn(mode: string): boolean {
|
||||
return mode === 'development';
|
||||
}
|
||||
|
Reference in New Issue
Block a user