UNPKG

4.8 kBJavaScriptView Raw
1/**
2 * @license MIT
3 * Copyright (c) 2016 Craig Monro (cmroanirgo)
4 **/
5
6/*
7* This is the api file for 'config' related helpers.
8*/
9"use strict";
10
11var l = require('ergo-utils').log.module('ergo-api-config');
12var _ = require('ergo-utils')._;
13var fs = require('ergo-utils').fs.extend(require('fs-extra'));
14var path = require('path');
15var Promise = require('bluebird');
16var parentFindFile = Promise.promisify(fs.parentFindFile);
17var Context = require('../lib/context');
18
19
20
21const CONFIG_NAME = 'config.ergo.js';
22
23/*
24a Config layout:
25
26var config = {
27 source_path: "source"
28 , layouts_path: "source/_layouts"
29 , partials_path: "source/_partials"
30 , themes_path: "source/_themes"
31 , theme: 'default_skel' // this is a subfolder of 'themes_path'
32 , out_path: "output"
33 , plugins_path: "plugins" // these are generally links to 'node_modules' folder
34 , filename_space_char: '-' // 'when we find this.html' we change it to this: 'when-we-find-this.html'
35
36 , default_properties: {
37 site_url: "http://demosite.example.com"
38 , site_title: "A Demo Site" // used in rss feeds, etc
39 , title: "A Demo Site - " // changed by each page, is the <title> block
40 , author: "Demo Author" // the default author, if needed
41 }
42
43 , plugins: "simpletag,textile,marked"
44 , plugin_options: {
45 textile: { breaks: false }
46 , marked: { ... }
47 }
48}
49*/
50
51
52
53var default_config = {
54 source_path: "source"
55 //, layouts_path: "source/_layouts"
56 //, partials_path: "source/_partials"
57 //, themes_path: "source/_themes"
58 //, theme: 'default_skel' // this is a subfolder of 'themes_path'
59 //, out_path: "output"
60 //, plugins_path: "plugins" // these are generally links to 'node_modules' folder
61 , filename_space_char: '-' // 'when we find this.html' we change it to this: 'when-we-find-this.html'
62
63 //, log_options: {
64 // default:{verbose:0,quiet:false}// & the super-secret 'debug'
65 //}
66
67 , default_extension: "html" // when changed by a user, api/plugin.DEF_EXTENISON is also updated
68 , exclude : "" // also excludes out,partials, themes & layout dirs & config.js as needed
69
70 , plugins: "default" // == simpletag,textile,marked
71 //, plugin_options: {
72 // textile: {breaks:true}
73 // }
74
75 , date_format : {// "dddd, mmmm dS, yyyy, h:MM:ss TT"
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 // ensure that this section is present
86 }
87
88 // shorten_field: "{more}",
89 // shorten_divider: "<a href=\"/{seo}\" class=\"more\">More &hellip;</a>"
90 //shorten_type: "markdown"
91};
92
93
94function _normaliseExt(ext) { // we don't use '.' in our extension info... but some might leak in here and there
95 if (ext && ext.length && ext[0]=='.')
96 return ext.substr(1);
97 return ext;
98}
99
100
101function _findConfigFilenameSync(working_dir) { // returns null if not found
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
107function _findConfigFilename(working_dir) { // returns null if not found
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
114var _singleton_context = null;
115
116function __getContext(configjs) { // always syncronous, due to require(configjs)
117 if (_singleton_context) {
118 l.logw("Attempt to find a new context, when one has already been created!")
119 return _singleton_context; // return the previously found 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
141function _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; // return the previously found context
146 }
147
148 return __getContext(_findConfigFilenameSync(working_dir));
149}
150function _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) // unfortunately, this is a sync' operation only
160 })
161}
162
163
164
165
166var _config = {
167 getContextP: _getContext // 'Promised' version
168 , getContextSync: _getContextSync
169 , findConfigFilenameP: _findConfigFilename // 'Promised' version
170 , findConfigFilenameSync: _findConfigFilenameSync
171};
172
173
174module.exports = _config;