Skip to content

Commit 8f3d6f9

Browse files
feat: add npm version hook
1 parent d2f8924 commit 8f3d6f9

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function verifyConditions(pluginConfig, context) {
1616
// If the npm publish plugin is used and has `npmPublish`, `tarballDir` or `pkgRoot` configured, validate them now in order to prevent any release if the configuration is wrong
1717
if (context.options.publish) {
1818
const publishPlugin =
19-
castArray(context.options.publish).find((config) => config.path && config.path === "@semantic-release/npm") || {};
19+
castArray(context.options.publish).find((config) => config.path && config.path === "semantic-release-monorepo-npm-plugin") || {};
2020

2121
pluginConfig.npmPublish = defaultTo(pluginConfig.npmPublish, publishPlugin.npmPublish);
2222
pluginConfig.tarballDir = defaultTo(pluginConfig.tarballDir, publishPlugin.tarballDir);

lib/prepare.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import { move } from "fs-extra";
33
import { execa } from "execa";
4+
import { readFile, writeFile } from 'fs/promises';
45

56
export default async function (
67
npmrc,
@@ -9,21 +10,40 @@ export default async function (
910
) {
1011
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
1112

12-
logger.log("Write version %s to package.json in %s", version, basePath);
1313

14-
const versionResult = execa(
15-
"npm",
16-
["version", version, "--userconfig", npmrc, "--no-git-tag-version", "--allow-same-version"],
17-
{
18-
cwd: basePath,
19-
env,
20-
preferLocal: true,
21-
}
22-
);
23-
versionResult.stdout.pipe(stdout, { end: false });
24-
versionResult.stderr.pipe(stderr, { end: false });
14+
// Directly modify package.json file to avoid npm version command issues
15+
try {
16+
logger.log("Directly modifying package.json file, setting version to: %s", version);
17+
const packageJsonPath = path.join(basePath, 'package.json');
18+
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8'));
19+
packageJson.version = version;
20+
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n', 'utf8');
21+
logger.log("Successfully updated package.json version to %s", version);
22+
} catch (error) {
23+
logger.error("Direct package.json modification failed:", error.message);
24+
25+
// If direct modification fails, try using npm version command
26+
logger.log("Trying npm version command as fallback...");
27+
try {
28+
const versionResult = execa(
29+
"npm",
30+
["version", version, "--userconfig", npmrc, "--no-git-tag-version", "--allow-same-version"],
31+
{
32+
cwd: basePath,
33+
env,
34+
preferLocal: true,
35+
}
36+
);
37+
versionResult.stdout.pipe(stdout, { end: false });
38+
versionResult.stderr.pipe(stderr, { end: false });
2539

26-
await versionResult;
40+
await versionResult;
41+
logger.log("npm version command executed successfully");
42+
} catch (npmError) {
43+
logger.error("npm version command also failed:", npmError.message);
44+
throw error; // Throw the original package.json modification error
45+
}
46+
}
2747

2848
if (tarballDir) {
2949
logger.log("Creating npm package version %s", version);

0 commit comments

Comments
 (0)