Skip to content

Commit

Permalink
feat: global config
Browse files Browse the repository at this point in the history
+ 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
  • Loading branch information
lucas-labs committed Oct 2, 2022
1 parent 910df80 commit f2d11ae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/coco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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('.');

Expand Down
23 changes: 16 additions & 7 deletions src/common/config/coco.config.ts
Original file line number Diff line number Diff line change
@@ -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 {};
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 0 additions & 11 deletions src/common/git/commands/check-git.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/common/git/commands/repoPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { execute } from '../executer';

/** Gets current repo top-level path */
export async function repoPath(repo: string): Promise<string> {
const command = ['rev-parse', '--show-toplevel'];
const { code, out } = await execute(repo, command);
if (code !== 0) return undefined;
return out?.trim();
}

0 comments on commit f2d11ae

Please sign in to comment.