forked from awesome-opencode/awesome-opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-json.js
More file actions
158 lines (135 loc) · 3.7 KB
/
Copy pathexport-json.js
File metadata and controls
158 lines (135 loc) · 3.7 KB
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
#!/usr/bin/env node
/**
* Export registry data as a flat JSON list.
* Usage:
* node scripts/export-json.js --pretty --output dist/registry.json
* node scripts/export-json.js --pretty > dist/registry.json
*/
const fs = require('fs');
const path = require('path');
const { readYamlDir, slugify } = require('./utils/yaml');
const { validateEntry, formatValidationErrors } = require('./utils/validation');
const CATEGORIES = ['plugins', 'themes', 'agents', 'projects', 'resources'];
function parseArgs(args) {
const outputFlagIndex = args.findIndex((arg) => arg === '--output');
const outputPath = outputFlagIndex !== -1 && args[outputFlagIndex + 1]
? args[outputFlagIndex + 1]
: null;
const pretty = args.includes('--pretty');
return { outputPath, pretty };
}
function buildProductId(entry) {
if (entry._fileName) {
return entry._fileName;
}
if (entry.name) {
return slugify(entry.name);
}
return '';
}
function sanitizeArray(value, fallback) {
if (Array.isArray(value) && value.length > 0) {
return value;
}
return fallback;
}
function mapEntry(entry, category) {
const productId = buildProductId(entry);
if (!productId) {
return null;
}
const output = {
productId,
type: category,
displayName: entry.name,
repoUrl: entry.repo,
tagline: entry.tagline,
description: entry.description,
scope: sanitizeArray(entry.scope, ['global']),
tags: sanitizeArray(entry.tags, []),
homepageUrl: entry.homepage,
installation: entry.installation,
minVersion: entry.min_version
};
return output;
}
function stripUndefined(data) {
const cleaned = {};
for (const [key, value] of Object.entries(data)) {
if (value !== undefined) {
cleaned[key] = value;
}
}
return cleaned;
}
async function loadEntries() {
const results = [];
const errors = [];
for (const category of CATEGORIES) {
const categoryPath = path.join(__dirname, '../data', category);
let entries = [];
try {
entries = await readYamlDir(categoryPath);
} catch (err) {
if (err.code === 'ENOENT') {
continue;
}
throw err;
}
for (const entry of entries) {
const result = validateEntry(entry, entry._filePath || categoryPath);
if (!result.valid) {
errors.push(formatValidationErrors(result));
continue;
}
const mapped = mapEntry(entry, category);
if (mapped) {
results.push(stripUndefined(mapped));
}
}
}
return { results, errors };
}
async function main() {
const { outputPath, pretty } = parseArgs(process.argv.slice(2));
const writeToStdout = !outputPath;
const log = (...args) => {
if (writeToStdout) {
console.error(...args);
} else {
console.log(...args);
}
};
const { results, errors } = await loadEntries();
results.sort((a, b) => {
const typeCompare = a.type.localeCompare(b.type);
if (typeCompare !== 0) {
return typeCompare;
}
return a.productId.localeCompare(b.productId);
});
if (errors.length > 0) {
for (const error of errors) {
log(error);
}
log(`\n⚠️ Skipped ${errors.length} invalid entr${errors.length === 1 ? 'y' : 'ies'}.`);
}
const json = JSON.stringify(results, null, pretty ? 2 : 0);
if (outputPath) {
const outputDir = path.dirname(outputPath);
if (outputDir && outputDir !== '.') {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, json);
log(`Saved ${results.length} entries to ${outputPath}`);
} else {
process.stdout.write(json);
}
if (errors.length > 0) {
process.exit(1);
}
}
main().catch((err) => {
console.error('Export failed:', err.message);
process.exit(1);
});