-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(natds-themes): add typescript type definitions generation on build
affects: @naturacosmeticos/natds-themes DSY-1463
- Loading branch information
Showing
6 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
|
||
declare const Themes: Themes.Themes | ||
declare namespace Themes { | ||
export interface Color { | ||
primary: string | ||
onPrimary: string | ||
primaryLight: string | ||
onPrimaryLight: string | ||
primaryDark: string | ||
onPrimaryDark: string | ||
secondary: string | ||
onSecondary: string | ||
secondaryLight: string | ||
onSecondaryLight: string | ||
secondaryDark: string | ||
onSecondaryDark: string | ||
background: string | ||
onBackground: string | ||
surface: string | ||
onSurface: string | ||
highlight: string | ||
highEmphasis: string | ||
mediumEmphasis: string | ||
lowEmphasis: string | ||
link: string | ||
onLink: string | ||
success: string | ||
onSuccess: string | ||
warning: string | ||
onWarning: string | ||
alert: string | ||
onAlert: string | ||
} | ||
|
||
export interface BorderRadius { | ||
none: number | ||
small: number | ||
medium: number | ||
large: number | ||
} | ||
|
||
export interface Default { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
lineHeight: number | ||
} | ||
|
||
export interface Button { | ||
default: Default | ||
} | ||
|
||
export interface Heading1 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Heading2 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Heading3 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Heading4 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Heading5 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Heading6 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Subtitle1 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Subtitle2 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Body1 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Body2 { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Caption { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Overline { | ||
fontSize: number | ||
fontWeight: number | ||
letterSpacing: number | ||
} | ||
|
||
export interface Opacity { | ||
transparent: number | ||
lower: number | ||
veryLow: number | ||
low: number | ||
mediumLow: number | ||
disabledLow: number | ||
disabled: number | ||
medium: number | ||
mediumHigh: number | ||
high: number | ||
opaque: number | ||
} | ||
|
||
export interface Size { | ||
none: number | ||
micro: number | ||
tiny: number | ||
small: number | ||
standard: number | ||
semi: number | ||
semiX: number | ||
medium: number | ||
mediumX: number | ||
large: number | ||
largeX: number | ||
largeXX: number | ||
largeXXX: number | ||
huge: number | ||
hugeX: number | ||
hugeXX: number | ||
hugeXXX: number | ||
veryHuge: number | ||
} | ||
|
||
export interface FontFamily { | ||
primary: string | ||
secondary: string | ||
branding: string | ||
code: string | ||
} | ||
|
||
export interface LineHeight { | ||
reset: number | ||
small: number | ||
medium: number | ||
large: number | ||
} | ||
|
||
export interface FontWeight { | ||
regular: number | ||
medium: number | ||
} | ||
|
||
export interface Typography { | ||
fontFamily: FontFamily | ||
lineHeight: LineHeight | ||
fontWeight: FontWeight | ||
} | ||
|
||
export interface Spacing { | ||
none: number | ||
micro: number | ||
tiny: number | ||
small: number | ||
standard: number | ||
semi: number | ||
large: number | ||
xLarge: number | ||
} | ||
|
||
export interface Elevation { | ||
none: string | ||
micro: string | ||
tiny: string | ||
small: string | ||
medium: string | ||
large: string | ||
largeX: string | ||
largeXX: string | ||
huge: string | ||
hugeX: string | ||
hugeXX: string | ||
} | ||
|
||
export interface Theme { | ||
color: Color | ||
borderRadius: BorderRadius | ||
button: Button | ||
heading1: Heading1 | ||
heading2: Heading2 | ||
heading3: Heading3 | ||
heading4: Heading4 | ||
heading5: Heading5 | ||
heading6: Heading6 | ||
subtitle1: Subtitle1 | ||
subtitle2: Subtitle2 | ||
body1: Body1 | ||
body2: Body2 | ||
caption: Caption | ||
overline: Overline | ||
opacity: Opacity | ||
size: Size | ||
typography: Typography | ||
spacing: Spacing | ||
elevation: Elevation | ||
} | ||
|
||
export interface BrandThemes { | ||
dark: Theme | ||
light: Theme | ||
} | ||
|
||
export interface Themes { | ||
aesop: BrandThemes, | ||
avon: BrandThemes, | ||
natura: BrandThemes, | ||
theBodyShop: BrandThemes, | ||
|
||
} | ||
|
||
} | ||
export = Themes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/natds-themes/config/shared/registerTypeDefinitionsAction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs, { existsSync } from 'fs'; | ||
import path from 'path'; | ||
import json2ts from 'json2ts'; | ||
import { brands } from './config'; | ||
|
||
const buildDefinitionPath = (config) => { | ||
const [buildFolder, platformFolder] = config.buildPath.split('/'); | ||
|
||
return `${buildFolder}/${platformFolder}/index.d.ts`; | ||
}; | ||
|
||
const fixTypeDefinitions = (types) => types | ||
.replace('RootObject', 'Theme') | ||
.replace(/;/gm, '') | ||
.replace(/(BorderRadiu)(\s|\n)/gm, '$1s$2'); | ||
|
||
const buildThemesInterface = (brandNames) => ` | ||
export interface BrandThemes { | ||
dark: Theme | ||
light: Theme | ||
} | ||
export interface Themes { | ||
${brandNames.map((brand) => `${brand}: BrandThemes,\n\t`).join('')} | ||
} | ||
`; | ||
|
||
const doAction = (dictionary, config) => { | ||
const definitionsPath = buildDefinitionPath(config); | ||
|
||
if (existsSync(path.join(__dirname, '../../', definitionsPath))) return false; | ||
|
||
const jsonFile = config.files.find(({ destination }) => destination.includes('.json')); | ||
const jsonThemeFile = fs.readFileSync(`${config.buildPath}${jsonFile.destination}`); | ||
const typeDefinitions = json2ts.convert(jsonThemeFile); | ||
|
||
const typeDefinitionFixes = fixTypeDefinitions(typeDefinitions); | ||
|
||
const finalDefinition = ` | ||
declare const Themes: Themes.Themes | ||
declare namespace Themes { | ||
${typeDefinitionFixes} | ||
${buildThemesInterface(brands)} | ||
} | ||
export = Themes | ||
`; | ||
|
||
return fs.writeFileSync(definitionsPath, finalDefinition); | ||
}; | ||
|
||
const undoAction = (dictionary, config) => { | ||
const definitionsPath = buildDefinitionPath(config); | ||
|
||
return fs.unlinkSync(definitionsPath); | ||
}; | ||
|
||
export const registerTypeDefinitionsAction = () => ({ | ||
do: doAction, | ||
name: 'create_type_definitions', | ||
undo: undoAction, | ||
}); | ||
|
||
export default registerTypeDefinitionsAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters