|
1 | 1 | /** |
2 | | - * ------------------------------------------------------------------------ |
3 | | - * Usage: |
4 | | - * |
5 | | - * ```shell |
6 | | - * node build.js --env asf # build all for asf |
7 | | - * node build.js --env echartsjs # build all for echartsjs. |
8 | | - * node build.js --env localsite # build all for localsite. |
9 | | - * node build.js --env dev # the same as "debug", dev the content of docs. |
10 | | - * # Check `./config` to see the available env |
11 | | - * ``` |
12 | | - * ------------------------------------------------------------------------ |
| 2 | + * @file For compatibiliy in case that some CI call this file. May not necessary. |
13 | 3 | */ |
14 | 4 |
|
15 | | -const md2json = require('./tool/md2json'); |
16 | | -const {extractDesc} = require('./tool/schemaHelper'); |
17 | | -const fs = require('fs'); |
18 | | -const fse = require('fs-extra'); |
19 | | -const marked = require('marked'); |
20 | | -const copydir = require('copy-dir'); |
21 | | -const chalk = require('chalk'); |
22 | | -// const MarkDownTOCRenderer = require('./tool/MarkDownTOCRenderer'); |
23 | | -const argv = require('yargs').argv; |
24 | | -const path = require('path'); |
25 | | -const assert = require('assert'); |
26 | | -const chokidar = require('chokidar'); |
27 | | -const {debounce} = require('lodash'); |
28 | | -const {getDocJSONPVarNname} = require('./src/shared'); |
29 | | - |
30 | | -const projectDir = __dirname; |
31 | | - |
32 | | -function initEnv() { |
33 | | - let envType = argv.env; |
34 | | - let isDev = argv.dev != null || argv.debug != null || argv.env === 'dev'; |
35 | | - |
36 | | - if (isDev) { |
37 | | - console.warn('============================='); |
38 | | - console.warn('!!! THIS IS IN DEV MODE !!!'); |
39 | | - console.warn('============================='); |
40 | | - envType = 'dev'; |
41 | | - } |
42 | | - |
43 | | - if (!envType) { |
44 | | - throw new Error('--env MUST be specified'); |
45 | | - } |
46 | | - |
47 | | - let config = require('./config/env.' + envType); |
48 | | - |
49 | | - const localOverridePath = './config/env.' + envType + '-local.js'; |
50 | | - if (fs.existsSync(path.join(__dirname, localOverridePath))) { |
51 | | - config = require(localOverridePath); |
52 | | - } |
53 | | - |
54 | | - assert(path.isAbsolute(config.releaseDestDir) && path.isAbsolute(config.ecWWWGeneratedDir)); |
55 | | - |
56 | | - config.envType = envType; |
57 | | - |
58 | | - return config; |
59 | | -} |
60 | | - |
61 | | -const config = initEnv(); |
62 | | - |
63 | | -const languages = ['zh', 'en']; |
64 | | - |
65 | | -config.gl = config.gl || {}; |
66 | | -for (let key in config) { |
67 | | - if (key !== 'gl' && !config.gl.hasOwnProperty(key)) { |
68 | | - config.gl[key] = config[key]; |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -async function md2jsonAsync(opt) { |
73 | | - |
74 | | - var newOpt = Object.assign({ |
75 | | - path: path.join(opt.language, opt.entry, '**/*.md'), |
76 | | - tplEnv: Object.assign({}, config, { |
77 | | - galleryViewPath: config.galleryViewPath.replace('${lang}', opt.language), |
78 | | - galleryEditorPath: config.galleryEditorPath.replace('${lang}', opt.language), |
79 | | - handbookPath: config.handbookPath.replace('${lang}', opt.language) |
80 | | - }), |
81 | | - imageRoot: config.imagePath |
82 | | - }, opt); |
83 | | - |
84 | | - function run(cb) { |
85 | | - md2json(newOpt).then(schema => { |
86 | | - writeSingleSchema(schema, opt.language, opt.entry, false); |
87 | | - writeSingleSchemaPartioned(schema, opt.language, opt.entry, false); |
88 | | - console.log(chalk.green('generated: ' + opt.language + '/' + opt.entry)); |
89 | | - cb && cb(); |
90 | | - }).catch(e => { |
91 | | - console.log(e); |
92 | | - }); |
93 | | - } |
94 | | - |
95 | | - var runDebounced = debounce(run, 500, { |
96 | | - leading: false, |
97 | | - trailing: true |
98 | | - }); |
99 | | - return await new Promise((resolve, reject) => { |
100 | | - run(resolve); |
101 | | - |
102 | | - if (argv.watch) { |
103 | | - chokidar.watch(path.resolve(__dirname, opt.language, opt.entry), { |
104 | | - ignoreInitial: true |
105 | | - }).on('all', (event, path) => { |
106 | | - console.log(path, event); |
107 | | - runDebounced(); |
108 | | - }); |
109 | | - } |
110 | | - }); |
111 | | -} |
112 | | - |
113 | | -function copyAsset() { |
114 | | - |
115 | | - const assetSrcDir = path.resolve(projectDir, 'asset'); |
116 | | - |
117 | | - function doCopy() { |
118 | | - for (let lang of languages) { |
119 | | - const assetDestDir = path.resolve(config.releaseDestDir, `${lang}/documents/asset`); |
120 | | - copydir.sync(assetSrcDir, assetDestDir); |
121 | | - } |
122 | | - } |
123 | | - var doCopyDebounced = debounce(doCopy, 500, { |
124 | | - leading: false, |
125 | | - trailing: true |
126 | | - }); |
127 | | - |
128 | | - doCopy(); |
129 | | - |
130 | | - if (argv.watch) { |
131 | | - chokidar.watch(assetSrcDir, { |
132 | | - ignoreInitial: true |
133 | | - }).on('all', (event, path) => { |
134 | | - console.log(path, event); |
135 | | - doCopyDebounced(); |
136 | | - }); |
137 | | - } |
138 | | - console.log('Copy asset done.'); |
139 | | -} |
140 | | - |
141 | | -async function run() { |
142 | | - |
143 | | - for (let language of languages) { |
144 | | - await md2jsonAsync({ |
145 | | - sectionsAnyOf: ['visualMap', 'dataZoom', 'series', 'graphic.elements', 'dataset.transform'], |
146 | | - entry: 'option', |
147 | | - language |
148 | | - }); |
149 | | - |
150 | | - await md2jsonAsync({ |
151 | | - entry: 'tutorial', |
152 | | - maxDepth: 1, |
153 | | - language |
154 | | - }); |
155 | | - |
156 | | - await md2jsonAsync({ |
157 | | - entry: 'api', |
158 | | - language |
159 | | - }); |
160 | | - |
161 | | - await md2jsonAsync({ |
162 | | - sectionsAnyOf: ['series'], |
163 | | - entry: 'option-gl', |
164 | | - // Overwrite |
165 | | - tplEnv: config.gl, |
166 | | - imageRoot: config.gl.imagePath, |
167 | | - language |
168 | | - }); |
169 | | - } |
170 | | - |
171 | | - console.log('Build doc done.'); |
172 | | - |
173 | | - copyAsset(); |
174 | | - |
175 | | - if (!argv.watch) { // Not in watch dev mode |
176 | | - try { |
177 | | - // TODO Do we need to debug changelog in the doc folder? |
178 | | - buildChangelog(); |
179 | | - buildCodeStandard(); |
180 | | - |
181 | | - copySite(); |
182 | | - } |
183 | | - catch (e) { |
184 | | - console.log('Error happens when copying to dest folders.'); |
185 | | - console.log(e); |
186 | | - } |
187 | | - } |
188 | | - |
189 | | - console.log('All done.'); |
190 | | -} |
191 | | - |
192 | | -function buildChangelog() { |
193 | | - for (let lang of languages) { |
194 | | - const srcPath = path.resolve(projectDir, `${lang}/changelog.md`); |
195 | | - const destPath = path.resolve(config.ecWWWGeneratedDir, `${lang}/documents/changelog-content.html`); |
196 | | - fse.outputFileSync( |
197 | | - destPath, |
198 | | - marked(fs.readFileSync(srcPath, 'utf-8')), |
199 | | - 'utf-8' |
200 | | - ); |
201 | | - console.log(chalk.green('generated: ' + destPath)); |
202 | | - } |
203 | | - console.log('Build changelog done.'); |
204 | | -} |
205 | | - |
206 | | -function buildCodeStandard() { |
207 | | - // support for Chinese character |
208 | | - const customRenderer = new marked.Renderer(); |
209 | | - customRenderer.heading = function(text, level, raw) { |
210 | | - const id = raw.toLowerCase().replace(/[^\w\u4e00-\u9fa5]+/g, '-'); |
211 | | - return `<h${level} id="${id}">${text}</h${level}>\n`; |
212 | | - }; |
213 | | - |
214 | | - for (let lang of languages) { |
215 | | - const codeStandardDestPath = path.resolve(config.ecWWWGeneratedDir, `${lang}/coding-standard-content.html`); |
216 | | - fse.ensureDirSync(path.dirname(codeStandardDestPath)); |
217 | | - fse.outputFileSync( |
218 | | - codeStandardDestPath, |
219 | | - marked(fs.readFileSync(`${lang}/coding-standard.md`, 'utf-8'), { renderer: customRenderer }), |
220 | | - 'utf-8' |
221 | | - ); |
222 | | - console.log(chalk.green('generated: ' + codeStandardDestPath)); |
223 | | - } |
224 | | - |
225 | | - console.log('Build code standard done.'); |
226 | | -} |
227 | | - |
228 | | -function copySite() { |
229 | | - const jsSrcPath = path.resolve(projectDir, 'public/js/doc-bundle.js'); |
230 | | - const cssSrcDir = path.resolve(projectDir, 'public/css'); |
231 | | - |
232 | | - // Copy js and css of doc site. |
233 | | - for (let lang of languages) { |
234 | | - const jsDestPath = path.resolve(config.releaseDestDir, `${lang}/js/doc-bundle.js`); |
235 | | - fse.copySync(jsSrcPath, jsDestPath); |
236 | | - console.log(chalk.green(`js copied to: ${jsDestPath}`)); |
237 | | - |
238 | | - const cssDestDir = path.resolve(config.releaseDestDir, `${lang}/css`); |
239 | | - fse.copySync(cssSrcDir, cssDestDir); |
240 | | - console.log(chalk.green(`css copied to: ${cssDestDir}`)); |
241 | | - } |
242 | | - |
243 | | - console.log('Copy site done.'); |
244 | | -} |
245 | | - |
246 | | -function writeSingleSchema(schema, language, docName, format) { |
247 | | - const destPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}.json`); |
248 | | - fse.ensureDirSync(path.dirname(destPath)); |
249 | | - fse.outputFileSync( |
250 | | - destPath, |
251 | | - format ? JSON.stringify(schema, null, 2) : JSON.stringify(schema), |
252 | | - 'utf-8' |
253 | | - ); |
254 | | - // console.log(chalk.green('generated: ' + destPath)); |
255 | | -} |
256 | | - |
257 | | -function writeSingleSchemaPartioned(schema, language, docName, format) { |
258 | | - const {outline, descriptions} = extractDesc(schema, docName); |
259 | | - |
260 | | - function convertToJS(basename, filePath) { |
261 | | - const content = fs.readFileSync(filePath, 'utf-8'); |
262 | | - const varName = getDocJSONPVarNname(basename); |
263 | | - const code = `window.${varName} = ${content}`; |
264 | | - fs.writeFileSync(filePath.replace(/\.json$/, '.js'), code, 'utf-8'); |
265 | | - } |
266 | | - |
267 | | - const outlineBasename = `${docName}-outline.json`; |
268 | | - const outlineDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${outlineBasename}`); |
269 | | - fse.ensureDirSync(path.dirname(outlineDestPath)); |
270 | | - fse.outputFileSync( |
271 | | - outlineDestPath, |
272 | | - format ? JSON.stringify(outline, null, 2) : JSON.stringify(outline), |
273 | | - 'utf-8' |
274 | | - ); |
275 | | - convertToJS(outlineBasename, outlineDestPath); |
276 | | - |
277 | | - function copyUIControlConfigs(source, target) { |
278 | | - for (let key in source) { |
279 | | - if (target[key]) { |
280 | | - if (source[key].uiControl && !target[key].uiControl) { |
281 | | - target[key].uiControl = source[key].uiControl; |
282 | | - } |
283 | | - if (source[key].exampleBaseOptions && !target[key].exampleBaseOptions) { |
284 | | - target[key].exampleBaseOptions = source[key].exampleBaseOptions; |
285 | | - } |
286 | | - } |
287 | | - else { |
288 | | - // console.error(`Unmatched option path ${key}`); |
289 | | - } |
290 | | - } |
291 | | - } |
292 | | - |
293 | | - function readOptionDesc(language, partKey) { |
294 | | - const descDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${partKey}.json`); |
295 | | - try { |
296 | | - const text = fs.readFileSync(descDestPath, 'utf-8'); |
297 | | - return JSON.parse(text); |
298 | | - } |
299 | | - catch(e) { |
300 | | - return; |
301 | | - } |
302 | | - } |
303 | | - |
304 | | - function writeOptionDesc(language, partKey, json) { |
305 | | - const descBasename = `${partKey}.json`; |
306 | | - const descDestPath = path.resolve(config.releaseDestDir, `${language}/documents/${docName}-parts/${descBasename}`); |
307 | | - fse.ensureDirSync(path.dirname(descDestPath)); |
308 | | - fse.outputFileSync( |
309 | | - descDestPath, |
310 | | - format ? JSON.stringify(json, null, 2) : JSON.stringify(json), |
311 | | - 'utf-8' |
312 | | - ); |
313 | | - convertToJS(descBasename, descDestPath); |
314 | | - } |
315 | | - |
316 | | - for (let partKey in descriptions) { |
317 | | - let partDescriptions = descriptions[partKey]; |
318 | | - |
319 | | - // Copy ui control config from zh to english. |
320 | | - if (language === 'zh') { |
321 | | - languages.forEach(function (otherLang) { |
322 | | - if (otherLang === 'zh') { |
323 | | - return; |
324 | | - } |
325 | | - const json = readOptionDesc(otherLang, partKey); |
326 | | - if (json) { |
327 | | - copyUIControlConfigs(partDescriptions, json); |
328 | | - writeOptionDesc(otherLang, partKey, json); |
329 | | - } |
330 | | - }); |
331 | | - } |
332 | | - else { |
333 | | - const json = readOptionDesc('zh', partKey); |
334 | | - if (json) { |
335 | | - copyUIControlConfigs(json, partDescriptions); |
336 | | - } |
337 | | - } |
338 | | - |
339 | | - writeOptionDesc(language, partKey, partDescriptions); |
340 | | - // console.log(chalk.green('generated: ' + descDestPath)); |
341 | | - } |
342 | | -}; |
343 | | - |
344 | | -run(); |
| 5 | +require('./build/build-doc.js'); |
0 commit comments