From 07187e6f0afa511c18097a5492fcfe5ef5cc1a9e Mon Sep 17 00:00:00 2001 From: alex-enchi Date: Thu, 18 Nov 2021 23:52:14 +0100 Subject: [PATCH] Add angular cli version support --- .../angular/generator-single-spa-angular.js | 28 +++++++++++++++---- .../src/angular/validate-cli-version.js | 11 ++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 packages/generator-single-spa/src/angular/validate-cli-version.js diff --git a/packages/generator-single-spa/src/angular/generator-single-spa-angular.js b/packages/generator-single-spa/src/angular/generator-single-spa-angular.js index 1510fcf4..8224ced7 100644 --- a/packages/generator-single-spa/src/angular/generator-single-spa-angular.js +++ b/packages/generator-single-spa/src/angular/generator-single-spa-angular.js @@ -6,17 +6,23 @@ const fs = require("fs"); const commandExists = util.promisify(require("command-exists")); const chalk = require("chalk"); const validate = require("../validate-naming"); +const validateCLIVersion = require("./validate-cli-version"); module.exports = class SingleSpaAngularGenerator extends Generator { + _globalInstallation = false; constructor(args, opts) { super(args, opts); this.option("projectName", { type: String, }); + this.option("angularCLIVersion", { + type: String, + }); } async getOptions() { - const answers = await this.prompt([ + this._globalInstallation = await commandExists("ng"); + const questions = [ { type: "input", name: "projectName", @@ -25,20 +31,30 @@ module.exports = class SingleSpaAngularGenerator extends Generator { when: !this.options.projectName, validate, }, - ]); + ]; + if (!this._globalInstallation) { + questions.push({ + type: "input", + name: "angularCLIVersion", + message: "Angular CLI version", + suffix: " (can use letters, numbers, dash or underscore)", + default: "latest", + when: !this.options.angularCLIVersion, + validateCLIVersion, + }); + } + const answers = await this.prompt(questions); Object.assign(this.options, answers, { framework: "angular" }); } async runAngularCli() { - const globalInstallation = await commandExists("ng"); - let command, args = []; - if (globalInstallation) { + if (this._globalInstallation) { command = "ng"; } else { command = "npx"; - args.push("@angular/cli"); + args.push(["@angular/cli", "@", this.options.angularCLIVersion].join("")); } if (process.platform === "win32") { diff --git a/packages/generator-single-spa/src/angular/validate-cli-version.js b/packages/generator-single-spa/src/angular/validate-cli-version.js new file mode 100644 index 00000000..fc333cc1 --- /dev/null +++ b/packages/generator-single-spa/src/angular/validate-cli-version.js @@ -0,0 +1,11 @@ +const ALLOWED_CHARACTERS = /^[a-zA-Z0-9\-]+$/; + +module.exports = function validateCLIVersion(input) { + if (!input) { + return `Cannot be empty!`; + } else if (!ALLOWED_CHARACTERS.test(input)) { + return `May only contain letters, numbers, dash or underscore`; + } else { + return true; + } +};