-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
218 lines (164 loc) · 7.28 KB
/
main.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
const { fetchSchemaList, promptForEntities, fetchMatchingFiles } = require('./github');
const { promptDestinationPath, saveFile, saveFilesFromURL, getFileContent } = require('./fileutil');
const { JSONSchemaFaker } = require('json-schema-faker');
const YAML = require('json-to-pretty-yaml');
const SCHEMALIST = 'schemaList.json';
const fetchSchemata = async (schemastore) => {
const destinationPath = await promptDestinationPath();
if (schemastore) {
await fetchSchemaStoreSchemata()
.then(async result => {
const schemata = result.schemas.flatMap(schema => {
return JSON.parse(`{ "${schema['name']}": "${schema['url']}" }`);
});
const answers = await promptForEntities(schemata);
console.log('Selected entities:', answers.entities);
const files = answers.entities.map(url => {
return JSON.parse(`{ "name":"${url.split('/').pop()}", "download_url": "${url}" }`);
})
saveFilesFromURL(files, destinationPath).then(() => {
console.log('Schemata downloaded successfully');
process.exit(0);
});
})
} else {
const { owner, repo, path } = initialize();
await fetchSchemaList(owner, repo, SCHEMALIST)
.then(schemaList => promptForEntities(schemaList))
.then(async answers => {
const selectedEntities = answers.entities;
console.log('Selected entities:', selectedEntities);
for (const prefix of selectedEntities) {
const matchingFiles = await fetchMatchingFiles(owner, repo, path, prefix);
console.log(`Files matching ${prefix}:`, matchingFiles.map(file => file.name));
await saveFilesFromURL(matchingFiles, destinationPath);
}
})
.catch(error => {
console.error(error.message);
});
}
};
const fetchSchemaStoreSchemata = () => {
const onstSchemaStore = require('@onlang-org/onst-schemastore');
return onstSchemaStore.fetchSchemaFromSchemaStore();
}
const showSchemata = async (schemastore) => {
if (schemastore) {
const result = await fetchSchemaStoreSchemata();
console.log(result.schemas.flatMap(entity => entity['name']));
} else {
const { owner, repo } = initialize();
fetchSchemaList(owner, repo, SCHEMALIST)
.then(schemaList => {
console.log(schemaList.flatMap(entity => Object.keys(entity)));
})
}
}
const createExamples = async (fileLinks, options) => {
for (const link of fileLinks) {
try {
const schema = await getFileContent(link);
const onlExample = YAML.stringify(JSONSchemaFaker.generate(JSON.parse(schema), { useExampleValue: options.example ? true : false, alwaysFakeOptionals: options.random ? true : false }));
if (options.write) {
await saveFile(onlExample, options.file ? options.file : link.split("/").pop().replace(".json", ".onl"), options.destination);
}
else
console.log(onlExample);
} catch (error) {
console.log(`Error creating example for ${link}`);
console.error(error.message);
}
}
};
const generateExampleONL = async (options) => {
if (options.write && !options.destination) {
options['destination'] = await promptDestinationPath();
}
if (options.schemastore) {
const storeSchemas = await fetchSchemaStoreSchemata();
const result = storeSchemas.schemas.flatMap(schema => {
return JSON.parse(`{ "${schema['name']}": "${schema['url']}" }`);
});
await promptForEntities(result).then(async answer => {
const fileLinks = answer.entities;
await createExamples(fileLinks, options).then(() => {
console.log('Examples created successfully');
process.exit(0);
})
});
} else {
const { owner, repo, path } = initialize();
const answers = await fetchSchemaList(owner, repo, SCHEMALIST)
.then(schemaList => promptForEntities(schemaList));
const selectedEntities = answers.entities;
console.log('Selected entities:', selectedEntities);
for (const prefix of selectedEntities) {
const matchingFiles = await fetchMatchingFiles(owner, repo, path, prefix);
await promptForEntities(matchingFiles.map(file => {
const name = file.name;
return JSON.parse(`{ "${name.replace(/\./g, '_')}": "${file.download_url}" }`);
})).then(async answers => {
const fileLinks = answers.entities;
fileLinks.forEach(async fileLink => {
const schema = await getFileContent(fileLink);
const onlExample = YAML.stringify(JSONSchemaFaker.generate(JSON.parse(schema), { useExampleValue: options.example ? true : false, alwaysFakeOptionals: options.random ? true : false }));
if (options.write) {
await saveFile(onlExample, options.file ? options.file : fileLink.split("/").pop().replace(".d.json", ".onl"), options.destination);
}
else
console.log(onlExample);
})
})
}
}
}
function initialize() {
// Load environment variables
if (!process.env.GITHUB_OWNER || !process.env.GITHUB_REPO || !process.env.GITHUB_PATH) {
console.error('GitHub owner, repo, or path is not specified in the .env file.');
//create new env file
const fs = require('fs');
const path = require('path');
const envFilePath = path.join(process.cwd(), '.env');
// Check if .env file exists
if (!fs.existsSync(envFilePath)) {
// Create .env file with default values
const defaultEnvContent = `GITHUB_OWNER=${Environment.GITHUB_OWNER}
GITHUB_REPO=${Environment.GITHUB_REPO}
GITHUB_PATH=${Environment.GITHUB_PATH}`;
fs.writeFileSync(envFilePath, defaultEnvContent);
} else {
// append .env file with default values
if (!process.env.GITHUB_OWNER) {
fs.writeFileSync(envFilePath, `\nGITHUB_OWNER=${Environment.GITHUB_OWNER}`, { flag: 'a' });
}
if (!process.env.GITHUB_REPO) {
fs.writeFileSync(envFilePath, `\nGITHUB_REPO=${Environment.GITHUB_REPO}`, { flag: 'a' });
}
if (!process.env.GITHUB_PATH) {
fs.writeFileSync(envFilePath, `\nGITHUB_PATH=${Environment.GITHUB_PATH}`, { flag: 'a' });
}
}
console.log('.env file created with default values.');
}
require('dotenv').config();
return { owner: process.env.GITHUB_OWNER, repo: process.env.GITHUB_REPO, path: process.env.GITHUB_PATH };
}
const FileType = {
SCHEMA: 'json',
ONLANG: 'onl'
}
const Environment = {
GITHUB_OWNER: 'onlang-org',
GITHUB_REPO: 'onst',
GITHUB_PATH: 'schema'
}
module.exports = {
fetchSchemata,
showSchemata,
fetchSchemaStoreSchemata,
generateExampleONL,
FileType,
Environment
}