-
Notifications
You must be signed in to change notification settings - Fork 60
/
upgrade.ts
124 lines (111 loc) · 3.73 KB
/
upgrade.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Args, Flags } from '@oclif/core'
import { rimrafSync } from 'rimraf'
import fs from 'fs'
import path from 'path'
import decompress from 'decompress'
import fetch from '../fetchWithProxy'
import JBrowseCommand from '../base'
export default class Upgrade extends JBrowseCommand {
static description = 'Upgrades JBrowse 2 to latest version'
static examples = [
'# Upgrades current directory to latest jbrowse release',
'$ jbrowse upgrade',
'',
'# Upgrade jbrowse instance at a specific filesystem path',
'$ jbrowse upgrade /path/to/jbrowse2/installation',
'',
'# Upgrade to a specific tag',
'$ jbrowse upgrade /path/to/jbrowse2/installation --tag v1.0.0',
'',
'# List versions available on github',
'$ jbrowse upgrade --listVersions',
'',
'# Upgrade from a specific URL',
'$ jbrowse upgrade --url https://sample.com/jbrowse2.zip',
'',
'# Get nightly release from main branch',
'$ jbrowse upgrade --nightly',
]
static args = {
localPath: Args.string({
required: false,
description: `Location where JBrowse 2 is installed`,
default: '.',
}),
}
static flags = {
help: Flags.help({ char: 'h' }),
// will need to account for pagenation once there is a lot of releases
listVersions: Flags.boolean({
char: 'l',
description: 'Lists out all versions of JBrowse 2',
}),
tag: Flags.string({
char: 't',
description:
'Version of JBrowse 2 to install. Format is v1.0.0.\nDefaults to latest',
}),
branch: Flags.string({
description: 'Download a development build from a named git branch',
}),
nightly: Flags.boolean({
description: 'Download the latest development build from the main branch',
}),
clean: Flags.boolean({
description: 'Removes old js,map,and LICENSE files in the installation',
}),
url: Flags.string({
char: 'u',
description: 'A direct URL to a JBrowse 2 release',
}),
}
async run() {
const { args: runArgs, flags: runFlags } = await this.parse(Upgrade)
const { localPath: argsPath } = runArgs as { localPath: string }
const { clean, listVersions, tag, url, branch, nightly } = runFlags
if (listVersions) {
const versions = (await this.fetchGithubVersions()).map(v => v.tag_name)
this.log(`All JBrowse versions:\n${versions.join('\n')}`)
this.exit()
}
this.debug(`Want to upgrade at: ${argsPath}`)
if (!argsPath) {
this.error(`No directory supplied`, { exit: 100 })
}
if (!fs.existsSync(path.join(argsPath, 'manifest.json'))) {
this.error(
`No manifest.json found in this directory, are you sure it is an
existing jbrowse 2 installation?`,
{ exit: 10 },
)
}
const locationUrl =
url ||
(nightly ? await this.getBranch('main') : '') ||
(branch ? await this.getBranch(branch) : '') ||
(tag ? await this.getTag(tag) : await this.getLatest())
this.log(`Fetching ${locationUrl}...`)
const response = await fetch(locationUrl)
if (!response.ok) {
this.error(`Failed to fetch: ${response.statusText}`, { exit: 100 })
}
const type = response.headers.get('content-type')
if (
url &&
type !== 'application/zip' &&
type !== 'application/octet-stream'
) {
this.error(
'The URL provided does not seem to be a JBrowse installation URL',
)
}
if (clean) {
rimrafSync(path.join(argsPath, 'static'))
fs.readdirSync(argsPath)
.filter(f => f.includes('worker.js'))
.forEach(f => fs.unlinkSync(path.join(argsPath, f)))
}
await decompress(Buffer.from(await response.arrayBuffer()), argsPath)
this.log(`Unpacked ${locationUrl} at ${argsPath}`)
}
}