UNPKG

2.76 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const fs = require('fs-extra');
13const ejs = require('ejs');
14const fm = require('front-matter');
15const path = require('path');
16const walk = require('ignore-walk');
17const context = require('./context');
18// for some reason lodash/fp takes 90ms to load.
19// inline what we use here with the regular lodash.
20const map = f => arr => arr.map(f);
21const filter = f => arr => arr.filter(f);
22const ignores = [
23 'prompt.js',
24 'index.js',
25 '.hygenignore',
26 '.DS_Store',
27 '.Spotlight-V100',
28 '.Trashes',
29 'ehthumbs.db',
30 'Thumbs.db',
31];
32const renderTemplate = (tmpl, locals, config) => typeof tmpl === 'string' ? ejs.render(tmpl, context(locals, config)) : tmpl;
33function getFiles(dir) {
34 return __awaiter(this, void 0, void 0, function* () {
35 const files = walk
36 .sync({ path: dir, ignoreFiles: ['.hygenignore'] })
37 .map(f => path.join(dir, f));
38 return files;
39 });
40}
41const render = (args, config) => __awaiter(void 0, void 0, void 0, function* () {
42 return getFiles(args.actionfolder)
43 .then(things => things.sort((a, b) => a.localeCompare(b))) // TODO: add a test to verify this sort
44 .then(filter(f => !ignores.find(ig => f.endsWith(ig)))) // TODO: add a
45 // test for ignoring prompt.js and index.js
46 .then(filter(file => (args.subaction ? file.match(args.subaction) : true)))
47 .then(map(file => fs.readFile(file).then(text => ({ file, text: text.toString() }))))
48 .then(_ => Promise.all(_))
49 .then(map(({ file, text }) => Object.assign({ file }, fm(text))))
50 .then(map(({ file, attributes, body }) => ({
51 file,
52 attributes: Object.entries(attributes).reduce((obj, [key, value]) => {
53 // eslint-disable-next-line no-param-reassign
54 obj[key] = renderTemplate(value, args, config);
55 return obj[key] && obj;
56 }, {}),
57 body: renderTemplate(body, args, config),
58 })));
59});
60module.exports = render;
61//# sourceMappingURL=render.js.map
\No newline at end of file