Passed
Push — master ( beba88...91d9d9 )
by Björn
02:26
created

config.js ➔ checkGUI   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
c 0
b 0
f 0
nc 2
dl 0
loc 31
rs 5.2653
nop 1

How to fix   Complexity   

Complexity

Complex classes like config.js ➔ checkGUI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
//var extend   = require('util')._extend;
2
var extend   = require('deep-extend');
3
var fs       = require('fs');
4
var path     = require('path');
5
var fm       = require('front-matter');
6
7
8
/**
9
 * checks for pattern doc options
10
 * @param {Patternlibrary} $pl
11
 * @returns
12
 */
13
function checkBase ( $pl ) {
14
	
15
	// patterns dir
16
	if (!$pl.options.partials || ($pl.options.partials == '')) {
17
		throw new Error('To generate pattern documentation a source "partials" path option must be set.');
18
	}
19
	
20
	// destination dir
21
	if (!$pl.options.dest || ($pl.options.dest == '')) {
22
		throw new Error('A destination directory "dest" option must be set.');
23
	}
24
	
25
	// serving basepath
26
	if (!$pl.options.basepath || ($pl.options.basepath == '')) {
27
		throw new Error('An URL "basepath" sub-path option must be set.');
28
	}
29
	
30
	// serving patterns sub-path
31
	if (!$pl.options.patternspath || ($pl.options.patternspath == '')) {
32
		throw new Error('An URL "patternspath" sub-path option must be set.');
33
	}
34
	
35
	// serving categories sub-path
36
	if (!$pl.options.categoriespath || ($pl.options.categoriespath == '')) {
37
		throw new Error('An URL "categoriespath" sub-path option must be set.');
38
	}
39
}
40
41
/**
42
 * checks for patterns options
43
 * @param {Patternlibrary} $pl
44
 * @returns
45
 */
46
function checkPatterns ( $pl ) {
47
	
48
		if (!$pl.options.pattern) {
49
			throw new Error('The pattern\'s options must be defined.');
50
		}
51
		
52
		if (!$pl.options.pattern.dirs) {
53
			throw new Error('The patterns\' "dirs" sub-path option must be set.');
54
		}
55
		if (!$pl.options.pattern.dirs.atoms) {
56
			throw new Error('The patterns\' "atoms" sub-path option must be set.');
57
		}
58
		if (!$pl.options.pattern.dirs.molecules) {
59
			throw new Error('The patterns\' "molecules" sub-path option must be set.');
60
		}
61
		if (!$pl.options.pattern.dirs.organisms) {
62
			throw new Error('The patterns\' "molecules" sub-path option must be set.');
63
		}
64
		if (!$pl.options.pattern.dirs.templates) {
65
			throw new Error('The patterns\' "molecules" sub-path option must be set.');
66
		}
67
		if (!$pl.options.pattern.dirs.pages) {
68
			throw new Error('The patterns\' "molecules" sub-path option must be set.');
69
		}
70
71
		if (!$pl.options.pattern.searchpath) {
72
			throw new Error('The patterns\' "searchpath" sub-path pattern option must be set.');
73
		}
74
		if (!$pl.options.pattern.target) {
75
			throw new Error('The patterns\' "target" filename option must be set.');
76
		}
77
78
}
79
80
/**
81
 * checks for patterns default doc adapter search-patterns options
82
 * @param {Patternlibrary} $pl
83
 * @returns
84
 */
85
function checkPatternsAdapterPatterns ( $pl ) {
86
	
87
		if (!$pl.options.pattern.source) {
88
			throw new Error('The patterns\' default adapter "source" search pattern option must be set.');
89
		}
90
		if (!$pl.options.pattern.readme) {
91
			throw new Error('The patterns\' default adapter "readme" search pattern option must be set.');
92
		}
93
		if (!$pl.options.pattern.scss) {
94
			throw new Error('The patterns\' default adapter "scss" search pattern option must be set.');
95
		}
96
		if (!$pl.options.pattern.javascript) {
97
			throw new Error('The patterns\' default adapter "javascript" search pattern option must be set.');
98
		}
99
		if (!$pl.options.pattern.tests) {
100
			throw new Error('The patterns\' default adapter "tests" search pattern option must be set.');
101
		}
102
		if (!$pl.options.pattern.changelog) {
103
			throw new Error('The patterns\' default adapter "changelog" search pattern option must be set.');
104
		}
105
		
106
}
107
108
/**
109
 * checks for gui options
110
 * @param {Patternlibrary} $pl
111
 * @returns
112
 */
