Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new): check for installed package managers #2495

Closed
Closed
45 changes: 40 additions & 5 deletions actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ export class NewAction extends AbstractAction {
}
}

const isThisPackageManagerInstalled = (packageManager: string) => {
try {
execSync(`${packageManager} -v`, {
stdio: 'ignore',
});
return true;
} catch {
return false;
}
};

const getApplicationNameInput = (inputs: Input[]) =>
inputs.find((input) => input.name === 'name');

Expand Down Expand Up @@ -163,12 +174,36 @@ const installPackages = async (
};

const askForPackageManager = async (): Promise<Answers> => {
const possiblePackageManagers = [
{
id: 'npm',
packageManager: PackageManager.NPM,
},
{
id: 'yarn',
packageManager: PackageManager.YARN,
},
{
id: 'pnpm',
packageManager: PackageManager.PNPM,
},
];
const installedPackageManagers: string[] = [];
possiblePackageManagers.forEach((entry) => {
if (isThisPackageManagerInstalled(entry.id)) {
installedPackageManagers.push(entry.packageManager);
}
});
if (installedPackageManagers.length == 0) {
console.error();
console.error(chalk.red(MESSAGES.NO_PACKAGE_MANAGER_WAS_FOUND));
console.error();
exit();
}
const questions: Question[] = [
generateSelect('packageManager')(MESSAGES.PACKAGE_MANAGER_QUESTION)([
PackageManager.NPM,
PackageManager.YARN,
PackageManager.PNPM,
]),
generateSelect('packageManager')(MESSAGES.PACKAGE_MANAGER_QUESTION)(
installedPackageManagers,
),
];
const prompt = inquirer.createPromptModule();
return await prompt(questions);
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const MESSAGES = {
RUNNER_EXECUTION_ERROR: (command: string) =>
`\nFailed to execute command: ${command}`,
PACKAGE_MANAGER_QUESTION: `Which package manager would you ${EMOJIS.HEART} to use?`,
NO_PACKAGE_MANAGER_WAS_FOUND:
'No package manager was found installed on your system.',
PACKAGE_MANAGER_INSTALLATION_IN_PROGRESS: `Installation in progress... ${EMOJIS.COFFEE}`,
PACKAGE_MANAGER_UPDATE_IN_PROGRESS: `Installation in progress... ${EMOJIS.COFFEE}`,
PACKAGE_MANAGER_UPGRADE_IN_PROGRESS: `Installation in progress... ${EMOJIS.COFFEE}`,
Expand Down