-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
75 lines (59 loc) · 2.27 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
const Plugin = require('broccoli-plugin');
const fg = require('fast-glob');
const fs = require('fs');
const only = (subject, props = []) => {
const keys = Object.keys(subject);
const result = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (props.includes(key)) {
result[key] = subject[key];
}
}
return result;
};
const IndexExtensionsToJson = function () {
return new Promise((resolve, reject) => {
const extensions = [];
const seenPackages = new Set();
return fg(['node_modules/*/package.json', 'node_modules/*/*/package.json'])
.then((results) => {
for (let i = 0; i < results.length; i++) {
const packagePath = results[i];
const packageJson = fs.readFileSync(packagePath);
let packageData = null;
try {
packageData = JSON.parse(packageJson);
} catch (e) {
console.warn(`Could not parse package.json at ${packagePath}:`, e);
continue;
}
if (!packageData || !packageData.keywords || !packageData.keywords.includes('fleetbase-extension')) {
continue;
}
// If we've seen this package before, skip it
if (seenPackages.has(packageData.name)) {
continue;
}
seenPackages.add(packageData.name);
extensions.push(only(packageData, ['name', 'description', 'version', 'fleetbase', 'keywords', 'license', 'repository']));
}
resolve(extensions);
})
.catch(reject);
});
};
module.exports = class FleetbaseExtensionsIndexer extends Plugin {
constructor() {
super([]);
}
async build() {
const extensionsJsonPath = this.outputPath + '/extensions.json';
// Check if extensions.json exists
if (fs.existsSync(extensionsJsonPath)) {
return;
}
const extensions = await IndexExtensionsToJson();
this.output.writeFileSync('extensions.json', JSON.stringify(extensions));
}
};