Skip to content

Commit 6ba7c3d

Browse files
author
Sebastian Benjamin
committed
Add fallback for tar package
1 parent e0d2de5 commit 6ba7c3d

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed
Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
11
import { execSync } from 'node:child_process';
22
import { existsSync, mkdirSync, copyFileSync, rmSync } from 'node:fs';
33
import { join, resolve } from 'node:path';
4-
import tar from 'tar';
54

65
const ROOT = resolve('.');
76
const BUILD = join(ROOT, 'buildCli');
87
const OUTDIR = join(ROOT, 'resources', 'external');
98
const OUTFILE = join(OUTDIR, 'jbrowse.js');
109

11-
if (existsSync(BUILD)) rmSync(BUILD, { recursive: true, force: true });
12-
mkdirSync(BUILD, { recursive: true });
13-
mkdirSync(OUTDIR, { recursive: true });
10+
async function extractTgz(tgzPath, cwd) {
11+
try {
12+
const mod = await import('tar').catch(() => null);
13+
const tar = mod?.default ?? mod;
14+
if (tar?.x) {
15+
await tar.x({ file: tgzPath, cwd });
16+
return;
17+
}
18+
} catch {
19+
}
20+
execSync(`tar -xzf "${tgzPath}" -C "${cwd}"`, { stdio: 'inherit', shell: true });
21+
}
1422

15-
console.log('Packing @jbrowse/cli (latest)…');
16-
const out = execSync('npm pack @jbrowse/cli', { cwd: BUILD, stdio: ['ignore', 'pipe', 'inherit'] }).toString().trim();
23+
async function main() {
24+
if (existsSync(BUILD)) rmSync(BUILD, { recursive: true, force: true });
25+
mkdirSync(BUILD, { recursive: true });
26+
mkdirSync(OUTDIR, { recursive: true });
1727

18-
// npm outputs the filename on stdout, e.g. "jbrowse-cli-3.6.4.tgz"
19-
const tgz = join(BUILD, out);
20-
console.log(`Downloaded: ${tgz}`);
28+
console.log('Packing @jbrowse/cli (latest)…');
29+
const out = execSync('npm pack @jbrowse/cli', {
30+
cwd: BUILD,
31+
stdio: ['ignore', 'pipe', 'inherit'],
32+
shell: true,
33+
})
34+
.toString()
35+
.trim();
2136

22-
console.log('Extracting tarball…');
23-
await tar.x({ file: tgz, cwd: BUILD });
37+
const tgz = join(BUILD, out);
38+
console.log(`Downloaded: ${tgz}`);
2439

25-
const bundled = join(BUILD, 'package', 'bundle', 'index.js');
26-
if (!existsSync(bundled)) {
27-
throw new Error(`bundle/index.js not found at ${bundled}`);
28-
}
40+
console.log('Extracting tarball…');
41+
await extractTgz(tgz, BUILD);
2942

30-
copyFileSync(bundled, OUTFILE);
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+
}
3147

32-
rmSync(BUILD, { recursive: true, force: true });
48+
copyFileSync(bundled, OUTFILE);
49+
console.log(`Copied bundle to ${OUTFILE}`);
50+
51+
rmSync(BUILD, { recursive: true, force: true });
52+
}
3353

34-
console.log(`Copied bundle to ${OUTFILE}`);
54+
main().catch(err => {
55+
console.error(err);
56+
process.exit(1);
57+
});

0 commit comments

Comments
 (0)