From 7d1793fbfef7d068fae247b9fe1bec87df86b9c3 Mon Sep 17 00:00:00 2001 From: Mihai Blaga Date: Sat, 22 May 2021 18:12:21 +0200 Subject: [PATCH 1/2] added packageManager cli option support --- packages/rnv/src/core/constants.js | 8 +++++++ .../rnv/src/core/systemManager/npmUtils.js | 24 +++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/rnv/src/core/constants.js b/packages/rnv/src/core/constants.js index 124f7c38d2..742f1f7bbe 100644 --- a/packages/rnv/src/core/constants.js +++ b/packages/rnv/src/core/constants.js @@ -427,6 +427,14 @@ export const PARAM_KEYS = { isRequired: true, description: 'Use custom name for ./renative.json. (applies only at root level)' }, + packageManager: { + value: 'value', + description: 'Specify the desired package manager to be used', + examples: [ + '--packageManager yarn', + '--packageManager npm' + ] + } }; Object.keys(PARAM_KEYS).forEach((k) => { diff --git a/packages/rnv/src/core/systemManager/npmUtils.js b/packages/rnv/src/core/systemManager/npmUtils.js index c22587f40e..5cbaab2fbf 100644 --- a/packages/rnv/src/core/systemManager/npmUtils.js +++ b/packages/rnv/src/core/systemManager/npmUtils.js @@ -154,11 +154,24 @@ export const installPackageDependencies = async (c, failOnError = false) => { const yarnLockPath = path.join(c.paths.project.dir, 'yarn.lock'); const npmLockPath = path.join(c.paths.project.dir, 'package-lock.json'); let command = 'npm install'; - if (fsExistsSync(yarnLockPath)) { - command = 'yarn'; - } else if (fsExistsSync(npmLockPath)) { - command = 'npm install'; - } else if (isYarnInstalled) { + + + const yarnLockExists = fsExistsSync(yarnLockPath); + const packageLockExists = fsExistsSync(npmLockPath); + + if (yarnLockExists || packageLockExists) { + // a lock file exists, defaulting to whichever is present + if (yarnLockExists && !isYarnInstalled) throw new Error('You have a yarn.lock file but you don\'t have yarn installed. Install it or delete yarn.lock'); + command = yarnLockExists ? 'yarn' : 'npm install'; + } else if (c.program.packageManager) { + // no lock file check cli option + if (['yarn', 'npm'].includes(c.program.packageManager)) { + command = c.program.packageManager === 'yarn' ? 'yarn' : 'npm install'; + } else { + throw new Error(`Unsupported package manager ${c.program.packageManager}. Only yarn and npm are supported at the moment.`); + } + } else { + // no cli option either, asking const { packageManager } = await inquirerPrompt({ type: 'list', name: 'packageManager', @@ -168,6 +181,7 @@ export const installPackageDependencies = async (c, failOnError = false) => { }); if (packageManager === 'yarn') command = 'yarn'; } + logTask('installPackageDependencies', `packageManager:(${command})`); try { From 4f23f10a68e4086031ea08eac7bac7536b6e1a25 Mon Sep 17 00:00:00 2001 From: Mihai Blaga Date: Sat, 22 May 2021 18:15:42 +0200 Subject: [PATCH 2/2] extra check --- packages/rnv/src/core/systemManager/npmUtils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/rnv/src/core/systemManager/npmUtils.js b/packages/rnv/src/core/systemManager/npmUtils.js index 5cbaab2fbf..1a6a422305 100644 --- a/packages/rnv/src/core/systemManager/npmUtils.js +++ b/packages/rnv/src/core/systemManager/npmUtils.js @@ -167,6 +167,7 @@ export const installPackageDependencies = async (c, failOnError = false) => { // no lock file check cli option if (['yarn', 'npm'].includes(c.program.packageManager)) { command = c.program.packageManager === 'yarn' ? 'yarn' : 'npm install'; + if (command === 'yarn' && !isYarnInstalled) throw new Error('You specified yarn as packageManager but it\'s not installed'); } else { throw new Error(`Unsupported package manager ${c.program.packageManager}. Only yarn and npm are supported at the moment.`); }