|
1 | 1 | const fs = require('fs-extra') |
2 | 2 | const path = require('path') |
3 | | -const themes = ['plgd'] |
| 3 | +const yargs = require('yargs') |
| 4 | +const { promisify } = require('util') |
| 5 | +const childProcess = require('child_process') |
4 | 6 |
|
5 | | -if (process.env.NODE_ENV !== 'production') { |
6 | | - themes.push('siemens') |
7 | | -} |
| 7 | +const exec = promisify(childProcess.exec) |
8 | 8 |
|
9 | | -const packagePath = process.cwd() |
10 | | -const buildPath = path.join(packagePath, './build') |
| 9 | +async function run(argv) { |
| 10 | + const { themes, verbose } = argv |
11 | 11 |
|
12 | | -const generalTheme = { |
13 | | - defaultTheme: 'plgd', |
14 | | - themes: [], |
15 | | -} |
| 12 | + const env = { |
| 13 | + NODE_ENV: 'production', |
| 14 | + BABEL_ENV: 'production', |
| 15 | + } |
| 16 | + |
| 17 | + const extensions = '--extensions .ts,.js,.tsx,.jsx' |
| 18 | + const cwd = path.resolve(__dirname, '../') |
| 19 | + |
| 20 | + // build theme babel |
| 21 | + const commandThemeBabel = `babel ./src/components/Atomic/_theme/ -d build/lib/theme ${extensions}` |
| 22 | + const { stderr, stdout } = await exec(commandThemeBabel, { |
| 23 | + env: { ...process.env, ...env }, |
| 24 | + cwd, |
| 25 | + }) |
| 26 | + |
| 27 | + if (stderr) { |
| 28 | + throw new Error(`'${commandThemeBabel}' failed with \n${stderr}`) |
| 29 | + } |
| 30 | + |
| 31 | + if (verbose && stdout) { |
| 32 | + console.log(stdout) |
| 33 | + } |
| 34 | + |
| 35 | + // build theme color |
| 36 | + const commandThemeColors = `babel ./src/components/Atomic/_utils/colors.ts --out-file build/lib/_utils/colors.js ${extensions}` |
| 37 | + const { stderrColor, stdoutColor } = await exec(commandThemeColors, { |
| 38 | + env: { ...process.env, ...env }, |
| 39 | + cwd, |
| 40 | + }) |
16 | 41 |
|
17 | | -const buildThemePath = path.join(buildPath, 'theme') |
18 | | -const publicThemePath = path.join(buildPath, '..', '..', '..', 'public', 'theme') |
| 42 | + if (stderrColor) { |
| 43 | + throw new Error(`'${commandThemeColors}' failed with \n${stderrColor}`) |
| 44 | + } |
19 | 45 |
|
20 | | -fs.ensureDirSync(buildThemePath) |
21 | | -fs.ensureDirSync(publicThemePath) |
| 46 | + if (verbose && stdoutColor) { |
| 47 | + console.log(stdoutColor) |
| 48 | + } |
22 | 49 |
|
23 | | -themes.forEach((theme) => { |
24 | | - const buildThemeSourcePath = path.join(buildPath, 'lib', 'theme', theme) |
25 | | - const buildColorsSourcePath = path.join(buildPath, 'lib', '_utils', 'colors.js') |
| 50 | + const buildPath = path.join(cwd, './build') |
26 | 51 |
|
27 | | - const d = require(buildThemeSourcePath) |
28 | | - const colors = require(buildColorsSourcePath) |
| 52 | + const generalTheme = { |
| 53 | + defaultTheme: themes[0], |
| 54 | + themes: [], |
| 55 | + } |
29 | 56 |
|
30 | | - fs.writeJsonSync(path.join(buildThemePath, `${theme}.json`), { ...d.default, colorPalette: colors.uiColorMap[theme] }) |
| 57 | + const buildThemePath = path.join(buildPath, 'theme') |
| 58 | + const publicThemePath = path.join(buildPath, '..', '..', '..', 'public', 'theme') |
31 | 59 |
|
32 | | - generalTheme.themes.push({ |
33 | | - [theme]: { ...d.default, colorPalette: colors.uiColorMap[theme] }, |
| 60 | + fs.ensureDirSync(buildThemePath) |
| 61 | + fs.ensureDirSync(publicThemePath) |
| 62 | + |
| 63 | + themes.forEach((theme) => { |
| 64 | + const buildThemeSourcePath = path.join(buildPath, 'lib', 'theme', theme) |
| 65 | + const buildColorsSourcePath = path.join(buildPath, 'lib', '_utils', 'colors.js') |
| 66 | + |
| 67 | + const d = require(buildThemeSourcePath) |
| 68 | + const colors = require(buildColorsSourcePath) |
| 69 | + |
| 70 | + fs.writeJsonSync(path.join(buildThemePath, `${theme}.json`), { ...d.default, colorPalette: colors.uiColorMap[theme] }) |
| 71 | + |
| 72 | + generalTheme.themes.push({ |
| 73 | + [theme]: { ...d.default, colorPalette: colors.uiColorMap[theme] }, |
| 74 | + }) |
34 | 75 | }) |
35 | | -}) |
36 | 76 |
|
37 | | -fs.writeJsonSync(path.join(publicThemePath, `theme.json`), generalTheme) |
| 77 | + fs.writeJsonSync(path.join(publicThemePath, `theme.json`), generalTheme) |
| 78 | + |
| 79 | + if (verbose) { |
| 80 | + console.log('Theme generated!') |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +yargs |
| 85 | + .command({ |
| 86 | + command: '$0', |
| 87 | + description: 'build theme file', |
| 88 | + builder: (command) => |
| 89 | + command |
| 90 | + .option('themes', { |
| 91 | + description: 'Themes array of names.', |
| 92 | + type: 'array', |
| 93 | + default: ['plgd'], |
| 94 | + }) |
| 95 | + .option('verbose', { type: 'boolean', default: true }), |
| 96 | + handler: run, |
| 97 | + }) |
| 98 | + .help() |
| 99 | + .strict(true) |
| 100 | + .version(false) |
| 101 | + .parse() |
0 commit comments