Skip to content

Commit

Permalink
feat: impl token build
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 21, 2020
1 parent b73069e commit 4d89da0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/cli/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { resolve } from 'path'
import { Command, flags } from '@oclif/command'

import { getProjectConfig } from '../core/project-config'
import { build } from '../core/build'

type Flags = { config: string }
type Args = { source: string }

export default class Build extends Command {
static flags = {
config: flags.string({
char: 'c',
description: 'Config path',
default: 'theme.config.json',
}),
}

async run() {
this.log('build command')
const { flags, args } = this.parse<Flags, Args>(Build)
const config = await getProjectConfig(process.cwd(), flags.config)
await build(config)
}
}
35 changes: 35 additions & 0 deletions src/core/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { writeFile, ensureDir } from 'fs-extra'
import { resolve } from 'path'
import deepmerge from 'deepmerge'

import { Config } from './project-config'
import { getThemeLayers } from './theme-layers'
import { flatTokens } from './flat-tokens'
import { transformTokens } from './transforms'
import { formats } from './formats'

export async function build(options: Config): Promise<any> {
// TODO: Add tokens validate.
// TODO: Add avalible transforms validate.
const themeLayers = await getThemeLayers(options.rootDir, { platforms: options.platforms, exclude: options.exclude })
for (const format in options.formats) {
// @ts-ignore
const { options, transforms } = options.formats[format]
const result = deepmerge(themeLayers, {})
for (const platform in themeLayers) {
const xxx = themeLayers[platform]
for (const q in xxx) {
const uuu = xxx[q]
const yyy = flatTokens(uuu.tokens)
const hhh = transformTokens(yyy, { transforms, ...options })
// @ts-ignore
result[platform][q].tokens = hhh
}
}
const result_to_write = formats[format](result, options)
for (const file of result_to_write) {
await ensureDir(resolve(process.cwd(), options.rootDir, 'build'))
await writeFile(resolve(process.cwd(), options.rootDir, 'build', file.fileName), file.content)
}
}
}

0 comments on commit 4d89da0

Please sign in to comment.