#!/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 = ''; 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. () => `` ); 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();