blob: 3d40e2dc8b66b21346467dd6269dbd388d614ef8 [file]
#!/usr/bin/env node
/*
* 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 nodeFS = require('fs');
const nodePath = require('path');
const assert = require('assert');
const commander = require('commander');
const chalk = require('chalk');
const globby = require('globby');
/**
* [NOTICE]:
* Require `echarts-examples` e2e test to be performed (see `echarts-examples/README.md`).
* This script copy the generated bundles as `echarts/test/ZEXAMPLE_*.html` to perform visual test.
*/
const ECHARTS_EXAMPLES_DIR = nodePath.resolve(__dirname, '../../../echarts-examples');
const ECHARTS_EXAMPLES_E2E_TEMPLATE_PATH = nodePath.resolve(ECHARTS_EXAMPLES_DIR, 'e2e/template.html');
const ECHARTS_EXAMPLES_E2E_GENERATED_BUNDLES_DIR = nodePath.resolve(ECHARTS_EXAMPLES_DIR, 'e2e/tmp/bundles');
const ECHARTS_TEST_HTML_DIR = nodePath.resolve(__dirname, '..');
const TEMPLATE_REPLACE_TOKEN = '<!-- [DO_NOT_MODIFY_THIS_LINE_USED_BY_EC_VISUAL_TEST_INJECTING_SCRIPT] -->';
const GENEREATED_TEST_PREFIXE = 'ZEXAMPLE_';
async function main() {
if(!dirExist(ECHARTS_EXAMPLES_DIR)) {
throw new Error(`${ECHARTS_EXAMPLES_DIR} is required!`);
}
if(!dirExist(ECHARTS_EXAMPLES_E2E_GENERATED_BUNDLES_DIR)) {
throw new Error(
`${ECHARTS_EXAMPLES_E2E_GENERATED_BUNDLES_DIR} is missing, which is generated by e2e test.`
+ ` See ${ECHARTS_EXAMPLES_DIR}/README.md for the guide.`
);
}
const tplContent = nodeFS.readFileSync(ECHARTS_EXAMPLES_E2E_TEMPLATE_PATH, {encoding:'utf-8'})
const bundlePathList = (await readFilePaths({
patterns: ['*.js'],
cwd: ECHARTS_EXAMPLES_E2E_GENERATED_BUNDLES_DIR
})).filter(bundlePath => {
return bundlePath.relative.indexOf('.minimal.') < 0
});
bundlePathList.forEach(bundlePath => {
if (!fileExist(bundlePath.absolute)) {
console.error(`${bundlePath.absolute} does not exists!`);
return;
}
const bundleContent = nodeFS.readFileSync(bundlePath.absolute, {encoding: 'utf-8'});
const testContent = tplContent.replace(
TEMPLATE_REPLACE_TOKEN,
// Use a cb to avoid `$$` => `$` issue of JS replace method.
() => `<script>
$.ajaxPrefilter(function (options) {
try {
// echarts-examples use port 3322. need rewrite.
const url = new URL(options.url, window.location.origin);
if (url.port === '3322') {
url.port = window.location.port;
options.url = url.toString();
}
} catch (e) {
console.log(e); // Invalid URL.
}
});
(function () {
${bundleContent}
})();
</script>`
);
const bundleName = nodePath.basename(bundlePath.relative, '.js');
const generatedHTMLPath =
nodePath.resolve(ECHARTS_TEST_HTML_DIR, `${GENEREATED_TEST_PREFIXE}${bundleName}.html`);
console.log(`Writing to ${generatedHTMLPath}`);
nodeFS.writeFileSync(generatedHTMLPath, testContent, {encoding: 'utf-8'});
});
console.log('All accomplished.');
}
function dirExist(path) {
return nodeFS.existsSync(path) && nodeFS.statSync(path).isDirectory();
}
function fileExist(path) {
return nodeFS.existsSync(path) && nodeFS.statSync(path).isFile();
}
/**
* @return Absolute path list.
*/
async function readFilePaths({patterns, cwd}) {
assert(patterns && cwd);
return (
// Return an array. Relative path.
await globby(patterns, {cwd})
).map(srcPath => ({
relative: srcPath,
absolute: nodePath.resolve(cwd, srcPath),
}));
}
main();