Skip to content

Commit

Permalink
feat: config file, closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 15, 2022
1 parent f928e9a commit 70dde60
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 19 deletions.
5 changes: 5 additions & 0 deletions examples/vue3/histoire.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'histoire'

export default defineConfig({
// outDir: 'hdist',
})
11 changes: 6 additions & 5 deletions packages/histoire/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
"import": "./dist/node/index.js",
"types": "./dist/node/index.d.ts"
},
"./*": "./*"
},
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/node/index.js",
"module": "./dist/node/index.js",
"types": "./dist/node/index.d.ts",
"bin": {
"histoire": "./bin.mjs"
},
Expand All @@ -38,6 +38,7 @@
"@vueuse/core": "^7.5.5",
"case": "^1.6.3",
"chokidar": "^3.5.3",
"defu": "^5.0.1",
"floating-vue": "^2.0.0-beta.5",
"fs-extra": "^10.0.0",
"globby": "^13.1.1",
Expand Down
9 changes: 2 additions & 7 deletions packages/histoire/src/node/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { join } from 'pathe'
import { build } from '../build.js'
import { HistoireConfig } from '../config.js'
import { resolveConfig } from '../config.js'
import { Context } from '../context.js'

export async function buildCommand () {
const config: HistoireConfig = {
sourceDir: join(process.cwd(), 'src'),
outDir: join(process.cwd(), 'histoire-dist'),
storyMatch: ['**/*.story.vue'],
}
const config = await resolveConfig()
const ctx: Context = {
config,
mode: 'build',
Expand Down
9 changes: 2 additions & 7 deletions packages/histoire/src/node/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { join } from 'pathe'
import { HistoireConfig } from '../config.js'
import { resolveConfig } from '../config.js'
import { Context } from '../context.js'
import { createServer } from '../server.js'

Expand All @@ -8,11 +7,7 @@ export interface DevOptions {
}

export async function devCommand (options: DevOptions) {
const config: HistoireConfig = {
sourceDir: join(process.cwd(), 'src'),
outDir: join(process.cwd(), 'histoire-dist'),
storyMatch: ['**/*.story.vue'],
}
const config = await resolveConfig()
const ctx: Context = {
config,
mode: 'dev',
Expand Down
84 changes: 84 additions & 0 deletions packages/histoire/src/node/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
import path from 'pathe'
import fs from 'fs'
import defu from 'defu'
import { createServer } from 'vite'
import { ViteNodeServer } from 'vite-node/server'
import { ViteNodeRunner } from 'vite-node/client'
import pc from 'picocolors'

export interface HistoireConfig {
sourceDir: string
outDir: string
storyMatch: string[]
}

export function getDefaultConfig (): HistoireConfig {
return {
sourceDir: 'src',
outDir: 'histoire-dist',
storyMatch: ['**/*.story.vue'],
}
}

export const configFileNames = [
'histoire.config.ts',
'histoire.config.js',
'.histoire.ts',
'.histoire.js',
]

export function resolveConfigFile (cwd: string = process.cwd()): string {
const { root } = path.parse(cwd)
let dir = cwd

while (dir !== root) {
for (const fileName of configFileNames) {
const searchPath = path.join(dir, fileName)
if (fs.existsSync(searchPath)) {
return searchPath
}
}
dir = path.dirname(dir)
}

return null
}

export async function loadConfigFile (configFile: string): Promise<Partial<HistoireConfig>> {
try {
const server = await createServer()
await server.pluginContainer.buildStart({})
const node = new ViteNodeServer(server)
const runner = new ViteNodeRunner({
root: path.dirname(configFile),
fetchModule (id) {
return node.fetchModule(id)
},
})
const result: { default: Partial<HistoireConfig> } = await runner.executeFile(configFile)
await server.close()
if (!result.default) {
throw new Error(`Expected default export in ${configFile}`)
}
return result.default
} catch (e) {
console.error(pc.red(`Error while loading ${configFile}`))
throw e
}
}

export async function resolveConfig (cwd: string = process.cwd()): Promise<HistoireConfig> {
let result: Partial<HistoireConfig>
const configFile = resolveConfigFile(cwd)
if (configFile) {
result = await loadConfigFile(configFile)
} else {
result = {}
}
return processConfig(defu(result, getDefaultConfig()))
}

export function processConfig (config: HistoireConfig): HistoireConfig {
config.sourceDir = path.resolve(process.cwd(), config.sourceDir)
config.outDir = path.resolve(process.cwd(), config.outDir)
return config
}

export function defineConfig (config: Partial<HistoireConfig>) {
return config
}
1 change: 1 addition & 0 deletions packages/histoire/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './config.js'
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70dde60

Please sign in to comment.