-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathbuild.js
49 lines (37 loc) · 1.77 KB
/
build.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
// Insomnia file format details:
// https://support.insomnia.rest/article/172-importing-and-exporting-data
const {promises: {readdir, stat, writeFile}} = require('fs');
const path = require('path');
const {createNewResource, insomniaExportTemplate, directoryContainsQuery} = require('./build-helpers');
const kitFilePath = `./builds/storefront-api-learning-kit-insomnia.json`;
const createResources = async (directory = './examples', depth = 0, parentId = 'wrk_1') => {
let resources = [];
const files = await readdir(directory);
for (const [index, fileName] of files.entries()) {
const filePath = path.join(directory, fileName);
let resource;
// We only care about directories
const stats = await stat(filePath);
if (!stats.isDirectory()) { continue; }
// Create an _id for each resource
const _id = `${parentId}_fld_${depth}_${index}`;
const resourceDetails = {fileName, filePath, _id, parentId, metaSortKey: index, stats};
// If directory contains .graphql file, create a request
if (await directoryContainsQuery(filePath)) {
resource = await createNewResource({_type: 'request', ...resourceDetails});
resources.push(resource);
continue;
}
// Otherwise, create a request group and look inside the folder
resource = await createNewResource({_type: 'request_group', ...resourceDetails});
resources.push(resource);
resources = resources.concat(await createResources(filePath, depth + 1, _id));
}
return resources;
};
(async () => {
const resources = await createResources();
insomniaExportTemplate.resources = insomniaExportTemplate.resources.concat(resources);
await writeFile(kitFilePath, JSON.stringify(insomniaExportTemplate));
console.log('Insomnia collection has been exported to', kitFilePath);
})();