UNPKG

2.45 kBJavaScriptView Raw
1'use-strict';
2
3// node
4const path = require('path');
5
6// npm
7
8
9const _ = require('lodash');
10const rrddir = require('recursive-readdir');
11const mmatch = require('micromatch'); // https://github.com/micromatch/micromatch
12const cp = require('cp-file');
13const write = require('write');
14const loggerFn = require('eazy-logger').Logger({
15 prefix: '[{blue:penny}] ',
16 useLevelPrefixes: true
17});
18
19
20const ignoreGlobs = [
21 '!**/_*/**/*.*', // ignore _folder
22 '!**/_*.*', // ignore _file
23 '!**/node_modules/**/*', // Node
24 '!**/.DS_Store', // macOS
25 '!.DS_Store', // macOS
26];
27
28const extGlobs = {
29 '.pug': ['**/*.pug'],
30 '.html': ['**/*.html'],
31 '.scss': ['**/*.scss'],
32 '.css': ['**/*.css', '!**/*.min.css'],
33 '.js': ['**/*.js', '!**/*.min.js'],
34};
35
36module.exports = function (srcDir, outDir, options) {
37
38 const { reqSrcExt } = options;
39 const srcOutExt = _.invert(reqSrcExt);
40 const srcExts = Object.keys(srcOutExt);
41 const srcRenderFns = _.mapValues(srcOutExt, (outExt, srcExt) => require(`./render-${srcExt.slice(1)}`)({srcDir, loggerFn, options}));
42 const srcOutReplacers = _.mapValues(srcOutExt, (outExt, srcExt) => {
43 return function(str) { return str.replace(new RegExp(`\\${srcExt}$`), outExt); };
44 });
45
46 /*
47 TODO
48 - render to a temp directory; move everything in to place, only if it all worked
49
50 */
51
52 rrddir(srcDir).then(files => {
53
54 /**
55 * FILTERING
56 */
57
58 const filesBySrc = mmatch(files, ['**/*'].concat(ignoreGlobs))
59 .reduce((obj, file) => {
60 const srcExt = srcExts.find(ext => mmatch(file, extGlobs[ext]).length);
61 obj[srcExt||'other'] = obj[srcExt||'other'] || [];
62 obj[srcExt||'other'].push(path.relative(srcDir, file));
63 return obj;
64 }, {});
65
66 /**
67 * PROCESSING
68 */
69
70 const srcRenders = [].concat(...srcExts.map((ext) => {
71 return filesBySrc[ext]
72 .map((file) => srcRenderFns[ext](path.join(srcDir, file))
73 .then((str) => write(path.join(outDir, srcOutReplacers[ext](file)), str))
74 .catch((err) => {
75
76 /**
77 * ERRORS
78 */
79
80 console.log(err);
81 })
82 );
83 }));
84
85 const copies = filesBySrc['other'].map((file) => cp(path.join(srcDir, file), path.join(outDir, file)));
86
87 Promise.all([...copies, ...srcRenders]).then(([...processed]) => {
88
89 /**
90 * SUCCESS
91 */
92
93 console.log(`processed ${processed.length} files`);
94 });
95 });
96};