Skip to content

Commit

Permalink
Merge branch 'feat/packageManager_option' into feat/auto-init-template
Browse files Browse the repository at this point in the history
* feat/packageManager_option:
  extra check
  added packageManager cli option support

# Conflicts:
#	packages/rnv/src/core/constants.js
#	packages/rnv/src/core/systemManager/npmUtils.js
  • Loading branch information
pavjacko committed May 24, 2021
2 parents 2bfdc84 + 4f23f10 commit c38d095
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/rnv/src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ export const PARAM_KEYS = {
value: 'value',
isRequired: true,
options: ['yarn', 'npm'],
description: 'Set specific package manager to use'
description: 'Set specific package manager to use',
examples: [
'--packageManager yarn',
'--packageManager npm'
]
}
};

Expand Down
30 changes: 21 additions & 9 deletions packages/rnv/src/core/systemManager/npmUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,26 @@ 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 (c.program.packageManager === 'yarn') {
command = 'yarn';
} else if (c.program.packageManager === 'npm') {
command = 'npm';
} else 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';
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.`);
}
} else {
// no cli option either, asking
const { packageManager } = await inquirerPrompt({
type: 'list',
name: 'packageManager',
Expand All @@ -174,6 +185,7 @@ export const installPackageDependencies = async (c, failOnError = false) => {
});
if (packageManager === 'yarn') command = 'yarn';
}

logTask('installPackageDependencies', `packageManager:(${command})`);

try {
Expand Down

0 comments on commit c38d095

Please sign in to comment.