|
| 1 | +import { build } from 'esbuild'; |
1 | 2 | import fs from 'fs'; |
2 | 3 | import path from 'path'; |
| 4 | +import { localDevConfig } from '../esbuild-config.js'; |
3 | 5 |
|
4 | 6 | import constants from './util/constants.js'; |
5 | 7 | import plotlyNode from './util/plotly_node.mjs'; |
6 | 8 |
|
7 | | -function caseInsensitive(a, b) { |
8 | | - return a.toLowerCase().localeCompare(b.toLowerCase()); |
9 | | -} |
| 9 | +const caseInsensitive = (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()); |
10 | 10 |
|
11 | | -function isArray(v) { |
12 | | - return Array.isArray(v); |
13 | | -} |
| 11 | +const { isArray } = Array; |
14 | 12 |
|
15 | | -function isObject(v) { |
16 | | - return typeof v === 'object' && v !== null && !isArray(v); |
17 | | -} |
| 13 | +const isObject = (v) => typeof v === 'object' && v !== null && !isArray(v); |
18 | 14 |
|
19 | | -function isArrayOfObjects(v) { |
20 | | - return isArray(v) && isObject(v[0]); |
21 | | -} |
| 15 | +const isArrayOfObjects = (v) => isArray(v) && isObject(v[0]); |
22 | 16 |
|
23 | | -function typeHandle(v) { |
24 | | - return isArrayOfObjects(v) ? sortArrayOfObjects(v) : isObject(v) ? sortObject(v) : v; |
25 | | -} |
| 17 | +const typeHandle = (v) => (isArrayOfObjects(v) ? sortArrayOfObjects(v) : isObject(v) ? sortObject(v) : v); |
26 | 18 |
|
27 | 19 | function sortArrayOfObjects(list) { |
28 | | - var newList = []; |
29 | | - for (var i = 0; i < list.length; i++) { |
| 20 | + const newList = []; |
| 21 | + for (let i = 0; i < list.length; i++) { |
30 | 22 | newList[i] = typeHandle(list[i]); |
31 | 23 | } |
32 | 24 |
|
33 | 25 | return newList; |
34 | 26 | } |
35 | 27 |
|
36 | 28 | function sortObject(obj) { |
37 | | - var allKeys = Object.keys(obj); |
| 29 | + const allKeys = Object.keys(obj); |
38 | 30 | allKeys.sort(caseInsensitive); |
39 | 31 |
|
40 | | - var newObj = {}; |
41 | | - for (var i = 0; i < allKeys.length; i++) { |
42 | | - var key = allKeys[i]; |
| 32 | + const newObj = {}; |
| 33 | + for (let i = 0; i < allKeys.length; i++) { |
| 34 | + const key = allKeys[i]; |
43 | 35 | newObj[key] = typeHandle(obj[key]); |
44 | 36 | } |
45 | 37 |
|
46 | 38 | return newObj; |
47 | 39 | } |
48 | 40 |
|
49 | 41 | function makeSchema(plotlyPath, schemaPath) { |
50 | | - var Plotly = plotlyNode(plotlyPath); |
51 | | - |
52 | | - var obj = Plotly.PlotSchema.get(); |
53 | | - var sortedObj = sortObject(obj); |
54 | | - var plotSchemaRaw = JSON.stringify(obj, null, 1); |
55 | | - var plotSchemaStr = JSON.stringify(sortedObj, null, 1); |
| 42 | + const Plotly = plotlyNode(plotlyPath); |
| 43 | + const obj = Plotly.PlotSchema.get(); |
| 44 | + const sortedObj = sortObject(obj); |
| 45 | + const plotSchemaRaw = JSON.stringify(obj, null, 1); |
| 46 | + const plotSchemaStr = JSON.stringify(sortedObj, null, 1); |
56 | 47 |
|
57 | 48 | fs.writeFileSync(schemaPath, plotSchemaStr); |
58 | 49 |
|
59 | | - var lenBeforeSort = plotSchemaRaw.length; |
60 | | - var lenAfterSort = plotSchemaStr.length; |
61 | | - var linesBeforeSort = plotSchemaRaw.split('\n').length; |
62 | | - var linesAfterSort = plotSchemaStr.split('\n').length; |
| 50 | + const lenBeforeSort = plotSchemaRaw.length; |
| 51 | + const lenAfterSort = plotSchemaStr.length; |
| 52 | + const linesBeforeSort = plotSchemaRaw.split('\n').length; |
| 53 | + const linesAfterSort = plotSchemaStr.split('\n').length; |
63 | 54 | if (linesAfterSort !== linesBeforeSort || lenAfterSort !== lenBeforeSort) { |
64 | 55 | throw 'plot schema should have the same length & number of lines before and after sort'; |
65 | 56 | } else { |
66 | 57 | console.log('ok ' + path.basename(schemaPath)); |
67 | 58 | } |
68 | 59 | } |
69 | 60 |
|
70 | | -var isDist = process.argv.indexOf('dist') !== -1; |
| 61 | +const isDist = process.argv.indexOf('dist') !== -1; |
| 62 | + |
| 63 | +const pathToSchema = isDist ? constants.pathToSchemaDist : constants.pathToSchemaDiff; |
71 | 64 |
|
72 | | -var pathToSchema = isDist ? constants.pathToSchemaDist : constants.pathToSchemaDiff; |
| 65 | +const pathToPlotly = isDist ? constants.pathToPlotlyDistWithMeta : constants.pathToPlotlyBuild; |
73 | 66 |
|
74 | | -var pathToPlotly = isDist ? constants.pathToPlotlyDistWithMeta : constants.pathToPlotlyBuild; |
| 67 | +// Build plotly.js to ensure changes to attributes are picked up. This is the same |
| 68 | +// process used in the test dashboard server script, but run only once. |
| 69 | +await build(localDevConfig); |
75 | 70 |
|
76 | 71 | // output plot-schema JSON |
77 | 72 | makeSchema(pathToPlotly, pathToSchema); |
0 commit comments