1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var os = require('os');
|
5 | var path = require('path');
|
6 | var utils = require('./utils');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function collections(config) {
|
23 | config = config || {};
|
24 |
|
25 | return function plugin(app, base) {
|
26 | if (!utils.isValid(app, 'generate-collections')) return;
|
27 | app.define('home', path.resolve.bind(path, os.homedir()));
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 | app.option(config);
|
34 |
|
35 |
|
36 | if (!app.files) app.create('files', { viewType: 'renderable'});
|
37 | if (!app.includes) app.create('includes', { viewType: 'partial' });
|
38 | if (!app.layouts) app.create('layouts', { viewType: 'layout' });
|
39 |
|
40 |
|
41 | if (app.isGenerator && !app.templates) {
|
42 | app.create('templates', { viewType: 'renderable' });
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | app.preLayout(/./, function(view, next) {
|
50 | if (utils.falsey(view.layout) && !view.isType('partial')) {
|
51 |
|
52 |
|
53 | view.layout = app.resolveLayout(view);
|
54 | if (utils.falsey(view.layout)) {
|
55 | view.layout = 'empty';
|
56 | }
|
57 | next();
|
58 | return;
|
59 | }
|
60 |
|
61 | if (view.isType('partial')) {
|
62 | view.options.layout = null;
|
63 | view.data.layout = null;
|
64 | view.layout = null;
|
65 | if (typeof view.partialLayout === 'string') {
|
66 | view.layout = view.partialLayout;
|
67 | }
|
68 | }
|
69 | next();
|
70 | });
|
71 |
|
72 |
|
73 | var regex = app.options.templatePathRegex || /./;
|
74 | app.templates.preWrite(regex, utils.renameFile(app));
|
75 | app.templates.onLoad(regex, function(view, next) {
|
76 | var userDefined = app.home('templates', view.basename);
|
77 | if (utils.exists(userDefined)) {
|
78 | view.contents = fs.readFileSync(userDefined);
|
79 | }
|
80 | utils.stripPrefixes(view);
|
81 | utils.parser.parse(view, next);
|
82 | });
|
83 |
|
84 |
|
85 | app.layout('empty', {content: '{% body %}'});
|
86 |
|
87 |
|
88 | if (utils.isObject(app.options.create)) {
|
89 | for (var key in app.options.create) {
|
90 | if (!app[key]) {
|
91 | app.create(key, app.options.create[key]);
|
92 | } else {
|
93 | app[key].option(app.options.create[key]);
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 |
|
99 | return plugin;
|
100 | };
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | module.exports = collections();
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | module.exports.create = collections;
|