UNPKG

2.78 kBJavaScriptView Raw
1"use strict";
2
3function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
5function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
7const fs = require('fs-extra');
8
9const ejs = require('ejs');
10
11const fm = require('front-matter');
12
13const path = require('path');
14
15const _require = require('path'),
16 resolve = _require.resolve;
17
18const walk = require('ignore-walk');
19
20const context = require('./context'); // for some reason lodash/fp takes 90ms to load.
21// inline what we use here with the regular lodash.
22
23
24const map = f => arr => arr.map(f);
25
26const filter = f => arr => arr.filter(f);
27
28const ignores = ['prompt.js', 'index.js', '.hygenignore', '.DS_Store', '.Spotlight-V100', '.Trashes', 'ehthumbs.db', 'Thumbs.db'];
29
30const renderTemplate = (tmpl, locals, config) => typeof tmpl === 'string' ? ejs.render(tmpl, context(locals, config)) : tmpl;
31
32function getFiles(_x) {
33 return _getFiles.apply(this, arguments);
34}
35
36function _getFiles() {
37 _getFiles = _asyncToGenerator(function* (dir) {
38 const files = walk.sync({
39 path: dir,
40 ignoreFiles: ['.hygenignore']
41 }).map(f => path.join(dir, f));
42 return files;
43 });
44 return _getFiles.apply(this, arguments);
45}
46
47const render =
48/*#__PURE__*/
49function () {
50 var _ref = _asyncToGenerator(function* (args, config) {
51 return yield getFiles(args.actionfolder).then(things => things.sort((a, b) => a.localeCompare(b))) // TODO: add a test to verify this sort
52 .then(filter(f => !ignores.find(ig => f.endsWith(ig)))) // TODO: add a
53 // test for ignoring prompt.js and index.js
54 .then(filter(file => args.subaction ? file.match(args.subaction) : true)).then(map(file => fs.readFile(file).then(text => ({
55 file,
56 text: text.toString()
57 })))).then(_ => Promise.all(_)).then(map(({
58 file,
59 text
60 }) => Object.assign({
61 file
62 }, fm(text)))).then(map(({
63 file,
64 attributes,
65 body
66 }) => ({
67 file,
68 attributes: Object.entries(attributes).reduce((obj, [key, value]) => (obj[key] = renderTemplate(value, args, config)) && obj, {}),
69 body: renderTemplate(body, args, config)
70 })));
71 });
72
73 return function render(_x2, _x3) {
74 return _ref.apply(this, arguments);
75 };
76}();
77
78module.exports = render;
\No newline at end of file