Skip to content

Commit

Permalink
feat: add fileName option
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 27, 2020
1 parent 951109f commit 585e4fd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 3 additions & 5 deletions src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,23 @@ export async function build(
onStart?: (format: string) => void,
onFinish?: (format: string, files: string[]) => void,
): Promise<void> {
// TODO: Add tokens validate.
// TODO: Add avalible transforms validate.
const themeLayers = await getThemeLayers(config.src, { platforms: config.platforms })
for (const format in config.formats) {
onStart && onStart(format)
const { outDir, options, transforms } = config.formats[format]
const { outDir, fileName, transforms } = config.formats[format]
// Copy layers for mutate in future.
const result = deepmerge(themeLayers, {})
for (const platform in themeLayers) {
const themeLayer = themeLayers[platform]
for (const layerKey in themeLayer) {
const flattenTokens = flatTokens(themeLayer[layerKey].tokens)
const transformedTokens = transformTokens(flattenTokens, { transforms, ...options })
const transformedTokens = transformTokens(flattenTokens, { transforms })
// @ts-ignore (FIXME: Fix transformTokens return type)
result[platform][layerKey].tokens = transformedTokens
}
}
// FIXME: Move formats to fn.
const result_to_write = formats[format](result, options)
const result_to_write = formats[format](result, { fileName })
const createdFiles = []
for (const file of result_to_write) {
const destFilePath = resolve(outDir, file.fileName)
Expand Down
2 changes: 1 addition & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export type Config = {
formats: {
[key: string]: {
outDir: string
fileName?: string
transforms: string[]
options?: {}
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/core/formats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { cssTemplate, esmTemplate, jsonTemplate } from './templates'

function getFileNameWithPlatform(fileName: string, platform: string, extension: string): string {
function getFileNameWithPlatform(folder: string, platform: string, fileName: string): string {
if (platform === 'common') {
return `${fileName}/index.${extension}`
return `${folder}/${fileName}`
}
return `${fileName}/${platform}/index.${extension}`
return `${folder}/${platform}/${fileName}`
}

type Formats = {
Expand All @@ -14,7 +14,7 @@ type Formats = {
}

export const formats: Formats = {
'css.flat': (platforms, options) => {
'css.flat': (platforms, options = { fileName: 'index.css' }) => {
const result = []
for (const platform in platforms) {
const layers = platforms[platform]
Expand All @@ -23,34 +23,35 @@ export const formats: Formats = {
composedTokens.push(...layers[layer].tokens)
}
result.push({
fileName: getFileNameWithPlatform(options.fileName, platform, 'css'),
// FIXME: Fix filePath.
fileName: getFileNameWithPlatform(options.fileName, platform, options.fileName),
content: cssTemplate(composedTokens, ':root'),
})
}
return result
},
'css.whitepaper': (platforms) => {
'css.whitepaper': (platforms, options) => {
const result = []
for (const platform in platforms) {
const layers = platforms[platform]
for (const layer in layers) {
const { name, meta, tokens } = layers[layer]
result.push({
fileName: getFileNameWithPlatform(name, platform, 'css'),
fileName: getFileNameWithPlatform(name, platform, options.fileName || 'index.css'),
content: cssTemplate(tokens, meta.css),
})
}
}
return result
},
'json.whitepaper': (platforms) => {
'json.whitepaper': (platforms, options) => {
const result = []
for (const platform in platforms) {
const layers = platforms[platform]
for (const layer in layers) {
const { name, tokens } = layers[layer]
result.push({
fileName: getFileNameWithPlatform(name, platform, 'json'),
fileName: getFileNameWithPlatform(name, platform, options.fileName || 'index.json'),
content: jsonTemplate(tokens),
})
}
Expand All @@ -66,6 +67,7 @@ export const formats: Formats = {
composedTokens.push(...layers[layer].tokens)
}
result.push({
// FIXME: Fix filePath.
fileName: getFileNameWithPlatform(options.fileName, platform, 'js'),
content: esmTemplate(composedTokens),
})
Expand Down

0 comments on commit 585e4fd

Please sign in to comment.