Skip to content

Commit 265e531

Browse files
authoredJul 7, 2021
Merge pull request #5776 from plotly/plot-schema-script
Sort plot-schema and add test to track plot-schema changes
2 parents 428629e + b29defe commit 265e531

File tree

10 files changed

+68511
-26
lines changed

10 files changed

+68511
-26
lines changed
 

‎.circleci/config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ jobs:
204204
echo https://$CIRCLE_BUILD_NUM-$PROJECT_NUM-gh.circle-artifacts.com/0/dist/plotly.js
205205
echo https://$CIRCLE_BUILD_NUM-$PROJECT_NUM-gh.circle-artifacts.com/0/dist/plotly.min.js
206206
echo https://$CIRCLE_BUILD_NUM-$PROJECT_NUM-gh.circle-artifacts.com/0/dist/plot-schema.json
207+
- run:
208+
name: Test plot-schema.json diff - If failed, after (npm start) you could run (npm run schema && git add test/plot-schema.json && git commit -m "update plot-schema diff")
209+
command: diff --unified --color dist/plot-schema.json test/plot-schema.json
207210
- run:
208211
name: Test plotly.min.js import using requirejs
209212
command: npm run test-requirejs

‎CONTRIBUTING.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ Three additional helpers exist that are refreshed every second:
138138
There is also a search bar in the top right of the dashboard. This fuzzy-searches
139139
image mocks based on their file name and trace type.
140140

141-
#### Alternative to test dashboard
141+
#### Step 5: Regenerate plot-schema in "test" folder then review & commit potential changes
142142

