-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateDb.js
90 lines (79 loc) · 2.56 KB
/
createDb.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
const fs = require("fs");
const glob = require("glob");
const matter = require("gray-matter");
const chokidar = require("chokidar");
const low = require("lowdb");
const yaml = require("js-yaml");
const FileSync = require("lowdb/adapters/FileSync");
const ora = require("ora");
const locateSpinner = ora("Locating config file...");
const initDbSpinner = ora("Creating database...");
const markdownSpinner = ora("Adding content from markdown files...");
module.exports = function initConfig(configFile, dbPath, isWatching, dbName) {
let config;
try {
locateSpinner.stop();
config = yaml.safeLoad(fs.readFileSync(configFile, "utf8"));
locateSpinner.clear();
} catch (err) {
return locateSpinner.fail(
`Ops! Could not locate Netlify config file. Please make sure you are referencing the correct path`
);
}
initDbSpinner.start();
const adapter = new FileSync(dbPath + `/${dbName}.json`);
const db = low(adapter);
initDatabase(config.collections);
markdownSpinner.start();
if (isWatching) {
watchFromMarkdown(config.collections);
markdownSpinner.info("Watching for changes in markdown files...");
} else {
generateFromMarkdown(config.collections);
markdownSpinner.stop();
}
function initDatabase(collections) {
initDbSpinner.color = "yellow";
const emptyDB = collections.reduce((acc, { name }) => {
return { ...acc, [name]: [] };
}, {});
db.defaults(emptyDB).write();
db.setState(emptyDB).write();
initDbSpinner.succeed("Created database!");
}
function generateFromMarkdown(collections) {
console.log(collections);
collections.forEach(({ folder, name }) => {
glob(folder, (err, files) => {
files.forEach((path) => {
addContent(path, name);
});
});
});
}
function watchFromMarkdown(collections) {
collections.forEach(({ folder, name }) => {
chokidar
.watch(folder)
.on("add", (path) => addContent(path, name))
.on("change", (path) => replaceContent(path, name))
.on("unlink", (path) => deleteContent(path, name));
});
}
function addContent(filePath, name) {
const { path, data, content } = matter.read(filePath);
db.get(name)
.push({ path, ...data, content })
.write();
}
function replaceContent(filePath, name) {
const { path, data, content } = matter.read(filePath);
db.get(name)
.find({ path: filePath })
.assign({ path, ...data, content })
.write();
}
function deleteContent(filePath, name) {
db.get(name).remove({ path: filePath }).write();
}
};