UNPKG

cloudapp-cli

Version:

67 lines (55 loc) 1.8 kB
#!/usr/bin/env node const download = require("download-git-repo"); const fs = require("fs-extra"); const path = require("path"); const { program } = require("commander"); const { run } = require("../lib/verify"); const { getLocalDir } = require("../lib/pkg"); program.argument("<appName>", "项目名"); program.option("-t, --template <template>", "创建模板", "CT_001"); program.parse(process.argv); async function init() { const appName = program.args[0]; const template = program.opts().template; const name = `cloudapp-${appName.toLowerCase()}`; const dir = getLocalDir(name); const tempDir = getLocalDir(".cloudapp-temp"); const templatesDir = path.join(tempDir, "templates"); try { await new Promise((resolve, reject) => { download( `cloudapp-developer/cloudapp-templates#main`, tempDir, { shallow: true }, function (err) { if (err) { return reject(err); } resolve(); } ); }); } catch (error) { console.error(`\n❌ - 模板下载失败`); return; } const isDirExist = fs.existsSync(templatesDir); if (!isDirExist) { fs.removeSync(tempDir); return console.error("\n❌ - 模板目录不存在"); } const files = await fs.promises.readdir(templatesDir, { withFileTypes: true, }); const templates = files.filter((f) => f.isDirectory()).map((f) => f.name); const target = templates.find((t) => t.startsWith(template)); if (!target) { fs.removeSync(tempDir); return console.log("\n❌ - 未找到匹配模板"); } console.log(`\n✅ + 成功加载模板: ${target}`); fs.copySync(path.join(templatesDir, target), dir); fs.removeSync(tempDir); console.log(`\n✅ + 项目创建成功: ${dir}`); } run(init);