Skip to content

Commit 337d114

Browse files
Better npm publish config #2
1 parent a9326d7 commit 337d114

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

scripts/publish-npm.mjs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { execSync } from "node:child_process";
2-
import { readFileSync } from "node:fs";
2+
import { readFileSync, writeFileSync } from "node:fs";
33
import { join } from "node:path";
44

55
const ref = process.env.GITHUB_REF || "";
@@ -38,9 +38,14 @@ const fileExistsOnNpm = (pkgName, version) => {
3838
}
3939
};
4040

41-
const getBaseVersion = (pkgPath) => {
41+
const readPackageJson = (pkgPath) => {
4242
const raw = readFileSync(join(pkgPath, "package.json"), "utf8");
43-
return JSON.parse(raw).version;
43+
return JSON.parse(raw);
44+
};
45+
46+
const writePackageJson = (pkgPath, data) => {
47+
const raw = `${JSON.stringify(data, null, 2)}\n`;
48+
writeFileSync(join(pkgPath, "package.json"), raw);
4449
};
4550

4651
const EMPTY_SHA = "0000000000000000000000000000000000000000";
@@ -107,7 +112,8 @@ const publishPackage = ({ path, name }, shouldPublish) => {
107112
return;
108113
}
109114

110-
const baseVersion = getBaseVersion(path);
115+
const pkgJson = readPackageJson(path);
116+
const baseVersion = pkgJson.version;
111117
let targetVersion = baseVersion;
112118
let distTag = "latest";
113119

@@ -134,13 +140,17 @@ const publishPackage = ({ path, name }, shouldPublish) => {
134140
npm_config_legacy_peer_deps: "true",
135141
npm_config_ignore_workspace_root_check: "true",
136142
};
137-
execSync(`npm version --no-git-tag-version ${targetVersion}`, { cwd: path });
138-
execSync(`npm publish --access public --tag ${distTag}`, {
139-
cwd: path,
140-
stdio: "inherit",
141-
env: npmEnv,
142-
});
143-
execSync(`git checkout -- package.json`, { cwd: path });
143+
pkgJson.version = targetVersion;
144+
writePackageJson(path, pkgJson);
145+
try {
146+
execSync(`npm publish --access public --tag ${distTag}`, {
147+
cwd: path,
148+
stdio: "inherit",
149+
env: npmEnv,
150+
});
151+
} finally {
152+
execSync(`git checkout -- package.json`, { cwd: path });
153+
}
144154
};
145155

146156
const changedPaths = getChangedPaths();

0 commit comments

Comments
 (0)