Skip to content

Commit b3bea1a

Browse files
add logging utility fn (#1143)
* add logging utility fn * reset * fix import
1 parent 1433b8b commit b3bea1a

File tree

7 files changed

+131
-13
lines changed

7 files changed

+131
-13
lines changed

scripts/buildTokens.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
207207
`src/tokens/functional/color/light/*.json5`,
208208
`src/tokens/functional/shadow/light.json5`,
209209
`src/tokens/functional/border/*.json5`,
210+
`src/tokens/component/*.json5`,
210211
],
211212
include: [`src/tokens/base/color/light/light.json5`, 'src/tokens/functional/size/border.json5'],
212213
},
@@ -238,6 +239,13 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
238239
platforms: {
239240
deprecated: deprecatedJson(`deprecated/${filename}.json`, buildOptions.prefix, buildOptions.buildPath),
240241
},
242+
log: {
243+
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
244+
verbosity: 'silent', // 'default' | 'silent' | 'verbose'
245+
errors: {
246+
brokenReferences: 'throw', // 'throw' | 'console'
247+
},
248+
},
241249
})
242250
await extendedSD.buildAllPlatforms()
243251
}

src/transformers/colorToHex.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {toHex} from 'color2k'
22
import {isColor} from '../filters/index.js'
33
import {getTokenValue} from './utilities/getTokenValue.js'
4-
import type {Transform, TransformedToken} from 'style-dictionary/types'
4+
import type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types'
55
import {alpha} from './utilities/alpha.js'
66
/**
77
* @description converts color tokens value to `hex6` or `hex8`
@@ -14,11 +14,11 @@ export const colorToHex: Transform = {
1414
type: 'value',
1515
transitive: true,
1616
filter: isColor,
17-
transform: (token: TransformedToken) => {
17+
transform: (token: TransformedToken, config: PlatformConfig) => {
1818
const alphaValue = token.alpha
1919
if (alphaValue === null || alphaValue === undefined) {
2020
return toHex(getTokenValue(token))
2121
}
22-
return toHex(alpha(getTokenValue(token), alphaValue, token))
22+
return toHex(alpha(getTokenValue(token), alphaValue, token, config))
2323
},
2424
}

src/transformers/colorToRgbAlpha.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {isColorWithAlpha} from '../filters/index.js'
22
import {alpha} from './utilities/alpha.js'
33
import {getTokenValue} from './utilities/getTokenValue.js'
4-
import type {Transform, TransformedToken} from 'style-dictionary/types'
4+
import type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types'
55
/**
66
* @description replaces tokens value with `rgba` color using the tokens `alpha` property to specify the value used for alpha
77
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
@@ -13,8 +13,8 @@ export const colorToRgbAlpha: Transform = {
1313
type: 'value',
1414
transitive: true,
1515
filter: isColorWithAlpha,
16-
transform: (token: TransformedToken) => {
16+
transform: (token: TransformedToken, config: PlatformConfig) => {
1717
if (token.alpha === null) return getTokenValue(token)
18-
return alpha(getTokenValue(token), token.alpha, token)
18+
return alpha(getTokenValue(token), token.alpha, token, config)
1919
},
2020
}

src/transformers/shadowToCss.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {alpha} from './utilities/alpha.js'
44
import {checkRequiredTokenProperties} from './utilities/checkRequiredTokenProperties.js'
55
import type {ShadowTokenValue} from '../types/shadowTokenValue.js'
66
import {getTokenValue} from './utilities/getTokenValue.js'
7-
import type {Transform, TransformedToken} from 'style-dictionary/types'
7+
import type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types'
88

99
/**
1010
* @description converts w3c shadow tokens in css shadow string
@@ -17,7 +17,7 @@ export const shadowToCss: Transform = {
1717
type: 'value',
1818
transitive: true,
1919
filter: isShadow,
20-
transform: (token: TransformedToken) => {
20+
transform: (token: TransformedToken, config: PlatformConfig) => {
2121
// extract value
2222
const value: ShadowTokenValue | ShadowTokenValue[] = getTokenValue(token)
2323
const valueProp = token.$value ? '$value' : 'value'
@@ -32,7 +32,7 @@ export const shadowToCss: Transform = {
3232
/*css box shadow: inset? | offset-x | offset-y | blur-radius | spread-radius | color */
3333
return `${shadow.inset === true ? 'inset ' : ''}${shadow.offsetX} ${shadow.offsetY} ${shadow.blur} ${
3434
shadow.spread
35-
} ${toHex(alpha(getTokenValue({...token, ...{[valueProp]: shadow}}, 'color'), shadow.alpha || 1, token))}`
35+
} ${toHex(alpha(getTokenValue({...token, ...{[valueProp]: shadow}}, 'color'), shadow.alpha || 1, token, config))}`
3636
})
3737
.join(', ')
3838
},

src/transformers/utilities/alpha.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import {rgba, parseToRgba} from 'color2k'
2-
import type {TransformedToken} from 'style-dictionary/types'
2+
import type {PlatformConfig, TransformedToken} from 'style-dictionary/types'
3+
import {log} from '../../utilities/log.js'
34
/**
45
* alpha
56
* @description takes a colors string like hex or rgba and returns an rgba color with the specified alpha value
67
* @param color color string like a `#334455` or `rgb(255,200,100)`
78
* @param desiredAlpha number
89
* @returns rgba value
910
*/
10-
export const alpha = (color: string, desiredAlpha: number, token?: TransformedToken): string => {
11+
export const alpha = (
12+
color: string,
13+
desiredAlpha: number,
14+
token?: TransformedToken,
15+
config?: PlatformConfig,
16+
): string => {
1117
const [r, g, b, a] = parseToRgba(color)
1218

1319
if (a < 1 && desiredAlpha < 1) {
14-
// eslint-disable-next-line no-console
15-
console.warn(
20+
log.info(
1621
`🚨 You are setting an alpha value of "${desiredAlpha}" for a color with an alpha value (${color}). The previous alpha value will be disregarded as if the color would have been 100% opaque.${
1722
token !== undefined ? `\n ↳ Token: "${token.name}" in file: "${token.filePath}"` : ''
1823
}`,
24+
config,
1925
)
2026
}
2127

src/utilities/log.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type {PlatformConfig} from 'style-dictionary/types'
2+
import {log} from './log.js'
3+
4+
describe('Utilities: log', () => {
5+
const consoleLogSpy = vi.spyOn(console, 'log')
6+
const consoleWarnSpy = vi.spyOn(console, 'warn')
7+
const consoleErrorSpy = vi.spyOn(console, 'error')
8+
9+
afterEach(() => {
10+
vi.clearAllMocks()
11+
})
12+
13+
it('logs correct type', () => {
14+
log.info('message.log')
15+
log.warning('message.warn')
16+
log.error('message.error')
17+
expect(consoleLogSpy).toHaveBeenLastCalledWith('message.log')
18+
expect(consoleWarnSpy).toHaveBeenLastCalledWith('message.warn')
19+
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error')
20+
})
21+
22+
it('only logs errors when verbosity silent', () => {
23+
const config = {
24+
log: {
25+
verbosity: 'silent',
26+
},
27+
} as PlatformConfig
28+
29+
log.info('message.log', config)
30+
log.warning('message.warn', config)
31+
log.error('message.error', config)
32+
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log')
33+
expect(consoleWarnSpy).not.toHaveBeenLastCalledWith('message.warn')
34+
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error')
35+
})
36+
37+
it('only logs errors & warnings when verbosity silent', () => {
38+
const config = {
39+
log: {
40+
verbosity: 'default',
41+
warnings: 'warn',
42+
},
43+
} as PlatformConfig
44+
45+
log.info('message.log', config)
46+
log.warning('message.warn', config)
47+
log.error('message.error', config)
48+
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log')
49+
expect(consoleWarnSpy).toHaveBeenLastCalledWith('message.warn')
50+
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error')
51+
})
52+
53+
it('only logs errors & warnings when verbosity silent', () => {
54+
const config = {
55+
log: {
56+
verbosity: 'default',
57+
warnings: 'disabled',
58+
},
59+
} as PlatformConfig
60+
61+
log.info('message.log', config)
62+
log.warning('message.warn', config)
63+
log.error('message.error', config)
64+
expect(consoleLogSpy).not.toHaveBeenLastCalledWith('message.log')
65+
expect(consoleWarnSpy).not.toHaveBeenLastCalledWith('message.warn')
66+
expect(consoleErrorSpy).toHaveBeenLastCalledWith('message.error')
67+
})
68+
})

src/utilities/log.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type {PlatformConfig} from 'style-dictionary/types'
2+
3+
const logMessage = (message: string, type: 'info' | 'warning' | 'error' = 'warning', config?: PlatformConfig) => {
4+
// early return if verbosity is silent
5+
if (config?.log?.verbosity === 'silent' && type !== 'error') {
6+
return
7+
}
8+
// early return if verbosity is default and type is info
9+
if (config?.log?.verbosity === 'default' && type === 'info') {
10+
return
11+
}
12+
// early return
13+
if ((config?.log?.warnings === 'disabled' || config?.log?.warnings === 'warn') && type === 'info') {
14+
return
15+
}
16+
// early return
17+
if (config?.log?.warnings === 'disabled' && type === 'warning') {
18+
return
19+
}
20+
if (type === 'warning') {
21+
// eslint-disable-next-line no-console
22+
return console.warn(message)
23+
}
24+
if (type === 'error') {
25+
// eslint-disable-next-line no-console
26+
return console.error(message)
27+
}
28+
// eslint-disable-next-line no-console
29+
console.log(message)
30+
}
31+
32+
export const log = {
33+
info: (message: string, config?: PlatformConfig) => logMessage(message, 'info', config),
34+
warning: (message: string, config?: PlatformConfig) => logMessage(message, 'warning', config),
35+
error: (message: string, config?: PlatformConfig) => logMessage(message, 'error', config),
36+
}

0 commit comments

Comments
 (0)