Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 21, 2024
1 parent 1de0a20 commit cad86bf
Show file tree
Hide file tree
Showing 24 changed files with 293 additions and 685 deletions.
143 changes: 140 additions & 3 deletions fixtures/input/example-0001.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type { BunPlugin } from 'bun'
import process from 'node:process'
import { generate, deepMerge } from '@stacksjs/dtsx'
import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx'
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'

/**
* Example of const declaration
*/
export const config: { [key: string]: string } = {
apiUrl: 'https://api.example.com',
export const conf: { [key: string]: string } = {
apiUrl: 'https://api.stacksjs.org',
timeout: '5000',
}

export const someObject = {
someString: 'Stacks',
someNumber: 1000,
}

/**
* Example of interface declaration
*/
Expand All @@ -27,6 +39,131 @@ export interface ResponseData {
* Example of function declaration
*/
export function fetchUsers(): Promise<ResponseData> {
return fetch(config.apiUrl)
return fetch(conf.apiUrl)
.then(response => response.json()) as Promise<ResponseData>
}

export interface ApiResponse<T> {
status: number
message: string
data: T
}

/**
* Example of another const declaration
*/
const settings: { [key: string]: any } = {
theme: 'dark',
language: 'en',
}

export interface Product {
id: number
name: string
price: number
}

/**
* Example of function declaration
*/
export function getProduct(id: number): Promise<ApiResponse<Product>> {
return fetch(`${settings.apiUrl}/products/${id}`)
.then(response => response.json()) as Promise<ApiResponse<Product>>
}

export interface AuthResponse {
token: string
expiresIn: number
}

export type AuthStatus = 'authenticated' | 'unauthenticated'

export function authenticate(user: string, password: string): Promise<AuthResponse> {
return fetch('/auth/login', {
method: 'POST',
body: JSON.stringify({ user, password }),
}).then(response => response.json()) as Promise<AuthResponse>
}

export const defaultHeaders = {
'Content-Type': 'application/json',
}

export function dts(options?: DtsGenerationOption): BunPlugin {
return {
name: 'bun-plugin-dtsx',

async setup(build) {
const cwd = options?.cwd ?? process.cwd()
const root = options?.root ?? build.config.root
const entrypoints = options?.entrypoints ?? build.config.entrypoints
const outdir = options?.outdir ?? build.config.outdir
const keepComments = options?.keepComments ?? true
const clean = options?.clean ?? false
const tsconfigPath = options?.tsconfigPath ?? './tsconfig.json'

await generate({
...options,
cwd,
root,
entrypoints,
outdir,
keepComments,
clean,
tsconfigPath,
})
},
}
}

interface Options<T> {
name: string
cwd?: string
defaultConfig: T
}

export async function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: Options<T>): Promise<T> {
const c = cwd ?? process.cwd()
const configPath = resolve(c, `${name}.config`)

if (existsSync(configPath)) {
try {
const importedConfig = await import(configPath)
const loadedConfig = importedConfig.default || importedConfig
return deepMerge(defaultConfig, loadedConfig)
}
catch (error) {
console.error(`Error loading config from ${configPath}:`, error)
}
}

return defaultConfig
}

// Get loaded config
// eslint-disable-next-line antfu/no-top-level-await
const dtsConfig: DtsGenerationConfig = await loadConfig({
name: 'dts',
cwd: process.cwd(),
defaultConfig: {
cwd: process.cwd(),
root: './src',
entrypoints: ['**/*.ts'],
outdir: './dist',
keepComments: true,
clean: true,
tsconfigPath: './tsconfig.json',
},
})

export { generate, dtsConfig }

export type { DtsGenerationOption }

export { config } from './config'
export * from './extract'
export * from './generate'
export * from './types'
export * from './utils'

export default dts
27 changes: 0 additions & 27 deletions fixtures/input/example-0002.ts

This file was deleted.

25 changes: 0 additions & 25 deletions fixtures/input/example-0003.ts

This file was deleted.

18 changes: 0 additions & 18 deletions fixtures/input/example-0004.ts

This file was deleted.

15 changes: 0 additions & 15 deletions fixtures/input/example-0005.ts

This file was deleted.

37 changes: 0 additions & 37 deletions fixtures/input/example-0006.ts

This file was deleted.

45 changes: 0 additions & 45 deletions fixtures/input/example-0007.ts

This file was deleted.

65 changes: 0 additions & 65 deletions fixtures/input/example-0008.ts

This file was deleted.

Loading

0 comments on commit cad86bf

Please sign in to comment.