Skip to content

Commit

Permalink
feat: support custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrbo committed Apr 29, 2024
1 parent bfac9b7 commit 80c7f7c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ImportMetaEnv {
*/
readonly VITE_GE: string
readonly VITE_OBJECT: string
readonly VITE_STRING: string
readonly VITE_STRING: 'abc' | 'bc'
readonly VITE_ABC: number
}

Expand Down
6 changes: 5 additions & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export default defineConfig({
plugins: [
Inspect(),
VitePlugin({
dts: 'types/auto-env.dts',
dts: 'types/auto-env.d.ts',
custom: {
VITE_GE: 'string',
VITE_STRING: `'abc' | 'bc'`,
},
}),
],
})
4 changes: 2 additions & 2 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Context {
}

scanEnv() {
const { dts, includes } = this.options
const { dts, includes, custom } = this.options
if (dts !== false) {
const envFiles = fg.sync(
includes,
Expand All @@ -41,7 +41,7 @@ export class Context {

envFiles.forEach((path) => {
const content = readFileSync(path, 'utf-8')
const envMap = parseMetaEnv(content)
const envMap = parseMetaEnv(content, custom)
envMap.forEach((env) => {
this._env.set(env.label, { ...env })
})
Expand Down
12 changes: 9 additions & 3 deletions src/core/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import createDebugger from 'debug'
import { isKeyOfObj } from '@vtrbo/utils'
import type { ResolvedOptions } from '../types'
import { getLikeType } from './utils'
import { VITE_PLUGIN_NAME } from './constant'
Expand All @@ -11,11 +12,11 @@ export interface Env {
remark: string
label: string
value: string
likely: 'string' | 'boolean' | 'number'
likely: 'string' | 'boolean' | 'number' | string
required: boolean
}

export function parseMetaEnv(data: string): Env[] {
export function parseMetaEnv(data: string, custom: ResolvedOptions['custom']): Env[] {
data = data.replace(/\r\n?/gm, '\n')
const regexp = /(?:^|^)\s*((?:\s*#.+\n)*)?(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?[^#\r\n]*(#.*)?(?:$|$)/gm

Expand All @@ -32,13 +33,18 @@ export function parseMetaEnv(data: string): Env[] {
if (isString)
value = value.replace(quoteRegExp, '$2').replace(/\\n/g, '\n').replace(/\\r/g, '\r')

let likely = isString ? 'string' : getLikeType(value)

if (isKeyOfObj(custom, label))
likely = custom[label]

const required = !['', null, undefined].includes(value)

meteEnv.push({
remark,
label,
value,
likely: isString ? 'string' : getLikeType(value),
likely,
required,
})
}
Expand Down
1 change: 1 addition & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export function resolveOptions(options?: Options): ResolvedOptions {
includes: options?.includes ? toArray(options.includes) : ENV_INCLUDES,
prefix: options?.prefix ? toArray(options.prefix) : ENV_PREFIX,
dts: options?.dts ?? ENV_DTS,
custom: options?.custom ? options.custom : {},
}
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ export interface Options {
* dts file path
*/
dts?: string | false

/**
* 自定义类型
*/
custom?: Record<string, string>
}

export interface ResolvedOptions {
includes: string[]
prefix: string[]
dts: string | false
custom: Record<string, string>
}

0 comments on commit 80c7f7c

Please sign in to comment.