-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
204 lines (186 loc) · 6.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict'
const fs = require('fs')
const path = require('path')
const u = require('@elife/utils')
const exec = require('./exec')
const git = require('isomorphic-git')
const http = require('isomorphic-git/http/node')
module.exports = {
load: load,
update: updatePkg,
installLatest: installLatest,
normalize: normalize
}
/* problem/
* When the user specifies a package name he can do so in a variety of
* forms:
* https://path.to/full/repo
* user/repo #On github
* repo #From everlife skills
* eskill-repo #Full name of everlife skills
* For each of these we would like to get the fetch url and the local
* directory name (which is reponame.org or just reponame for everlife
* packages).
*
* way/
* If the package is a complete url we just use it. Otherwise we check
* if we are given a package in the format `org/repo` and assume it is a
* github URL. Otherwise we default to a Everlife skill repo on github
* (with prefix `eskill-`). Then we set the name of the local directory.
*/
function normalize(pkg) {
let fetch = ''
if(pkg.indexOf("://") > 0) {
fetch = pkg
} else if(pkg.indexOf("/") > 0) {
fetch = `https://github.com/${pkg}.git`
} else if(pkg.startsWith("eskill-") > 0) {
fetch = `https://github.com/everlifeai/${pkg}.git`
} else {
fetch = `https://github.com/everlifeai/eskill-${pkg}.git`
}
return {
fetch: fetch,
name: get_pathname_1(fetch)
}
/* outcome/
* We get rid of the url prefix (http/https) and remove any trailing
* '.git' repo. Then we split the url into it's parts and join it in
* reverse to form the full name (we ignore standard location
* (github.com) and standard organization (everlifeai)
*/
function get_pathname_1(fetch) {
let name = fetch.substring(fetch.indexOf('://')+3)
name = name.replace(/\.git$/,'')
let parts = name.split('/')
if(parts[0] == 'github.com') parts.shift()
if(parts[0] == 'everlifeai') parts.shift()
return parts.reverse().join('.')
}
}
/* problem/
* We need to install the latest version of the package in the given
* location not in ASAR file path.
*
* way/
* If the given path is ASAR path, we are returning a given path,
* because all the everlife packages are preloaded in the ASAR file.
* Else We will ensure the given location exists, we normalize the package
* name and delete if it already exists. Then we install the package
* into the given location.
*/
function installLatest(pkg, path_, cb) {
pkg = normalize(pkg)
u.showMsg(`Installing ${pkg.name}...`)
if (path_.includes('/app.asar/') ||
path_.includes('\\app.asar\\')) {
cb(null, path.join(path_, pkg.name))
} else{
u.ensureExists(path_, (err, loc) => {
if(err) cb(err)
else {
let pkgloc = path.join(loc, pkg.name)
if(fs.existsSync(pkgloc)) {
u.showMsg(`Deleting package from '${pkgloc}/'...`)
del_pkg_1(pkgloc, (err) => {
if(err) cb(err)
else installPkg(pkg, loc, cb)
})
} else {
installPkg(pkg, loc, cb)
}
}
})
}
/* outcome/
* Because deleting a directory recursively is a dangerous
* operation, we perform a sanity check that the directory does seem
* to be an everlife directory (for now it's an 'eskill-'
* directory). Otherwise we refuse to delete it
*/
function del_pkg_1(pkgloc, cb) {
let name = path.basename(pkgloc)
if(!name.startsWith('eskill-')) cb(`Refusing to delete non-standard directory ${name}...`)
else u.rmdir(pkgloc, cb)
}
}
/* problem/
* We need to ensure an everlife packaged code is installed in a given
* location not in ASAR file path.
*
* way/
* If the given path is ASAR path, we are returning a given path,
* because all the everlife packages are preloaded in the ASAR file.
* Else we will ensure the given location exists, we normalize the package
* name and check if it already exists. If it does we do nothing
* otherwise we install the package into the given location.
*/
function load(pkg, path_, cb) {
pkg = normalize(pkg)
u.showMsg(`Ensuring ${pkg.name} loaded...`)
if (path_.includes('/app.asar/') ||
path_.includes('\\app.asar\\')) {
cb(null, path.join(path_, pkg.name))
} else{
u.ensureExists(path_, (err, loc) => {
if(err) cb(err)
else {
let pkgloc = path.join(loc, pkg.name)
if(fs.existsSync(pkgloc)) {
u.showMsg(`Package exists in location '${pkgloc}/'...`)
cb(null, pkgloc)
} else {
installPkg(pkg, loc, cb)
}
}
})
}
}
/* outcome/
* Clone the package and call `npm install` to set up the dependencies.
*/
function installPkg(pkg, loc, cb) {
let pkgloc = path.join(loc, pkg.name)
clone_pkg_1(pkg.fetch, (err) => {
if(err) cb(err)
else npmSetup(pkgloc, (err) => {
if(err) cb(err)
else cb(null, pkgloc)
})
})
function clone_pkg_1(url, cb) {
u.showMsg(`Cloning ${url}...`)
u.ensureExists(pkgloc, (err) => {
git.clone({
fs,
http,
dir: pkgloc,
url,
})
.then(() => cb())
.catch(err => {
u.showErr(err)
u.rmdir(pkgloc, (err) => {
cb('Installation failed')
})
})
})
}
}
function npmSetup(pkgloc, cb) {
u.showMsg(`NPM setup ${pkgloc}...`)
exec((/^win/.test(process.platform)?'npm.cmd':'npm'), ['install'], pkgloc, null, null, cb)
}
function updatePkg(pkgloc, cb) {
update_pkg_1(pkgloc, (err) => {
npmSetup(pkgloc, (err2) => {
if(err2) cb(err2)
else if(err) cb(err)
else cb()
})
})
function update_pkg_1(pkgloc, cb) {
u.showMsg(`Updating ${pkgloc}...`)
exec('git', ['pull', '--rebase'], pkgloc, null, null, cb)
}
}