Skip to content

Commit

Permalink
Create temporary lerna.json if it doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
  • Loading branch information
t1m0thyj committed Feb 2, 2024
1 parent d88f823 commit 0345062
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion dist/changelog.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 25 additions & 7 deletions dist/lerna.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/changelog/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ function getPackageChangelog(context: IContext, changelogFile: string, headerLin
}

function updatePackageChangelog(context: IContext, changelogFile: string, headerLine: string): boolean {
if (context.version.new !== context.version.old && fs.existsSync(changelogFile)) {
if (fs.existsSync(changelogFile)) {
const oldContents = fs.readFileSync(changelogFile, "utf-8");
const newVersion = context.version.overrides[path.dirname(changelogFile)]?.new || context.version.new;
const newContents = oldContents.replace(headerLine, `## \`${newVersion}\``);
if (oldContents.includes(`## \`${newVersion}\``)) {
return false;
}

const newContents = oldContents.replace(headerLine, `## \`${newVersion}\``);
if (newContents !== oldContents) {
fs.writeFileSync(changelogFile, newContents);
context.logger.info(`Updated version header in ${changelogFile}`)
Expand Down
3 changes: 3 additions & 0 deletions packages/lerna/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { IPluginConfig as NpmPluginConfig } from "@octorelease/npm";

export const IS_LERNA_JSON_TEMP = Symbol();

export type IPluginConfig = NpmPluginConfig & {
pruneShrinkwrap?: boolean | string[];
versionIndependent?: string[];
[IS_LERNA_JSON_TEMP]?: boolean;
};
24 changes: 17 additions & 7 deletions packages/lerna/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import * as fs from "fs";
import * as path from "path";
import { IContext } from "@octorelease/core";
import { DEFAULT_NPM_REGISTRY, utils as npmUtils } from "@octorelease/npm";
import { IPluginConfig } from "./config";
import { IPluginConfig, IS_LERNA_JSON_TEMP } from "./config";
import * as utils from "./utils";

export default async function (context: IContext, config: IPluginConfig): Promise<void> {
let publishConfig;
try {
const lernaJson = JSON.parse(fs.readFileSync("lerna.json", "utf-8"));
context.version.new = lernaJson.version;
publishConfig = lernaJson.command?.publish;
} catch {
throw new Error(`Missing or invalid lerna.json in branch ${context.branch.name}`);
config[IS_LERNA_JSON_TEMP] = !fs.existsSync("lerna.json");
if (!config[IS_LERNA_JSON_TEMP]) {
try {
const lernaJson = JSON.parse(fs.readFileSync("lerna.json", "utf-8"));
context.version.new = lernaJson.version;
publishConfig = lernaJson.command?.publish;
} catch {
throw new Error(`Missing or invalid lerna.json in branch ${context.branch.name}`);
}
}

try {
Expand All @@ -37,6 +40,13 @@ export default async function (context: IContext, config: IPluginConfig): Promis
if (publishConfig == null) {
publishConfig = packageJson.publishConfig;
}
if (config[IS_LERNA_JSON_TEMP]) {
context.version.new = packageJson.version;
fs.writeFileSync("lerna.json", JSON.stringify({
version: packageJson.version,
useWorkspaces: true
}, null, 2));
}
} catch {
throw new Error(`Missing or invalid package.json in branch ${context.branch.name}`);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/lerna/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import findUp from "find-up";
import * as glob from "@actions/glob";
import { IContext } from "@octorelease/core";
import { utils as npmUtils } from "@octorelease/npm";
import { IPluginConfig } from "./config";
import { IPluginConfig, IS_LERNA_JSON_TEMP } from "./config";
import * as utils from "./utils";

export default async function (context: IContext, config: IPluginConfig): Promise<void> {
Expand All @@ -47,7 +47,10 @@ export default async function (context: IContext, config: IPluginConfig): Promis
}

await utils.lernaPostVersion(); // Update lockfile because lerna doesn't
context.changedFiles.push("lerna.json", "package.json");
context.changedFiles.push("package.json");
if (!config[IS_LERNA_JSON_TEMP]) {
context.changedFiles.push("lerna.json");
}
const lockfilePath = await findUp(["pnpm-lock.yaml", "yarn.lock", "npm-shrinkwrap.json", "package-lock.json"]);
if (lockfilePath != null) {
context.changedFiles.push(path.relative(context.rootDir, lockfilePath));
Expand Down

0 comments on commit 0345062

Please sign in to comment.