Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update linked packages in package-lock.json #1844

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/plugins/node-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import {BranchName} from '../util/branch-name';
import {jsonStringify} from '../util/json-stringify';
import {Changelog} from '../updaters/changelog';
import {
WorkspacePlugin,
addPath,
appendDependenciesSectionToChangelog,
DependencyGraph,
DependencyNode,
WorkspacePlugin,
WorkspacePluginOptions,
appendDependenciesSectionToChangelog,
addPath,
} from './workspace';
import {PatchVersionUpdate} from '../versioning-strategy';
import {PackageLockJson} from '../updaters/node/package-lock-json';

class Package extends LernaPackage {
constructor(
Expand Down Expand Up @@ -65,6 +66,7 @@ interface NodeWorkspaceOptions extends WorkspacePluginOptions {
export class NodeWorkspace extends WorkspacePlugin<Package> {
private alwaysLinkLocal: boolean;
private packageGraph?: PackageGraph;

constructor(
github: GitHub,
targetBranch: string,
Expand All @@ -74,6 +76,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
super(github, targetBranch, repositoryConfig, options);
this.alwaysLinkLocal = options.alwaysLinkLocal === false ? false : true;
}

protected async buildAllPackages(
candidates: CandidateReleasePullRequest[]
): Promise<{
Expand Down Expand Up @@ -201,6 +204,13 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
this.logger
);
}
} else if (
update.path === addPath(existingCandidate.path, 'package-lock.json')
) {
update.updater = new PackageLockJson({
version: Version.parse(updatedPackage.version),
versionsMap: updatedVersions,
});
}
return update;
});
Expand Down Expand Up @@ -228,6 +238,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
}
return existingCandidate;
}

protected newCandidate(
pkg: Package,
updatedVersions: VersionsMap
Expand Down Expand Up @@ -300,6 +311,14 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
),
}),
},
{
path: addPath(updatedPackage.location, 'package-lock.json'),
createIfMissing: false,
updater: new PackageLockJson({
version: version,
versionsMap: updatedVersions,
}),
},
],
labels: [],
headRefName: BranchName.ofTargetBranch(this.targetBranch).toString(),
Expand All @@ -320,6 +339,18 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
_updatedVersions: VersionsMap
): CandidateReleasePullRequest[] {
// NOP for node workspaces

// not sure about this
// candidates.forEach(c => {
// c.pullRequest.updates.push({
// path: 'package-lock.json',
// updater: new PackageLockJson({
// version: c.pullRequest.version!,
// versionsMap: _updatedVersions,
// }),
// createIfMissing: false,
// });
// });
return candidates;
}

Expand Down
2 changes: 2 additions & 0 deletions src/strategies/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Node extends BaseStrategy {
): Promise<Update[]> {
const updates: Update[] = [];
const version = options.newVersion;
const versionsMap = options.versionsMap;
const packageName = (await this.getPackageName()) ?? '';
const lockFiles = ['package-lock.json', 'npm-shrinkwrap.json'];
lockFiles.forEach(lockFile => {
Expand All @@ -39,6 +40,7 @@ export class Node extends BaseStrategy {
createIfMissing: false,
updater: new PackageLockJson({
version,
versionsMap,
}),
});
});
Expand Down
15 changes: 14 additions & 1 deletion src/updaters/node/package-lock-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {DefaultUpdater} from '../default';
type LockFileV2 = {
version: string;
lockfileVersion?: number;
packages: Record<string, {version: string}>;
packages: Record<string, {version: string; name: string}>;
};

/**
Expand All @@ -33,6 +33,19 @@ export class PackageLockJson extends DefaultUpdater {
parsed.version = this.version.toString();
if (parsed.lockfileVersion === 2) {
parsed.packages[''].version = this.version.toString();
this.versionsMap?.forEach((version, linkedPackage) => {
logger.info(
`updating from ${
parsed.packages[linkedPackage].version
} to ${version.toString()}`
);
const packageKey = Object.keys(parsed.packages).find(
p => parsed.packages[p].name === linkedPackage
);
if (!packageKey)
throw Error(`${packageKey} not found in ${parsed.packages}`);
parsed.packages[packageKey].version = version.toString();
});
}
return jsonStringify(parsed, content);
}
Expand Down