forked from betagouv/api.gouv.fr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-dataset.js
85 lines (73 loc) · 2.03 KB
/
build-dataset.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
const { readFile, readdir } = require('fs').promises;
const { join } = require('path');
const frontmatter = require('front-matter');
const API_DIR = join(__dirname, '_api');
const SERVICE_DIR = join(__dirname, '_service');
const DEFAULT_LOGO = 'logo-beta-gouv.svg';
function apiToAPIV1(api) {
return {
title: api.title,
url: `/api/${api.slug}`,
description: api.tagline,
image: api.logo || DEFAULT_LOGO,
domain: api.domain,
owner: api.owner,
owner_acronym: api.owner_acronym,
themes: api.themes,
contract: api.contract,
uptime: api.uptime,
visits_2019: api.visits_2019,
keywords: api.keywords,
clients: api.clients,
partners: api.partners,
};
}
function expandAPIsWithServices(apis, services) {
services.forEach(service => {
if (service.api && service.api.length > 0) {
service.api.forEach(apiTitle => {
const api = apis.find(api => api.title === apiTitle);
if (!api.services) {
api.services = [];
}
api.services.push(service.slug);
});
}
});
}
function expandServicesWithAPIs(services, apis) {
services.forEach(service => {
const apiList = service.api.map(title => {
const api = apis.find(api => api.title === title);
return api.slug;
});
service.api = apiList;
});
}
async function readMarkdownDir(dir) {
const files = await readdir(dir);
return Promise.all(
files.map(async fileName => {
const filePath = join(dir, fileName);
const file = await readFile(filePath, { encoding: 'utf-8' });
const json = frontmatter(file);
return {
slug: fileName.split('.md')[0],
...json.attributes,
content: json.body,
};
})
);
}
async function buildDataset() {
const apis = await readMarkdownDir(API_DIR);
const services = await readMarkdownDir(SERVICE_DIR);
expandAPIsWithServices(apis, services);
expandServicesWithAPIs(services, apis);
return {
summary: apis.map(apiToAPIV1),
apis,
services,
};
}
module.exports = { buildDataset };