11import { execSync } from 'node:child_process' ;
2- import { existsSync , mkdirSync , copyFileSync , rmSync } from 'node:fs' ;
2+ import { existsSync , mkdirSync , copyFileSync , rmSync , readFileSync } from 'node:fs' ;
33import { join , resolve } from 'node:path' ;
44
55const ROOT = resolve ( '.' ) ;
66const BUILD = join ( ROOT , 'buildCli' ) ;
77const OUTDIR = join ( ROOT , 'resources' , 'external' ) ;
88const OUTFILE = join ( OUTDIR , 'jbrowse.js' ) ;
99
10+ function getResolvedCoreVersion ( ) {
11+ // Prefer lockfile, fallback to node_modules
12+ const lockPath = join ( ROOT , 'package-lock.json' ) ;
13+ if ( existsSync ( lockPath ) ) {
14+ const lock = JSON . parse ( readFileSync ( lockPath , 'utf8' ) ) ;
15+ const v =
16+ lock ?. packages ?. [ 'node_modules/@jbrowse/core' ] ?. version ||
17+ lock ?. dependencies ?. [ '@jbrowse/core' ] ?. version ;
18+ if ( v ) return v ;
19+ }
20+
21+ const corePkg = join ( ROOT , 'node_modules' , '@jbrowse' , 'core' , 'package.json' ) ;
22+ if ( existsSync ( corePkg ) ) {
23+ return JSON . parse ( readFileSync ( corePkg , 'utf8' ) ) . version ;
24+ }
25+
26+ throw new Error ( 'Could not determine resolved @jbrowse/core version (no lockfile entry and no node_modules).' ) ;
27+ }
28+
1029async function extractTgz ( tgzPath , cwd ) {
1130 try {
1231 const mod = await import ( 'tar' ) . catch ( ( ) => null ) ;
@@ -20,32 +39,46 @@ async function extractTgz(tgzPath, cwd) {
2039 execSync ( `tar -xzf "${ tgzPath } " -C "${ cwd } "` , { stdio : 'inherit' , shell : true } ) ;
2140}
2241
42+ function findCliEntrypoint ( buildDir ) {
43+ const candidates = [
44+ join ( buildDir , 'package' , 'bundle' , 'index.js' ) ,
45+ join ( buildDir , 'package' , 'lib' , 'index.js' )
46+ ] ;
47+
48+ for ( const p of candidates ) {
49+ if ( existsSync ( p ) ) return p ;
50+ }
51+
52+ throw new Error (
53+ `Could not find @jbrowse/cli entrypoint. Tried:\n` +
54+ candidates . map ( c => ` - ${ c } ` ) . join ( '\n' )
55+ ) ;
56+ }
57+
2358async function main ( ) {
2459 if ( existsSync ( BUILD ) ) rmSync ( BUILD , { recursive : true , force : true } ) ;
2560 mkdirSync ( BUILD , { recursive : true } ) ;
2661 mkdirSync ( OUTDIR , { recursive : true } ) ;
2762
28- console . log ( 'Packing @jbrowse/cli (latest)…' ) ;
29- const out = execSync ( 'npm pack @jbrowse/cli' , {
63+ const coreVersion = getResolvedCoreVersion ( ) ;
64+ const cliSpec = process . env . JBROWSE_CLI_VERSION || coreVersion ;
65+
66+ console . log ( `Packing @jbrowse/cli@${ cliSpec } (core resolved: ${ coreVersion } )…` ) ;
67+ const out = execSync ( `npm pack @jbrowse/cli@${ cliSpec } ` , {
3068 cwd : BUILD ,
3169 stdio : [ 'ignore' , 'pipe' , 'inherit' ] ,
3270 shell : true ,
33- } )
34- . toString ( )
35- . trim ( ) ;
71+ } ) . toString ( ) . trim ( ) ;
3672
3773 const tgz = join ( BUILD , out ) ;
3874 console . log ( `Downloaded: ${ tgz } ` ) ;
3975
4076 console . log ( 'Extracting tarball…' ) ;
4177 await extractTgz ( tgz , BUILD ) ;
4278
43- const bundled = join ( BUILD , 'package' , 'bundle' , 'index.js' ) ;
44- if ( ! existsSync ( bundled ) ) {
45- throw new Error ( `bundle/index.js not found at ${ bundled } ` ) ;
46- }
79+ const entry = findCliEntrypoint ( BUILD ) ;
4780
48- copyFileSync ( bundled , OUTFILE ) ;
81+ copyFileSync ( entry , OUTFILE ) ;
4982 console . log ( `Copied bundle to ${ OUTFILE } ` ) ;
5083
5184 rmSync ( BUILD , { recursive : true , force : true } ) ;
0 commit comments