This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmeta-file.js
190 lines (153 loc) · 5.3 KB
/
meta-file.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
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var readJSON = require('read-json');
var fs = require('graceful-fs');
var setImmediate = require('timers').setImmediate;
module.exports = MetaFile;
function MetaFile(opts) {
if (!(this instanceof MetaFile)) {
return new MetaFile(opts);
}
var self = this;
self.fileName = opts.fileName;
self._lastDate = null;
self._remotes = null;
self._version = null;
self._shasums = null;
}
MetaFile.prototype.readFile = function readFile(cb) {
var self = this;
readJSON(self.fileName, onFile);
function onFile(err, meta) {
if (err && err.code === 'ENOENT') {
self._remotes = {};
return cb(null);
} else if (err) {
if (err.constructor.name === 'SyntaxError') {
err.message = 'Corrupt meta.json file: ' + err.message;
}
return cb(err);
}
self._remotes = meta.remotes;
self._version = meta.version;
self._lastDate = new Date(meta.time || Date.now());
self._shasums = meta.shasums;
cb(null);
}
};
MetaFile.prototype.getSha = function getSha(directoryName) {
var self = this;
var remote = self._remotes[directoryName];
if (!remote) {
return null;
}
return remote.sha;
};
MetaFile.prototype.getShasums = function getShasums(directoryName) {
var self = this;
var remote = self._remotes[directoryName];
if (!remote) {
return null;
}
return remote.shasums;
};
MetaFile.prototype._updateVersion = function version(opts) {
opts = opts || {};
var self = this;
var newDate = opts.time ? new Date(opts.time) : new Date();
if (+newDate > +self._lastDate) {
self._lastDate = newDate;
}
};
MetaFile.prototype.updateRecord =
function updateRecord(directoryName, opts, callback) {
opts = opts || {};
var self = this;
self._updateVersion(opts);
self._remotes[directoryName] = {
time: opts.time ? opts.time : self._lastDate.toISOString(),
version: opts.version ? opts.version : self._lastDate.getTime(),
sha: opts.sha,
shasums: opts.shasums
};
self.save(callback);
};
MetaFile.prototype.getRecord = function getRecord(service) {
var self = this;
return self._remotes[service];
};
MetaFile.prototype.publish = function publish(opts, callback) {
opts = opts || {};
var self = this;
self._updateVersion(opts);
self._shasums = opts.shasums || {};
self.save(callback);
};
// a lock should be set that delays this if updateRecord is in progress
MetaFile.prototype.getDependencies = function getDependencies(callback) {
var self = this;
setImmediate(function onSetImmediate() {
callback(null, self._remotes);
});
};
MetaFile.prototype.toJSON = function toJSON() {
var self = this;
var date = self._lastDate;
var json = {};
// date will only exist for the registry meta.json file and should
// be the date of the most recently updated file.
// Services should have a different field to indicate what version
// of the monorepo to install from. TODO: what name?
if (date) {
json.time = date.toISOString();
// "version" is a poor choice for a name here. It really should
// be something like mtime. i.e. last time a new thrift definition
// was published.
json.version = date.getTime();
}
if (self._shasums) {
json.shasums = self._shasums;
}
if (self._remotes) { // sort remotes to simplify diffs / reviews
json.remotes = sortObject(self._remotes);
}
return json;
};
// sort object properties by their names in lexicographical order
function sortObject(unsorted) {
var sorted = {};
Object.keys(unsorted).sort().forEach(function forEachSort(key) {
sorted[key] = unsorted[key];
});
return sorted;
}
MetaFile.prototype.toJSONString = function toJSONString() {
var self = this;
return JSON.stringify(self.toJSON(), null, 4) + '\n';
};
MetaFile.prototype.save = function save(callback) {
var self = this;
fs.writeFile(self.fileName, self.toJSONString(), 'utf8', callback);
};
MetaFile.prototype.time = function time() {
var self = this;
return self._lastDate;
};