1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | "use strict";
|
10 |
|
11 | var l = require('ergo-utils').log.module('ergo-api-config');
|
12 | var _ = require('ergo-utils')._;
|
13 | var fs = require('ergo-utils').fs.extend(require('fs-extra'));
|
14 | var path = require('path');
|
15 | var Promise = require('bluebird');
|
16 | var parentFindFile = Promise.promisify(fs.parentFindFile);
|
17 | var Context = require('../lib/context');
|
18 |
|
19 |
|
20 |
|
21 | const CONFIG_NAME = 'config.ergo.js';
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | var default_config = {
|
54 | source_path: "source"
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | , filename_space_char: '-'
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | , default_extension: "html"
|
68 | , exclude : ""
|
69 |
|
70 | , plugins: "default"
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | , date_format : {
|
76 | day : "d",
|
77 | month: "mmm",
|
78 | year: "yyyy",
|
79 | time: "hh:MM",
|
80 | full: "d mmm yyyy",
|
81 | }
|
82 |
|
83 | , default_post_type: 'post'
|
84 | , post_types: {
|
85 |
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | };
|
92 |
|
93 |
|
94 | function _normaliseExt(ext) {
|
95 | if (ext && ext.length && ext[0]=='.')
|
96 | return ext.substr(1);
|
97 | return ext;
|
98 | }
|
99 |
|
100 |
|
101 | function _findConfigFilenameSync(working_dir) {
|
102 | working_dir = working_dir || process.cwd();
|
103 | l.logd('Searching for config.js in ' + working_dir)
|
104 | return fs.parentFindFileSync(working_dir, CONFIG_NAME);
|
105 | }
|
106 |
|
107 | function _findConfigFilename(working_dir) {
|
108 | working_dir = working_dir || process.cwd();
|
109 | l.logd('Searching for config.js in ' + working_dir)
|
110 |
|
111 | return parentFindFile(working_dir, CONFIG_NAME);
|
112 | }
|
113 |
|
114 | var _singleton_context = null;
|
115 |
|
116 | function __getContext(configjs) {
|
117 | if (_singleton_context) {
|
118 | l.logw("Attempt to find a new context, when one has already been created!")
|
119 | return _singleton_context;
|
120 | }
|
121 |
|
122 | if (!configjs) {
|
123 | l.logw('Configuration file not found.')
|
124 | return null;
|
125 | }
|
126 | try {
|
127 | var config = _.extend({}, default_config, require(configjs));
|
128 |
|
129 | var plugin = require('./plugin');
|
130 | plugin.changeDefaultExtension(config.default_extension)
|
131 |
|
132 | _singleton_context = new Context(config, configjs);
|
133 | return _singleton_context;
|
134 | }
|
135 | catch (e) {
|
136 | l.loge(_.niceStackTrace(e))
|
137 | throw e;
|
138 | }
|
139 | }
|
140 |
|
141 | function _getContextSync(working_dir)
|
142 | {
|
143 | if (_singleton_context) {
|
144 | l.logw("Attempt to find a new context, when one has already been created!")
|
145 | return _singleton_context;
|
146 | }
|
147 |
|
148 | return __getContext(_findConfigFilenameSync(working_dir));
|
149 | }
|
150 | function _getContext(working_dir) // async version
|
151 | {
|
152 | if (_singleton_context) {
|
153 | l.logw("Attempt to find a new context, when one has already been created!")
|
154 | return Promise.resolve(_singleton_context);
|
155 | }
|
156 |
|
157 | return _findConfigFilename(working_dir)
|
158 | .then(function(configjs) {
|
159 | return __getContext(configjs)
|
160 | })
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | var _config = {
|
167 | getContextP: _getContext
|
168 | , getContextSync: _getContextSync
|
169 | , findConfigFilenameP: _findConfigFilename
|
170 | , findConfigFilenameSync: _findConfigFilenameSync
|
171 | };
|
172 |
|
173 |
|
174 | module.exports = _config;
|