1 | "use strict";
|
2 | var __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 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | const fs = require('fs-extra');
|
13 | const ejs = require('ejs');
|
14 | const fm = require('front-matter');
|
15 | const path = require('path');
|
16 | const walk = require('ignore-walk');
|
17 | const context = require('./context');
|
18 |
|
19 |
|
20 | const map = f => arr => arr.map(f);
|
21 | const filter = f => arr => arr.filter(f);
|
22 | const ignores = [
|
23 | 'prompt.js',
|
24 | 'index.js',
|
25 | '.hygenignore',
|
26 | '.DS_Store',
|
27 | '.Spotlight-V100',
|
28 | '.Trashes',
|
29 | 'ehthumbs.db',
|
30 | 'Thumbs.db',
|
31 | ];
|
32 | const renderTemplate = (tmpl, locals, config) => typeof tmpl === 'string' ? ejs.render(tmpl, context(locals, config)) : tmpl;
|
33 | function 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 | }
|
41 | const 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)))
|
44 | .then(filter(f => !ignores.find(ig => f.endsWith(ig))))
|
45 |
|
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 |
|
54 | obj[key] = renderTemplate(value, args, config);
|
55 | return obj[key] && obj;
|
56 | }, {}),
|
57 | body: renderTemplate(body, args, config),
|
58 | })));
|
59 | });
|
60 | module.exports = render;
|
61 |
|
\ | No newline at end of file |