Skip to content

Commit

Permalink
Misc refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 25, 2023
1 parent 8bdf6a9 commit 6c799b7
Showing 1 changed file with 62 additions and 32 deletions.
94 changes: 62 additions & 32 deletions products/jbrowse-cli/src/commands/add-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,53 @@ function makeLocationProtocol(protocol: string) {
}
}

function fileOperation({
srcFilename,
destFilename,
mode,
}: {
srcFilename: string
destFilename: string
mode: string
}) {
if (mode === 'copy') {
return copyFile(srcFilename, destFilename, COPYFILE_EXCL)
} else if (mode === 'move') {
return rename(srcFilename, destFilename)
} else if (mode === 'symlink') {
return symlink(path.resolve(srcFilename), destFilename)
}
return undefined
}

// get path of destination, and remove file at that path if it exists and force
// is set
function destinationFn({
destinationDir,
srcFilename,
subDir,
force,
}: {
destinationDir: string
srcFilename: string
subDir: string
force: boolean
}) {
const dest = path.resolve(
path.join(destinationDir, subDir, path.basename(srcFilename)),
)
if (force) {
try {
fs.unlinkSync(dest)
} catch (e) {
/* unconditionally unlinkSync, due to
* https://github.com/nodejs/node/issues/14025#issuecomment-754021370
* and https://github.com/GMOD/jbrowse-components/issues/2768 */
}
}
return dest
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Track = Record<string, any>

Expand Down Expand Up @@ -188,7 +235,7 @@ export default class AddTrack extends JBrowseCommand {

let { trackType, trackId, name, assemblyNames } = runFlags

const configDirectory = path.dirname(this.target)
const configDir = path.dirname(this.target)
if (!argsTrack) {
this.error(
'No track provided. Example usage: jbrowse add-track yourfile.bam',
Expand All @@ -197,7 +244,7 @@ export default class AddTrack extends JBrowseCommand {
}

if (subDir) {
const dir = path.join(configDirectory, subDir)
const dir = path.join(configDir, subDir)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
Expand Down Expand Up @@ -300,9 +347,7 @@ export default class AddTrack extends JBrowseCommand {
configContents.tracks = []
}

const idx = configContents.tracks.findIndex(
configTrack => configTrack.trackId === trackId,
)
const idx = configContents.tracks.findIndex(c => c.trackId === trackId)

if (idx !== -1) {
this.debug(`Found existing trackId ${trackId} in configuration`)
Expand All @@ -319,36 +364,21 @@ export default class AddTrack extends JBrowseCommand {
configContents.tracks.push(trackConfig)
}

// get path of destination, and remove file at that path if it exists and
// force is set
const destinationFn = (dir: string, file: string) => {
const dest = path.resolve(path.join(dir, subDir, path.basename(file)))
if (force) {
try {
fs.unlinkSync(dest)
} catch (e) {
/* unconditionally unlinkSync, due to
* https://github.com/nodejs/node/issues/14025#issuecomment-754021370
* and https://github.com/GMOD/jbrowse-components/issues/2768 */
}
}
return dest
}

const loadType =
(load as 'copy' | 'inPlace' | 'move' | 'symlink' | undefined) || 'inPlace'

const callbacks = {
copy: (src: string, dest: string) => copyFile(src, dest, COPYFILE_EXCL),
move: (src: string, dest: string) => rename(src, dest),
symlink: (src: string, dest: string) => symlink(path.resolve(src), dest),
}
if (loadType !== 'inPlace') {
if (load && load !== 'inPlace') {
await Promise.all(
Object.values(this.guessFileNames({ location, index, bed1, bed2 }))
.filter(f => !!f)
.map(src =>
callbacks[loadType](src, destinationFn(configDirectory, src)),
.map(srcFilename =>
fileOperation({
mode: load,
srcFilename,
destFilename: destinationFn({
destinationDir: configDir,
srcFilename,
force,
subDir,
}),
}),
),
)
}
Expand Down

0 comments on commit 6c799b7

Please sign in to comment.