From 6c799b75319330ba33ad738ed5fa9fd19a17badc Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 25 Nov 2023 10:11:04 -0500 Subject: [PATCH] Misc refactor --- .../jbrowse-cli/src/commands/add-track.ts | 94 ++++++++++++------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/products/jbrowse-cli/src/commands/add-track.ts b/products/jbrowse-cli/src/commands/add-track.ts index d429027fc9..0dc2031f4c 100644 --- a/products/jbrowse-cli/src/commands/add-track.ts +++ b/products/jbrowse-cli/src/commands/add-track.ts @@ -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 @@ -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', @@ -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) } @@ -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`) @@ -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, + }), + }), ), ) }