Skip to content

Commit

Permalink
fix: 🐛 simplify config file writing by directly using husky hook cont…
Browse files Browse the repository at this point in the history
…ents without conditions
  • Loading branch information
shlroland committed Oct 11, 2024
1 parent 91ebe94 commit 1cc6a73
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
8 changes: 2 additions & 6 deletions packages/cli/src/tasks/config/config-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@ export function configsFactory(moduleType: 'module' | 'commonjs' | undefined): C
options: [
{
filePath: huskyConfigFilePaths.preCommit,
fileContent: moduleType === 'module'
? esmConfigFactory(huskyConfig.hooks['pre-commit'], '')
: cjsConfigFactory(huskyConfig.hooks['pre-commit'], ''),
fileContent: huskyConfig.hooks['pre-commit'],
},
{
filePath: huskyConfigFilePaths.commitMsg,
fileContent: moduleType === 'module'
? esmConfigFactory(huskyConfig.hooks['commit-msg'], '')
: cjsConfigFactory(huskyConfig.hooks['commit-msg'], ''),
fileContent: huskyConfig.hooks['commit-msg'],
},
],
},
Expand Down
16 changes: 11 additions & 5 deletions packages/cli/src/tasks/config/write-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CheckConfigResult } from '../../types'
import type { CheckConfigResult, ConfigValue } from '../../types'
import fs from 'node:fs/promises'
import path from 'node:path'
import c from 'picocolors'
import { LintTools } from '../../constants'
import { deleteFile, getModuleType } from '../../utils'
import {
Expand All @@ -12,19 +13,24 @@ import {
export async function writeConfig(configs: CheckConfigResult[]) {
const cwd = process.cwd()
const moduleType = await getModuleType(cwd)
const pendingConfigs = configsFactory(moduleType)

for (const config of configs) {
if (config.shouldOverride === true && config.exitedFilePath) {
await deleteFile(config.exitedFilePath)
}
else if (config.shouldOverride === false) {
console.log(`The ${c.cyan(config.moduleName)} config will not be written. You should check the config file manually.`)
continue
}

const pendingConfig = pendingConfigs[config.moduleName]

await writeConfigFile(cwd, config, moduleType)
await writeConfigFile(cwd, pendingConfig)
}
}

async function writeConfigFile(cwd: string, config: CheckConfigResult, moduleType: 'module' | 'commonjs' | undefined) {
const configs = configsFactory(moduleType)
const pendingConfig = configs[config.moduleName]
async function writeConfigFile(cwd: string, pendingConfig: ConfigValue) {
if (pendingConfig.preInit) {
const result = await pendingConfig.preInit()
if (result === 'skip') {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export interface ConfigOptions {
fileContent: string
}

export type Configs = Record<ConfigChoice, {
export interface ConfigValue {
preInit?: () => Promise<void | 'skip'>
options: ConfigOptions[]
}>
}

export type Configs = Record<ConfigChoice, ConfigValue>

0 comments on commit 1cc6a73

Please sign in to comment.