@lark-project/cli
Version:
飞书项目插件开发工具
166 lines (165 loc) • 6.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const webpack_1 = __importDefault(require("webpack"));
const fs_extra_1 = __importStar(require("fs-extra"));
const ora_1 = __importDefault(require("ora"));
const path_1 = __importDefault(require("path"));
const yaml_1 = __importDefault(require("yaml"));
const webpack_format_messages_1 = __importDefault(require("webpack-format-messages"));
const Compressor_1 = __importDefault(require("../../../utils/Compressor"));
const paths_1 = __importDefault(require("../../../config/paths"));
const webpack_config_1 = require("../../../config/webpack.config");
const utils_1 = require("../../../utils");
const logger_1 = require("../../../../utils/logger");
const types_1 = require("../../../types");
const Factory_1 = __importDefault(require("../../config/Factory"));
const statsStrOptions = {
errors: true,
warnings: true,
colors: true,
assets: true,
chunks: true,
modules: false,
};
class BuildRunner {
static async run(options) {
const { zip = false } = options || {};
const spinner = (0, ora_1.default)('Start compiling ...');
spinner.start();
try {
this.collectAndCheckInfo();
await fs_extra_1.default.emptyDir(paths_1.default.pluginBuild);
await this.compile();
// 生成 yaml 配置文件
await this.generateYaml();
spinner.succeed();
console.log('Compiled successfully.\n');
}
catch (err) {
spinner.fail('Compilation failed.');
(0, utils_1.error)(err.message);
}
if (!zip) {
return;
}
const zipSpinner = (0, ora_1.default)('Start compressing ...');
zipSpinner.start();
try {
const buffer = await Compressor_1.default.zipBuffer(paths_1.default.pluginBuild);
const zipPath = path_1.default.join(paths_1.default.pluginBuildZip, `${Date.now()}.zip`);
if (!(0, fs_extra_1.pathExistsSync)(paths_1.default.pluginBuildZip)) {
(0, fs_extra_1.mkdirSync)(paths_1.default.pluginBuildZip);
}
(0, fs_extra_1.writeFileSync)(zipPath, buffer);
zipSpinner.succeed();
console.log(`Compressed successfully: ${zipPath} \n`);
}
catch (e) {
zipSpinner.fail('Compressed failed.');
(0, utils_1.error)(e.message);
}
}
static handleError(err, stats) {
if (err) {
return {
errors: err.message ? [err.message] : [],
warnings: [],
};
}
return (0, webpack_format_messages_1.default)(stats);
}
}
_a = BuildRunner;
BuildRunner.compile = () => new Promise((resolve, reject) => {
const config = (0, webpack_config_1.getWebpackConfig)('production');
const compiler = (0, webpack_1.default)(config);
compiler.run((err, stats) => {
var _b;
const messages = _a.handleError(err, stats);
if ((_b = messages === null || messages === void 0 ? void 0 : messages.errors) === null || _b === void 0 ? void 0 : _b.length) {
logger_1.logger.error(messages.errors.join('\n\n'));
reject(Error('Compilation failed.'));
return;
}
logger_1.logger.success(stats === null || stats === void 0 ? void 0 : stats.toString(Object.assign(Object.assign({}, statsStrOptions), { stats })));
resolve(compiler);
});
});
BuildRunner.resetYamlConfig = () => {
_a.yamlConfig = {};
};
BuildRunner.collectAndCheckInfo = () => {
var _b, _c;
const pluginConfig = Factory_1.default.getConfig(types_1.EConfigType.Plugin);
const pluginId = (_b = pluginConfig.get('pluginID', false)) !== null && _b !== void 0 ? _b : '';
const version = (_c = pluginConfig.get('version', false)) !== null && _c !== void 0 ? _c : '';
if (!(0, utils_1.validatePluginId)(pluginId)) {
(0, utils_1.error)('\r\nPlugin ID is illegal.');
}
if (!(0, utils_1.validatePluginVersion)(version)) {
(0, utils_1.error)('\r\nVersion is illegal.');
}
_a.pluginId = pluginId;
_a.version = version;
};
BuildRunner.generateMappings = (fileName, folder) => {
if (!['js', 'css', 'assets'].includes(folder)) {
return;
}
const key = `${_a.pluginId}-${_a.version}-${fileName}`;
const value = `${folder}/${fileName}`;
if (!_a.yamlConfig.file) {
_a.yamlConfig.file = {};
}
_a.yamlConfig.file[key] = value;
};
BuildRunner.collectFileMappings = async (rootPath, folder) => {
// 读取传入的文件目录
const files = await fs_extra_1.default.readdir(rootPath);
for (let fileName of files) {
const filePath = path_1.default.join(rootPath, fileName);
const fileStat = await fs_extra_1.default.stat(filePath);
const name = path_1.default.basename(filePath);
if (fileStat.isFile()) {
_a.generateMappings(name, folder);
continue;
}
if (fileStat.isDirectory()) {
await _a.collectFileMappings(filePath, name);
}
}
};
BuildRunner.generateYaml = async () => {
_a.resetYamlConfig();
(0, utils_1.set)(_a.yamlConfig, 'basic.version', _a.version);
await _a.collectFileMappings(paths_1.default.pluginBuild);
await fs_extra_1.default.writeFile(paths_1.default.pluginBuildConfig, yaml_1.default.stringify(_a.yamlConfig));
};
exports.default = BuildRunner;