Skip to content

Commit a0293f2

Browse files
theme generator arguments
1 parent d58a226 commit a0293f2

3 files changed

Lines changed: 91 additions & 27 deletions

File tree

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
"build-storybook": "storybook build",
2929
"deploy-storybook": "storybook-to-ghpages --dry-run",
3030
"icons:create": "npx @svgr/cli --config-file ./config/.svgrrc.js -d ./src/components/Atomic/Icon/components ./src/components/Atomic/Icon/assets",
31-
":generate:theme:raw": "NODE_ENV=production BABEL_ENV=production babel ./src/components/Atomic/_theme/ -d build/lib/theme --extensions .ts,.js,.tsx,.jsx,",
32-
":generate:theme:colors": "NODE_ENV=production BABEL_ENV=production babel ./src/components/Atomic/_utils/colors.ts --out-file build/lib/_utils/colors.js --extensions .ts,.js,.tsx,.jsx,",
3331
":generate:theme:build": "node ./scripts/build.theme.js",
3432
":generate:theme:clean": "rimraf ./build/lib",
35-
":generate:theme": "npm-run-all :generate:theme:raw :generate:theme:colors :generate:theme:build :generate:theme:clean"
33+
":generate:theme": "npm-run-all :generate:theme:build :generate:theme:clean"
3634
},
3735
"dependencies": {
3836
"@babel/core": "^7.22.15",

scripts/build.theme.js

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,101 @@
11
const fs = require('fs-extra')
22
const path = require('path')
3-
const themes = ['plgd']
3+
const yargs = require('yargs')
4+
const { promisify } = require('util')
5+
const childProcess = require('child_process')
46

5-
if (process.env.NODE_ENV !== 'production') {
6-
themes.push('siemens')
7-
}
7+
const exec = promisify(childProcess.exec)
88

9-
const packagePath = process.cwd()
10-
const buildPath = path.join(packagePath, './build')
9+
async function run(argv) {
10+
const { themes, verbose } = argv
1111

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+
})
1641

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+
}
1945

20-
fs.ensureDirSync(buildThemePath)
21-
fs.ensureDirSync(publicThemePath)
46+
if (verbose && stdoutColor) {
47+
console.log(stdoutColor)
48+
}
2249

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')
2651

27-
const d = require(buildThemeSourcePath)
28-
const colors = require(buildColorsSourcePath)
52+
const generalTheme = {
53+
defaultTheme: themes[0],
54+
themes: [],
55+
}
2956

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')
3159

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+
})
3475
})
35-
})
3676

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()

src/components/Atomic/_theme/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import lGet from 'lodash/get'
33

44
export type ThemeType = Partial<typeof plgd>
55

6+
export const defaultTheme = 'plgd'
7+
68
export function get(from: object, key: string, def?: any): any {
79
const g = lGet(from, `colors.${key}`, def)
810
return g === '' ? def : g

0 commit comments

Comments
 (0)