-
Notifications
You must be signed in to change notification settings - Fork 9
/
bbb-generator.js
144 lines (116 loc) · 4.01 KB
/
bbb-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* BBB generator base class for Yeoman
*/
"use strict";
var path = require("path");
var yo = require("yeoman-generator");
var _ = require("lodash");
var beautify = require("js-beautify");
/**
* BBB Generator base constructor
* Extend Yeoman base generator
*/
var Generator = yo.generators.Base.extend({
constructor: function() {
yo.generators.Base.apply(this, arguments);
// Set default paths
this.destinationRoot(process.cwd());
this.sourceRoot(path.join(__dirname, "../../templates"));
// Set writing filters
this.dest.registerWriteFilter("normalizeJS", this.filterNormalizeJS.bind(this));
this.dest.registerWriteFilter("normalizeJSON", this.filterNormalizeJSON.bind(this));
this.dest.registerWriteFilter("normalizeHTML", this.filterNormalizeHTML.bind(this));
this.src.registerWriteFilter("normalizeJS", this.filterNormalizeJS.bind(this));
this.src.registerWriteFilter("normalizeJSON", this.filterNormalizeJSON.bind(this));
this.src.registerWriteFilter("normalizeHTML", this.filterNormalizeHTML.bind(this));
// Get existing configurations
var packageJSON;
try {
packageJSON = this.dest.readJSON("package.json");
} catch(e) { packageJSON = {}; }
this.pkg = packageJSON;
this.config.defaults({
name : "",
testFramework : "qunit",
moduleStyle : "amd",
indent: {
style : "space",
size : 2
},
paths: {
base : ".",
tests : "test",
modules : "app/modules"
}
});
},
/**
* Stringify an object and normalize whitespace with project preferences.
* @param {object} obj Raw object containing valid JSON value (no functions)
* @return {string} JSON stringified object with normalized whitespace
*/
normalizeJSON: function(obj) {
if (!_.isObject(obj)) { throw new Error("normalizeJSON take an object"); }
var qte = this.config.get("indent").size || 1;
var indentChar = (this.config.get("indent").style === "space") ? " " : "\t";
var indent = "";
while (qte--) {
indent += indentChar;
}
return JSON.stringify(obj, null, indent);
},
filterNormalizeJSON: function(data) {
if (path.extname(data.path) !== ".json") {
return data;
}
data.contents = this.normalizeJSON(JSON.parse(data.contents));
return data;
},
/**
* Normalize a JavaScript code string with project settings
* TODO: Enhance with style guide support
* @param {String} code JavaScript code contained in a String
* @return {String} Normalized JavaScript code
*/
normalizeJS: function(code) {
return beautify.js_beautify(code.toString(), {
"indent_size" : this.config.get("indent").size,
"indent_char" : this.config.get("indent").style === "space" ? " " : "\t",
"indent_with_tabs" : this.config.get("indent").style === "space" ? false : true,
"keep_array_indentation" : true,
"keep_function_indentation" : true,
"space_before_conditional" : true,
"preserve_newlines" : true,
"break_chained_methods" : false
});
},
filterNormalizeJS: function(data) {
if (path.extname(data.path) !== ".js") {
return data;
}
data.contents = this.normalizeJS(data.contents);
return data;
},
/**
* Normalize a HTML code string with project settings
* @param {String} code HTML code contained in a String
* @return {String} Normalized HTML code
*/
normalizeHTML: function(code) {
return beautify.html(code.toString(), {
"indent_size" : this.config.get("indent").size || 1,
"indent_char" : this.config.get("indent").style === "space" ? " " : "\t",
"preserve_newlines" : true,
"wrap_line_length" : 0
});
},
filterNormalizeHTML: function(data) {
if (path.extname(data.path) !== ".html") {
return data;
}
data.contents = this.normalizeHTML(data.contents);
return data;
}
});
Generator._name = "bbb";
module.exports = Generator;