|
| 1 | +/** @babel */ |
| 2 | + |
| 3 | +import path from 'path' |
| 4 | +import fs from 'fs' |
| 5 | +import child_process from 'child_process' |
| 6 | + |
| 7 | +export function home (...p) { |
| 8 | + const key = process.platform === 'win32' ? 'USERPROFILE' : 'HOME' |
| 9 | + return path.join(process.env[key], ...p) |
| 10 | +} |
| 11 | + |
| 12 | +export function juliaHome (...p) { |
| 13 | + let juliaHome = process.env.JULIA_HOME |
| 14 | + if (!juliaHome) juliaHome = home('.julia') |
| 15 | + return path.join(juliaHome, ...p) |
| 16 | +} |
| 17 | + |
| 18 | +export function jlpath () { |
| 19 | + return expandHome(atom.config.get('julia-client.juliaPath')) |
| 20 | +} |
| 21 | + |
| 22 | +export function expandHome (p) { |
| 23 | + return p.startsWith('~') ? p.replace('~', home()) : p |
| 24 | +} |
| 25 | + |
| 26 | +export function fullPath (p) { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + if (fs.existsSync(p)) return resolve(p) |
| 29 | + const current_dir = process.cwd() |
| 30 | + const exepath = path.dirname(process.execPath) |
| 31 | + try { |
| 32 | + process.chdir(exepath) |
| 33 | + const realpath = fs.realpathSync(p) |
| 34 | + fs.existsSync(realpath) && resolve(realpath) |
| 35 | + } catch (err) { |
| 36 | + console.log(err) |
| 37 | + } finally { |
| 38 | + try { |
| 39 | + process.chdir(current_dir) |
| 40 | + } catch (err) { |
| 41 | + console.error(err) |
| 42 | + } |
| 43 | + } |
| 44 | + if (process.platform === 'win32') { |
| 45 | + if (/[a-zA-Z]\:/.test(p)) return reject("Couldn't resolve path.") |
| 46 | + } |
| 47 | + const which = process.platform === 'win32' ? 'where' : 'which' |
| 48 | + child_process.exec(`${which} "${p}"`, (err, stdout, stderr) => { |
| 49 | + if (err) return reject(stderr) |
| 50 | + const p = stdout.trim() |
| 51 | + if (fs.existsSync(p)) return resolve(p) |
| 52 | + return reject('Couldn\'t resolve path.') |
| 53 | + }) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +export function getVersion (path = jlpath()) { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + child_process.exec(`"${path}" --version`, (err, stdout, stderr) => { |
| 60 | + if (err) return reject(stderr) |
| 61 | + const res = stdout.match(/(\d+)\.(\d+)\.(\d+)/) |
| 62 | + if (!res) return reject('Couldn\'t resolve version.') |
| 63 | + const [_, major, minor, patch] = res |
| 64 | + return resolve({ major, minor, patch }) |
| 65 | + }) |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +export function projectDir () { |
| 70 | + if (atom.config.get('julia-client.juliaOptions.persistWorkingDir')) { |
| 71 | + return new Promise(resolve => { |
| 72 | + const p = atom.config.get('julia-client.juliaOptions.workingDir') |
| 73 | + try { |
| 74 | + fs.stat(p, (err, stats) => { |
| 75 | + if (err) { |
| 76 | + return resolve(atomProjectDir()) |
| 77 | + } else { |
| 78 | + return resolve(p) |
| 79 | + } |
| 80 | + }) |
| 81 | + } catch (err) { |
| 82 | + return resolve(atomProjectDir()) |
| 83 | + } |
| 84 | + }) |
| 85 | + } else { |
| 86 | + return atomProjectDir() |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function atomProjectDir () { |
| 91 | + const dirs = atom.workspace.project.getDirectories() |
| 92 | + let ws = process.env.HOME |
| 93 | + if (!ws) { |
| 94 | + ws = process.env.USERPROFILE |
| 95 | + } |
| 96 | + if (dirs.length === 0 || dirs[0].path.match('app.asar')) { |
| 97 | + return Promise.resolve(ws) |
| 98 | + } |
| 99 | + return new Promise(resolve => { |
| 100 | + // use the first open project folder (or its parent folder for files) if |
| 101 | + // it is valid |
| 102 | + try { |
| 103 | + fs.stat(dirs[0].path, (err, stats) => { |
| 104 | + if (!err) return resolve(ws) |
| 105 | + if (stats.isFile()) return resolve(path.dirname(dirs[0].path)) |
| 106 | + return resolve(dirs[0].path) |
| 107 | + }) |
| 108 | + } catch (err) { |
| 109 | + return resolve(ws) |
| 110 | + } |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +function packageDir (...s) { |
| 115 | + const packageRoot = path.resolve(__dirname, '..', '..') |
| 116 | + return path.join(packageRoot, ...s) |
| 117 | +} |
| 118 | + |
| 119 | +export const script = (...s) => packageDir('script', ...s) |
| 120 | + |
| 121 | +export function getPathFromTreeView (el) { |
| 122 | + // invoked from tree-view context menu |
| 123 | + let pathEl = el.closest('[data-path]') |
| 124 | + if (!pathEl) { |
| 125 | + // invoked from command with focusing on tree-view |
| 126 | + const activeEl = el.querySelector('.tree-view .selected') |
| 127 | + if (activeEl) pathEl = activeEl.querySelector('[data-path]') |
| 128 | + } |
| 129 | + if (pathEl) return pathEl.dataset.path |
| 130 | + return null |
| 131 | +} |
| 132 | + |
| 133 | +export function getDirPathFromTreeView (el) { |
| 134 | + // invoked from tree-view context menu |
| 135 | + let dirEl = el.closest('.directory') |
| 136 | + if (!dirEl) { |
| 137 | + // invoked from command with focusing on tree-view |
| 138 | + const activeEl = el.querySelector('.tree-view .selected') |
| 139 | + if (activeEl) dirEl = activeEl.closest('.directory') |
| 140 | + } |
| 141 | + if (dirEl) { |
| 142 | + const pathEl = dirEl.querySelector('[data-path]') |
| 143 | + if (pathEl) return pathEl.dataset.path |
| 144 | + } |
| 145 | + return null |
| 146 | +} |
| 147 | + |
| 148 | +export const readCode = (path) => fs.readFileSync(path, 'utf-8') |
0 commit comments