@lark-project/cli
Version:
飞书项目插件开发工具
82 lines (81 loc) • 3.56 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 chalk_1 = __importDefault(require("chalk"));
const path_2 = require("path");
const use_yarn_1 = require("../../../utils/use-yarn");
const utils_1 = require("../../utils");
const utils_2 = require("../../utils");
const constants_1 = require("../../config/constants");
const execa_1 = __importDefault(require("execa"));
class BaseTemplate {
constructor({ cwd, name, pluginId, pluginSecret }) {
if (!(0, utils_1.validatePluginId)(pluginId)) {
(0, utils_1.error)(`Plugin ID ${pluginId} is illegal.`);
}
this.name = name;
this.pluginId = pluginId;
this.pluginSecret = pluginSecret;
this.cwd = cwd || process.cwd();
this.targetDir = (0, path_2.join)(this.cwd, name);
this.pkgManager = (0, use_yarn_1.useYarn)() ? 'yarn' : 'npm';
}
async init(_) { }
async installDependency(targetDir) {
console.log('\nInstalling packages. This might take a couple of minutes.\n');
const payload = {
stdio: 'inherit',
cwd: targetDir,
shell: true,
};
await (0, execa_1.default)(this.pkgManager, ['install'], payload);
}
async success() {
console.log(`\nSuccess! Created ${this.name} at ${this.targetDir}`);
console.log('\nWe suggest that you begin by typing:\n');
console.log(`${chalk_1.default.cyan('cd')} ${this.name}\n`);
console.log(chalk_1.default.cyan(`${this.pkgManager} start\n`));
}
async initConfig() {
var _a;
try {
/** 替换模板文件内容 */
await Promise.all([
(0, utils_2.renderTemplate)({
folderPath: this.targetDir,
fileName: 'package.json.ejs',
data: {
plugin_name: this.name,
},
}),
(0, utils_2.renderTemplate)({
folderPath: path_1.default.join(this.targetDir, 'src', 'constants'),
fileName: 'index.ts.ejs',
data: {
plugin_id: this.pluginId,
},
}),
]);
const configPath = path_1.default.join(this.targetDir, constants_1.PATH.MANIFEST_FILE);
const isExist = await fs_extra_1.default.exists(configPath);
const preConfig = isExist ? (_a = (await fs_extra_1.default.readJson(configPath, { throws: false }))) !== null && _a !== void 0 ? _a : {} : {};
/** 写入配置文件 */
const config = Object.assign(Object.assign({}, preConfig), { domain: constants_1.DEFAULT_CONFIG.DOMAIN, pluginID: this.pluginId, version: '0.0.1', pluginSecret: (0, utils_1.encrypt)(this.pluginSecret) });
await fs_extra_1.default.writeJson(configPath, config, {
spaces: 2,
EOL: '\r\n',
});
await this.installDependency(this.targetDir);
this.success();
}
catch (err) {
await fs_extra_1.default.remove(this.targetDir);
(0, utils_1.error)(chalk_1.default.red('Create plugin project error: ', err.message));
}
}
}
exports.default = BaseTemplate;