forked from theScottyJam/snap.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildContent.js
47 lines (40 loc) · 1.29 KB
/
buildContent.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
const fs = require('fs')
function tryReadFile(fileName, { fallbackValue }) {
try {
return fs.readFileSync(fileName, 'utf-8')
} catch (error) {
if (error.code === 'ENOENT') {
return fallbackValue
}
throw error;
}
}
function buildContentFile({ sourcePath, destPath }) {
const entryInfo = JSON.parse(fs.readFileSync(`${sourcePath}/info.json`, 'utf-8'))
const entries = entryInfo.map(({categoryHeading, entries}) => {
const loadedEntries = entries.map(name => {
const entryBasePath = `${sourcePath}/${name}`
let src = tryReadFile(`${entryBasePath}/src.js`, { fallbackValue: null });
if (src !== null) {
src = src.trim();
}
return {
name,
manifest: JSON.parse(fs.readFileSync(`${entryBasePath}/manifest.json`, 'utf-8')),
description: fs.readFileSync(`${entryBasePath}/description.md`, 'utf-8'),
src,
test: tryReadFile(`${entryBasePath}/test.js`, { fallbackValue: '' }),
}
})
return { categoryHeading, entries: loadedEntries }
})
fs.writeFileSync(destPath, JSON.stringify(entries), 'utf-8')
}
buildContentFile({
sourcePath: './content/utils',
destPath: './public/utilsContent.json',
})
buildContentFile({
sourcePath: './content/nolodash',
destPath: './public/nolodashContent.json',
})