-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfeatures2html.js
202 lines (167 loc) · 6.62 KB
/
features2html.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// DEFAULT SETTINGS
var FILE_ENCODING = 'utf-8';
var INPUTDIR = 'examples/features';
var TEMPLATESDIR = 'default/templates';
var PRODUCTNAME = 'My Product Name';
var AUTHOR = 'John Doe';
var OUTPUTFILE = null;
var LANGUAGE = 'en';
var BREAKBEFOREWORD = null;
var DOCTEMPLATE, FEATURETEMPLATE;
// MODULES
var commander = require('commander'),
fs = require('fs'),
handlebars = require('handlebars'),
linereader = require('line-reader'),
underscore = require('underscore'),
underscorestring = require('underscore.string'),
async = require('async'),
moment = require('moment'),
path = require('path'),
i18n = require('i18next');
// options
commander
.version('0.1')
.option('-i, --input-dir [path]', 'read feature files from path (default: examples/features)')
.option('-t, --templates-dir [path]', 'read the files doc_template.html, feature_template.html and style.css from path (default: default/templates)')
.option('-o, --output-file [path]', 'send output to file path (default: output_features2html/feature_YYYYMMDD_HHmm.html)')
.option('-p, --product-name [string]', 'The name of your product used in headline and header (default: My Product Name)')
.option('-a, --author [string]', 'The author name used in header (default: John Doe)')
.option('-b, --break-before-word [string]', 'create a line break before every occurrance of this word in the background (default: null)')
.option('-l, --lang [en|sv]', 'language used in feature files (default: en)');
// commands
commander
.command('create')
.description('Create html from feature files')
.action(createCommand);
// Check if called without command
if (process.argv.length < 3) {
commander.help();
}
// parse commands
commander.parse(process.argv);
function setup(done) {
INPUTDIR = commander.inputDir || INPUTDIR;
TEMPLATESDIR = commander.templatesDir || TEMPLATESDIR;
OUTPUTFILE = commander.outputFile || OUTPUTFILE;
LANGUAGE = commander.lang || LANGUAGE;
AUTHOR = commander.author || AUTHOR;
PRODUCTNAME = commander.productName || PRODUCTNAME;
BREAKBEFOREWORD = commander.breakBeforeWord || BREAKBEFOREWORD;
DOCTEMPLATE = TEMPLATESDIR + '/doc_template.html';
FEATURETEMPLATE = TEMPLATESDIR + '/feature_template.html';
i18n.init({ lng: LANGUAGE, resGetPath: path.dirname(require.main.filename) + '/locales/__lng__/__ns__.json'}, function(t) {
done();
});
}
function createCommand() {
setup(create);
}
function create(){
var docHandlebarTemplate = handlebars.compile(fs.readFileSync(DOCTEMPLATE, FILE_ENCODING));
var featureHandlebarTemplate = handlebars.compile(fs.readFileSync(FEATURETEMPLATE, FILE_ENCODING));
var cssStyles = fs.readFileSync(TEMPLATESDIR + '/style.css', FILE_ENCODING);
parseFeatures(function(features) {
var featuresHtml = '';
if (features) {
for (var i = 0; i < features.length; i++) {
featuresHtml += featureHandlebarTemplate(features[i]);
}
}
var docData = new Object();
docData.cssStyles = cssStyles;
docData.creationdate = moment().format('LL');
docData.author = AUTHOR;
docData.productname = PRODUCTNAME;
docData.featuresHtml = featuresHtml;
var docHtml = docHandlebarTemplate(docData);
if (OUTPUTFILE) {
writeOutput(docHtml, OUTPUTFILE);
} else {
// write to default output dir. Create first if necessary
fs.mkdir('output_features2html',function(e){
if(!e || (e && e.code === 'EEXIST')){
var outputFilepath = 'output_features2html/features_' + moment().format('YYYYMMDD_HHmm') + '.html';
writeOutput(docHtml, outputFilepath);
} else {
console.log(e);
}
});
}
});
}
function writeOutput(html, outputfile) {
fs.writeFileSync(outputfile, html, FILE_ENCODING);
console.log('DONE! HTML was written to %s', outputfile);
}
function parseFeatures(callback) {
var allFiles = fs.readdirSync(INPUTDIR);
var featureFiles = underscore.filter(allFiles, function(item) {
return underscorestring.endsWith(item,'.feature');
});
var sortedFeatureFiles = featureFiles.sort();
var sortedFeaturesFullpath = underscore.map(sortedFeatureFiles, function(filename) {
return INPUTDIR + '/' + filename;
});
async.mapSeries(sortedFeaturesFullpath, parseFeatureFile, function(err, results) {
callback(results);
});
}
function parseFeatureFile(featureFilename, callback) {
var feature = new Object();
feature.background = '';
feature.scenarios = [];
var scenario = new Object();
scenario.content = '';
var foundMultirowScenario = false;
var featureLineWasFound = false;
var scenariosStarted = false;
linereader.eachLine(featureFilename, function(line) {
if (lineIndicatesThatANewScenarioBegins(line) && foundMultirowScenario) {
// new scenario found. start parsing new scenario
feature.scenarios.push(scenario);
scenario = new Object();
scenario.content = '';
foundMultirowScenario = false;
scenariosStarted = true;
}
if (lineIndicatesThatANewScenarioBegins(line) || foundMultirowScenario) {
// We are parsing a scenario. It may be a new scenario or a row within a scenario
foundMultirowScenario = true;
scenariosStarted = true;
// Handle sidenote
if (i18nStringContains(line, 'sidenote')) {
scenario.sidenote = line.replace(i18n.t('sidenote'), '');
} else {
// Handle scenario content
if (scenario.content) {
scenario.content = scenario.content + '\n' + line;
} else {
scenario.content = line;
}
}
}
if (!i18nStringContains(line, 'feature') && !scenariosStarted && featureLineWasFound) {
// Everything between feature and first scenario goes into feature.background, except background keyword
var fixedline = BREAKBEFOREWORD ? line.replace(BREAKBEFOREWORD, '</p><p class="p-after-p">' + BREAKBEFOREWORD) : line;
fixedline = fixedline + '<br/>';
feature.background = feature.background + ' ' + fixedline.replace(i18n.t('background'), '');
}
if (i18nStringContains(line, 'feature')) {
feature.name = line.replace(i18n.t('feature'), '');
featureLineWasFound = true;
}
}).then(function () {
// Add last scenario, if exists
if (scenario && scenario.content) {
feature.scenarios.push(scenario);
}
callback(null, feature);
});
}
function lineIndicatesThatANewScenarioBegins(line) {
return i18nStringContains(line, 'scenario') || i18nStringContains(line, 'scenario_outline') || i18nStringContains(line, 'sidenote') || i18nStringContains(line, 'background');
}
function i18nStringContains(orgstring, i18nkey) {
return orgstring.indexOf(i18n.t(i18nkey)) !== -1;
}