@lark-project/cli
Version:
飞书项目插件开发工具
52 lines (51 loc) • 2.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadCodeBase = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const promises_1 = require("stream/promises");
const logger_1 = require("../../../utils/logger");
const unzipper_1 = __importDefault(require("unzipper"));
const fs_extra_1 = require("fs-extra");
const GITHUB = {
DEFAULT_BRANCH: 'v2', // 默认主分支
ORG: 'larksuite', // 模板代码仓库 Org
REPOSITORY: 'lark-project-template', // 模板代码仓库
};
async function downloadCodeBase(targetDir, templateId) {
var _a;
const repoData = await fs_1.default.readFileSync(path_1.default.join(__dirname, '../../../template/v2.zip'));
const unzipped = await unzipper_1.default.Open.buffer(Buffer.from(repoData));
const entries = unzipped.files;
const rootDir = ((_a = entries[0]) === null || _a === void 0 ? void 0 : _a.type) === 'Directory' ? path_1.default.basename(entries[0].path) : '';
const projectDirFiles = [];
for (const entry of entries) {
// 获取project下的所有文件夹名称
const regex = /^.*\/project\/([^.\/]+)\/.*$/;
const matchRes = entry.path.match(regex);
if (matchRes && matchRes[1] && !projectDirFiles.includes(matchRes[1])) {
projectDirFiles.push(matchRes[1]);
}
}
if (!projectDirFiles.includes(templateId)) {
logger_1.logger.error(`The templateId "${templateId}" is not valid, please check and retry.`);
process.exit(1);
}
for (const entry of entries) {
const filePath = rootDir ? path_1.default.relative(rootDir, entry.path) : entry.path;
const absolutePath = path_1.default.join(targetDir, filePath);
const { type } = entry;
if (type === 'Directory') {
await (0, fs_extra_1.mkdir)(absolutePath, { recursive: true });
}
else if (type === 'File') {
await (0, fs_extra_1.mkdir)(path_1.default.dirname(absolutePath), { recursive: true });
const writeStream = (0, fs_extra_1.createWriteStream)(absolutePath);
await (0, promises_1.pipeline)(entry.stream(), writeStream);
}
}
}
exports.downloadCodeBase = downloadCodeBase;