-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.js
190 lines (162 loc) · 4.97 KB
/
meta.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
const octokit = require('@octokit/rest')()
const yaml = require('js-yaml');
const { accessKeyId, secretAccessKey, region, Bucket } = getS3Creds();
const s3 = require('s3');
const s3Client = s3.createClient({
s3Options: {
accessKeyId,
secretAccessKey,
region,
},
});
module.exports = { getSiteConfig }
getSiteConfig();
function getS3Creds() {
return process.env.AWS_ENV ? {
accessKeyId: process.env.AWS_accessKeyId,
secretAccessKey: process.env.AWS_secretAccessKey,
region: process.env.AWS_region,
Bucket: process.env.AWS_Bucket,
} : require('./s3Creds');
}
async function getSiteConfig() {
const config = await getConfig();
const nodeToDir = downloadOutputs(config.contentMap);
return formatSiteConfig(config, nodeToDir, {
'/': {
page: '/index',
query: config
}
});
}
function formatSiteConfig({nodes, contentMap, metaMap}, nodeToDir, baseObj) {
return nodes.reduce((result, config) => ({
...result,
[`/node/${config.key}`]: {
page: '/nodePage',
query: {
config,
content: contentMap[config.key],
meta: metaMap[config.key],
staticDir: nodeToDir[config.key],
}
}
}), baseObj)
}
function downloadOutputs(contentsMap) {
const keyToOuputPaths = Object.keys(contentsMap)
.reduce((result, key) => ({
...result,
[key]: getAllPaths(contentsMap[key])
}), {})
const nodeToFileKey = {}
Object.keys(keyToOuputPaths).forEach(
nodeKey => (
keyToOuputPaths[nodeKey].forEach(sourcePath => {
const Key = getFileKey(nodeKey, sourcePath);
nodeToFileKey[nodeKey] = nodeToFileKey[nodeKey] || {};
nodeToFileKey[nodeKey] = {
...nodeToFileKey[nodeKey],
[sourcePath]: Key
}
const uploader = s3Client.downloadFile({
localFile: `static/built/${Key}`,
s3Params: { Bucket, Key },
});
uploader.on('error', function(err) {
console.error(`unable to download ${Key}`, err.stack);
});
uploader.on('progress', function() {
// future dev enhancement: add progress bar
});
uploader.on('end', () => console.log('downloaded', Key));
})
)
);
return nodeToFileKey;
}
function getFileKey(nodeKey, sourcePath) {
return `${nodeKey}/${getRemotePath(sourcePath)}`;
}
async function getConfig() {
const nodes = await getNodesConfig();
const contentMap = await getContentMap(nodes);
const metaMap = await getMetaMap(nodes);
return {
nodes,
contentMap,
metaMap
}
}
async function getMetaMap(nodesConfig) {
return nodesConfig.reduce(async (resultPromise, {key, repo: nodeRepo }) => {
const result = await resultPromise;
const [ owner, repo ] = trimSlashes(nodeRepo).split('/');
const nodeMeta = await getNodeMeta(owner, repo);
return {
...result,
[key]: nodeMeta
};
}, Promise.resolve({}));
}
async function getContentMap(nodesConfig) {
return nodesConfig.reduce(async (resultPromise, {key, repo: nodeRepo }) => {
const result = await resultPromise;
const [ owner, repo ] = trimSlashes(nodeRepo).split('/');
const nodeContent = await getNodeContent(owner, repo);
return {
...result,
[key]: nodeContent
};
}, Promise.resolve({}));
}
async function getNodesConfig() {
const result = await octokit.repos.getContents({
owner: 'otim-project',
repo: 'root',
path: 'nodes.yaml',
ref: 'master'
});
return parseYamlConfig(result.data.content);
}
async function getNodeMeta(owner, repo) {
const result = await octokit.repos.getContents({
owner,
repo,
path: '.otim/meta.yaml',
ref: 'master'
})
return parseYamlConfig(result.data.content);
}
async function getNodeContent(owner, repo) {
const result = await octokit.repos.getContents({
owner,
repo,
path: '.otim/content.yaml',
ref: 'master'
})
return parseYamlConfig(result.data.content);
}
function parseYamlConfig(rawFile) {
return yaml.load(Buffer.from(rawFile, 'base64').toString());
}
function getRemotePath(sourcePath) {
return `${trimSlashes(strimExtension(sourcePath))}.pdf`;
}
function trimSlashes(s) {
return s.replace(/^\/|\/$/g, '');
}
function getAllPaths(nodes) {
return nodes.reduce((result, {path, sub}) => {
if (sub) {
return [...result, ...getAllPaths(sub)]
}
if (path) {
return [...result, path];
}
return result
}, []);
}
function strimExtension(path) {
return path.split('.').slice(0, -1).join('.')
}