From 1cc6a7382e99977220d023374b14723ac0b12485 Mon Sep 17 00:00:00 2001 From: shlroland Date: Fri, 11 Oct 2024 14:27:41 +0800 Subject: [PATCH] fix: :bug: simplify config file writing by directly using husky hook contents without conditions --- packages/cli/src/tasks/config/config-content.ts | 8 ++------ packages/cli/src/tasks/config/write-config.ts | 16 +++++++++++----- packages/cli/src/types.ts | 6 ++++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/tasks/config/config-content.ts b/packages/cli/src/tasks/config/config-content.ts index 495ce66a..0fb329c1 100644 --- a/packages/cli/src/tasks/config/config-content.ts +++ b/packages/cli/src/tasks/config/config-content.ts @@ -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'], }, ], }, diff --git a/packages/cli/src/tasks/config/write-config.ts b/packages/cli/src/tasks/config/write-config.ts index 25da9927..29c5eb7c 100644 --- a/packages/cli/src/tasks/config/write-config.ts +++ b/packages/cli/src/tasks/config/write-config.ts @@ -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 { @@ -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') { diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 1f6a7b33..add5f770 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -21,7 +21,9 @@ export interface ConfigOptions { fileContent: string } -export type Configs = Record Promise options: ConfigOptions[] -}> +} + +export type Configs = Record