Skip to content

Commit

Permalink
feat(config): Enable specifying path for histoire config file (#612)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Chau <guillaume.b.chau@gmail.com>
  • Loading branch information
peterbud and Akryum committed Oct 25, 2023
1 parent 27ca2c1 commit fb1c47f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/histoire/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"markdown-it-emoji": "^2.0.2",
"micromatch": "^4.0.5",
"mrmime": "^1.0.1",
"pathe": "^0.2.0",
"pathe": "^1.1.1",
"picocolors": "^1.0.0",
"sade": "^1.8.1",
"shiki-es": "^0.2.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/histoire/src/node/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ program.version(version)
program.command('dev')
.describe('open the stories in your browser for development')
.option('-p, --port <port>', 'Listening port of the server')
.option('-c, --config <file>', `[string] use specified config file`)
.option('--open', 'Open in your default browser')
.action(async (options) => {
const { devCommand } = await import('./commands/dev.js')
Expand All @@ -24,9 +25,10 @@ program.command('dev')

program.command('build')
.describe('build the histoire final app you can deploy')
.action(async () => {
.option('-c, --config <file>', `[string] use specified config file`)
.action(async (options) => {
const { buildCommand } = await import('./commands/build.js')
return buildCommand()
return buildCommand(options)
})

program.command('preview')
Expand Down
7 changes: 6 additions & 1 deletion packages/histoire/src/node/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { build } from '../build.js'
import { createContext } from '../context.js'

export async function buildCommand () {
export interface BuildOptions {
config?: string
}

export async function buildCommand (options: BuildOptions) {
const ctx = await createContext({
configFile: options.config,
mode: 'build',
})
await build(ctx)
Expand Down
4 changes: 3 additions & 1 deletion packages/histoire/src/node/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { createServer } from '../server.js'
export interface DevOptions {
port: number
open?: boolean
config?: string
}

export async function devCommand (options: DevOptions) {
let stop: () => Promise<void>

async function start () {
const ctx = await createContext({
configFile: options.config,
mode: 'dev',
})
const { server, viteConfigFile, close } = await createServer(ctx, {
Expand Down Expand Up @@ -50,7 +52,7 @@ export async function devCommand (options: DevOptions) {

stop = await start()

const configFile = await resolveConfigFile()
const configFile = await resolveConfigFile(undefined, options.config)
if (configFile) {
const watcher = chokidar.watch(configFile, {
ignoreInitial: true,
Expand Down
17 changes: 11 additions & 6 deletions packages/histoire/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ export const configFileNames = [
'.histoire.js',
]

export function resolveConfigFile (cwd: string = process.cwd()): string {
return findUp(cwd, configFileNames)
export function resolveConfigFile (cwd: string = process.cwd(), configFile?: string): string {
if (configFile) {
// explicit config path is always resolved from cwd
return path.resolve(configFile)
} else {
return findUp(cwd, configFileNames)
}
}

export async function loadConfigFile (configFile: string): Promise<Partial<HistoireConfig>> {
Expand Down Expand Up @@ -244,11 +249,11 @@ export const mergeConfig = createDefu((obj: any, key, value) => {
}
})

export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMode): Promise<HistoireConfig> {
export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMode, configFile: string): Promise<HistoireConfig> {
let result: Partial<HistoireConfig>
const configFile = resolveConfigFile(cwd)
if (configFile) {
result = await loadConfigFile(configFile)
const resolvedConfigFile = resolveConfigFile(cwd, configFile)
if (resolvedConfigFile) {
result = await loadConfigFile(resolvedConfigFile)
}
const viteConfig = await resolveViteConfig({}, 'serve')
const viteHistoireConfig = (viteConfig.histoire ?? {}) as HistoireConfig
Expand Down
3 changes: 2 additions & 1 deletion packages/histoire/src/node/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export interface Context {

export interface CreateContextOptions {
mode: Context['mode']
configFile?: string
}

export async function createContext (options: CreateContextOptions): Promise<Context> {
const config = await resolveConfig(process.cwd(), options.mode)
const config = await resolveConfig(process.cwd(), options.mode, options.configFile)
const command = options.mode === 'dev' ? 'serve' : 'build'
const viteConfig = await resolveViteConfig({}, command)

Expand Down
7 changes: 1 addition & 6 deletions packages/histoire/src/node/util/find-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import path from 'pathe'
import fs from 'node:fs'

export function findUp (cwd: string = process.cwd(), fileNames: string[]): string {
let { root } = path.parse(cwd)
const { root } = path.parse(cwd)
let dir = cwd

// Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
if (root === '' && dir[1] === ':') {
root = dir.substring(0, 2)
}

while (dir !== root) {
for (const fileName of fileNames) {
const searchPath = path.join(dir, fileName)
Expand Down
8 changes: 3 additions & 5 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 fb1c47f

Please sign in to comment.