-
Notifications
You must be signed in to change notification settings - Fork 635
/
generate-examples.js
205 lines (173 loc) · 5.97 KB
/
generate-examples.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
202
203
204
205
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var jsdom = require('jsdom');
var Q = require('q');
var onlyStandAlone = [
"0117-basic-routing-show-hide-map-example.html"
];
var isAnExample = function(filename) {
if (filename === '0000-viewer.html') {
return false;
}
return /[0-9][0-9][0-9][0-9].*\.html/.test(filename);
};
var isJavascript = function(filename) {
return /.*\.js/.test(filename);
};
var deleteFileIfJavascript = function(filename) {
var df = Q.defer();
if (isJavascript(filename)) {
fs.unlink(filename, function() {
df.resolve();
});
} else {
df.resolve();
}
return df.promise;
};
var cleanJavascriptFilesFromControllersDirectory = function(dir) {
var df = Q.defer();
fs.readdir(dir, function(err, list) {
var l = [];
var files = list.map(function(file) {
return path.join(dir, file);
});
files.forEach(deleteFileIfJavascript);
Q.allSettled(l).then(function(result) {
df.resolve();
});
});
return df.promise;
};
var writeController = function(script, examplefile, controllers_directory) {
var df = Q.defer();
var scriptLines = script.split('\n');
var outfilename;
var outScript = [];
for (var i = 0; i < scriptLines.length; i++) {
var line = scriptLines[i];
// Remove empty lines
if (line.replace(/^\s+|\s+$/g, '') === '') {
continue;
}
// Remove the module initializacion line
if (line.search('angular.module') !== -1) {
continue;
}
// Extract controller name
if (line.search('app.controller') !== -1 && !outfilename) {
var controller = line.match(/app.controller\((\'|\")([^'"]*)/);
if (controller && controller.length > 2 && controller[2]) {
outfilename = controller[2] + '.js';
}
}
outScript.push(line);
}
if (outfilename) {
outfilename = path.join(controllers_directory, outfilename);
if (!fs.existsSync(outfilename)) {
fs.writeFile(outfilename, outScript.join('\n'), function() {
df.resolve();
});
} else {
console.log('The controller name is duplicated: ' + outfilename)
df.reject();
}
} else {
console.log('Can\'t identify the controller name in the example ' + examplefile)
df.reject();
}
return df.promise;
};
var generateControllersFromExamples = function(examples_directory, controllers_directory) {
var df = Q.defer();
fs.readdir(examples_directory, function(err, list) {
var l = [];
list.forEach(function(filename) {
if (isAnExample(filename)) {
var html = fs.readFileSync(path.join(__dirname, 'examples', filename));
jsdom.env({
html: html.toString(),
done: function(err, window) {
var scripts = window.document.getElementsByTagName('script');
var last = scripts.length -1;
var script = scripts[last].innerHTML;
l.push(writeController(script, filename, controllers_directory));
}
});
}
});
Q.allSettled(l).then(function() {
df.resolve();
});
});
return df.promise;
};
var extractId = function(filename) {
var arr = filename.replace('.html', '').split('-');
arr.splice(0,2);
return arr.join('-');
};
var extractTitle = function(filename) {
var html = fs.readFileSync(path.join(__dirname, 'examples', filename));
var title;
var arr = html.toString().split('\n');
for (var i = 0; i< arr.length; i++) {
var line = arr[i];
if (line.search('<h1>') !== -1) {
title = line.replace('<h1>', '').replace('</h1>', '').replace(/^ */, '');
}
}
return title;
};
var extractDescription = function(filename) {
};
var extractDate = function(filename) {
var stats = fs.statSync(filename);
return stats.mtime;
};
var generateExamplesJSONFile = function(examples_directory, json_file) {
var df = Q.defer();
var examples = {};
fs.readdir(examples_directory, function(err, list) {
list.forEach(function(filename) {
if (isAnExample(filename)) {
var section = filename.split('-')[1];
var id = extractId(filename);
var extUrl = filename;
var title = extractTitle(filename);
var description = extractDescription(filename);
var date = extractDate(path.join(examples_directory, filename));
if (!(section in examples)) {
examples[section] = [];
}
examples[section].push({
date: date,
section: section,
onlyStandAlone: onlyStandAlone.indexOf(extUrl) !== -1,
id: '/' + section + '/' + id,
extUrl: extUrl,
title: title,
description: description
});
}
});
fs.writeFile(json_file, JSON.stringify(examples, null, 4), function(err) {
df.resolve();
});
});
return df.promise;
};
var controllers_directory = path.join(__dirname, 'examples', 'js', 'controllers');
mkdirp(controllers_directory, function(err) {
cleanJavascriptFilesFromControllersDirectory(controllers_directory).then(function() {
var examples_directory = path.join(__dirname, 'examples');
generateControllersFromExamples(examples_directory, controllers_directory).then(function() {
var json_file = path.join(__dirname, 'examples', 'json', 'examples.json');
generateExamplesJSONFile(examples_directory, json_file);
});
});
});