Skip to content

Commit

Permalink
fix(version): add better msg for missing npmClient with sync lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jun 9, 2022
1 parent ab86e50 commit 01e26b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Expand Up @@ -149,7 +149,9 @@ describe('run install lockfile-only', () => {

expect(logSpy).toHaveBeenCalledWith(
'lock',
expect.stringContaining('we could not sync or locate "pnpm-lock.yaml" from path')
expect.stringContaining(
`we could not sync or locate "pnpm-lock.yaml" by using "pnpm" client at location ${cwd}`
)
);
expect(lockFileOutput).toBe(undefined);
});
Expand Down
19 changes: 13 additions & 6 deletions packages/version/src/lib/update-lockfile-version.ts
Expand Up @@ -2,6 +2,7 @@ import log from 'npmlog';
import path from 'path';
import loadJsonFile from 'load-json-file';
import { promises as fsPromises, renameSync } from 'node:fs';
import os from 'os';
import semver from 'semver';
import writeJsonFile from 'write-json-file';
import { exec, execSync, Package } from '@lerna-lite/core';
Expand Down Expand Up @@ -132,15 +133,15 @@ export async function runInstallLockFileOnly(
case 'pnpm':
inputLockfileName = 'pnpm-lock.yaml';
if (await validateFileExists(path.join(cwd, inputLockfileName))) {
log.verbose(`lock`, `updating lock file via "pnpm install --lockfile-only"`);
log.verbose('lock', `updating lock file via "pnpm install --lockfile-only"`);
await exec('pnpm', ['install', '--lockfile-only'], { cwd });
outputLockfileName = inputLockfileName;
}
break;
case 'yarn':
inputLockfileName = 'yarn.lock';
if (await validateFileExists(path.join(cwd, inputLockfileName))) {
log.verbose(`lock`, `updating lock file via "yarn install --mode update-lockfile"`);
log.verbose('lock', `updating lock file via "yarn install --mode update-lockfile"`);
await exec('yarn', ['install', '--mode', 'update-lockfile'], { cwd });
outputLockfileName = inputLockfileName;
}
Expand All @@ -155,21 +156,21 @@ export async function runInstallLockFileOnly(
// for npm version >=8.5.0 we can call "npm install --package-lock-only"
// when lower then we call "npm shrinkwrap --package-lock-only" and rename "npm-shrinkwrap.json" back to "package-lock.json"
if (semver.gte(localNpmVersion, '8.5.0')) {
log.verbose(`lock`, `updating lock file via "npm install --package-lock-only"`);
log.verbose('lock', `updating lock file via "npm install --package-lock-only"`);
await exec('npm', ['install', '--package-lock-only'], { cwd });
} else {
// TODO: eventually remove in future and/or major release
// with npm, we need to do update the lock file in 2 steps
// 1. using shrinkwrap will delete current lock file and create new "npm-shrinkwrap.json" but will avoid npm retrieving package version info from registry
log.verbose(`lock`, `updating lock file via "npm shrinkwrap --package-lock-only".`);
log.verbose('lock', `updating lock file via "npm shrinkwrap --package-lock-only".`);
log.warn(
`npm`,
`Your npm version is lower than 8.5.0, we recommend upgrading your npm client to avoid the use of "npm shrinkwrap" instead of the regular (better) "npm install --package-lock-only".`
);
await exec('npm', ['shrinkwrap', '--package-lock-only'], { cwd });

// 2. rename "npm-shrinkwrap.json" back to "package-lock.json"
log.verbose(`lock`, `renaming "npm-shrinkwrap.json" file back to "package-lock.json"`);
log.verbose('lock', `renaming "npm-shrinkwrap.json" file back to "package-lock.json"`);
renameSync('npm-shrinkwrap.json', 'package-lock.json');
}

Expand All @@ -179,7 +180,13 @@ export async function runInstallLockFileOnly(
}

if (!outputLockfileName) {
log.error('lock', `we could not sync or locate "${inputLockfileName}" from path ${cwd}`);
log.error(
'lock',
[
`we could not sync or locate "${inputLockfileName}" by using "${npmClient}" client at location ${cwd}`,
`Note: if you were expecting a different lock file name, make sure to add "npmClient" into your "lerna.json" config.`,
].join(os.EOL)
);
}
return outputLockfileName;
}
Expand Down

0 comments on commit 01e26b1

Please sign in to comment.