forked from jeffThompson/Recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
63 lines (50 loc) · 2.04 KB
/
build.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
const { basename, extname, resolve } = require('path');
const { cpSync, readdirSync, readFileSync, rmSync, mkdirSync } = require('fs');
const { buildRecipes } = require('./src/buildRecipes');
const buildRecipeIndex = require('./src/buildRecipeIndex');
const configs = require('./config');
const MARKDOWN_EXT = '.md';
function getRecipeFileList({ recipesPath } = {}) {
return readdirSync(recipesPath)
.filter(fileName => extname(fileName) === MARKDOWN_EXT)
.map(fileName => ({
file: resolve(recipesPath, fileName),
name: basename(fileName, MARKDOWN_EXT),
}));
}
function readTemplates({ templatesPath }) {
return {
indexTemplate: readFileSync(resolve(templatesPath, 'index.html'), { encoding: 'utf8' }),
recipeTemplate: readFileSync(resolve(templatesPath, 'recipe.html'), { encoding: 'utf8' }),
};
}
function setupOutputDir(outputPath) {
rmSync(outputPath, { recursive: true, force: true });
mkdirSync(outputPath);
}
function copyStatic(staticPath, outputPath) {
['scripts', 'styles'].forEach(dir => cpSync(resolve(staticPath, dir), resolve(outputPath, dir), { recursive: true }));
}
function copyImages(imagesPath, outputPath) {
cpSync(imagesPath, resolve(outputPath, 'images'), { recursive: true });
}
function main(configs) {
const options = {
...configs,
imagesPath: resolve(__dirname, configs.imageDir),
outputPath: resolve(__dirname, configs.outputDir),
recipesPath: resolve(__dirname, configs.recipeDir),
staticPath: resolve(__dirname, './src/static/'),
templatesPath: resolve(__dirname, './src/templates/'),
};
const fileList = getRecipeFileList(options);
const { indexTemplate, recipeTemplate } = readTemplates(options);
setupOutputDir(options.outputPath);
copyStatic(options.staticPath, options.outputPath);
copyImages(options.imagesPath, options.outputPath);
buildRecipes(recipeTemplate, options, fileList);
buildRecipeIndex(indexTemplate, options, fileList);
// eslint-disable-next-line no-console
console.log(`Processed ${fileList.length} recipes`);
}
main(configs);