-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (90 loc) · 2.66 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// inspired by electron-download, electron-prebuilt
// inspired by nvm, nrm, nwjs
// inspired by prt
// todo: use promise
var download = require('electron-download')
var extract = require('extract-zip')
var exec = require('child_process').exec
var basename = require('path').basename
// var extname = require('path').extname
var dirname = require('path').dirname
var join = require('path').join
var fs = require('fs')
var os = require('os')
exports.install = install
exports.use = use
exports.list = list
exports.curr = curr
function curr(cb) {
exec('electron -v', function (err, stdout) {
if (!err) {
stdout = ' ' + stdout + ' '
var mat = stdout.match(/\s+(v\d+(\.\d+)+)\s+/)
if (mat) return cb(null, mat[1])
}
cb(new Error('no electron in use'))
})
}
function list (cb) {
fs.readdir(join(os.homedir(), '.electron'), function (err, keys) {
if (err) return cb(err)
var items = keys.reduce(function (acc, key) {
if (key.indexOf('.zip') < 0) {
var mat = key.match(/^electron\-(.+?)\-(.+?)\-(.+?)$/)
if (mat) acc.push(mat.slice(1, 4))
}
return acc
}, [])
cb(null, items)
})
}
function install (opts, cb) {
opts.platform = opts.platform || process.platform
opts.arch = opts.arch || process.arch
download(opts, function (err, zip) {
if (err) return cb(err)
var dir = join(dirname(zip), basename(zip, '.zip'))
extract(zip, { dir: dir }, function (err) {
// if (err && err.code !== 'EEXIST') return cb(err)
if (err) return cb(err)
cb()
})
})
}
function use (opts, cb) {
opts.platform = opts.platform || process.platform
opts.arch = opts.arch || process.arch
var bin = {
darwin: 'Electron.app/Contents/MacOS/Electron',
freebsd: 'electron',
linux: 'electron',
win32: 'electron.exe'
}[opts.platform]
var dir = ['electron', 'v' + opts.version, opts.platform, opts.arch].join('-')
var dist = join(os.homedir(), '.electron', dir, bin)
fs.exists(dist, function (exists) {
if (!exists) {
// console.warn('v' + opts.version + ' is not installed')
return cb(new Error('v' + opts.version + ' not installed'))
}
crosslink(dist, 'electron', cb)
})
}
function crosslink (file, base, cb) {
if (process.platform === 'win32') {
winlink(file, cb)
} else {
var target = '/usr/local/bin/' + base
fs.unlink(target, function () {
fs.symlink(file, target, cb)
})
}
}
// experimental: windows support
function winlink (file, base, cb) {
// var ext = extname(file)
// var base = basename(file, ext)
var cmdfile = join(process.env.windir, base + '.cmd')
var cmd = '"' + file + '" %*'
fs.writeFile(cmdfile, cmd, cb)
}