Skip to content

Commit 4ebea6f

Browse files
hextrazaSebastian Benjamin
andauthored
Set built JBrowse CLI version to the @jbrowse/core version (#370)
* Set built JBrowse CLI version to the @jbrowse/core version * Floor version to 3.6.0 --------- Co-authored-by: Sebastian Benjamin <sebastiancbenjamin@gmail.com>
1 parent b79ea18 commit 4ebea6f

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

jbrowse/resources/external/fetch-jbrowse-bundle.mjs

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
11
import { execSync } from 'node:child_process';
2-
import { existsSync, mkdirSync, copyFileSync, rmSync } from 'node:fs';
2+
import { existsSync, mkdirSync, copyFileSync, rmSync, readFileSync } from 'node:fs';
33
import { join, resolve } from 'node:path';
44

55
const ROOT = resolve('.');
66
const BUILD = join(ROOT, 'buildCli');
77
const OUTDIR = join(ROOT, 'resources', 'external');
88
const 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+
29+
function semverLt(a, b) {
30+
const pa = String(a).match(/(\d+)\.(\d+)\.(\d+)/);
31+
const pb = String(b).match(/(\d+)\.(\d+)\.(\d+)/);
32+
if (!pa || !pb) return false;
33+
for (let i = 1; i <= 3; i++) {
34+
const da = Number(pa[i]);
35+
const db = Number(pb[i]);
36+
if (da !== db) return da < db;
37+
}
38+
return false;
39+
}
40+
41+
function floorVersion(spec, min) {
42+
const m = String(spec).match(/^(\^|~|>=)?\s*(\d+\.\d+\.\d+)(.*)$/);
43+
if (!m) return spec;
44+
const [, op = '', v, rest = ''] = m;
45+
return semverLt(v, min) ? `${op}${min}${rest}` : spec;
46+
}
47+
1048
async function extractTgz(tgzPath, cwd) {
1149
try {
1250
const mod = await import('tar').catch(() => null);
@@ -20,32 +58,46 @@ async function extractTgz(tgzPath, cwd) {
2058
execSync(`tar -xzf "${tgzPath}" -C "${cwd}"`, { stdio: 'inherit', shell: true });
2159
}
2260

61+
function findCliEntrypoint(buildDir) {
62+
const candidates = [
63+
join(buildDir, 'package', 'bundle', 'index.js'),
64+
join(buildDir, 'package', 'lib', 'index.js')
65+
];
66+
67+
for (const p of candidates) {
68+
if (existsSync(p)) return p;
69+
}
70+
71+
throw new Error(
72+
`Could not find @jbrowse/cli entrypoint. Tried:\n` +
73+
candidates.map(c => ` - ${c}`).join('\n')
74+
);
75+
}
76+
2377
async function main() {
2478
if (existsSync(BUILD)) rmSync(BUILD, { recursive: true, force: true });
2579
mkdirSync(BUILD, { recursive: true });
2680
mkdirSync(OUTDIR, { recursive: true });
2781

28-
console.log('Packing @jbrowse/cli (latest)…');
29-
const out = execSync('npm pack @jbrowse/cli', {
82+
const coreVersion = getResolvedCoreVersion();
83+
const cliSpec = floorVersion(process.env.JBROWSE_CLI_VERSION || coreVersion, '3.6.0');
84+
85+
console.log(`Packing @jbrowse/cli@${cliSpec} (core resolved: ${coreVersion})…`);
86+
const out = execSync(`npm pack @jbrowse/cli@${cliSpec}`, {
3087
cwd: BUILD,
3188
stdio: ['ignore', 'pipe', 'inherit'],
3289
shell: true,
33-
})
34-
.toString()
35-
.trim();
90+
}).toString().trim();
3691

3792
const tgz = join(BUILD, out);
3893
console.log(`Downloaded: ${tgz}`);
3994

4095
console.log('Extracting tarball…');
4196
await extractTgz(tgz, BUILD);
4297

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-
}
98+
const entry = findCliEntrypoint(BUILD);
4799

48-
copyFileSync(bundled, OUTFILE);
100+
copyFileSync(entry, OUTFILE);
49101
console.log(`Copied bundle to ${OUTFILE}`);
50102

51103
rmSync(BUILD, { recursive: true, force: true });

0 commit comments

Comments
 (0)