143-
Use the [`plotly-mock-viewer`](https://github.com/rreusser/plotly-mock-viewer)
144-
which has live-reloading and a bunch of other cool features.
145-
An online version of `plotly-mock-viewer` is available at <https://rreusser.github.io/plotly-mock-viewer/>
146-
which uses <https://cdn.plot.ly/plotly-latest.min.js>
143+
```bash
144+
npm run schema
145+
```
146+
147+
#### Step 6: Review & commit potential changes made to test/plot-schema.json
148+
149+
> If you are editing attribute descriptions or implementing a new feature this file located in the test folder records the proposed changes to the API. Note that there is another plot-schema.json file located in the dist folder, which should only be updated by the maintainers at release time.
150+
151+
**IMPORTANT:** please do not change and commit any files in the "dist" folder
147152

148-
#### Other npm scripts
153+
#### Other npm scripts that may be of interest in development
149154

150155
- `npm run preprocess`: pre-processes the css and svg source file in js. This
151156
script must be run manually when updating the css and svg source files.

‎draftlogs/5776_change.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Sort plot-schema and add test to track plot-schema changes [[#5776](https://github.com/plotly/plotly.js/pull/5776)]

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
"custom-bundle": "node tasks/custom_bundle.js",
2525
"bundle": "node tasks/bundle.js",
2626
"extra-bundles": "node tasks/extra_bundles.js",
27+
"schema": "node tasks/schema.js",
2728
"stats": "node tasks/stats.js",
2829
"find-strings": "node tasks/find_locale_strings.js",
2930
"preprocess": "node tasks/preprocess.js",
3031
"use-draftlogs": "node tasks/use_draftlogs.js",
3132
"empty-draftlogs": "node tasks/empty_draftlogs.js",
3233
"empty-dist": "node tasks/empty_dist.js",
33-
"build": "npm run empty-dist && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles && npm run stats",
34+
"build": "npm run empty-dist && npm run preprocess && npm run find-strings && npm run bundle && npm run extra-bundles && npm run schema dist && npm run stats",
3435
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.js",
3536
"watch": "node tasks/watch.js",
3637
"lint": "eslint --version && eslint .",

‎tasks/bundle.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ var prependFile = require('prepend-file');
66
var constants = require('./util/constants');
77
var common = require('./util/common');
88
var _bundle = require('./util/browserify_wrapper');
9-
var makeSchema = require('./util/make_schema');
109
var wrapLocale = require('./util/wrap_locale');
1110

1211
var header = constants.licenseDist + '\n';
1312
var pathToLib = constants.pathToLib;
1413
var pathToDist = constants.pathToDist;
15-
var pathToSchema = constants.pathToSchema;
1614
var pathToPlotlyDist = constants.pathToPlotlyDist;
1715
var pathToPlotlyIndex = constants.pathToPlotlyIndex;
1816
var pathToPlotlyDistMin = constants.pathToPlotlyDistMin;
@@ -67,15 +65,14 @@ tasks.push(function(done) {
6765
});
6866
});
6967

70-
// Browserify plotly.js with meta and output plot-schema JSON
68+
// Browserify plotly.js with meta
7169
tasks.push(function(done) {
7270
_bundle(pathToPlotlyIndex, pathToPlotlyDistWithMeta, {
7371
standalone: 'Plotly',
7472
noCompress: true
7573
}, function() {
7674
prependFile(pathToPlotlyDistWithMeta, header, common.throwOnError);
7775

78-
makeSchema(pathToPlotlyDistWithMeta, pathToSchema);
7976
done();
8077
});
8178
});

‎tasks/schema.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
var constants = require('./util/constants');
5+
var plotlyNode = require('./util/plotly_node');
6+
7+
function caseInsensitive(a, b) {
8+
return a.toLowerCase().localeCompare(b.toLowerCase());
9+
}
10+
11+
function isArray(v) {
12+
return Array.isArray(v);
13+
}
14+
15+
function isObject(v) {
16+
return typeof v === 'object' && v !== null && !(isArray(v));
17+
}
18+
19+
function isArrayOfObjects(v) {
20+
return isArray(v) && isObject(v[0]);
21+
}
22+
23+
function typeHandle(v) {
24+
return (
25+
isArrayOfObjects(v) ? sortArrayOfObjects(v) :
26+
isObject(v) ? sortObject(v) :
27+
v
28+
);
29+
}
30+
31+
function sortArrayOfObjects(list) {
32+
var newList = [];
33+
for(var i = 0; i < list.length; i++) {
34+
newList[i] = typeHandle(list[i]);
35+
}
36+
37+
return newList;
38+
}
39+
40+
function sortObject(obj) {
41+
var allKeys = Object.keys(obj);
42+
allKeys.sort(caseInsensitive);
43+
44+
var newObj = {};
45+
for(var i = 0; i < allKeys.length; i++) {
46+
var key = allKeys[i];
47+
newObj[key] = typeHandle(obj[key]);
48+
}
49+
50+
return newObj;
51+
}
52+
53+
function makeSchema(plotlyPath, schemaPath) {
54+
var Plotly = plotlyNode(plotlyPath);
55+
56+
var obj = Plotly.PlotSchema.get();
57+
var sortedObj = sortObject(obj);
58+
var plotSchemaRaw = JSON.stringify(obj, null, 1);
59+
var plotSchemaStr = JSON.stringify(sortedObj, null, 1);
60+
61+
fs.writeFileSync(schemaPath, plotSchemaStr);
62+
63+
var lenBeforeSort = plotSchemaRaw.length;
64+
var lenAfterSort = plotSchemaStr.length;
65+
var linesBeforeSort = plotSchemaRaw.split('\n').length;
66+
var linesAfterSort = plotSchemaStr.split('\n').length;
67+
if(linesAfterSort !== linesBeforeSort || lenAfterSort !== lenBeforeSort) {
68+
throw 'plot schema should have the same length & number of lines before and after sort';
69+
} else {
70+
console.log('ok ' + path.basename(schemaPath));
71+
}
72+
}
73+
74+
var isDist = process.argv.indexOf('dist') !== -1;
75+
76+
var pathToSchema = isDist ?
77+
constants.pathToSchemaDist :
78+
constants.pathToSchemaDiff;
79+
80+
var pathToPlotly = isDist ?
81+
constants.pathToPlotlyDistWithMeta :
82+
constants.pathToPlotlyBuild;
83+
84+
// output plot-schema JSON
85+
makeSchema(pathToPlotly, pathToSchema);

‎tasks/util/constants.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var pkg = require('../../package.json');
55
var pathToRoot = path.join(__dirname, '../../');
66
var pathToSrc = path.join(pathToRoot, 'src/');
77
var pathToLib = path.join(pathToRoot, 'lib/');
8-
var pathToImageTest = path.join(pathToRoot, 'test/image');
8+
var pathToTest = path.join(pathToRoot, 'test/');
9+
var pathToImageTest = path.join(pathToTest, 'image/');
910
var pathToStrictD3Module = path.join(pathToRoot, 'test/strict-d3.js');
1011
var pathToDraftlogs = path.join(pathToRoot, 'draftlogs/');
1112
var pathToDist = path.join(pathToRoot, 'dist/');
@@ -186,7 +187,8 @@ module.exports = {
186187
pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'),
187188
pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'),
188189

189-
pathToSchema: path.join(pathToDist, 'plot-schema.json'),
190+
pathToSchemaDiff: path.join(pathToTest, 'plot-schema.json'),
191+
pathToSchemaDist: path.join(pathToDist, 'plot-schema.json'),
190192
pathToTranslationKeys: path.join(pathToDist, 'translation-keys.txt'),
191193

192194
partialBundleNames: partialBundleNames,

‎tasks/util/make_schema.js

-13
This file was deleted.

‎tasks/util/watchified_bundle.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = function makeWatchifiedBundle(onFirstBundleCallback) {
2121
var b = browserify(constants.pathToPlotlyIndex, {
2222
debug: true,
2323
standalone: 'Plotly',
24+
ignoreTransform: './tasks/compress_attributes.js',
2425
transform: [],
2526
cache: {},
2627
packageCache: {},

‎test/plot-schema.json

+68,403
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.