Skip to content

Commit a14f267

Browse files
authored
use stable dist tag when publishing (#11012)
1 parent 703e9c9 commit a14f267

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cli/cli.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,38 @@ async function ciAsync() {
444444

445445
lintJSONInDirectory(path.resolve("."));
446446
lintJSONInDirectory(path.resolve("docs"));
447+
let pkg = readJson("package.json")
447448

448-
function npmPublishAsync() {
449+
async function npmPublishAsync() {
449450
if (!npmPublish) return Promise.resolve();
451+
452+
let latest: pxt.semver.Version;
453+
454+
try {
455+
const version = await nodeutil.npmLatestVersionAsync(pkg["name"]);
456+
latest = pxt.semver.parse(version);
457+
}
458+
catch (e) {
459+
// no latest tag
460+
}
461+
462+
let distTag: string;
463+
464+
if (latest) {
465+
const current = pxt.semver.parse(pkg["version"]);
466+
467+
if (pxt.semver.cmp(current, latest) < 0) {
468+
distTag = `stable${current.major}.${current.minor}`;
469+
}
470+
}
471+
472+
if (distTag) {
473+
return nodeutil.runNpmAsync("publish", "--tag", distTag);
474+
}
475+
450476
return nodeutil.runNpmAsync("publish");
451477
}
452478

453-
let pkg = readJson("package.json")
454479
if (pkg["name"] == "pxt-core") {
455480
pxt.log("pxt-core build");
456481

cli/nodeutil.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ export function runNpmAsync(...args: string[]) {
114114
return runNpmAsyncWithCwd(".", ...args);
115115
}
116116

117+
export async function npmLatestVersionAsync(packageName: string) {
118+
const output = await spawnWithPipeAsync({
119+
cmd: addCmd("npm"),
120+
args: ["view", packageName, "dist-tags.latest"],
121+
cwd: ".",
122+
});
123+
124+
return output.toString("utf8").trim()
125+
}
126+
117127
export interface NpmRegistry {
118128
_id: string;
119129
_name: string;

0 commit comments

Comments
 (0)