-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.mjs
More file actions
85 lines (81 loc) · 2.39 KB
/
bundle.mjs
File metadata and controls
85 lines (81 loc) · 2.39 KB
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
import path from 'path';
import {readFile, writeFile} from 'fs/promises';
import {promisify} from 'util';
import glob from 'glob';
import jsyaml from 'js-yaml';
import md5 from 'md5';
const outputDir = path.resolve('output');
function main() {
return Promise.all([
promisify(glob)('series/*.yml')
.then((files) => Promise.all(files.map((file) => readFile(file))))
.then((buffers) =>
Promise.all(buffers.map((buffer) => jsyaml.safeLoad(buffer))),
)
.then((array) =>
array.map(({title, books, relatedBooks, authors, concluded}) => ({
series: {
_id: md5(title),
title,
concluded,
books: books.map(({title: bookTitle, ...book}) => ({
...book,
book: md5(bookTitle),
})),
relatedBooks:
relatedBooks?.map((book) => ({
...book,
_id: md5(book.title),
})) || [],
relatedAuthors: authors.map((author) => md5(author.name)),
},
// eslint-disable-next-line no-unused-vars
books: books.map(({serial, ...book}) => ({
...book,
_id: md5(book.title),
isbn: book.isbn ? String(book.isbn) : null,
authors: authors.map(({name, ...author}) => ({
...author,
author: md5(name),
})),
})),
// eslint-disable-next-line no-unused-vars
authors: authors.map(({roles, ...author}) => ({
...author,
_id: md5(author.name),
})),
})),
)
.then((array) =>
array.reduce(
(pre, cur) => ({
series: [...pre.series, cur.series],
books: [...pre.books, ...cur.books],
authors: [...pre.authors, ...cur.authors],
}),
{
books: [],
authors: [],
series: [],
},
),
)
.then(({series, authors, books}) =>
Promise.all([
writeFile(
path.resolve(outputDir, 'series.json'),
JSON.stringify(series),
),
writeFile(
path.resolve(outputDir, 'authors.json'),
JSON.stringify(authors),
),
writeFile(
path.resolve(outputDir, 'books.json'),
JSON.stringify(books),
),
]),
),
]);
}
main();