-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnpm_bundle_to_provides.js
executable file
·82 lines (69 loc) · 2.09 KB
/
npm_bundle_to_provides.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
#!/usr/bin/node
let fs = require('fs');
let process = require('process');
function readNpmPackageJsonPathsFromStdinAsPromise()
{
return new Promise((accepted, rejected) => {
let data = '';
process.stdin.on('data', (new_data) => {
data += new_data;
});
process.stdin.on('end', () => {
let files = data.split('\n');
files = files.filter((file) => file.length > 1);
accepted(files);
});
});
}
function readAllNpmJsons(paths)
{
let promises = paths.map(path => loadNpmPackageJsonAsPromise(path));
return Promise.all(promises);
}
function loadNpmPackageJsonAsPromise(path)
{
return new Promise((accepted, rejected) => {
fs.readFile(path, 'utf8', (err, json_string) => {
if (err) {
rejected(err);
return;
}
let json = JSON.parse(json_string);
if (json.name === undefined || json.name.length <= 0 || json.version.length <= 0) {
rejected("Error processing " + path);
}
accepted(json);
});
});
}
function getSortedUniqueNpmJsons(npm_jsons)
{
npm_jsons.sort((a, b) => {
let ret = a.name.localeCompare(b.name);
if (ret == 0)
ret = a.version.localeCompare(b.version);
return ret;
});
return npm_jsons.filter((json, idx, array) => {
if (idx == 0)
return true;
return array[idx - 1].name !== json.name || array[idx - 1].version !== json.version;
});
}
function npmJsonsToRpmProvidesStrings(npm_jsons)
{
return npm_jsons.map(json => {
let s = 'Provides: bundled(node-' + json.name + ') = ' + json.version;
return s;
});
}
function printToStdoutWithNewlines(provides)
{
process.stdout.write(provides.join('\\n'));
}
// -- MAIN --
readNpmPackageJsonPathsFromStdinAsPromise()
.then(paths => readAllNpmJsons(paths))
.then(npm_jsons => getSortedUniqueNpmJsons(npm_jsons))
.then(npm_jsons => npmJsonsToRpmProvidesStrings(npm_jsons))
.then(provides => printToStdoutWithNewlines(provides));