113
function checkGUI ( $pl ) {
114
	if ($pl.options.nogui !== false) {
115
		if (!$pl.options.gui) {
116
			throw new Error('GUI options must be defined.');
117
		}
118
		if (!$pl.options.gui.pages) {
119
			throw new Error('To generate GUI a source "pages" path option must be set.');
120
		}
121
		if (!$pl.options.gui.partials) {
122
			throw new Error('To generate GUI a source "partials" path option must be set.');
123
		}
124
		if (!$pl.options.gui.layouts) {
125
			throw new Error('To generate GUI a source "layouts" path option must be set.');
126
		}
127
		if (!$pl.options.gui.layout) {
128
			throw new Error('To generate GUI a default "layout" template option must be set.');
129
		}
130
		if (!$pl.options.gui.docpage) {
131
			throw new Error('To generate GUI a default "docpage" template option must be set.');
132
		}
133
		if (!$pl.options.gui.dashboard) {
134
			throw new Error('To generate GUI a default "dashboard" template option must be set.');
135
		}
136
		if (!$pl.options.gui.patternlist) {
137
			throw new Error('To generate GUI a default "patternlist" template option must be set.');
138
		}
139
		if (!$pl.options.gui.categorylist) {
140
			throw new Error('To generate GUI a default "categorylist" template option must be set.');
141
		}
142
	}
143
}
144
145
/**
146
 * checks for static pages options
147
 * @param {Patternlibrary} $pl
148
 * @returns
149
 */
150
function checkStaticPages ( $pl ) {
151
	if ($pl.options.staticpages !== false) {
152
		if (!$pl.options.root) {
153
			throw new Error('To generate static pages a source "root" path option must be set.');
154
		}
155
		if (!$pl.options.layouts) {
156
			throw new Error('To generate static pages a source "layouts" path option must be set.');
157
		}
158
	}
159
}
160
161
162
module.exports = function(opts) {
163
164
	if (!opts) {
165
		opts = {};
166
	}
167
	
168
    this.options = extend(this.options, {
169
	    pageRoot  : process.cwd()
170
    }, opts);
171
    
172
    
173
    checkBase(this);
174
    
175
    checkPatterns(this);
176
    checkPatternsAdapterPatterns(this);
177
    
178
    checkGUI(this);
179
    
180
    checkStaticPages(this);
181
182
    // initialises doc page as body-partial and compiled template
183
    /*if (this.options.gui.layout) {
184
        if (this.options.gui.docpage) {
185
		    try {
186
		    	var pagefile   = path.join(this.options.root, this.options.basepath, this.options.pattern.docpage);
187
		        var pageSource = fs.readFileSync(pagefile);
188
		        var page       = fm(pageSource.toString());
189
		    	if (page.attributes.layout != '') {
190
		        	this.layout = page.attributes.layout;
191
		        }
192
		    } catch (e) {
193
		        throw new Error('Error loading Patternlibrary doc page template file: ' + e.message);
194
		    }
195
		    
196
            // Now create Handlebars templates out of them
197
            this.template = this.handlebars.compile(page.body, {noEscape: true});
198
                    
199
            // Finally, add the page as a partial called "body", and render the layout template
200
            this.handlebars.registerPartial('body', this.template);
201
            
202
        } else {
203
            throw new Error('No path to a doc page template was set in Patternlibrary.config().');
204
        }
205
    } else {
206
        throw new Error('No path to a layout was set in Patternlibrary.config().');
207
    }*/
208
209
    return this;
210
}
211