forked from apache/echarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
142 lines (128 loc) · 4.8 KB
/
util.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const path = require('path');
const fse = require('fs-extra');
const https = require('https');
const fs = require('fs');
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const util = require('util');
const config = require('./config');
function modifyEChartsCode(code) {
return code.replace(/Math.random/g, '__random__inner__');
}
module.exports.testNameFromFile = function(fileName) {
return path.basename(fileName, '.html');
};
module.exports.fileNameFromTest = function (testName) {
return testName + '.html';
};
function getVersionDir(version) {
version = version || 'local';
return `tmp/__version__/${version}`;
};
module.exports.getVersionDir = getVersionDir;
module.exports.getActionsFullPath = function (testName) {
return path.join(__dirname, 'actions', testName + '.json');
};
module.exports.getEChartsTestFileName = function () {
return `echarts.test-${config.testVersion}.js`;
};
module.exports.prepareEChartsLib = function (version) {
let versionFolder = path.join(__dirname, getVersionDir(version));
fse.ensureDirSync(versionFolder);
if (!version || version === 'local') {
// Developing version, make sure it's new build
fse.copySync(path.join(__dirname, '../../dist/echarts.js'), `${versionFolder}/echarts.js`);
let code = modifyEChartsCode(fs.readFileSync(`${versionFolder}/echarts.js`, 'utf-8'));
fs.writeFileSync(`${versionFolder}/${module.exports.getEChartsTestFileName()}`, code, 'utf-8');
return Promise.resolve();
}
return new Promise(resolve => {
let testLibPath = `${versionFolder}/${module.exports.getEChartsTestFileName()}`;
if (!fs.existsSync(testLibPath)) {
const file = fs.createWriteStream(`${versionFolder}/echarts.js`);
console.log(`Downloading echarts@${version} from `, `https://cdn.jsdelivr.net/npm/echarts@${version}/dist/echarts.js`);
https.get(`https://cdn.jsdelivr.net/npm/echarts@${version}/dist/echarts.js`, response => {
response.pipe(file);
file.on('finish', () => {
let code = modifyEChartsCode(fs.readFileSync(`${versionFolder}/echarts.js`, 'utf-8'));
fs.writeFileSync(testLibPath, code, 'utf-8');
resolve();
});
});
}
else {
resolve();
}
});
};
module.exports.fetchVersions = function () {
return new Promise((resolve, reject) => {
https.get(`https://registry.npmjs.org/echarts`, res => {
if (res.statusCode !== 200) {
res.destroy();
reject('Failed fetch versions from https://registry.npmjs.org/echarts');
return;
}
var buffers = [];
res.on('data', buffers.push.bind(buffers));
res.on('end', function () {
try {
var data = Buffer.concat(buffers);
resolve(Object.keys(JSON.parse(data).versions));
}
catch (e) {
reject(e.toString());
}
});
});
});
};
module.exports.buildRuntimeCode = async function () {
const bundle = await rollup.rollup({
input: path.join(__dirname, 'runtime/main.js'),
plugins: [
resolve(),
commonjs(),
{
resolveId(importee) {
return importee === 'crypto' ? importee : null;
},
load(id) {
// seedrandom use crypto as external module
return id === 'crypto' ? 'export default null;' : null;
}
}
]
});
const output = await bundle.generate({
format: 'iife',
name: 'autorun'
});
return output.code;
};
module.exports.waitTime = function (time) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, time);
});
}