Skip to content

Commit 16ef814

Browse files
committed
add code to add --legacy-peer-deps for npm v7+
1 parent 413dd86 commit 16ef814

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

bin/helpers/packageInstaller.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
fileHelpers = require('./fileHelpers'),
66
logger = require("./logger").winstonLogger,
77
Constants = require('./constants'),
8-
process = require('process'),
98
utils = require('./utils'),
9+
{ get_version } = require('./usageReporting'),
10+
process = require('process'),
1011
{ spawn } = require('child_process'),
1112
util = require('util');
1213

@@ -64,15 +65,30 @@ const packageInstall = (packageDir) => {
6465
logger.info(`Packages were installed locally successfully.`);
6566
resolve('Packages were installed successfully.');
6667
} else {
67-
logger.error(`Some error occurred while installing packages. Error code ${code}`);
68+
logger.error(`Some error occurred while installing packages. Error code ${code}. Please read npm_install_debug.log for more info.}`);
6869
reject(`Packages were not installed successfully. Error code ${code}`);
6970
}
7071
};
7172
const nodeProcessErrorCallback = (error) => {
7273
logger.error(`Some error occurred while installing packages: %j`, error);
7374
reject(`Packages were not installed successfully. Error Description ${util.format('%j', error)}`);
7475
};
75-
nodeProcess = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install', '--loglevel', 'verbose', '>', '../npm_install_debug.log', '2>&1'], {cwd: packageDir, shell: true});
76+
77+
let nodeProcess;
78+
logger.debug(`Fetching npm version and its major version`);
79+
const npm_version = get_version('npm')
80+
const npm_major_version = utils.getMajorVersion(npm_version);
81+
logger.debug(`Fetched npm version: ${npm_version} and its major version: ${npm_major_version}`);
82+
83+
// add --legacy-peer-deps flag while installing dependencies for npm v7+
84+
// For more info please read "Peer Dependencies" section here -> https://github.blog/2021-02-02-npm-7-is-now-generally-available/
85+
if (parseInt(npm_major_version) >= 7) {
86+
logger.debug(`Running NPM install command: npm install --legacy-peer-deps --loglevel verbose > ../npm_install_debug.log`);
87+
nodeProcess = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install', '--legacy-peer-deps', '--loglevel', 'verbose', '>', '../npm_install_debug.log', '2>&1'], {cwd: packageDir, shell: true});
88+
} else {
89+
logger.debug(`Running NPM install command: 'npm install --loglevel verbose > ../npm_install_debug.log'`);
90+
nodeProcess = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install', '--loglevel', 'verbose', '>', '../npm_install_debug.log', '2>&1'], {cwd: packageDir, shell: true});
91+
}
7692
nodeProcess.on('close', nodeProcessCloseCallback);
7793
nodeProcess.on('error', nodeProcessErrorCallback);
7894
});

bin/helpers/usageReporting.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,5 @@ function send(args) {
291291
module.exports = {
292292
send,
293293
cli_version_and_path,
294+
get_version,
294295
};

bin/helpers/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,3 +1203,21 @@ exports.getVideoConfig = (cypressJson) => {
12031203
logger.debug(`Setting videoUploadOnPasses = ${conf.videoUploadOnPasses}`);
12041204
return conf;
12051205
}
1206+
1207+
exports.getMajorVersion = (version) => {
1208+
try {
1209+
if (!version || !version.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/)) {
1210+
return null;
1211+
}
1212+
1213+
const matches = version.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/)
1214+
if(matches && matches.length >= 2) {
1215+
return matches[1].replace('.','');
1216+
} else {
1217+
return null;
1218+
}
1219+
} catch(error) {
1220+
logger.debug(`Some Error occurred while fetching major version of ${version}. Returning null. Error Details: ${error}`)
1221+
return null;
1222+
}
1223+
}

0 commit comments

Comments
 (0)