feat(cli): add support for config file (#245)

* feat(cli): add config files and examples in docs

* docs(www): remove cli advanced options

---------

Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
Prince Hernandez
2023-05-28 03:21:11 -03:00
committed by GitHub
parent 22f23b7db3
commit dffbe89f7d
5 changed files with 87 additions and 15 deletions

View File

@@ -0,0 +1,63 @@
import { cosmiconfig } from "cosmiconfig"
import * as z from "zod"
export const COMPONENTS_DIR = "./components/ui/"
export const UTILS_LOCATION = "@/lib/utils"
export const COMPONENT_ALIAS = "@/components/ui/"
/**
* this is the name of the key we are looking for, the following are the intended values to look for:
* - shadcn-ui property in package.json
* - .shadcn-uirc file in JSON or YAML format
* - .shadcn-uirc.json, .shadcn-uirc.yaml, .shadcn-uirc.yml, .shadcn-uirc.js, or .shadcn-uirc.cjs file
* - shadcn-uirc, shadcn-uirc.json, shadcn-uirc.yaml, shadcn-uirc.yml, shadcn-uirc.js or shadcn-uirc.cjs file inside a .config subdirectory
* - shadcn-ui.config.js or shadcn-ui.config.cjs CommonJS module exporting an object
*/
const explorer = cosmiconfig("shadcn-ui")
const configSchema = z.object({
componentsDirInstallation: z.string(),
askForDir: z.boolean(),
utilsLocation: z.string(),
componentDirAlias: z.string(),
})
export type Config = z.infer<typeof configSchema>
export async function getCliConfig(): Promise<Config> {
const defaultConfig: Config = {
componentsDirInstallation: COMPONENTS_DIR,
askForDir: true,
utilsLocation: UTILS_LOCATION,
componentDirAlias: COMPONENT_ALIAS,
}
const userDefinedConfig = await getConfigFromEverywhere()
return {
...defaultConfig,
...userDefinedConfig,
askForDir: !userDefinedConfig.componentsDirInstallation,
}
}
export async function getConfigFromEverywhere() {
try {
const configResult = await explorer.search()
if (!configResult) {
// we should always return an object so we can then merge with
// the base config
return {}
}
const { config } = configResult
const parsedConfig = configSchema.partial().parse(config)
return parsedConfig
} catch (e) {
// lets just show the error to the user to aware about the issue, but lets not handle it.
console.log(e)
return {}
}
}

View File

@@ -5,7 +5,6 @@ import fs from "fs-extra"
export async function getProjectInfo() {
const info = {
tsconfig: null,
alias: null,
srcDir: false,
appDir: false,
srcComponentsUiDir: false,
@@ -14,12 +13,9 @@ export async function getProjectInfo() {
try {
const tsconfig = await getTsConfig()
const paths = tsconfig?.compilerOptions?.paths
const alias = paths ? Object.keys(paths)[0].replace("*", "") : null
return {
tsconfig,
alias,
srcDir: existsSync(path.resolve("./src")),
appDir:
existsSync(path.resolve("./app")) ||