-
Notifications
You must be signed in to change notification settings - Fork 181
/
build.js
339 lines (288 loc) · 10.6 KB
/
build.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* ------------------------------------------------------------------------
* Usage:
*
* ```shell
* node build.js --env asf # build all for asf
* node build.js --env echartsjs # build all for echartsjs.
* node build.js --env localsite # build all for localsite.
* node build.js --env dev # the same as "debug", dev the content of docs.
* # Check `./config` to see the available env
* ```
* ------------------------------------------------------------------------
*/
const md2json = require('./tool/md2json');
const {extractDesc} = require('./tool/schemaHelper');
const fs = require('fs');
const fse = require('fs-extra');
const marked = require('marked');
const copydir = require('copy-dir');
const chalk = require('chalk');
// const MarkDownTOCRenderer = require('./tool/MarkDownTOCRenderer');
const argv = require('yargs').argv;
const path = require('path');
const assert = require('assert');
const chokidar = require('chokidar');
const {debounce} = require('lodash');
const {getDocJSONPVarNname} = require('./src/shared');
const projectDir = __dirname;
function initEnv() {
let envType = argv.env;
let isDev = argv.dev != null || argv.debug != null || argv.env === 'dev';
if (isDev) {
console.warn('=============================');
console.warn('!!! THIS IS IN DEV MODE !!!');
console.warn('=============================');
envType = 'dev';
}
if (!envType) {
throw new Error('--env MUST be specified');
}
let config = require('./config/env.' + envType);
assert(path.isAbsolute(config.releaseDestDir) && path.isAbsolute(config.ecWWWGeneratedDir));
config.envType = envType;
return config;
}
const config = initEnv();
const languages = ['zh', 'en'];
config.gl = config.gl || {};
for (let key in config) {
if (key !== 'gl' && !config.gl.hasOwnProperty(key)) {
config.gl[key] = config[key];
}
}
async function md2jsonAsync(opt) {
var newOpt = Object.assign({
path: path.join(opt.language, opt.entry, '**/*.md'),
tplEnv: Object.assign({}, config, {
galleryViewPath: config.galleryViewPath.replace('${lang}', opt.language),
galleryEditorPath: config.galleryEditorPath.replace('${lang}', opt.language),
handbookPath: config.handbookPath.replace('${lang}', opt.language)
}),
imageRoot: config.imagePath
}, opt);
function run(cb) {
md2json(newOpt).then(schema => {
writeSingleSchema(schema, opt.language, opt.entry, false);
writeSingleSchemaPartioned(schema, opt.language, opt.entry, false);
console.log(chalk.green('generated: ' + opt.language + '/' + opt.entry));
cb && cb();
}).catch(e => {
console.log(e);
});
}
var runDebounced = debounce(run, 500, {
leading: false,
trailing: true
});
return await new Promise((resolve, reject) => {
run(resolve);
if (argv.watch) {
chokidar.watch(path.resolve(__dirname, opt.language, opt.entry), {
ignoreInitial: true
}).on('all', (event, path) => {
console.log(path, event);
runDebounced();
});
}
});
}
function copyAsset() {
const assetSrcDir = path.resolve(projectDir, 'asset');
function doCopy() {
for (let lang of languages) {
const assetDestDir = path.resolve(config.releaseDestDir, `${lang}/documents/asset`);
copydir.sync(assetSrcDir, assetDestDir);
}
}
var doCopyDebounced = debounce(doCopy, 500, {
leading: false,
trailing: true
});
doCopy();
if (argv.watch) {
chokidar.watch(assetSrcDir, {
ignoreInitial: true
}).on('all', (event, path) => {
console.log(path, event);
doCopyDebounced();
});
}
console.log('Copy asset done.');
}
async function run() {
for (let language of languages) {
await md2jsonAsync({
sectionsAnyOf: ['visualMap', 'dataZoom', 'series', 'graphic.elements', 'dataset.transform'],
entry: 'option',
language
});
await md2jsonAsync({
entry: 'tutorial',
maxDepth: 1,
language
});
await md2jsonAsync({
entry: 'api',
language
});
await md2jsonAsync({
sectionsAnyOf: ['series'],
entry: 'option-gl',
// Overwrite
tplEnv: config.gl,
imageRoot: config.gl.imagePath,
language
});
}
console.log('Build doc done.');
copyAsset();
if (!argv.watch) { // Not in watch dev mode
try {
// TODO Do we need to debug changelog in the doc folder?
buildChangelog();
buildCodeStandard();
copySite();
}
catch (e) {
console.log('Error happens when copying to dest folders.');
console.log(e);
}
}
console.log('All done.');
}
function buildChangelog() {
for (let lang of languages) {
const srcPath = path.resolve(projectDir, `${lang}/changelog.md`);
const destPath = path.resolve(config.ecWWWGeneratedDir, `${lang}/documents/changelog-content.html`);
fse.outputFileSync(
destPath,
marked(fs.readFileSync(srcPath, 'utf-8')),
'utf-8'
);
console.log(chalk.green('generated: ' + destPath));
}
console.log('Build changelog done.');
}
function buildCodeStandard() {
// support for Chinese character
const customRenderer = new marked.Renderer();
customRenderer.heading = function(text, level, raw) {
const id = raw.toLowerCase().replace(/[^\w\u4e00-\u9fa5]+/g, '-');
return `<h${level} id="${id}">${text}</h${level}>\n`;
};
for (let lang of languages) {
const codeStandardDestPath = path.resolve(config.ecWWWGeneratedDir, `${lang}/coding-standard-content.html`);
fse.ensureDirSync(path.dirname(codeStandardDestPath));
fse.outputFileSync(
codeStandardDestPath,
marked(fs.readFileSync(`${lang}/coding-standard.md`, 'utf-8'), { renderer: customRenderer }),
'utf-8'
);
console.log(chalk.green('generated: ' + codeStandardDestPath));
}
console.log('Build code standard done.');
}
function copySite() {
const jsSrcPath = path.resolve(projectDir, 'public/js/doc-bundle.js');
const cssSrcDir = path.resolve(projectDir, 'public/css');
// Copy js and css of doc site.
for (let lang of languages) {
const jsDestPath = path.resolve(config.releaseDestDir, `${lang}/js/doc-bundle.js`);
fse.copySync(jsSrcPath, jsDestPath);
console.log(chalk.green(`js copied to: ${jsDestPath}`));
const cssDestDir = path.resolve(config.releaseDestDir, `${lang}/css`);
fse.copySync(cssSrcDir, cssDestDir);
console.log(chalk.green(`css copied to: ${cssDestDir}`));
}
console.log('Copy site done.');
}
function writeSingleSchema(schema, language, docName, format) {
const destPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}.json`);
fse.ensureDirSync(path.dirname(destPath));
fse.outputFileSync(
destPath,
format ? JSON.stringify(schema, null, 2) : JSON.stringify(schema),
'utf-8'
);
// console.log(chalk.green('generated: ' + destPath));
}
function writeSingleSchemaPartioned(schema, language, docName, format) {
const {outline, descriptions} = extractDesc(schema, docName);
function convertToJS(basename, filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const varName = getDocJSONPVarNname(basename);
const code = `window.${varName} = ${content}`;
fs.writeFileSync(filePath.replace(/\.json$/, '.js'), code, 'utf-8');
}
const outlineBasename = `${docName}-outline.json`;
const outlineDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${outlineBasename}`);
fse.ensureDirSync(path.dirname(outlineDestPath));
fse.outputFileSync(
outlineDestPath,
format ? JSON.stringify(outline, null, 2) : JSON.stringify(outline),
'utf-8'
);
convertToJS(outlineBasename, outlineDestPath);
function copyUIControlConfigs(source, target) {
for (let key in source) {
if (target[key]) {
if (source[key].uiControl && !target[key].uiControl) {
target[key].uiControl = source[key].uiControl;
}
if (source[key].exampleBaseOptions && !target[key].exampleBaseOptions) {
target[key].exampleBaseOptions = source[key].exampleBaseOptions;
}
}
else {
// console.error(`Unmatched option path ${key}`);
}
}
}
function readOptionDesc(language, partKey) {
const descDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${partKey}.json`);
try {
const text = fs.readFileSync(descDestPath, 'utf-8');
return JSON.parse(text);
}
catch(e) {
return;
}
}
function writeOptionDesc(language, partKey, json) {
const descBasename = `${partKey}.json`;
const descDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${descBasename}`);
fse.ensureDirSync(path.dirname(descDestPath));
fse.outputFileSync(
descDestPath,
format ? JSON.stringify(json, null, 2) : JSON.stringify(json),
'utf-8'
);
convertToJS(descBasename, descDestPath);
}
for (let partKey in descriptions) {
let partDescriptions = descriptions[partKey];
// Copy ui control config from zh to english.
if (language === 'zh') {
languages.forEach(function (otherLang) {
if (otherLang === 'zh') {
return;
}
const json = readOptionDesc(otherLang, partKey);
if (json) {
copyUIControlConfigs(partDescriptions, json);
writeOptionDesc(otherLang, partKey, json);
}
});
}
else {
const json = readOptionDesc('zh', partKey);
if (json) {
copyUIControlConfigs(json, partDescriptions);
}
}
writeOptionDesc(language, partKey, partDescriptions);
// console.log(chalk.green('generated: ' + descDestPath));
}
};
run();