-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathfetch-asyncapi-example.js
123 lines (105 loc) · 3.82 KB
/
fetch-asyncapi-example.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
import fs from 'fs';
import unzipper from 'unzipper';
import path from 'path';
import { Parser } from '@asyncapi/parser';
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { RamlDTSchemaParser } from '@asyncapi/raml-dt-schema-parser';
import { pipeline } from 'stream';
import { promisify } from 'util';
const streamPipeline = promisify(pipeline);
const parser = new Parser({
schemaParsers: [
AvroSchemaParser(),
OpenAPISchemaParser(),
RamlDTSchemaParser(),
]
});
const SPEC_EXAMPLES_ZIP_URL = 'https://github.com/asyncapi/spec/archive/refs/heads/master.zip';
const EXAMPLE_DIRECTORY = path.join(__dirname, '../assets/examples');
const TEMP_ZIP_NAME = 'spec-examples.zip';
const fetchAsyncAPIExamplesFromExternalURL = () => {
try {
return new Promise((resolve, reject) => {
fetch(SPEC_EXAMPLES_ZIP_URL)
.then(async (res) => {
if (res.status !== 200) {
return reject(new Error(`Failed to fetch examples from ${SPEC_EXAMPLES_ZIP_URL}`));
}
const file = fs.createWriteStream(TEMP_ZIP_NAME);
await streamPipeline(res.body, file);
console.log('Fetched ZIP file');
resolve();
})
.catch(reject);
});
} catch (error) {
console.error(error);
}
};
const unzipAsyncAPIExamples = async () => {
return new Promise((resolve, reject) => {
if (!fs.existsSync(EXAMPLE_DIRECTORY)) {
fs.mkdirSync(EXAMPLE_DIRECTORY);
}
fs.createReadStream(TEMP_ZIP_NAME)
.pipe(unzipper.Parse())
.on('entry', async (entry) => {
const fileName = entry.path;
if (fileName.includes('examples/') && fileName.includes('.yml') && entry.type === 'File') {
const fileContent = await entry.buffer();
const fileNameWithExtension = fileName.split('examples/')[1];
fs.writeFileSync(path.join(EXAMPLE_DIRECTORY, fileNameWithExtension), fileContent.toString());
} else {
entry.autodrain();
}
}).on('close', () => {
console.log('Unzipped all examples from ZIP');
resolve();
}).on('error', (error) => {
reject(new Error(`Error in unzipping from ZIP: ${error.message}`));
});
});
};
const buildCLIListFromExamples = async () => {
const files = fs.readdirSync(EXAMPLE_DIRECTORY);
const examples = files.filter(file => file.includes('.yml')).sort();
const buildExampleList = examples.map(async example => {
const examplePath = path.join(EXAMPLE_DIRECTORY, example);
const exampleContent = fs.readFileSync(examplePath, { encoding: 'utf-8' });
try {
const { document } = await parser.parse(exampleContent);
// Failed for some reason to parse this spec file (document is undefined), ignore for now
if (!document) {
return;
}
const title = document.info().title();
const protocols = listAllProtocolsForFile(document);
return {
name: protocols ? `${title} - (protocols: ${protocols})` : title,
value: example
};
} catch (error) {
console.error(error);
}
});
const exampleList = (await Promise.all(buildExampleList)).filter(item => !!item);
const orderedExampleList = exampleList.sort((a, b) => a.name.localeCompare(b.name));
fs.writeFileSync(path.join(EXAMPLE_DIRECTORY, 'examples.json'), JSON.stringify(orderedExampleList, null, 4));
};
const listAllProtocolsForFile = (document) => {
const servers = document.servers();
if (servers.length === 0) {
return '';
}
return servers.all().map(server => server.protocol()).join(',');
};
const tidyUp = async () => {
fs.unlinkSync(TEMP_ZIP_NAME);
};
(async () => {
await fetchAsyncAPIExamplesFromExternalURL();
await unzipAsyncAPIExamples();
await buildCLIListFromExamples();
await tidyUp();
})();