From 4497a6724296a899d72d18c28279c5a5c27129ba Mon Sep 17 00:00:00 2001 From: Danny Eck Date: Fri, 11 Aug 2023 19:07:12 -0400 Subject: [PATCH 1/5] refactor: mv *Generator#install impls to super --- .../generator-single-spa/src/PnpmGenerator.js | 16 +++++++++++++--- .../src/react/generator-single-spa-react.js | 9 +-------- .../src/root-config/generator-root-config.js | 9 +-------- .../src/svelte/generator-single-spa-svelte.js | 9 +-------- .../generator-single-spa-util-module.js | 9 +-------- 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/packages/generator-single-spa/src/PnpmGenerator.js b/packages/generator-single-spa/src/PnpmGenerator.js index f4c40b6c..bbb0ebb4 100644 --- a/packages/generator-single-spa/src/PnpmGenerator.js +++ b/packages/generator-single-spa/src/PnpmGenerator.js @@ -9,7 +9,7 @@ module.exports = class PnpmGenerator extends Generator { } // This overrides https://github.com/yeoman/generator/blob/9cd93ee0fe8babd26cce4e22a6f1db7637573801/lib/actions/install.js#L119 // to include support for pnpm installations - installDependencies(options) { + _installDependencies(options) { options = options || {}; const msg = { commands: [], @@ -42,7 +42,7 @@ module.exports = class PnpmGenerator extends Generator { if (options.pnpm !== false) { msg.commands.push("pnpm install"); - this.pnpmInstall(null, getOptions(options.pnpm)); + this._pnpmInstall(null, getOptions(options.pnpm)); } assert( @@ -63,7 +63,17 @@ module.exports = class PnpmGenerator extends Generator { this.log(msg.template(tplValues)); } } - pnpmInstall(pkgs, options, spawnOptions) { + _pnpmInstall(pkgs, options, spawnOptions) { this.scheduleInstallTask("pnpm", pkgs, options, spawnOptions); } + install() { + if (!this.skipInstall) { + this._installDependencies({ + npm: this.options.packageManager === "npm", + yarn: this.options.packageManager === "yarn", + pnpm: this.options.packageManager === "pnpm", + bower: false, + }); + } + } }; diff --git a/packages/generator-single-spa/src/react/generator-single-spa-react.js b/packages/generator-single-spa/src/react/generator-single-spa-react.js index 372d4bc3..fe2a241d 100644 --- a/packages/generator-single-spa/src/react/generator-single-spa-react.js +++ b/packages/generator-single-spa/src/react/generator-single-spa-react.js @@ -211,14 +211,7 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { } } install() { - if (!this.skipInstall) { - this.installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); - } + super.install(); } finished() { this.on(`${this.options.packageManager}Install:end`, () => { diff --git a/packages/generator-single-spa/src/root-config/generator-root-config.js b/packages/generator-single-spa/src/root-config/generator-root-config.js index cf3b3ddd..f0233ede 100644 --- a/packages/generator-single-spa/src/root-config/generator-root-config.js +++ b/packages/generator-single-spa/src/root-config/generator-root-config.js @@ -205,14 +205,7 @@ module.exports = class SingleSpaRootConfigGenerator extends PnpmGenerator { } } install() { - if (!this.skipInstall) { - this.installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); - } + super.install(); } finished() { this.on(`${this.options.packageManager}Install:end`, () => { diff --git a/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js b/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js index 93d0f62a..4d75b5da 100644 --- a/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js +++ b/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js @@ -157,14 +157,7 @@ module.exports = class SingleSpaSvelteGenerator extends PnpmGenerator { } } install() { - if (!this.skipInstall) { - this.installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); - } + super.install(); } finished() { this.on(`${this.options.packageManager}Install:end`, () => { diff --git a/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js b/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js index 09a1a4fe..45d4e45f 100644 --- a/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js +++ b/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js @@ -172,14 +172,7 @@ module.exports = class SingleSpaUtilModuleGenerator extends PnpmGenerator { } } install() { - if (!this.skipInstall) { - this.installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); - } + super.install(); } finished() { this.on(`${this.options.packageManager}Install:end`, () => { From c5065fbed6aaf70547029002f8646c934cae44f1 Mon Sep 17 00:00:00 2001 From: Danny Eck Date: Fri, 11 Aug 2023 19:12:37 -0400 Subject: [PATCH 2/5] refactor: rm alwaystrue conditional Was this intentional? The condition is always true so `_installDependencies` is always called. Verified by running: `pnpx create-single-spa@4.1.4 --moduleType util-module --skipInstall` ``` ? Directory for new project __test ? Which framework do you want to use? none ? Which package manager do you want to use? pnpm ? Will this project use Typescript? No ? Organization name (can use letters, numbers, dash or underscore) org ? Project name (can use letters, numbers, dash or underscore) proj Initialized empty Git repository in /Users/deck/Workspaces/create-single-spa/__test/.git/ Initialized git repository create __test/package.json create __test/jest.config.js create __test/babel.config.json create __test/.eslintrc create __test/.prettierignore create __test/.gitignore create __test/.husky/pre-commit create __test/webpack.config.js create __test/src/org-proj.js No change to package.json was detected. No package manager install will be executed. Skipping install command: pnpm install I'm all done. Just run pnpm install to install the required dependencies. ``` --- packages/generator-single-spa/src/PnpmGenerator.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/generator-single-spa/src/PnpmGenerator.js b/packages/generator-single-spa/src/PnpmGenerator.js index bbb0ebb4..ebac0f55 100644 --- a/packages/generator-single-spa/src/PnpmGenerator.js +++ b/packages/generator-single-spa/src/PnpmGenerator.js @@ -67,13 +67,11 @@ module.exports = class PnpmGenerator extends Generator { this.scheduleInstallTask("pnpm", pkgs, options, spawnOptions); } install() { - if (!this.skipInstall) { - this._installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); - } + this._installDependencies({ + npm: this.options.packageManager === "npm", + yarn: this.options.packageManager === "yarn", + pnpm: this.options.packageManager === "pnpm", + bower: false, + }); } }; From c343e1e18c0a241ba84f91897f7748984d65e7f2 Mon Sep 17 00:00:00 2001 From: Danny Eck Date: Fri, 11 Aug 2023 19:32:30 -0400 Subject: [PATCH 3/5] feat: add the `skipGit` option Adds the `skipGit` option to the SingleSpaGenerator. Refactors (dedupes + hoists) the git initialization routine. Moves git init to the `install` priority queue. --- .../generator-single-spa/src/PnpmGenerator.js | 16 +++++++++ .../src/generator-single-spa.js | 4 +++ .../src/react/generator-single-spa-react.js | 9 ----- .../src/root-config/generator-root-config.js | 9 ----- .../src/svelte/generator-single-spa-svelte.js | 9 ----- .../generator-single-spa-util-module.js | 9 ----- .../generator-single-spa/test/git.test.js | 35 +++++++++++++++++++ 7 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 packages/generator-single-spa/test/git.test.js diff --git a/packages/generator-single-spa/src/PnpmGenerator.js b/packages/generator-single-spa/src/PnpmGenerator.js index ebac0f55..ed5e7fa1 100644 --- a/packages/generator-single-spa/src/PnpmGenerator.js +++ b/packages/generator-single-spa/src/PnpmGenerator.js @@ -73,5 +73,21 @@ module.exports = class PnpmGenerator extends Generator { pnpm: this.options.packageManager === "pnpm", bower: false, }); + this._gitInit(); + } + _gitInit() { + if (this.options.skipGit || this.options["skip-git"]) { + this.log("Skipping git initialization"); + return; + } else { + const childGitInitProcess = this.spawnCommandSync("git", ["init"]); + if (childGitInitProcess.error) { + this.log(chalk.red("************")); + this.log(chalk.red("Cannot initialize git repository")); + this.log(chalk.red("************")); + } else { + this.log(chalk.green("Initialized git repository")); + } + } } }; diff --git a/packages/generator-single-spa/src/generator-single-spa.js b/packages/generator-single-spa/src/generator-single-spa.js index 1b1f2bfa..f44c663e 100644 --- a/packages/generator-single-spa/src/generator-single-spa.js +++ b/packages/generator-single-spa/src/generator-single-spa.js @@ -37,6 +37,10 @@ module.exports = class SingleSpaGenerator extends Generator { type: Boolean, }); + this.option("skipGit", { + type: Boolean, + }); + if (args.length > 0 && !this.options.dir) { this.options.dir = args[0]; } diff --git a/packages/generator-single-spa/src/react/generator-single-spa-react.js b/packages/generator-single-spa/src/react/generator-single-spa-react.js index fe2a241d..d6d331a4 100644 --- a/packages/generator-single-spa/src/react/generator-single-spa-react.js +++ b/packages/generator-single-spa/src/react/generator-single-spa-react.js @@ -200,15 +200,6 @@ module.exports = class SingleSpaReactGenerator extends PnpmGenerator { } ); } - - const childGitInitProcess = this.spawnCommandSync("git", ["init"]); - if (childGitInitProcess.error) { - console.log(chalk.red("\n************")); - console.log(chalk.red("Cannot initialize git repository")); - console.log(chalk.red("************\n")); - } else { - console.log(chalk.green("\nInitialized git repository\n")); - } } install() { super.install(); diff --git a/packages/generator-single-spa/src/root-config/generator-root-config.js b/packages/generator-single-spa/src/root-config/generator-root-config.js index f0233ede..642fba66 100644 --- a/packages/generator-single-spa/src/root-config/generator-root-config.js +++ b/packages/generator-single-spa/src/root-config/generator-root-config.js @@ -194,15 +194,6 @@ module.exports = class SingleSpaRootConfigGenerator extends PnpmGenerator { ); this.fs.extendJSON(this.destinationPath("package.json"), layoutJson); } - - const childGitInitProcess = this.spawnCommandSync("git", ["init"]); - if (childGitInitProcess.error) { - console.log(chalk.red("\n************")); - console.log(chalk.red("Cannot initialize git repository")); - console.log(chalk.red("************\n")); - } else { - console.log(chalk.green("\nInitialized git repository\n")); - } } install() { super.install(); diff --git a/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js b/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js index 4d75b5da..a06f5491 100644 --- a/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js +++ b/packages/generator-single-spa/src/svelte/generator-single-spa-svelte.js @@ -146,15 +146,6 @@ module.exports = class SingleSpaSvelteGenerator extends PnpmGenerator { // this.options // ); // } - - const childGitInitProcess = this.spawnCommandSync("git", ["init"]); - if (childGitInitProcess.error) { - console.log(chalk.red("\n************")); - console.log(chalk.red("Cannot initialize git repository")); - console.log(chalk.red("************\n")); - } else { - console.log(chalk.green("\nInitialized git repository\n")); - } } install() { super.install(); diff --git a/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js b/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js index 45d4e45f..6a6435a3 100644 --- a/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js +++ b/packages/generator-single-spa/src/util-module/generator-single-spa-util-module.js @@ -161,15 +161,6 @@ module.exports = class SingleSpaUtilModuleGenerator extends PnpmGenerator { } ); } - - const childGitInitProcess = this.spawnCommandSync("git", ["init"]); - if (childGitInitProcess.error) { - console.log(chalk.red("\n************")); - console.log(chalk.red("Cannot initialize git repository")); - console.log(chalk.red("************\n")); - } else { - console.log(chalk.green("\nInitialized git repository\n")); - } } install() { super.install(); diff --git a/packages/generator-single-spa/test/git.test.js b/packages/generator-single-spa/test/git.test.js new file mode 100644 index 00000000..a3672bca --- /dev/null +++ b/packages/generator-single-spa/test/git.test.js @@ -0,0 +1,35 @@ +const generator = require("../src/generator-single-spa"); +const helpers = require("yeoman-test"); + +describe("generator-single-spa", () => { + it("can do git init", async () => { + const runResult = await helpers + .create(generator) + .withOptions({ + moduleType: "root-config", + packageManager: "pnpm", + orgName: "org", + projectName: "proj", + layout: false, + skipInstall: true, + }) + .run(); + runResult.assertFile(".git/HEAD"); + }); + + it("can skip git init", async () => { + const runResult = await helpers + .create(generator) + .withOptions({ + moduleType: "root-config", + packageManager: "pnpm", + orgName: "org", + projectName: "proj", + layout: false, + skipInstall: true, + skipGit: true, + }) + .run(); + runResult.assertNoFile(".git/HEAD"); + }); +}); From 2a0b2b997ffb73c2dd66b6ea89389b0ebad0d22d Mon Sep 17 00:00:00 2001 From: Danny Eck Date: Fri, 11 Aug 2023 19:40:58 -0400 Subject: [PATCH 4/5] refactor: PnpnGenerator#install simplified --- .../generator-single-spa/src/PnpmGenerator.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/generator-single-spa/src/PnpmGenerator.js b/packages/generator-single-spa/src/PnpmGenerator.js index ed5e7fa1..3e997bef 100644 --- a/packages/generator-single-spa/src/PnpmGenerator.js +++ b/packages/generator-single-spa/src/PnpmGenerator.js @@ -9,8 +9,13 @@ module.exports = class PnpmGenerator extends Generator { } // This overrides https://github.com/yeoman/generator/blob/9cd93ee0fe8babd26cce4e22a6f1db7637573801/lib/actions/install.js#L119 // to include support for pnpm installations - _installDependencies(options) { - options = options || {}; + _installDependencies() { + const options = { + npm: this.options.packageManager === "npm", + yarn: this.options.packageManager === "yarn", + pnpm: this.options.packageManager === "pnpm", + bower: false, + }; const msg = { commands: [], template: _.template( @@ -67,12 +72,7 @@ module.exports = class PnpmGenerator extends Generator { this.scheduleInstallTask("pnpm", pkgs, options, spawnOptions); } install() { - this._installDependencies({ - npm: this.options.packageManager === "npm", - yarn: this.options.packageManager === "yarn", - pnpm: this.options.packageManager === "pnpm", - bower: false, - }); + this._installDependencies(); this._gitInit(); } _gitInit() { From 321591f59de21f6afbae53b61ab62d6aef6a1efe Mon Sep 17 00:00:00 2001 From: Danny Eck Date: Fri, 11 Aug 2023 20:23:47 -0400 Subject: [PATCH 5/5] chore: add changeset --- .changeset/young-drinks-notice.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/young-drinks-notice.md diff --git a/.changeset/young-drinks-notice.md b/.changeset/young-drinks-notice.md new file mode 100644 index 00000000..8047a16f --- /dev/null +++ b/.changeset/young-drinks-notice.md @@ -0,0 +1,5 @@ +--- +"generator-single-spa": minor +--- + +add skipGit option