-
Notifications
You must be signed in to change notification settings - Fork 2
/
version-map.js
242 lines (204 loc) · 7.19 KB
/
version-map.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function() {
var AWS, Q, VersionMap, semver, utils, _,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
AWS = require('aws-sdk');
Q = require('q');
_ = require('underscore');
semver = require('semver');
utils = require('./s3-utils');
VersionMap = (function() {
function VersionMap(options) {
this.versionDirectory = __bind(this.versionDirectory, this);
this.removeMajor = __bind(this.removeMajor, this);
this.updateTag = __bind(this.updateTag, this);
this.addVersion = __bind(this.addVersion, this);
this.downloadTags = __bind(this.downloadTags, this);
this.uploadTags = __bind(this.uploadTags, this);
this.downloadRegistry = __bind(this.downloadRegistry, this);
this.uploadRegistry = __bind(this.uploadRegistry, this);
this.updateTagsFromRegistry = __bind(this.updateTagsFromRegistry, this);
this.removeMajorFromTags = __bind(this.removeMajorFromTags, this);
this.updateTags = __bind(this.updateTags, this);
this.updateRegistry = __bind(this.updateRegistry, this);
this.s3Client = new AWS.S3();
this.bucket = options.bucket || 'vtex-versioned';
this.dryRun = options.dryRun;
if (this.dryRun) {
console.log('\nWARNING: VersionMap running in dry run mode. No changes will actually be made.\n');
}
this.registryPath = "registry/v2/registry.json";
this.tagsPath = "tags/v2/tags.json";
}
VersionMap.prototype.updateRegistry = function(registry, pkg) {
if (!pkg.name) {
throw new Error("Required property name not found");
}
if (!pkg.version) {
throw new Error("Required property version not found");
}
if (!pkg.backend && pkg.hosts && pkg.paths) {
throw new Error("Required property for creation backend not found");
}
if (!registry[pkg.name]) {
registry[pkg.name] = {
name: pkg.name,
versions: {}
};
}
if (pkg.backend) {
registry[pkg.name].backend = pkg.backend;
}
if (pkg.paths) {
registry[pkg.name].paths = pkg.paths;
}
if (pkg.hosts) {
registry[pkg.name].hosts = pkg.hosts;
}
if (pkg.main) {
registry[pkg.name].main = pkg.main;
}
registry[pkg.name].versions[pkg.version] = {};
registry[pkg.name].versions[pkg.version].version = pkg.version;
registry[pkg.name].versions[pkg.version].created = new Date();
registry[pkg.name].versions[pkg.version].rootRewrite = this.versionDirectory(pkg);
return registry;
};
VersionMap.prototype.updateTags = function(tags, name, version, tag) {
var major;
if (!name) {
throw new Error("Required property name is null or undefined");
}
if (!version) {
throw new Error("Required property version is null or undefined");
}
if (!tag) {
throw new Error("Required property tag is null or undefined");
}
if (tag !== "stable" && tag !== "next" && tag !== "beta" && tag !== "alpha") {
throw new Error("Tag must be one of: stable, next, beta, alpha");
}
if (!tags[name]) {
tags[name] = {
stable: {},
next: {},
beta: {},
alpha: {}
};
}
major = semver(version).major;
tags[name][tag][major] = version;
return tags;
};
VersionMap.prototype.removeMajorFromTags = function(tags, name, major, tag) {
if (!name) {
throw new Error("Required property name is null or undefined");
}
if (!major) {
throw new Error("Required property major is null or undefined");
}
if (!tag) {
throw new Error("Required property tag is null or undefined");
}
if (tag !== "stable" && tag !== "next" && tag !== "beta" && tag !== "alpha") {
throw new Error("Tag must be one of: stable, next, beta, alpha");
}
delete tags[name][tag][major];
return tags;
};
VersionMap.prototype.updateTagsFromRegistry = function(tags, registry) {
var biggest, majorName, majors, project, projectName, tagName, tagNames;
for (projectName in registry) {
project = registry[projectName];
tagNames = _.groupBy(project.versions, function(v) {
return semver(v.version).prerelease[0] || 'stable';
});
for (tagName in tagNames) {
majors = _.groupBy(tagNames[tagName], function(v) {
return semver(v.version).major;
});
for (majorName in majors) {
biggest = majors[majorName].sort(function(a, b) {
return semver.rcompare(a.version, b.version);
})[0];
tags = this.updateTags(tags, projectName, biggest.version, tagName);
}
}
}
return tags;
};
/*
Uploads a registry object to s3
*/
VersionMap.prototype.uploadRegistry = function(registry) {
return utils.putObject(registry, this.s3Client, this.bucket, this.registryPath, this.dryRun);
};
/*
Returns a registry object.
*/
VersionMap.prototype.downloadRegistry = function() {
return utils.getObject(this.s3Client, this.bucket, this.registryPath);
};
/*
Uploads a tags object to s3
*/
VersionMap.prototype.uploadTags = function(tags) {
return utils.putObject(tags, this.s3Client, this.bucket, this.tagsPath, this.dryRun);
};
/*
Returns a tags object.
*/
VersionMap.prototype.downloadTags = function() {
return utils.getObject(this.s3Client, this.bucket, this.tagsPath);
};
/*
Adds version to the registry
*/
VersionMap.prototype.addVersion = function(pack) {
var _this = this;
return this.downloadRegistry().then(function(registry) {
var updatedRegistry;
updatedRegistry = _this.updateRegistry(registry, pack);
return _this.uploadRegistry(updatedRegistry);
}).fail(function(err) {
console.log("Could not update registry", err);
return err;
});
};
/*
Updates tag in tags object
*/
VersionMap.prototype.updateTag = function(name, version, tag) {
var _this = this;
return this.downloadTags().then(function(tags) {
var updatedTags;
updatedTags = _this.updateTags(tags, name, version, tag);
return _this.uploadTags(updatedTags);
}).fail(function(err) {
console.log("Could not update tags", err);
return err;
});
};
/*
Removes major from this tag in tags object
*/
VersionMap.prototype.removeMajor = function(name, major, tag) {
var _this = this;
return this.downloadTags().then(function(tags) {
var updatedTags;
updatedTags = _this.removeMajorFromTags(tags, name, major, tag);
return _this.uploadTags(updatedTags);
}).fail(function(err) {
console.log("Could not remove major from tags", err);
return err;
});
};
/*
Returns the version directory for the given package
*/
VersionMap.prototype.versionDirectory = function(packageObj) {
return packageObj.name + "/" + packageObj.version;
};
return VersionMap;
})();
module.exports = VersionMap;
}).call(this);