/
add-track-json.ts
83 lines (73 loc) · 2.49 KB
/
add-track-json.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
import { flags } from '@oclif/command'
import { promises as fsPromises } from 'fs'
import JBrowseCommand, { Config } from '../base'
export default class AddTrackJson extends JBrowseCommand {
// @ts-ignore
target: string
static description =
'Add a track configuration directly from a JSON hunk to the JBrowse 2 configuration'
static examples = [
'$ jbrowse add-track-json track.json',
'$ jbrowse add-track-json track.json --update',
]
static args = [
{
name: 'track',
required: true,
description: `track JSON file or command line arg blob`,
},
]
static flags = {
update: flags.boolean({
char: 'u',
description: `update the contents of an existing track, matched based on trackId`,
}),
target: flags.string({
description:
'path to config file in JB2 installation directory to write out to.\nCreates ./config.json if nonexistent',
}),
out: flags.string({
description: 'synonym for target',
}),
}
async run() {
const { args, flags: runFlags } = this.parse(AddTrackJson)
const output = runFlags.target || runFlags.out || '.'
const isDir = (await fsPromises.lstat(output)).isDirectory()
this.target = isDir ? `${output}/config.json` : output
const { track: inputtedTrack } = args as { track: string }
this.debug(`Sequence location is: ${inputtedTrack}`)
const { update } = runFlags
const config: Config = await this.readJsonFile(this.target)
this.debug(`Found existing config file ${this.target}`)
const track = await this.readInlineOrFileJson(inputtedTrack)
if (!config.tracks) {
config.tracks = []
}
const idx = config.tracks.findIndex(
({ trackId }: { trackId: string }) => trackId === track.trackId,
)
if (idx !== -1) {
const existing = config.tracks[idx].name
this.debug(`Found existing track ${existing} in configuration`)
if (update) {
this.debug(`Overwriting track ${existing} in configuration`)
config.tracks[idx] = track
} else {
this.error(
`Cannot add track ${track.name}, a track with that trackId already exists: ${existing}`,
{ exit: 160 },
)
}
} else {
config.tracks.push(track)
}
this.debug(`Writing configuration to file ${this.target}`)
await this.writeJsonFile(this.target, config)
this.log(
`${idx !== -1 ? 'Overwrote' : 'Added'} assembly "${track.name}" ${
idx !== -1 ? 'in' : 'to'
} ${this.target}`,
)
}
}