Skip to content

Commit

Permalink
feat: support variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 8, 2022
1 parent b4b2b81 commit d2cd735
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
20 changes: 15 additions & 5 deletions src/config.ts
Expand Up @@ -11,11 +11,17 @@ import { objectPick, toArray } from '@antfu/utils'
import { findConfigTypePath, which } from './utils'
import type { Config, ConfigReplace, ConfigTemplate } from './types'

export type TemplateNormalized = Omit<ConfigTemplate, 'git' | 'children'> & {
git: NonNullable<Required<ConfigTemplate['git']>>
children?: TemplateNormalized[]
replaces: ConfigReplace[]
}
type MergeObject<O, T> = Omit<O, keyof T> & T

export type TemplateNormalized = MergeObject<
ConfigTemplate,
{
children?: TemplateNormalized[]
git: NonNullable<Required<ConfigTemplate['git']>>
replaces: ConfigReplace[]
variables: NonNullable<ConfigTemplate['variables']>
}
>
export type ConfigNormalized = {
templates: Array<TemplateNormalized>
}
Expand Down Expand Up @@ -185,6 +191,10 @@ export function normalizeConfig(config: Config): ConfigNormalized {
...normalizeReplaces(config.replaces),
...normalizeReplaces(template.replaces),
],
variables: {
...(config.variables || {}),
...(template.variables || {}),
},
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/features/variable.ts
@@ -0,0 +1,26 @@
import enquirer from 'enquirer'
import type { TemplateNormalized } from '../config'
import type { ProjectInfo } from '../types'

export async function variable(
template: TemplateNormalized,
project: ProjectInfo
) {
for (const [key, variable] of Object.entries(template.variables)) {
const def =
typeof variable === 'function'
? await variable({ template, project })
: variable

const { value } = await enquirer.prompt<{ value: string }>({
name: 'value',

type: def.type,
message: def.message,
initial: def.initial,
required: def.required,
})

project.variables[key] = value
}
}
10 changes: 7 additions & 3 deletions src/index.ts
Expand Up @@ -8,6 +8,7 @@ import { editConfig, getConfig } from './config'
import { git } from './features/git'
import { replace } from './features/replace'
import { command } from './features/command'
import { variable } from './features/variable'

import type { ProjectInfo } from './types'
import type { TemplateNormalized } from './config'
Expand Down Expand Up @@ -87,14 +88,17 @@ async function create({
url,
folderName,
path: projectPath,
variables: {},
}

await variable(template, project)

const emitter = degit(url)
await emitter.clone(projectPath)

git(template, project)
replace(template, project)
command(template, project)
await git(template, project)
await replace(template, project)
await command(template, project)

consola.success(
`${chalk.green.bold(`Done. Now run:`)}\n\n ${chalk.blueBright(
Expand Down
42 changes: 31 additions & 11 deletions src/types.ts
@@ -1,9 +1,16 @@
import type { Arrayable } from '@antfu/utils'
import type { TemplateNormalized } from './config'
import type { Arrayable, Awaitable } from '@antfu/utils'

export interface ProjectInfo {
url: string
folderName: string
path: string
variables: Record<string, any>
}

export interface Context {
project: ProjectInfo
template: TemplateNormalized
}

export interface ConfigGit {
Expand All @@ -14,16 +21,18 @@ export interface ConfigGit {
}

export type ConfigReplaceFrom = Arrayable<string | RegExp>
export type ConfigReplaceFromCallback = (options: {
file: string
project: ProjectInfo
}) => ConfigReplaceFrom
export type ConfigReplaceFromCallback = (
options: {
file: string
} & Context
) => ConfigReplaceFrom
export type ConfigReplaceTo = Arrayable<string>
export type ConfigReplaceToCallback = (options: {
match: string
file: String
project: ProjectInfo
}) => ConfigReplaceTo
export type ConfigReplaceToCallback = (
options: {
match: string
file: String
} & Context
) => ConfigReplaceTo

export interface ConfigReplace {
include?: Arrayable<string>
Expand All @@ -36,6 +45,11 @@ export interface ConfigReplace {
ignoreCase?: boolean
}

export type ConfigVariable = { message: string; initial?: string } & {
type: 'input'
required?: boolean
}

export interface ConfigTemplate {
name: string
color?: string
Expand All @@ -49,6 +63,11 @@ export interface ConfigTemplate {
add?: boolean
}

variables?: Record<
string,
ConfigVariable | ((ctx: Context) => Awaitable<ConfigVariable>)
>

replaces?:
| ({ items: Arrayable<Partial<ConfigReplace>> } & Partial<ConfigReplace>)
| ConfigReplace[]
Expand All @@ -57,6 +76,7 @@ export interface ConfigTemplate {
commands?: Arrayable<string>
}

export interface Config extends Pick<ConfigTemplate, 'git' | 'replaces'> {
export interface Config
extends Pick<ConfigTemplate, 'git' | 'replaces' | 'variables'> {
templates?: Arrayable<ConfigTemplate>
}

0 comments on commit d2cd735

Please sign in to comment.