@lark-project/cli
Version:
飞书项目插件开发工具
48 lines (47 loc) • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const axios_1 = __importDefault(require("axios"));
const __1 = require("..");
const constants_1 = require("../../config/constants");
const Compressor_1 = __importDefault(require("../Compressor"));
class Generator {
constructor({ options, targetDir }) {
this.options = options;
this.targetDir = targetDir;
}
async downloadAndUnzip() {
const { tag } = this.options;
const { data } = await (0, __1.wrapOraLoading)(({ owner, repo, ref }) => {
return axios_1.default.get(`https://api.github.com/repos/${owner}/${repo}/zipball/${ref}`, {
responseType: 'arraybuffer',
});
}, 'waiting download and unzip template', {
owner: constants_1.GITHUB.ORG,
repo: constants_1.GITHUB.REPOSITORY,
ref: tag || constants_1.GITHUB.DEFAULT_BRANCH,
});
const zipFilePath = path_1.default.join(process.cwd(), 'plugin_template.zip'); // 创建位置
await fs_extra_1.default.writeFile(zipFilePath, Buffer.from(data));
// 模板下载完成,zip 文件临时存储本地磁盘
try {
const targetDir = path_1.default.resolve(process.cwd(), this.targetDir);
await Compressor_1.default.unzip(zipFilePath, targetDir);
}
catch (err) {
(0, __1.error)(`Template unzip failed: ${err.message}`);
}
finally {
// 清理 zip 文件
await fs_extra_1.default.unlink(zipFilePath);
}
}
async initProject() {
await this.downloadAndUnzip();
}
}
exports.default = Generator;