From f2d11ae17e38be4c7d164f156e8b01f5f75a0512 Mon Sep 17 00:00:00 2001 From: Lucas Colombo Date: Sun, 2 Oct 2022 17:19:00 -0300 Subject: [PATCH] feat: global config + Implement global configuration feature which gives the possibility to store a config file in the user home directory that will override the default config. ref: #9 pr: #10 --- src/coco.tsx | 11 ++++++----- src/common/config/coco.config.ts | 23 ++++++++++++++++------- src/common/git/commands/check-git.ts | 11 ----------- src/common/git/commands/repoPath.ts | 9 +++++++++ 4 files changed, 31 insertions(+), 23 deletions(-) delete mode 100644 src/common/git/commands/check-git.ts create mode 100644 src/common/git/commands/repoPath.ts diff --git a/src/coco.tsx b/src/coco.tsx index c981cad..be87f88 100644 --- a/src/coco.tsx +++ b/src/coco.tsx @@ -2,8 +2,8 @@ import { render } from 'ink'; import React from 'react'; -import { getConfig } from './common/config/coco.config'; -import { checkGit } from './common/git/commands/check-git'; +import { getConfig } from './common/config/coco.config'; +import { repoPath } from './common/git/commands/repoPath'; import { listStaged } from './common/git/commands/list-staged'; import { i18n, LoadDictonary } from './common/i18n/i18n'; import { CocoApp } from './views/CocoApp'; @@ -12,12 +12,13 @@ import c from 'chalk'; run(); async function run() { - const config = getConfig(); - + const currentRepo = await repoPath('.') await LoadDictonary(); // check if we are inside a git repo - if (await checkGit('.')) { + if (currentRepo) { + const config = getConfig(currentRepo); + // check repo status const staged = await listStaged('.'); diff --git a/src/common/config/coco.config.ts b/src/common/config/coco.config.ts index 39d60bd..c05462b 100644 --- a/src/common/config/coco.config.ts +++ b/src/common/config/coco.config.ts @@ -1,12 +1,13 @@ import { Config, ConventionalCommitType } from '../types/coco.types'; import { existsSync, readFileSync } from 'fs'; +import { homedir } from 'os'; import { parse } from 'yaml'; /** Loads user config file if it exists */ -export function loadUserConfig(cwd = process.cwd()) { +export function loadConfigFile(path: string) { const cfgFileNames = ['coco.yaml', 'coco.yml', '.cocorc']; const configPath = cfgFileNames - .map((name) => `${cwd}/${name}`) + .map((name) => `${path}/${name}`) .find((name) => existsSync(name)); if (!configPath) return {}; @@ -108,18 +109,26 @@ const defaultConfig: Config = { askBreakingChange: true, }; -export function getConfig(): Config { - const userCfg = loadUserConfig(); +export function getConfig(repoPath: string): Config { + const homeDir = homedir(); + let config = mergeConfig(homeDir, defaultConfig); + config = mergeConfig(repoPath, config); - const config = Object.keys(defaultConfig) + return config; +} + +export function mergeConfig(cwd: string, cfg: Config): Config { + const userCfg = loadConfigFile(cwd); + + const config = Object.keys(cfg) .map((k): keyof Config => k as keyof Config) .reduce((acc, key) => { const userOverride = userCfg[key]; - if (userOverride) { + if (userOverride !== undefined) { acc[key] = userCfg[key]; } return acc; - }, defaultConfig); + }, cfg); return config; } diff --git a/src/common/git/commands/check-git.ts b/src/common/git/commands/check-git.ts deleted file mode 100644 index 0fdf5a7..0000000 --- a/src/common/git/commands/check-git.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { execute } from '../executer'; - -/** Checks if repo is a path to a git repository */ -export async function checkGit(repo: string): Promise { - const command = ['rev-parse', '--is-inside-work-tree']; - const { code, out } = await execute(repo, command); - - if (code !== 0) return false; - - return out?.trim() === 'true'; -} diff --git a/src/common/git/commands/repoPath.ts b/src/common/git/commands/repoPath.ts new file mode 100644 index 0000000..e4954b5 --- /dev/null +++ b/src/common/git/commands/repoPath.ts @@ -0,0 +1,9 @@ +import { execute } from '../executer'; + +/** Gets current repo top-level path */ +export async function repoPath(repo: string): Promise { + const command = ['rev-parse', '--show-toplevel']; + const { code, out } = await execute(repo, command); + if (code !== 0) return undefined; + return out?.trim(); +}