UNPKG

@yandex/themekit

Version:

Build system of design-tokens for any platforms

56 lines (55 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = require("fs-extra"); const path_1 = require("path"); const themekit_core_1 = require("@yandex/themekit-core"); const config_loader_1 = require("../utils/config-loader"); const theme_loader_1 = require("../utils/theme-loader"); const watcher_1 = require("../utils/watcher"); const command_1 = require("../utils/command"); class Build extends command_1.Command { async run() { const { entry, output } = config_loader_1.loadConfig(path_1.resolve(this.flags.config)); this.build(entry, output); if (this.flags.watch) { this.startWatching(entry, output); } } startWatching(entries, output) { const sources = []; for (const entry in entries) { const theme = theme_loader_1.loadThemeFiles(entries[entry]); sources.push(...theme.aliases, ...theme.sources); } watcher_1.watch(sources, () => this.build(entries, output)); } build(entries, output) { for (const entry in entries) { const { tokens, aliases } = theme_loader_1.loadTheme(entries[entry]); const result = themekit_core_1.compile({ tokens, output, context: { entry, aliases } }); for (const [outputName, files] of Object.entries(result)) { for (const file of files) { const destination = file.destination.replace(/\[entry\]/g, entry); const filePath = path_1.join(process.cwd(), output[outputName].buildPath, destination); const fileDir = path_1.dirname(filePath); fs_extra_1.ensureDirSync(fileDir); fs_extra_1.writeFileSync(filePath, file.content); } } } } } exports.default = Build; Build.description = 'Builds themes for configured formats.'; Build.flags = { ...command_1.Command.flags, config: command_1.flags.string({ char: 'c', description: 'The path to a themekit config file', default: 'themekit.config.{js,json,yml}', }), watch: command_1.flags.boolean({ char: 'w', description: 'Auto rebuilds themes after change sources', }), };