Skip to content

Commit

Permalink
feat: add ability to specify a package manager of choice
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Jun 7, 2021
1 parent 598008a commit 76092b8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/generators/src/init-generator.ts
Expand Up @@ -99,8 +99,15 @@ export default class InitGenerator extends CustomGenerator {
}
}

public installPlugins(): void {
const packager = this.utils.getPackageManager();
public async installPlugins(): Promise<void> {
const { packager } = await Question.List(
this,
"packager",
"Pick a package manager:",
this.utils.getAvailableInstallers(),
"npm",
false,
);
const opts: {
dev?: boolean;
"save-dev"?: boolean;
Expand Down
25 changes: 25 additions & 0 deletions packages/webpack-cli/lib/utils/get-available-installers.js
@@ -0,0 +1,25 @@
const { sync } = require("execa");

const utils = require("./");

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

function getAvailableInstallers() {
const installers = ["npm", "yarn", "pnpm"];
const availableInstallers = installers.filter((installer) => isPmInstalled(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
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

0 comments on commit 76092b8

Please sign in to comment.