Skip to content

Commit

Permalink
Update package-lock.json after bumping packages (#675)
Browse files Browse the repository at this point in the history
* Update package-lock.json after bumping packages

* Change files

* move to separate function
  • Loading branch information
ecraig12345 authored Jun 14, 2022
1 parent 78b8ee3 commit 4f3cd13
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions change/beachball-3e377ae5-505a-4a93-ac31-b03334e67971.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Update package-lock.json after bumping packages",
"packageName": "beachball",
"email": "[email protected]",
"dependentChangeType": "patch"
}
23 changes: 21 additions & 2 deletions src/bump/performBump.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { unlinkChangeFiles } from '../changefile/unlinkChangeFiles';
import { writeChangelog } from '../changelog/writeChangelog';
import { spawnSync } from 'child_process';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import { unlinkChangeFiles } from '../changefile/unlinkChangeFiles';
import { writeChangelog } from '../changelog/writeChangelog';
import { BumpInfo } from '../types/BumpInfo';
import { BeachballOptions, HooksOptions } from '../types/BeachballOptions';
import { PackageDeps, PackageInfos } from '../types/PackageInfo';
import { findProjectRoot } from '../paths';

export function writePackageJson(modifiedPackages: Set<string>, packageInfos: PackageInfos) {
for (const pkgName of modifiedPackages) {
Expand Down Expand Up @@ -34,6 +37,21 @@ export function writePackageJson(modifiedPackages: Set<string>, packageInfos: Pa
}
}

/**
* If `package-lock.json` exists, runs `npm install --package-lock-only` to update it.
*/
export function updatePackageLock(cwd: string) {
const root = findProjectRoot(cwd);
if (root && fs.existsSync(path.join(root, 'package-lock.json'))) {
console.log('Updating package-lock.json after bumping packages');
const npm = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
const res = spawnSync(npm, ['install', '--package-lock-only'], { stdio: 'inherit' });
if (res.status !== 0) {
console.warn('Updating package-lock.json failed. Continuing...');
}
}
}

/**
* Performs the bump, writes to the file system
*
Expand All @@ -45,6 +63,7 @@ export async function performBump(bumpInfo: BumpInfo, options: BeachballOptions)
await callHook('prebump', bumpInfo, options);

writePackageJson(modifiedPackages, packageInfos);
updatePackageLock(options.path);

if (options.generateChangelog) {
// Generate changelog
Expand Down
3 changes: 2 additions & 1 deletion src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getPackageInfos } from '../monorepo/getPackageInfos';
import { listPackageVersionsByTag } from '../packageManager/listPackageVersions';
import semver from 'semver';
import { setDependentVersions } from '../bump/setDependentVersions';
import { writePackageJson } from '../bump/performBump';
import { writePackageJson, updatePackageLock } from '../bump/performBump';

export async function sync(options: BeachballOptions) {
const packageInfos = getPackageInfos(options.path);
Expand Down Expand Up @@ -40,4 +40,5 @@ export async function sync(options: BeachballOptions) {
Object.keys(dependentModifiedPackages).forEach(pkg => modifiedPackages.add(pkg));

writePackageJson(modifiedPackages, packageInfos);
updatePackageLock(options.path);
}

0 comments on commit 4f3cd13

Please sign in to comment.