UNPKG

quicksilver-cli

Version:
163 lines (162 loc) 8.45 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const command_1 = require("./command"); const field_1 = require("./field/field"); const GradleUtil = __importStar(require("../util/gradle")); const ProjectUtil = __importStar(require("../util/project")); const prompt_1 = require("../util/prompt"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); class CreateCommand extends command_1.Command { async init() { await super.init(); await ProjectUtil.validateGradleProject(this.workspace, this.cacheKeyWorkspace); } async afterParse() { await super.afterParse(); const args = this.args; if ("lib" in args) { this.args.lib = await ProjectUtil.promptSubProject("lib", args.lib); this.logger.info(`The name of lib-sub project is '${this.rootProjectName}-lib-${this.args.lib}'`); } if ("module" in args) { this.args.module = await ProjectUtil.promptSubProject("module", args.module); this.logger.info(`The name of module-sub project is '${this.rootProjectName}-lib-${this.args.module}'`); } if ("run" in args) { const moduleNames = ProjectUtil.getModuleNames(this.workspace, this.args.module); if (moduleNames.length == 0) { this.logger.warn("Create a module project firstly, and then create a run project for it."); this.args.module = await ProjectUtil.promptSubProject("module", this.args.run); if (this.args.run != "") { this.args.run = ""; } this.logger.info(`The name of module-sub project is '${this.rootProjectName}-lib-${this.args.module}'`); const runProjectName = `${this.rootProjectName}-run` + (this.args.run == "" ? "" : `-${this.args.run}`); this.logger.info(`The name of run-sub project is '${runProjectName}'`); } else { moduleNames.push(""); const runNames = ProjectUtil.getAvailableRunNames(this.workspace, this.args.module); if (runNames.length == 0) { this.logger.warn(`All module projects have own run project for running. the command to create run project will be ignored.`); delete this.args.run; } else { this.args.run = await prompt_1.prompt({ question: `Please select a name from [${runNames.join(",")}] for the run-sub project: `, value: args.run, validators: [ { message: "A invalid name '${value}' for run-project", predictor: value => moduleNames.includes(value), }, { message: "Exists project '${rootName}-run${value == '' ? '' : '-' + value}', please select other name.", predictor: value => runNames.includes(value), args: { "rootName": this.rootProjectName } } ] }); const runProjectName = `${this.rootProjectName}-run` + (this.args.run == "" ? "" : `-${this.args.run}`); this.logger.info(`The name of run-sub project is '${runProjectName}'`); } } } } async run() { const args = this.args; if ("lib" in args) { await this.createLibProject(args.lib); } await ProjectUtil.createBuildProjectIfNotExists(this.workspace); if ("module" in args) { await this.createModuleProject(args.module); } if ("run" in args) { await this.createRunProject(args.run); } await GradleUtil.projects(this.workspace); } async createLibProject(name) { const projectName = this.rootProjectName + "-lib-" + name; const projectPath = path.join(this.workspace, projectName); if (ProjectUtil.existsSubGradleProject(projectPath)) { this.logger.warn(`Exists lib-sub project ${projectName}, the cli to create lib-sub project will be ignored.`); return; } const gradlePath = path.join(projectPath, "build.gradle"); fs.mkdirSync(projectPath, { recursive: true }); fs.writeFileSync(gradlePath, GradleUtil.getLibProjectGradle()); GradleUtil.modifySettingsGradle(projectPath); this.logger.info(`Created lib-sub project '${projectName}'.`); } async createModuleProject(name) { const projectName = this.rootProjectName + "-module-" + name; const projectPath = path.join(this.workspace, projectName); if (ProjectUtil.existsSubGradleProject(projectPath)) { this.logger.warn(`Exists module-sub project ${projectName}, the cli to create module project will be ignored.`); return; } const gradle = GradleUtil.getModuleProjectGradle(this.rootProjectName, name); const gradlePath = path.join(projectPath, "build.gradle"); fs.mkdirSync(projectPath, { recursive: true }); fs.writeFileSync(gradlePath, gradle); GradleUtil.modifySettingsGradle(projectPath); this.logger.info(`Created module-sub project '${projectName}'.`); } async createRunProject(name) { let projectName = this.rootProjectName + "-run"; let moduleProjectName = this.rootProjectName + "-module-"; if (name == "") { const moduleNames = ProjectUtil.getModuleNames(this.workspace); const dependentName = moduleNames.length == 1 ? moduleNames[0] : await prompt_1.prompt({ question: `Please select a module name from [${moduleNames.join(",")}] that depended by the run-sub project: `, validators: [{ message: 'A invalid module name', predictor: value => moduleNames.includes(value) }] }); moduleProjectName += dependentName; } else { projectName += "-" + name; moduleProjectName += name; } const projectPath = path.join(this.workspace, projectName); if (ProjectUtil.existsSubGradleProject(projectPath)) { this.logger.warn(`Exists run-sub project ${projectName}, the cli to create run project will be ignored.`); return; } const moduleProjectPath = path.join(this.workspace, moduleProjectName); if (!ProjectUtil.existsSubGradleProject(moduleProjectPath)) { return Promise.reject(`Not exists module-sub project '${moduleProjectName}', create run-sub project '${projectName}' error.`); } fs.mkdirSync(projectPath, { recursive: true }); const gradle = GradleUtil.getRunProjectGradle(moduleProjectName); const gradlePath = path.join(projectPath, "build.gradle"); fs.writeFileSync(gradlePath, gradle); GradleUtil.modifySettingsGradle(projectName); this.logger.info(`Created run-sub project '${projectName}'.`); } async validate() { await super.validate(); } } CreateCommand.NAME = "create"; CreateCommand.ALIAS = ["c"]; CreateCommand.FIELDS = [ { name: "lib", description: "Create lib-sub project.", aliases: ["l", "lib"], type: field_1.FieldType.String, example: "base" }, { name: "module", description: "Create module-sub project.", aliases: ["m", "module"], type: field_1.FieldType.String, example: "main" }, { name: "run", description: "Create run-sub project.", aliases: ["r", "run"], type: field_1.FieldType.String, example: "" } ]; CreateCommand.DESCRIPTION = "Create sub projects."; exports.CreateCommand = CreateCommand;