11import type { WorkerRunnerFunction } from 'lage' ;
22
33import { $ , 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
632export 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