Skip to content

Commit f1aecfc

Browse files
Sudeep PatelSudeep Patel
authored andcommitted
fix(detection.js, createstrapi.js, packagedetection.js,yarnpackagehelper.js): bugfix for yarn pm
bugfix to give use the option to choose package manager of their choice if yarn npm both exists and if yarn is provided in the quickstart option and yarn not installed install it for them.
1 parent 4e62acb commit f1aecfc

File tree

4 files changed

+159
-7
lines changed

4 files changed

+159
-7
lines changed

utils/createStrapi.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const createStrapiProject = async () => {
3838
}
3939
]);
4040

41+
/* eslint-disable */
4142
async function checkPathAccessibility(targetPath) {
4243
if (!path.isAbsolute(targetPath)) {
4344
console.error(`${chalk.bold.red(

utils/detection.js

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const path = require(`path`);
22
const { spinner, chalk, constants, access } = require(`./utils`);
33
const { setConfig, config } = require(`./config`);
44
const fetch = require(`node-fetch`);
5+
const { checkInstalledPackages } = require(`./packageDetection`);
6+
const prompts = require(`prompts`);
7+
const { checkForYarnPackage } = require(`./yarnPackageHelper`);
58
const { readFile } = require(`fs`).promises;
69

710
const detectDownloadsAndStars = async () => {
@@ -60,6 +63,7 @@ const detectProjectType = async () => {
6063

6164
const detectPackageManager = async () => {
6265
spinner.start(` 💻 Detecting package manager... `);
66+
spinner.stop();
6367
try {
6468
if (config.quickStart) {
6569
spinner.stopAndPersist({
@@ -70,23 +74,72 @@ const detectPackageManager = async () => {
7074
: `${chalk.bold.greenBright(`NPM`)}`
7175
} set by cli arguments \n`
7276
});
77+
await checkForYarnPackage();
7378
return;
7479
}
75-
await access(`yarn.lock`, constants.R_OK);
76-
config.packageManager = `yarn`;
7780
} catch (error) {
7881
config.packageManager = `npm`;
79-
}
80-
if (!config.quickStart) {
8182
spinner.stopAndPersist({
8283
symbol: `📦`,
83-
text: ` ${chalk.bold.yellow(
84+
text: ` Exception Occured! falling back to ${chalk.bold.blueBright(
8485
config.packageManager.toUpperCase()
85-
)} detected \n`
86+
)} \n`
8687
});
8788
}
88-
};
8989

90+
if (!config.quickStart) {
91+
const installedPackages = await checkInstalledPackages();
92+
if (installedPackages.length === 2) {
93+
spinner.stopAndPersist({
94+
symbol: `📂`,
95+
text: `Detected Yarn & NPM ... \n`
96+
});
97+
const choosePackageManager = await prompts([
98+
{
99+
name: `packageManager`,
100+
message: `Which package manager do you want to use ? (Strapi Recommends yarn)`,
101+
type: `select`,
102+
choices: [
103+
{
104+
title: `Yarn`,
105+
value: `yarn`,
106+
},
107+
{
108+
title: `NPM`,
109+
value: `npm`,
110+
},
111+
]
112+
}
113+
]);
114+
config.packageManager = choosePackageManager[`packageManager`];
115+
spinner.stopAndPersist({
116+
symbol: `\n📦`,
117+
text: `Using ${chalk.bold.blueBright(
118+
config.packageManager.toUpperCase()
119+
)} \n`
120+
});
121+
return;
122+
} else {
123+
try {
124+
await access(`yarn.lock`, constants.R_OK);
125+
config.packageManager = `yarn`;
126+
if (!installedPackages.includes(`yarn`)) {
127+
await checkForYarnPackage();
128+
}
129+
return;
130+
} catch (error) {
131+
config.packageManager = `npm`;
132+
}
133+
spinner.stopAndPersist({
134+
symbol: `📦`,
135+
text: ` ${chalk.bold.yellow(
136+
config.packageManager.toUpperCase()
137+
)} detected \n`
138+
});
139+
return;
140+
}
141+
}
142+
};
90143
const detectStrapiProject = async () => {
91144
spinner.start(` 💻 Detecting Strapi project... `);
92145
try {

utils/packageDetection.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { exec } = require(`child_process`);
2+
const isWindows = process.platform === `win32`;
3+
4+
async function checkPackage(packageName) {
5+
return new Promise((resolve) => {
6+
const command = isWindows ? `where ${packageName}` : `which ${packageName}`;
7+
exec(command, (error) => {
8+
if (error) {
9+
resolve(false);
10+
} else {
11+
resolve(true);
12+
}
13+
});
14+
});
15+
}
16+
17+
async function checkInstalledPackages() {
18+
const npmExists = await checkPackage(`npm`);
19+
const yarnExists = await checkPackage(`yarn`);
20+
21+
if (npmExists && yarnExists) {
22+
return [`npm`, `yarn`];
23+
} else if (npmExists) {
24+
return [`npm`];
25+
} else if (yarnExists) {
26+
return [`yarn`];
27+
} else {
28+
return [];
29+
}
30+
}
31+
32+
module.exports = {
33+
checkInstalledPackages
34+
};

utils/yarnPackageHelper.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const { exec } = require(`child_process`);
2+
const { config } = require(`./config`);
3+
const {checkInstalledPackages} = require(`./packageDetection`);
4+
const prompts = require(`prompts`);
5+
const { spinner, chalk } = require(`./utils`);
6+
7+
async function installYarn() {
8+
return new Promise((resolve, reject) => {
9+
exec(`npm install -g yarn`, (error, stdout, stderr) => {
10+
if (error) {
11+
console.error(`Error: ${error.message}`);
12+
reject(error);
13+
return;
14+
}
15+
if (stderr) {
16+
console.error(`Stderr: ${stderr}`);
17+
reject(new Error(stderr));
18+
return;
19+
}
20+
resolve(stdout);
21+
});
22+
});
23+
}
24+
25+
async function checkForYarnPackage() {
26+
if (config.packageManager === `yarn`) {
27+
const checkIfYarnIsInstalled = await checkInstalledPackages();
28+
let response = ``;
29+
if (!checkIfYarnIsInstalled.includes(`yarn`)) {
30+
response = await prompts([
31+
{
32+
name: `installYarnPrompt`,
33+
message: `Yarn not installed! Shall we install it for you? (Strapi Recommended)`,
34+
active: `Yes`,
35+
inactive: `No`,
36+
type: `toggle`
37+
}
38+
]);
39+
40+
if (response[`installYarnPrompt`] === true) {
41+
await installYarn();
42+
config.packageManager = `yarn`;
43+
spinner.stopAndPersist({
44+
text: `\n✅ Yarn installed successfully!\n`
45+
});
46+
} else {
47+
config.packageManager = `npm`;
48+
}
49+
}
50+
51+
spinner.stopAndPersist({
52+
symbol: `📦`,
53+
text: ` Using ${chalk.bold.blueBright(
54+
config.packageManager.toUpperCase()
55+
)} \n`
56+
});
57+
}
58+
}
59+
60+
61+
module.exports = {
62+
installYarn,
63+
checkForYarnPackage
64+
};

0 commit comments

Comments
 (0)