UNPKG

@ywfe/cli

Version:

遥望前端开发命令行工具

112 lines (111 loc) 5.73 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.serve = void 0; const chalk_1 = __importDefault(require("chalk")); const messages_1 = require("../util/messages"); const fs_extra_1 = require("fs-extra"); const vite_1 = require("../util/vite"); const vite_2 = require("vite"); const path_1 = __importDefault(require("path")); const serve = (inputFileName, options) => __awaiter(void 0, void 0, void 0, function* () { const fileName = inputFileName || 'build.json'; yield (0, vite_1.checkViteBuildConfig)(fileName); const filePath = path_1.default.join(process.cwd(), `./${fileName}`); const partPath = `../../config/temp/`; try { yield (0, fs_extra_1.copy)(filePath, path_1.default.join(__dirname, partPath, fileName)); yield (0, fs_extra_1.copy)(path_1.default.join(process.cwd(), './package.json'), path_1.default.join(__dirname, partPath, 'package.json')); if ((0, fs_extra_1.existsSync)(path_1.default.join(process.cwd(), './package-lock.json'))) { yield (0, fs_extra_1.copy)(path_1.default.join(process.cwd(), './package-lock.json'), path_1.default.join(__dirname, partPath, 'package-lock.json')); } if ((0, fs_extra_1.existsSync)(path_1.default.join(process.cwd(), './yarn.lock'))) { yield (0, fs_extra_1.copy)(path_1.default.join(process.cwd(), './yarn.lock'), path_1.default.join(__dirname, partPath, 'yarn.lock')); } console.log(chalk_1.default.green(messages_1.MESSAGES.ADD_CONFIG_TIPS)); } catch (err) { throw err; } const targetPath = path_1.default.join(process.cwd(), './src/vite-serve/'); (0, fs_extra_1.mkdirsSync)(targetPath); const { isH5, isHtmlReplacement } = yield (0, vite_1.getViteBuildConfig)(fileName); const routerFile = path_1.default.join(process.cwd(), './src/router/index.tsx'); if ((0, fs_extra_1.existsSync)(routerFile)) { yield getStartPagesByRouteAndConfig(routerFile); } else { console.log(chalk_1.default.red(messages_1.MESSAGES.NOT_FOUND_ROUTER_FILE)); process.exit(1); } if (isHtmlReplacement) { const filePath = path_1.default.join(process.cwd(), './public/index.html'); if (!(0, fs_extra_1.existsSync)(filePath)) { console.log(chalk_1.default.red(messages_1.MESSAGES.PROJECT_WITHOUT_HTML)); process.exit(1); } else { yield (0, fs_extra_1.copy)(filePath, path_1.default.join(__dirname, '../.temp/index.html')); } } const res = yield createViteServe(isH5, isHtmlReplacement); return res; }); exports.serve = serve; const getStartPagesByRouteAndConfig = (routerFile) => __awaiter(void 0, void 0, void 0, function* () { const data = yield (0, fs_extra_1.readFile)(routerFile); const dataStr = data.toString('utf-8'); const newDataArr = dataStr.split('\n').filter((item) => item.length > 0); const length = newDataArr.length; let contentStr = ''; const tempRouterFile = path_1.default.join(process.cwd(), './src/vite-serve/index.tsx'); yield (0, fs_extra_1.writeFile)(tempRouterFile, 'import { lazy } from "react";\n'); newDataArr.map((line, index) => { var _a, _b, _c; if (line.startsWith('import') && line.indexOf('pages/') > -1) { const newLine = line.replace(';', ''); const componentName = (_b = (_a = newLine .match(/(?<=import).*?(?=from)/)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.trim(); const index = newLine.indexOf('from'); const filePath = (_c = newLine.substring(index + 4)) === null || _c === void 0 ? void 0 : _c.trim(); const lazyStr = `const ${componentName} = lazy(() => import(${filePath}));\n`; (0, fs_extra_1.appendFileSync)(tempRouterFile, lazyStr); } else if (line.startsWith('import')) { (0, fs_extra_1.appendFileSync)(tempRouterFile, `${line}\n`); } else if (line.length > 0 && !line.includes('//') && index !== length - 1) { contentStr += `${line}\n`; } }); contentStr += `};\n`; yield (0, fs_extra_1.appendFileSync)(tempRouterFile, contentStr); }); const createViteServe = (isH5, isHtmlReplacement) => __awaiter(void 0, void 0, void 0, function* () { const server = yield (0, vite_2.createServer)({ configFile: path_1.default.join(__dirname, isH5 ? '../../config/vite.config.h5.js' : '../../config/vite.config.js'), root: isHtmlReplacement ? path_1.default.join(__dirname, '../.temp/') : path_1.default.join(__dirname, '../../config/vite/'), server: { host: 'localhost', port: 3001, }, }); yield server.listen(); server.printUrls(); return { success: true }; });