Skip to content

Commit f35a1a9

Browse files
committed
npm publish tarballs
1 parent 1f542f7 commit f35a1a9

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

.ado/azure-pipelines.publish.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ extends:
118118
yarn config set npmPublishAccess public
119119
yarn config set npmPublishRegistry "https://registry.npmjs.org"
120120
yarn config set npmAuthToken $(npmAuth)
121-
displayName: 'Configure yarn for npm publishing'
121+
npm config set //registry.npmjs.org/:_authToken $(npmAuth)
122+
displayName: 'Configure npm publishing auth'
122123
123124
- script: |
124125
# https://github.com/changesets/changesets/issues/432
@@ -130,7 +131,8 @@ extends:
130131
yarn config unset npmPublishAccess
131132
yarn config unset npmAuthToken
132133
yarn config unset npmPublishRegistry
133-
displayName: 'Cleanup yarn npm config'
134+
npm config delete //registry.npmjs.org/:_authToken
135+
displayName: 'Cleanup npm publishing auth'
134136
condition: always()
135137
136138
- script: |

.github/workflows/pr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ jobs:
320320
- name: Build packages
321321
run: yarn build
322322

323+
- name: Pack packages
324+
run: yarn lage pack --verbose --grouped
325+
323326
- name: Simulate publish
324327
run: yarn lage publish-dry-run --verbose --grouped
325328

lage.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const config = {
6060
type: 'worker',
6161
options: {
6262
worker: 'scripts/src/worker/publish.mts',
63+
outputDir: '_packed',
6364
},
6465
cache: false,
6566
},
@@ -68,6 +69,7 @@ const config = {
6869
type: 'worker',
6970
options: {
7071
worker: 'scripts/src/worker/publish.mts',
72+
outputDir: '_packed',
7173
dryRun: true,
7274
},
7375
cache: false,

scripts/src/worker/publish.mts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
import type { WorkerRunnerFunction } from 'lage';
22

33
import { $, fs } from 'zx';
4-
import { join } from 'node:path';
4+
import { join, resolve } from 'node:path';
5+
6+
/**
7+
* Find the tarball for a package in the output directory.
8+
* Returns the full path if found, undefined otherwise.
9+
*/
10+
async function findTarball(pkg: { name: string; version: string }, outputDir: string): Promise<string | undefined> {
11+
const stagingDir = resolve(outputDir);
12+
const safeName = (pkg.name as string).replace(/@/g, '').replace(/\//g, '-');
13+
const tgzFilename = `${safeName}-${pkg.version}.tgz`;
14+
const tgzPath = join(stagingDir, tgzFilename);
15+
16+
if (await fs.pathExists(tgzPath)) {
17+
return tgzPath;
18+
}
19+
20+
// Fallback: match by package name prefix in case version format differs
21+
if (await fs.pathExists(stagingDir)) {
22+
const files = await fs.readdir(stagingDir);
23+
const match = files.find((f: string) => f.startsWith(`${safeName}-`) && f.endsWith('.tgz'));
24+
if (match) {
25+
return join(stagingDir, match);
26+
}
27+
}
28+
29+
return undefined;
30+
}
531

632
export const run: WorkerRunnerFunction = async ({ target }) => {
733
const pkg = await fs.readJson(join(target.cwd, 'package.json'));
@@ -11,7 +37,18 @@ export const run: WorkerRunnerFunction = async ({ target }) => {
1137
}
1238

1339
const dryRun = target.options?.dryRun ?? false;
14-
const args = ['--tolerate-republish', ...(dryRun ? ['--dry-run'] : [])];
40+
const outputDir = target.options?.outputDir as string | undefined;
1541

16-
await $({ cwd: target.cwd, verbose: true })`yarn npm publish ${args}`;
42+
// If an outputDir is configured, look for a pre-packed tarball
43+
const tarball = outputDir ? await findTarball(pkg, outputDir) : undefined;
44+
45+
if (tarball) {
46+
// yarn npm publish doesn't support tarballs, so use npm directly
47+
const args = ['publish', tarball, '--access', 'public', ...(dryRun ? ['--dry-run'] : [])];
48+
await $({ cwd: target.cwd, verbose: true })`npm ${args}`;
49+
} else {
50+
// No tarball found — publish from source (local dev / dry-run)
51+
const args = ['--tolerate-republish', ...(dryRun ? ['--dry-run'] : [])];
52+
await $({ cwd: target.cwd, verbose: true })`yarn npm publish ${args}`;
53+
}
1754
};

0 commit comments

Comments
 (0)