Skip to content

Commit

Permalink
Merge pull request #83 from strapi-community/bugfix/package-manager-f…
Browse files Browse the repository at this point in the history
…ixes

package manager choice bugfix
  • Loading branch information
Eventyret committed Jun 6, 2023
2 parents 4e62acb + f1aecfc commit 9ba4883
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 7 deletions.
1 change: 1 addition & 0 deletions utils/createStrapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const createStrapiProject = async () => {
}
]);

/* eslint-disable */
async function checkPathAccessibility(targetPath) {
if (!path.isAbsolute(targetPath)) {
console.error(`${chalk.bold.red(
Expand Down
67 changes: 60 additions & 7 deletions utils/detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const path = require(`path`);
const { spinner, chalk, constants, access } = require(`./utils`);
const { setConfig, config } = require(`./config`);
const fetch = require(`node-fetch`);
const { checkInstalledPackages } = require(`./packageDetection`);
const prompts = require(`prompts`);
const { checkForYarnPackage } = require(`./yarnPackageHelper`);
const { readFile } = require(`fs`).promises;

const detectDownloadsAndStars = async () => {
Expand Down Expand Up @@ -60,6 +63,7 @@ const detectProjectType = async () => {

const detectPackageManager = async () => {
spinner.start(` 💻 Detecting package manager... `);
spinner.stop();
try {
if (config.quickStart) {
spinner.stopAndPersist({
Expand All @@ -70,23 +74,72 @@ const detectPackageManager = async () => {
: `${chalk.bold.greenBright(`NPM`)}`
} set by cli arguments \n`
});
await checkForYarnPackage();
return;
}
await access(`yarn.lock`, constants.R_OK);
config.packageManager = `yarn`;
} catch (error) {
config.packageManager = `npm`;
}
if (!config.quickStart) {
spinner.stopAndPersist({
symbol: `📦`,
text: ` ${chalk.bold.yellow(
text: ` Exception Occured! falling back to ${chalk.bold.blueBright(
config.packageManager.toUpperCase()
)} detected \n`
)} \n`
});
}
};

if (!config.quickStart) {
const installedPackages = await checkInstalledPackages();
if (installedPackages.length === 2) {
spinner.stopAndPersist({
symbol: `📂`,
text: `Detected Yarn & NPM ... \n`
});
const choosePackageManager = await prompts([
{
name: `packageManager`,
message: `Which package manager do you want to use ? (Strapi Recommends yarn)`,
type: `select`,
choices: [
{
title: `Yarn`,
value: `yarn`,
},
{
title: `NPM`,
value: `npm`,
},
]
}
]);
config.packageManager = choosePackageManager[`packageManager`];
spinner.stopAndPersist({
symbol: `\n📦`,
text: `Using ${chalk.bold.blueBright(
config.packageManager.toUpperCase()
)} \n`
});
return;
} else {
try {
await access(`yarn.lock`, constants.R_OK);
config.packageManager = `yarn`;
if (!installedPackages.includes(`yarn`)) {
await checkForYarnPackage();
}
return;
} catch (error) {
config.packageManager = `npm`;
}
spinner.stopAndPersist({
symbol: `📦`,
text: ` ${chalk.bold.yellow(
config.packageManager.toUpperCase()
)} detected \n`
});
return;
}
}
};
const detectStrapiProject = async () => {
spinner.start(` 💻 Detecting Strapi project... `);
try {
Expand Down
34 changes: 34 additions & 0 deletions utils/packageDetection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { exec } = require(`child_process`);
const isWindows = process.platform === `win32`;

async function checkPackage(packageName) {
return new Promise((resolve) => {
const command = isWindows ? `where ${packageName}` : `which ${packageName}`;
exec(command, (error) => {
if (error) {
resolve(false);
} else {
resolve(true);
}
});
});
}

async function checkInstalledPackages() {
const npmExists = await checkPackage(`npm`);
const yarnExists = await checkPackage(`yarn`);

if (npmExists && yarnExists) {
return [`npm`, `yarn`];
} else if (npmExists) {
return [`npm`];
} else if (yarnExists) {
return [`yarn`];
} else {
return [];
}
}

module.exports = {
checkInstalledPackages
};
64 changes: 64 additions & 0 deletions utils/yarnPackageHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { exec } = require(`child_process`);
const { config } = require(`./config`);
const {checkInstalledPackages} = require(`./packageDetection`);
const prompts = require(`prompts`);
const { spinner, chalk } = require(`./utils`);

async function installYarn() {
return new Promise((resolve, reject) => {
exec(`npm install -g yarn`, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
reject(new Error(stderr));
return;
}
resolve(stdout);
});
});
}

async function checkForYarnPackage() {
if (config.packageManager === `yarn`) {
const checkIfYarnIsInstalled = await checkInstalledPackages();
let response = ``;
if (!checkIfYarnIsInstalled.includes(`yarn`)) {
response = await prompts([
{
name: `installYarnPrompt`,
message: `Yarn not installed! Shall we install it for you? (Strapi Recommended)`,
active: `Yes`,
inactive: `No`,
type: `toggle`
}
]);

if (response[`installYarnPrompt`] === true) {
await installYarn();
config.packageManager = `yarn`;
spinner.stopAndPersist({
text: `\n✅ Yarn installed successfully!\n`
});
} else {
config.packageManager = `npm`;
}
}

spinner.stopAndPersist({
symbol: `📦`,
text: ` Using ${chalk.bold.blueBright(
config.packageManager.toUpperCase()
)} \n`
});
}
}


module.exports = {
installYarn,
checkForYarnPackage
};

0 comments on commit 9ba4883

Please sign in to comment.