-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
271 lines (244 loc) · 6.75 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env node
var repl = require('repl');
var fs = require('fs');
var async = require('async');
var path = require('path');
var request = require('./request');
var program = require('commander');
var mkdirp = require('mkdirp');
var validation = require('./validation.js');
var Rx = require('rx');
var scrapers = [];
var mergeFiles = [];
function includeDirectory(directoryPath) {
fs.readdirSync(directoryPath).map(function(scraperName) {
return path.join(directoryPath,scraperName);
}).forEach(includeScraper);
}
function includeScraper(scraperPath) {
scraperPath = path.resolve('.', scraperPath);
var scraper = require(scraperPath);
scraper.path = scraperPath;
scraper.id = path.basename(scraperPath, '.js');
scrapers.push(scraper);
}
function mergeFile(mergePath) {
mergeFiles.push(mergePath);
}
program
.option('-d, --directory <path>', 'Include directory of scrapers',includeDirectory)
.option('-s, --scraper <path>', 'Include specific scraper',includeScraper)
.option('-m, --merge <path>', 'Merge existing results with the scraped results.',mergeFile)
.parse(process.argv);
if (!program.directory && !program.scraper) {
includeDirectory(__dirname + '/scrapers');
}
var repositoryDefinitions = [
{
name: 'all',
filter: function(distribution) { return true; }
},
{
name: 'main',
filter: function(distribution) { return distribution.tags.indexOf('hybrid') >= 0; }
},
{
name: 'syslinux',
filter: function(distribution) { return distribution.tags.indexOf('nonhybrid') >= 0; }
}
];
function getRxScraper(scraper) {
if (typeof scraper === 'function') {
throw new Error('Invalid scraper', scraper);
}
// Rx based distribution
return Rx.Observable.defer(function() {
return Rx.Observable.just(scraper);
});
}
function resolveDistributionReleases(distribution) {
return distribution.releases.toArray()
.map(releases => Object.merge(distribution, {
releases: releases
}));
}
function loadDistributionsFromFile(path) {
return Rx.Observable.create(function(observer) {
var completed = false;
var StreamArray = require("stream-json/utils/StreamArray");
var stream = StreamArray.make();
stream.output.on("data", function(object){
if (completed) { return; }
observer.onNext(object.value);
});
stream.output.on("end", function(){
if (completed) { return; }
observer.onCompleted();
completed = true;
});
stream.input.on('error', function(err) {
if (completed) { return; }
console.error(err);
observer.onCompleted();
completed = true;
});
fs.createReadStream(path)
.on('error', function(err) {
console.error('Attempting to load',path,'has failed with error:', err);
console.error('Skipping ', path);
observer.onCompleted();
completed = true;
})
.pipe(stream.input);
});
}
function loadMergeFiles() {
return Rx.Observable.from(mergeFiles)
.concatMap(mergeFile => loadDistributionsFromFile(mergeFile))
.distinct(distribution => distribution.id);
}
function intersperse(observable, separator) {
var first = true;
return observable.flatMap(item => {
if (first) {
first = false;
return [item];
} else {
return [separator, item];
}
});
}
function toJsonArray(objects) {
return objects
.map(obj => JSON.stringify(obj))
.intersperse(',')
.startWith('[')
.concat(Rx.Observable.just(']'));
}
function writeJsonArrayToStream(stream, objects) {
return toJsonArray(objects)
.doOnNext(str => stream.write(str));
}
function doWriteFile(filePath, objects) {
return Rx.Observable.using(
function() {
// Initialize stream
var disposableStream = Rx.Disposable.create(function() {
this.stream.end();
});
disposableStream.stream = fs.createWriteStream(filePath);
return disposableStream;
}, function(disposableStream) {
var stream = disposableStream.stream;
return objects
.doOnNext(item => {
stream.write(item)
});
});
}
Rx.Observable.prototype.intersperse = function(separator) {
return intersperse(this, separator);
};
Rx.Observable.prototype.toJsonArray = function() {
return toJsonArray(this);
};
Rx.Observable.prototype.doWriteFile = function(filePath) {
return doWriteFile(filePath, this);
};
//
Rx.Observable.prototype.reduceConcat = function(accumulator, initialState, stateMapper) {
return this.materialize()
.scan((acc, notification) => {
switch(notification.kind) {
case 'N':
var item = notification.value;
return {
state: accumulator(acc.state, item),
observable: Rx.Observable.just(item)
};
case 'E':
return {
state: acc.state,
observable: Rx.Observable.throw(notification.error)
};
case 'C':
return {
state: acc.state,
observable: stateMapper(acc.state)
};
default:
throw new Error();
}
}, { state: initialState, observable: Rx.Observable.empty() })
.concatMap(acc => acc.observable);
};
function writeDistributions(filePath, distributions) {
return distributions
.toJsonArray()
.doWriteFile(filePath);
}
var rxMkdirp = Rx.Observable.fromNodeCallback(mkdirp);
function writeDistributionsToFile(distributions, filePath) {
return distributions.toJsonArray().doWriteFile(filePath).reduce(_ => true, true);
}
function writeRepositories(distributions) {
return Rx.Observable.from(repositoryDefinitions)
.flatMap(repositoryDefinition => distributions
.filter(repositoryDefinition.filter)
.toJsonArray()
.doWriteFile(repositoryDefinition.name + '.json')
)
.reduce(_ => true, true);
}
Rx.Observable.from(scrapers)
.doOnNext(scraper => {
console.log('Loading', scraper.id,'...');
})
.map(scraper => getRxScraper(scraper)
.catch(err => Rx.Observable.just({
id: scraper.id,
error: err,
releases: Rx.Observable.empty()
}))
)
.merge(4)
.concatMap(distribution => resolveDistributionReleases(distribution)
.doOnNext(distribution => {
if (distribution.error) { return; }
const errors = validation.validateDistribution(distribution)
if (errors && errors.length > 0) {
throw errors
}
})
.catch(err => Rx.Observable.just(Object.merge(distribution, {
error: err,
releases: []
})))
)
.doOnNext(distribution => {
if (distribution.error) {
console.log('Error ', distribution.id, distribution.error);
} else {
console.log('Resolved ', distribution.id);
}
})
.filter(distribution => !distribution.error)
.reduceConcat((state, distribution) => {
return state.concat([distribution.id]);
}, [], state => {
// Concat the distributions from mergeFiles that weren't found by the current run.
return loadMergeFiles()
.filter(distribution => state.indexOf(distribution.id) === -1);
})
.publish(distributions => writeRepositories(distributions))
.ignoreElements()
.subscribe(
result => {},
err => {
console.error('Error:', err);
process.exitCode = 1;
},
() => {
console.log('done');
}
);