@ywfe/cli
Version:
遥望前端开发命令行工具
117 lines (116 loc) • 5.45 kB
JavaScript
;
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.startViteBuild = exports.build = void 0;
const vite_1 = require("../util/vite");
const chalk_1 = __importDefault(require("chalk"));
const path_1 = __importDefault(require("path"));
const fs_extra_1 = require("fs-extra");
const messages_1 = require("../util/messages");
const thread_pool_runner_1 = __importDefault(require("../thread-pool-runner"));
const vite_2 = require("vite");
const build = (inputFileName, options) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const fileName = inputFileName || 'build.json';
const filePath = path_1.default.join(process.cwd(), `./${fileName}`);
const { changeId, dps, threadPool } = options;
try {
yield (0, vite_1.checkViteBuildConfig)(fileName);
yield (0, fs_extra_1.copy)(filePath, path_1.default.join(__dirname, '../../config/temp/', fileName));
console.log(chalk_1.default.green(messages_1.MESSAGES.ADD_CONFIG_TIPS));
let data = yield (0, vite_1.getBuildPageInfoWithChangeId)(fileName, {
dpsEnv: dps,
inputChangeId: changeId,
});
const { pages } = data;
const proCodePages = pages.filter((item) => (item === null || item === void 0 ? void 0 : item.develop_type) === 1);
if (proCodePages.length > 0) {
const buildInfo = yield (0, vite_1.getFilePathWithPath)(proCodePages);
const allPageName = buildInfo.map((i) => {
return i.file_path;
});
const newAllPageName = Array.from(new Set(allPageName));
yield (0, fs_extra_1.remove)(path_1.default.join(process.cwd(), './dist'));
const start = Date.now();
if (((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node) > '14.21.0' && threadPool !== '0') {
yield thread_pool_runner_1.default(newAllPageName);
}
else {
const buildFn = (newAllPageName) => __awaiter(void 0, void 0, void 0, function* () {
while (newAllPageName.length) {
const pageName = newAllPageName.pop();
yield (0, exports.startViteBuild)(pageName);
}
});
yield buildFn(newAllPageName);
}
const cost = Date.now() - start;
console.log(`编译耗时${cost / 1000}s`);
yield (0, fs_extra_1.remove)(path_1.default.join(__dirname, `../../.page`));
console.log(chalk_1.default.green(messages_1.MESSAGES.BUILD_SUCCESS_TIPS));
}
else {
console.log('该变更绑定的页面都是lowCode开发方式,无需构建');
}
}
catch (e) {
console.log(e);
process.exit(1);
}
});
exports.build = build;
const getViteConfigFilePath = (pageName) => {
const libName = (0, vite_1.generateLibName)(pageName);
return path_1.default.join(__dirname, `../../.page/${libName}.config.js`);
};
const getViteConfig = (partConfigPath) => __awaiter(void 0, void 0, void 0, function* () {
const configPath = path_1.default.join(__dirname, partConfigPath);
const dataStr = yield (0, fs_extra_1.readFile)(configPath);
return dataStr.toString('utf-8');
});
const createViteConfig = (pageName) => __awaiter(void 0, void 0, void 0, function* () {
(0, fs_extra_1.mkdirsSync)(path_1.default.join(__dirname, '../../.page/'));
const partConfigPath = pageName.indexOf('h5/') > -1 ? '../../config/vite.config.h5.js' : '../../config/vite.config.js';
const viteConfig = yield getViteConfig(partConfigPath);
const libName = (0, vite_1.generateLibName)(pageName);
const newViteConfig = viteConfig
.replace(/ENTRY_PAGE_PATH/g, `./src/pages/${pageName}/index.tsx`)
.replace(/LIB_PAGE_NAME/g, libName)
.replace(/FILE_PAGE_NAME/g, libName)
.replace(/ROOT_PAGE_PATH/g, process.cwd().replace(/\\/g, '\\\\'));
try {
const newPath = getViteConfigFilePath(pageName);
yield (0, fs_extra_1.outputFile)(newPath, newViteConfig);
return newPath;
}
catch (err) {
throw err;
}
});
const startViteBuild = (pageName) => __awaiter(void 0, void 0, void 0, function* () {
try {
const configFile = yield createViteConfig(pageName);
yield (0, vite_2.build)({
configFile,
});
return { success: true };
}
catch (error) {
return {
success: false,
message: error,
};
}
});
exports.startViteBuild = startViteBuild;