import { watch } from 'fs' import { debounce } from 'lodash' import { exec } from 'child_process' import compiler from './utils/compiler' import print from './utils/print' import { writeFile } from './utils/fs' import { portIsOccupied, rimRaf } from './utils/common' import { ServerConfig } from './types/dev' import proxy from './proxy' const SERVER_PORT = 5003 const PROXY_PORT = 5033 const SERVER_ROOT_DIR = './' const SERVER_CONFIG_PATH = './server.json' const DEV_FILE = [/src\/.*/, /dev\.less/, /dev\.tsx/, /devSource\/.*/] const INPUT_FILE = './dev.tsx' const OUTPUT_FILE = './dev/dev.js' let ServerPort = SERVER_PORT let ProxyProt = PROXY_PORT const compile = debounce(async () => { let success = true print.tip(`Compiling...`) await compiler(INPUT_FILE, OUTPUT_FILE).catch(({ message }) => { success = false print.error(`Compile Exception: ${message}`, true) }) if (success) print.success(`Compile Successed: http://localhost:${ProxyProt}`, true) }, 500) async function writeServeConfig(serverPort: number): Promise { const config: ServerConfig = { open: false, port: serverPort, server: { baseDir: SERVER_ROOT_DIR, }, } await writeFile(SERVER_CONFIG_PATH, JSON.stringify(config), { flag: 'w' }) } ;(async (): Promise => { await rimRaf('./dev') ServerPort = await portIsOccupied(SERVER_PORT) ProxyProt = await portIsOccupied(PROXY_PORT) await writeServeConfig(ServerPort) await compile() exec(`lite-server -c ${SERVER_CONFIG_PATH}`, { maxBuffer: 99999999, }) proxy(ProxyProt, ServerPort) })() watch('./', { recursive: true }, (_, filename) => { if (!DEV_FILE.find(_ => _.test(filename))) return compile() })