|
| 1 | +import { existsSync } from 'fs' |
| 2 | +import { rm, writeFile } from 'fs/promises' |
| 3 | +import type { Core as Instance } from 'browser-style-dictionary/types/browser' |
| 4 | +import StyleDictionary from 'browser-style-dictionary/browser.js' |
| 5 | +import { tsTypesDeclaration, tsFull, jsFull } from './formats' |
| 6 | +import type { NuxtDesignTokens } from './index' |
| 7 | + |
| 8 | +export const stubTokens = async (buildPath: string, force = false) => { |
| 9 | + const files = { |
| 10 | + 'tokens.css': () => '/* This file is empty because no tokens has been provided. */', |
| 11 | + 'tokens.scss': () => '/* This file is empty because no tokens has been provided. */', |
| 12 | + 'tokens.json': () => '{}', |
| 13 | + 'index.js': jsFull, |
| 14 | + 'index.ts': tsFull, |
| 15 | + 'types.d.ts': tsTypesDeclaration |
| 16 | + } |
| 17 | + |
| 18 | + for (const [file, stubbingFunction] of Object.entries(files)) { |
| 19 | + const path = `${buildPath}${file}` |
| 20 | + |
| 21 | + if (force && existsSync(path)) { await rm(path) } |
| 22 | + |
| 23 | + if (!existsSync(path)) { await writeFile(path, stubbingFunction ? stubbingFunction({ tokens: {} }) : '') } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export const generateTokens = async ( |
| 28 | + tokens: NuxtDesignTokens, |
| 29 | + buildPath: string, |
| 30 | + silent = true |
| 31 | +) => { |
| 32 | + if (!tokens || !Object.keys(tokens).length) { |
| 33 | + await stubTokens(buildPath, true) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + let styleDictionary: Instance = StyleDictionary |
| 38 | + |
| 39 | + styleDictionary.fileHeader = {} |
| 40 | + |
| 41 | + styleDictionary.registerTransformGroup({ |
| 42 | + name: 'tokens-js', |
| 43 | + transforms: ['name/cti/kebab', 'size/px', 'color/hex'] |
| 44 | + }) |
| 45 | + |
| 46 | + styleDictionary.registerFormat({ |
| 47 | + name: 'typescript/types-declaration', |
| 48 | + formatter ({ dictionary }) { |
| 49 | + return tsTypesDeclaration(dictionary) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + styleDictionary.registerFormat({ |
| 54 | + name: 'typescript/full', |
| 55 | + formatter ({ dictionary }) { |
| 56 | + return tsFull(dictionary) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + styleDictionary.registerFormat({ |
| 61 | + name: 'javascript/full', |
| 62 | + formatter ({ dictionary }) { |
| 63 | + return jsFull(dictionary) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + styleDictionary = await styleDictionary.extend({ |
| 68 | + tokens, |
| 69 | + platforms: { |
| 70 | + scss: { |
| 71 | + transformGroup: 'tokens-js', |
| 72 | + buildPath, |
| 73 | + files: [ |
| 74 | + { |
| 75 | + destination: 'tokens.scss', |
| 76 | + format: 'scss/variables' |
| 77 | + } |
| 78 | + ] |
| 79 | + }, |
| 80 | + |
| 81 | + json: { |
| 82 | + transformGroup: 'tokens-js', |
| 83 | + buildPath, |
| 84 | + files: [ |
| 85 | + { |
| 86 | + destination: 'tokens.json', |
| 87 | + format: 'json/flat' |
| 88 | + } |
| 89 | + ] |
| 90 | + }, |
| 91 | + |
| 92 | + ts: { |
| 93 | + transformGroup: 'tokens-js', |
| 94 | + buildPath, |
| 95 | + files: [ |
| 96 | + { |
| 97 | + destination: 'index.ts', |
| 98 | + format: 'typescript/full' |
| 99 | + }, |
| 100 | + { |
| 101 | + destination: 'types.d.ts', |
| 102 | + format: 'typescript/types-declaration' |
| 103 | + } |
| 104 | + ] |
| 105 | + }, |
| 106 | + |
| 107 | + js: { |
| 108 | + transformGroup: 'tokens-js', |
| 109 | + buildPath, |
| 110 | + files: [ |
| 111 | + { |
| 112 | + destination: 'index.js', |
| 113 | + format: 'javascript/full' |
| 114 | + } |
| 115 | + ] |
| 116 | + }, |
| 117 | + |
| 118 | + css: { |
| 119 | + transformGroup: 'tokens-js', |
| 120 | + buildPath, |
| 121 | + files: [ |
| 122 | + { |
| 123 | + destination: 'tokens.css', |
| 124 | + format: 'css/variables' |
| 125 | + } |
| 126 | + ] |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + // Weird trick to disable nasty logging |
| 132 | + if (silent) { |
| 133 | + // @ts-ignore |
| 134 | + // eslint-disable-next-line no-console |
| 135 | + console._log = console.log |
| 136 | + // eslint-disable-next-line no-console |
| 137 | + console.log = () => {} |
| 138 | + } |
| 139 | + |
| 140 | + styleDictionary.cleanAllPlatforms() |
| 141 | + |
| 142 | + await new Promise(resolve => setTimeout(resolve, 10)) |
| 143 | + |
| 144 | + styleDictionary.buildAllPlatforms() |
| 145 | + |
| 146 | + await new Promise(resolve => setTimeout(resolve, 10)) |
| 147 | + |
| 148 | + // Weird trick to disable nasty logging |
| 149 | + if (silent) { |
| 150 | + // @ts-ignore |
| 151 | + // eslint-disable-next-line no-console |
| 152 | + console.log = console._log |
| 153 | + } |
| 154 | +} |
0 commit comments