Skip to content

Commit

Permalink
feat: add flags for selected builds
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Aug 23, 2020
1 parent c5627ca commit 2425f72
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { build } from '../core/build'
import { loadTheme } from '../core/load-theme'
import { throttle, flatten } from '../core/utils'

type Flags = { config: string; watch: boolean }
type Flags = {
config: string
watch: boolean
entry: string[]
output: string[]
}

export default class Build extends Command {
static description = 'Builds themes for configured formats.'
Expand All @@ -22,11 +27,24 @@ export default class Build extends Command {
char: 'w',
description: 'Auto rebuilds themes after change sources.',
}),
entry: flags.string({
char: 'e',
multiple: true,
description: 'Builds selected entries.',
}),
output: flags.string({
char: 'o',
multiple: true,
description: 'Builds selected outputs.',
}),
}

async run() {
const { flags } = this.parse<Flags, any>(Build)
const config = await loadConfig(resolve(flags.config))
const config = await loadConfig(resolve(flags.config), {
entries: flags.entry,
outputs: flags.output,
})

await build(config)

Expand Down
23 changes: 21 additions & 2 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@ import fg from 'fast-glob'
export type Config = {
entry: Record<string, string>
output: Record<string, Platform>
[key: string]: any
}

export async function loadConfig(path: string): Promise<Config> {
export async function loadConfig(
path: string,
filters: { entries?: string[]; outputs?: string[]; [key: string]: any },
): Promise<Config> {
filters
const resolvedPath = fg.sync(path)
if (resolvedPath.length === 0) {
throw new Error(`Cannot load config from "${path}", please check path or file are exists.`)
}
return require(resolvedPath[0])
const config: Config = require(resolvedPath[0])
if (filters.entries !== undefined || filters.outputs !== undefined) {
const fields = [
['outputs', 'output'],
['entries', 'entry'],
]
for (const [filter, field] of fields) {
for (const key in config[field]) {
if (filters[filter] !== undefined && !filters[filter].includes(key)) {
delete config[field][key]
}
}
}
}
return config
}

0 comments on commit 2425f72

Please sign in to comment.