This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathregistry.js
224 lines (195 loc) · 6.05 KB
/
registry.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var Promise = require('rsvp').Promise;
var asp = require('rsvp').denodeify;
var fs = require('graceful-fs');
var exec = require('child_process').exec;
var semver = require('semver');
var path = require('path');
var rimraf = require('rimraf');
function extend(a, b) {
for (var p in b)
a[p] = b[p];
return a;
}
var defaultRepo = 'https://github.com/jspm/registry.git';
var registry = module.exports = function registry(options, ui) {
this.ui = ui;
this.registryPath = options.tmpDir;
this.registryUpdated = false;
this.repo = options.repo || defaultRepo;
this.execOptions = {
cwd: options.tmpDir,
timeout: (options.timeout || 0) * 1000,
killSignal: 'SIGKILL'
};
this.username = options.username;
this.password = options.password;
}
registry.configure = function(config, ui) {
return ui.input('Enter the registry repo path', config.repo || defaultRepo)
.then(function(repo) {
if (repo.substr(0, 2) == '~/')
repo = path.resolve(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH, repo.substr(2));
else if (repo.substr(0, 1) == '.')
repo = path.resolve(repo);
config.repo = repo;
return config;
});
}
registry.prototype.parse = function(name) {
var parts = name.split('/');
return {
package: parts[0],
path: parts.splice(1).join('/')
};
}
registry.prototype.locate = function(repo) {
return this.updateRegistry()
.then(function(registry) {
// NB support versioned redirects
var registryEntry = registry[repo];
if (!registryEntry)
return { notfound: true };
return { redirect: registryEntry };
});
}
// registry endpoint is in charge of overrides, special hook
registry.prototype.getOverride = function(endpoint, repo, version, givenOverride) {
// we get a folder listing for the right name
// if we have a listing we filter to the override versions for that package
// we take the highest override that creates a semver range we are compatible with
var packageParts = repo.split('/');
var overrideName = packageParts.pop();
var overrideDir = path.resolve(this.registryPath, 'package-overrides', endpoint, packageParts.join('/'));
var ui = this.ui;
return this.updateRegistry()
.then(function() {
return asp(fs.readdir)(overrideDir);
})
.then(function(files) {
var overrideFile = files
// find the files for this override name
.filter(function(file) {
return file.substr(0, overrideName.length) == overrideName && file.substr(overrideName.length, 1) == '@';
})
// derive versions
.map(function(file) {
return {
version: file.substr(overrideName.length + 1, file.length - overrideName.length - 6),
file: file
};
})
// filter to only semver compatible overrides
.filter(function(item) {
if (semver.valid(version))
return semver.satisfies(version, '^' + item.version);
else
return version == item.version;
})
// sort to find latest
.sort(function(a, b) {
return semver.compare(a.version, b.version);
})
.map(function(item) {
return item.file;
})
.pop();
// return that loaded override
if (!overrideFile)
return;
return asp(fs.readFile)(path.resolve(overrideDir, overrideFile))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
return;
}
}, function(err) {
if (err.code === 'ENOENT')
return;
ui.log('warn', 'Override file `' + overrideFile + '` found, but JSON is invalid');
});
}, function(err) {
if (err.code === 'ENOENT')
return;
throw err;
})
.then(function(override) {
// if an existing override, let it extend this override
if (givenOverride)
extend(override = (override || {}), givenOverride);
return override;
});
}
registry.prototype.createRegistry = function() {
var ui = this.ui;
ui.log('info', 'Creating registry cache...');
var self = this;
return asp(rimraf)(path.resolve(this.registryPath))
.then(function() {
return asp(fs.mkdir)(path.resolve(self.registryPath));
})
.then(function() {
// Default windows shell doesn't support UNC paths => escape it
var repo = self.repo;
if (process.platform == 'win32' && (/^[\\\/]{2,}[^\\\/]+[\\\/]+[^\\\/]+/).test(repo)) repo = repo.replace(/\\/g, '/');
return asp(exec)('git clone --depth=1 ' + repo + ' .', self.execOptions);
})
.catch(function(err) {
ui.log('err', 'Error creating registry file\n' + (err.stack || err));
});
}
registry.prototype.updateRegistry = function() {
if (this.updatePromise_)
return Promise.resolve(this.updatePromise_);
var ui = this.ui;
var registryPath = this.registryPath;
var remoteString = this.repo;
var execOptions = this.execOptions;
var self = this;
return this.updatePromise_ = asp(exec)('git remote show origin -n', execOptions)
.then(function(output) {
output = output.toString();
var curRepoMatch = output.match(/Fetch URL: ([^\n]+)/m);
if (!curRepoMatch || curRepoMatch[1] != self.repo)
return self.createRegistry();
// if the registry does exist, update it
ui.log('info', 'Updating registry cache...');
return asp(exec)('git fetch --all && git reset --hard origin/master', execOptions)
.then(function(stdout, stderr) {
if (stderr)
throw stderr;
})
.catch(function(err) {
if (typeof err == 'string') {
err = new Error(err);
err.hideStack = true;
}
err.retriable = true;
throw err;
});
}, function(err) {
// if the registry does not exist, do a git clone
if (err.toString().indexOf('fatal:') != -1)
return self.createRegistry();
throw err;
})
.then(function() {
return asp(fs.readFile)(path.resolve(registryPath, 'registry.json'))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
return {};
}
}, function(err) {
if (err.code === 'ENOENT')
return {};
ui.log('warn', 'Registry file is invalid.');
});
})
.then(function(json) {
return json;
});
}