diff --git a/lib/misc/paths.coffee b/lib/misc/paths.coffee deleted file mode 100644 index 0b63f385..00000000 --- a/lib/misc/paths.coffee +++ /dev/null @@ -1,125 +0,0 @@ -path = require 'path' -fs = require 'fs' -child_process = require 'child_process' - -module.exports = - - home: (p...) -> - key = if process.platform == 'win32' then 'USERPROFILE' else 'HOME' - path.join process.env[key], p... - - juliaHome: (p...) -> - path.join (process.env.JULIA_HOME or @home '.julia'), p... - - jlpath: -> - @expandHome(atom.config.get("julia-client.juliaPath")) - - expandHome: (p) -> - if p.startsWith '~' then p.replace '~', @home() else p - - fullPath: (p) -> - new Promise (resolve, reject) -> - if fs.existsSync(p) then resolve(p) - - current_dir = process.cwd() - exepath = path.dirname(process.execPath) - - try - process.chdir(exepath) - realpath = fs.realpathSync(p) - if fs.existsSync(realpath) then resolve(realpath) - catch err - console.error(err) - finally - try - process.chdir(current_dir) - catch err - console.error(err) - - if process.platform is 'win32' - if /[a-zA-Z]\:/.test(p) - reject("Couldn't resolve path.") - return - - which = if process.platform is 'win32' then 'where' else 'which' - proc = child_process.exec "#{which} \"#{p}\"", (err, stdout, stderr) -> - return reject(stderr) if err? - p = stdout.trim() - return resolve(p) if fs.existsSync(p) - reject("Couldn't resolve path.") - - getVersion: (path = @jlpath()) -> - new Promise (resolve, reject) => - proc = child_process.exec "\"#{path}\" --version", (err, stdout, stderr) => - return reject(stderr) if err? - res = stdout.match /(\d+)\.(\d+)\.(\d+)/ - return reject("Couldn't resolve version.") unless res? - [_, major, minor, patch] = res - resolve {major, minor, patch} - - projectDir: -> - if atom.config.get('julia-client.juliaOptions.persistWorkingDir') - return new Promise (resolve) => - p = atom.config.get('julia-client.juliaOptions.workingDir') - try - fs.stat p, (err, stats) => - if err - resolve(@atomProjectDir()) - else - resolve(p) - catch err - resolve(@atomProjectDir()) - else - return @atomProjectDir() - - atomProjectDir: -> - dirs = atom.workspace.project.getDirectories() - ws = process.env.HOME || process.env.USERPROFILE - if dirs.length is 0 or dirs[0].path.match 'app.asar' - return Promise.resolve ws - new Promise (resolve) -> - # use the first open project folder (or its parent folder for files) if - # it is valid - try - fs.stat dirs[0].path, (err, stats) => - if err? - resolve(ws) - return - - if stats.isFile() - resolve path.dirname dirs[0].path - else - resolve dirs[0].path - catch err - resolve(ws) - - packageDir: (s...) -> - packageRoot = path.resolve __dirname, '..', '..' - path.join packageRoot, s... - - script: (s...) -> @packageDir 'script', s... - - getPathFromTreeView: (el) -> - # invoked from tree-view context menu - pathEl = el.closest('[data-path]') - if not pathEl - # invoked from command with focusing on tree-view - activeEl = el.querySelector('.tree-view .selected') - pathEl = activeEl.querySelector('[data-path]') if activeEl - return pathEl.dataset.path if pathEl - return null - - getDirPathFromTreeView: (el) -> - # invoked from tree-view context menu - dirEl = el.closest('.directory') - if not dirEl - # invoked from command with focusing on tree-view - activeEl = el.querySelector('.tree-view .selected') - dirEl = activeEl.closest('.directory') if activeEl - if dirEl - pathEl = dirEl.querySelector('[data-path]') - return pathEl.dataset.path if pathEl - return null - - readCode: (path) -> - fs.readFileSync(path, 'utf-8') diff --git a/lib/misc/paths.js b/lib/misc/paths.js new file mode 100644 index 00000000..ce8af6eb --- /dev/null +++ b/lib/misc/paths.js @@ -0,0 +1,147 @@ +/** @babel */ + +import path from 'path' +import fs from 'fs' +import child_process from 'child_process' + +export function home (...p) { + const key = process.platform === 'win32' ? 'USERPROFILE' : 'HOME' + return path.join(process.env[key], ...p) +} + +export function juliaHome (...p) { + const juliaHome = (process.env.JULIA_HOME || home('.julia')) + return path.join(juliaHome, ...p) +} + +export function jlpath () { + return expandHome(atom.config.get('julia-client.juliaPath')) +} + +export function expandHome (p) { + return p.startsWith('~') ? p.replace('~', home()) : p +} + +export function fullPath (p) { + return new Promise((resolve, reject) => { + if (fs.existsSync(p)) return resolve(p) + const current_dir = process.cwd() + const exepath = path.dirname(process.execPath) + try { + process.chdir(exepath) + const realpath = fs.realpathSync(p) + fs.existsSync(realpath) && resolve(realpath) + } catch (err) { + console.log(err) + } finally { + try { + process.chdir(current_dir) + } catch (err) { + console.error(err) + } + } + if (process.platform === 'win32') { + if (/[a-zA-Z]\:/.test(p)) return reject("Couldn't resolve path.") + } + const which = process.platform === 'win32' ? 'where' : 'which' + child_process.exec(`${which} "${p}"`, (err, stdout, stderr) => { + if (err) return reject(stderr) + const p = stdout.trim() + if (fs.existsSync(p)) return resolve(p) + return reject('Couldn\'t resolve path.') + }) + }) +} + +export function getVersion (path = jlpath()) { + return new Promise((resolve, reject) => { + child_process.exec(`"${path}" --version`, (err, stdout, stderr) => { + if (err) return reject(stderr) + const res = stdout.match(/(\d+)\.(\d+)\.(\d+)/) + if (!res) return reject('Couldn\'t resolve version.') + const [_, major, minor, patch] = res + return resolve({ major, minor, patch }) + }) + }) +} + +export function projectDir () { + if (atom.config.get('julia-client.juliaOptions.persistWorkingDir')) { + return new Promise(resolve => { + const p = atom.config.get('julia-client.juliaOptions.workingDir') + try { + fs.stat(p, (err, stats) => { + if (err) { + return resolve(atomProjectDir()) + } else { + return resolve(p) + } + }) + } catch (err) { + return resolve(atomProjectDir()) + } + }) + } else { + return atomProjectDir() + } +} + +function atomProjectDir () { + const dirs = atom.workspace.project.getDirectories() + let ws = process.env.HOME + if (!ws) { + ws = process.env.USERPROFILE + } + if (dirs.length === 0 || dirs[0].path.match('app.asar')) { + return Promise.resolve(ws) + } + return new Promise(resolve => { + // use the first open project folder (or its parent folder for files) if + // it is valid + try { + fs.stat(dirs[0].path, (err, stats) => { + if (err) return resolve(ws) + if (stats.isFile()) return resolve(path.dirname(dirs[0].path)) + return resolve(dirs[0].path) + }) + } catch (err) { + return resolve(ws) + } + }) +} + +function packageDir (...s) { + const packageRoot = path.resolve(__dirname, '..', '..') + return path.join(packageRoot, ...s) +} + +export const script = (...s) => packageDir('script', ...s) + +export function getPathFromTreeView (el) { + // invoked from tree-view context menu + let pathEl = el.closest('[data-path]') + if (!pathEl) { + // invoked from command with focusing on tree-view + const activeEl = el.querySelector('.tree-view .selected') + if (activeEl) pathEl = activeEl.querySelector('[data-path]') + } + if (pathEl) return pathEl.dataset.path + return null +} + +export function getDirPathFromTreeView (el) { + // invoked from tree-view context menu + let dirEl = el.closest('.directory') + if (!dirEl) { + // invoked from command with focusing on tree-view + const activeEl = el.querySelector('.tree-view .selected') + if (activeEl) dirEl = activeEl.closest('.directory') + } + if (dirEl) { + const pathEl = dirEl.querySelector('[data-path]') + if (pathEl) return pathEl.dataset.path + } + return null +} + +export const readCode = (path) => fs.readFileSync(path, 'utf-8')