UNPKG

mstr-viz

Version:

A new dev tool for creating custom visualizations

130 lines (118 loc) 3.96 kB
const path = require('path'); const fs = require('fs-extra'); const { spawnSync } = require('child_process'); const write = require('../utils/write'); const copy = require('../utils/copy'); const space = require('../utils/space'); const generateConfigXML = require('./generateConfigXML'); const generateStyleCatalog = require('./generateStyleCatalog'); const { plugin_name, path_of_plugins, manual, icon_name, format_panel_build_entry, source_map, } = require('./loadConfig')(process.cwd()); // todo: unit test const MODE = process.argv[2]; const LOCAL = MODE === 'local'; const DEV = MODE === 'development'; const WEB_INF_ONLY = MODE === 'web-inf'; const PROJECT_ROOT = process.cwd(); const PLUGIN_DIR = path.resolve( LOCAL ? path.resolve(PROJECT_ROOT, 'dist') : path_of_plugins, plugin_name, ); const WEB_INF_DIR = path.resolve(PLUGIN_DIR, 'WEB-INF'); const I18N_DIR = path.resolve(WEB_INF_DIR, 'classes/resources'); const STYLE_CATALOG_FILE = path.resolve(WEB_INF_DIR, 'xml/styleCatalog.xml'); const CONFIGURATION_FILE = path.resolve( WEB_INF_DIR, 'xml/config/visualizations.xml', ); const CUSTOM_VIS_TOKEN = path.resolve(PLUGIN_DIR, '_custom_vis_'); const buildWebInf = () => { if (manual && fs.existsSync(path.join(PROJECT_ROOT, 'WEB-INF'))) { fs.ensureDirSync(WEB_INF_DIR); fs.copySync(path.join(PROJECT_ROOT, 'WEB-INF'), WEB_INF_DIR); } else { // generate xml config const configXML = generateConfigXML(plugin_name); write(CONFIGURATION_FILE, configXML); // generate style catalog const styleCatalog = generateStyleCatalog(plugin_name); write(STYLE_CATALOG_FILE, styleCatalog); } // copy i18n file const i18nDir = path.join(PROJECT_ROOT, 'i18n'); if (fs.existsSync(i18nDir)) { fs.ensureDirSync(I18N_DIR); fs.readdirSync(i18nDir) .filter(f => f.endsWith('.properties')) .forEach(f => { fs.copyFileSync(path.join(i18nDir, f), path.join(I18N_DIR, f)); }); } }; const buildJS = () => { // get icon file const iconFileName = icon_name || 'icon.'; const iconFile = fs .readdirSync(path.join(PROJECT_ROOT, 'src/images')) .find(name => name.startsWith(iconFileName)) || ''; // there's no icon file // if (!iconFile) { // throw new Error('Please provide a icon file in the project root directory!'); // } const mode = DEV ? 'development' : 'production'; // execSync('npm link mstr-viz'); // todo-yimin: add test case for there's whitespaces in PROJECT_ROOT const shouldBuildReact = format_panel_build_entry !== undefined && format_panel_build_entry !== ''; const commonConfigs = [ 'webpack', `--env=mode=${mode}`, `--env=projectRoot=${space.escape(PROJECT_ROOT)}`, `--env=distDir=${space.escape(PLUGIN_DIR)}`, `--env=icon=${iconFile}`, `--env=pluginName=${plugin_name}`, `--env=formatPanelSrcBuildEntry=${format_panel_build_entry}`, ]; if (source_map) { commonConfigs.push(`--env=sourceMap=${source_map}`); } const { status } = spawnSync( 'npx', commonConfigs.concat(`--env=shouldBuildReact=${shouldBuildReact}`), { stdio: 'inherit', shell: process.platform === 'win32', cwd: __dirname }, ); if (status !== 0) { process.exit(status); } }; module.exports = () => { if (!LOCAL && fs.existsSync(PLUGIN_DIR) && !fs.existsSync(CUSTOM_VIS_TOKEN)) { throw new Error( `There's already a visualization named ${plugin_name} in directory '${path_of_plugins}'!`, ); } // init plugin dir and create token file if (!fs.existsSync(CUSTOM_VIS_TOKEN)) { write(CUSTOM_VIS_TOKEN, "IT'S JUST A TOKEN FILE"); } if (DEV) { if (!fs.existsSync(WEB_INF_DIR)) { buildWebInf(); } buildJS(); } else if (WEB_INF_ONLY) { buildWebInf(); } else { // default buildWebInf(); buildJS(); } // copy readme & changelog copy(PROJECT_ROOT, PLUGIN_DIR, 'README.md'); copy(PROJECT_ROOT, PLUGIN_DIR, 'CHANGELOG.md'); };