Skip to content

Commit

Permalink
feat(init-generator): add ability to specify a package manager of cho…
Browse files Browse the repository at this point in the history
…ice (#2769)

* feat: add ability to specify a package manager of choice

* chore: choose default installer based on the lock file presence

* test: update test suite
  • Loading branch information
jamesgeorge007 authored Jun 9, 2021
1 parent 59d644d commit e53f164
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/generators/src/init-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,17 @@ export default class InitGenerator extends CustomGenerator {
}
}

public installPlugins(): void {
const packager = this.utils.getPackageManager();
public async installPlugins(): Promise<void> {
// Prompt for the package manager of choice
const defaultPackager = this.utils.getPackageManager();
const { packager } = await Question.List(
this,
"packager",
"Pick a package manager:",
this.utils.getAvailableInstallers(),
defaultPackager,
this.force,
);
const opts: {
dev?: boolean;
"save-dev"?: boolean;
Expand Down
25 changes: 25 additions & 0 deletions packages/webpack-cli/lib/utils/get-available-installers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { sync } = require("execa");

const utils = require("./");

function hasPmInstalled(packageManager) {
try {
sync(packageManager, ["--version"]);
return packageManager;
} catch (err) {
return false;
}
}

function getAvailableInstallers() {
const installers = ["npm", "yarn", "pnpm"];
const availableInstallers = installers.filter((installer) => hasPmInstalled(installer));

if (!availableInstallers.length) {
utils.logger.error("No package manager found.");
process.exit(2);
}
return availableInstallers;
}

module.exports = getAvailableInstallers;
4 changes: 4 additions & 0 deletions packages/webpack-cli/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module.exports = {
return require("./dynamic-import-loader");
},

get getAvailableInstallers() {
return require("./get-available-installers");
},

get getPackageManager() {
return require("./get-package-manager");
},
Expand Down
17 changes: 12 additions & 5 deletions test/init/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe("init command", () => {
const { stdout, stderr } = await runPromptWithAnswers(
assetsPath,
["init"],
[`${DOWN}${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, ENTER],
[`${DOWN}${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, ENTER, ENTER],
);

expect(stdout).toContain("Project has been initialised with webpack!");
Expand All @@ -170,7 +170,7 @@ describe("init command", () => {
const { stdout, stderr } = await runPromptWithAnswers(
assetsPath,
["init"],
[`${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, ENTER],
[`${DOWN}${ENTER}`, `n${ENTER}`, `n${ENTER}`, `n${ENTER}`, ENTER, ENTER],
);

expect(stdout).toContain("Project has been initialised with webpack!");
Expand Down Expand Up @@ -205,6 +205,7 @@ describe("init command", () => {
`n${ENTER}`,
`n${ENTER}`,
`n${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -239,6 +240,7 @@ describe("init command", () => {
`n${ENTER}`,
`y${ENTER}`,
`n${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -279,6 +281,7 @@ describe("init command", () => {
`n${ENTER}`,
`n${ENTER}`,
`y${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -313,6 +316,7 @@ describe("init command", () => {
`y${ENTER}`,
`y${ENTER}`,
`n${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -353,6 +357,7 @@ describe("init command", () => {
`n${ENTER}`,
`n${ENTER}`,
`n${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -387,6 +392,7 @@ describe("init command", () => {
`n${ENTER}`,
`n${ENTER}`,
`n${ENTER}`,
ENTER,
],
);

Expand All @@ -412,7 +418,7 @@ describe("init command", () => {
const { stdout, stderr } = await runPromptWithAnswers(
assetsPath,
["init"],
[ENTER, ENTER, `n${ENTER}`, `n${ENTER}`, ENTER],
[ENTER, ENTER, `n${ENTER}`, `n${ENTER}`, ENTER, ENTER],
);

expect(stdout).toContain("Do you want to use webpack-dev-server?");
Expand Down Expand Up @@ -445,6 +451,7 @@ describe("init command", () => {
`${DOWN}${ENTER}`,
ENTER,
`n${ENTER}`,
ENTER,
],
);

Expand Down Expand Up @@ -476,7 +483,7 @@ describe("init command", () => {
const { stdout, stderr } = await runPromptWithAnswers(
assetsPath,
["init"],
[ENTER, `n${ENTER}`, ENTER, `n${ENTER}`, ENTER],
[ENTER, `n${ENTER}`, ENTER, `n${ENTER}`, ENTER, ENTER],
);

expect(stdout).toContain(
Expand Down Expand Up @@ -504,7 +511,7 @@ describe("init command", () => {
const { stdout, stderr } = await runPromptWithAnswers(
assetsPath,
["init"],
[ENTER, `n${ENTER}`, ENTER, ENTER, ENTER],
[ENTER, `n${ENTER}`, ENTER, ENTER, ENTER, ENTER],
);

expect(stdout).toContain("Do you want to add PWA support?");
Expand Down

0 comments on commit e53f164

Please sign in to comment.