@lark-project/cli
Version:
飞书项目插件开发工具
91 lines (90 loc) • 4.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pluginInfoMiddleware = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const paths_1 = __importDefault(require("../config/paths"));
const Factory_1 = __importDefault(require("../model/config/Factory"));
const types_1 = require("../types");
const logger_1 = require("../../utils/logger");
const pluginInfoMiddleware = (devServer) => {
if (!(devServer === null || devServer === void 0 ? void 0 : devServer.app)) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', '*');
next();
});
devServer.app.get('/api/v1/plugin/js', function (req, res) {
const mfs = devServer.compiler.outputFileSystem;
const dirPath = path_1.default.join(paths_1.default.pluginBuild, 'js'); // 指定目录路径
const regex = /^(\w*\.)?index\.js$/; // 正则表达式
mfs.readdir(dirPath, async (err, files) => {
if (err) {
return res.status(500).send('Internal Server Error');
}
const filteredFiles = files === null || files === void 0 ? void 0 : files.filter(file => regex.test(file));
const statAsync = (0, util_1.promisify)(devServer.compiler.outputFileSystem.stat);
const latestFile = {
filePath: '',
mtimeMs: 0,
};
for (let file of filteredFiles) {
const filePath = path_1.default.join(dirPath, file);
try {
const modifyTime = (await statAsync(filePath)).mtimeMs; // 文件最近修改时间戳
if (modifyTime > latestFile.mtimeMs) {
latestFile.filePath = filePath;
latestFile.mtimeMs = modifyTime;
}
}
catch (e) {
logger_1.logger.error(e);
}
}
if (latestFile.filePath) {
mfs.readFile(latestFile.filePath, (err, data) => {
if (err) {
return res.status(500).send('Internal Server Error');
}
res.set('Content-Type', 'application/javascript; charset=utf-8');
res.send(data);
});
}
else {
return res.status(404).send('Not Found');
}
});
});
devServer.app.get('/api/v1/plugin/info', function (req, res) {
var _a, _b, _c;
try {
const config = Factory_1.default.getConfig(types_1.EConfigType.Plugin);
const pluginId = (_a = config.get('pluginID', false)) !== null && _a !== void 0 ? _a : '';
const version = (_b = config.get('version', false)) !== null && _b !== void 0 ? _b : '';
const pluginConfig = fs_extra_1.default.readJsonSync(paths_1.default.pluginConfigJson);
const { agent_info, runtime_version } = pluginConfig !== null && pluginConfig !== void 0 ? pluginConfig : {};
res.json({
data: {
version,
pluginId,
enable: true,
name: agent_info === null || agent_info === void 0 ? void 0 : agent_info.name,
integrate_points: (_c = agent_info === null || agent_info === void 0 ? void 0 : agent_info.integrate_points) !== null && _c !== void 0 ? _c : [],
runtime_version,
},
statusCode: 0,
});
}
catch (_d) {
res.json({ data: null, statusCode: 1001 });
}
});
};
exports.pluginInfoMiddleware = pluginInfoMiddleware;