Skip to content

Commit

Permalink
feat: user privided configuration
Browse files Browse the repository at this point in the history
Added support for loading user defined config files.
It will now open any of the following if present and
merge it with default config:
    coco.yaml
    coco.yml
    .cocorc

Closes: #2
  • Loading branch information
lucas-labs committed Oct 1, 2022
1 parent 5350c73 commit 4da8063
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,7 @@
},
"author": "Lucas Colombo <[email protected]> (https://lucaslabs.tech/)",
"scripts": {
"coco": "node bin/coco.js",
"coco": "coco",
"build": "tsc",
"dev": "ts-node --project tsconfig.json src/coco.tsx",
"pretest": "npm run build",
Expand All @@ -27,10 27,10 @@
"url": "https://github.com/lucas-labs/coco.git"
},
"bugs": {
"url" : "https://github.com/lucas-labs/coco/issues",
"email" : "[email protected]"
"url": "https://github.com/lucas-labs/coco/issues",
"email": "[email protected]"
},
"keywords" : [
"keywords": [
"commit",
"commits",
"conventional",
Expand Down Expand Up @@ -69,7 69,8 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"string-width": "4.2.3",
"strip-ansi": "6.0.1"
"strip-ansi": "6.0.1",
"yaml": "^2.1.1"
},
"devDependencies": {
"@ava/typescript": "^3.0.1",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/coco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@

import { render } from 'ink';
import React from 'react';
import { defaultConfig } from './common/config/coco.config';
import { getConfig } from './common/config/coco.config';
import { checkGit } from './common/git/commands/check-git';
import { listStaged } from './common/git/commands/list-staged';
import { i18n, LoadDictonary } from './common/i18n/i18n';
Expand All @@ -12,7 12,7 @@ import c from 'chalk';
run();

async function run() {
const config = defaultConfig;
const config = getConfig();

await LoadDictonary();

Expand Down
49 changes: 46 additions & 3 deletions src/common/config/coco.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 1,21 @@
import { Config, ConventionalCommitType } from '../types/coco.types';
import { existsSync, readFileSync } from 'fs';
import { parse } from 'yaml';

/** Loads user config file if it exists */
export function loadUserConfig(cwd = process.cwd()) {
const cfgFileNames = ['coco.yaml', 'coco.yml', '.cocorc'];
const configPath = cfgFileNames
.map((name) => `${cwd}/${name}`)
.find((name) => existsSync(name));

if (!configPath) return {};

return parse(readFileSync(configPath, 'utf8'));
}

/** default commit types if no types config is provided */
export const defaultTypes: ConventionalCommitType[] = [
const defaultTypes: ConventionalCommitType[] = [
{
desc: 'Introduces a new feature',
name: 'feat',
Expand Down Expand Up @@ -75,12 89,41 @@ export const defaultTypes: ConventionalCommitType[] = [
* We'll merge this config with the user
* provided config
*/
export const defaultConfig: Config = {
const defaultConfig: Config = {
types: defaultTypes,
useEmoji: false,
// TODO: accept a custom array of scopes
// and instead of asking the user to type
// the scope, present the list as it's
// now being done with types
scopes: [],

// TODO: emoji/gitmoji support
useEmoji: false,

// TODO: add support for configuring if scope,
// body, footer and bc steps should be asked
askScope: true,
askBody: true,
askFooter: true,
askBreakingChange: true,
};

export function getConfig(): Config {
const userCfg = loadUserConfig();

const config = Object.keys(defaultConfig)
.map((k): keyof Config => k as keyof Config)
.reduce((acc, key) => {
const userOverride = userCfg[key];
if (userOverride) {
acc[key] = userCfg[key];
}
return acc;
}, defaultConfig);

return config;
}

/** Get type by its name by searching in a list of types */
export const getCommitType = (types: ConventionalCommitType[], name: string) => {
return types.find((type) => type.name === name);
Expand Down
6 changes: 5 additions & 1 deletion src/common/types/coco.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,11 @@ export interface ConventionalCommitType {
export interface Config {
types: ConventionalCommitType[];
useEmoji: boolean;
scopes?: string[];
scopes: string[];
askScope: boolean;
askBody: boolean;
askFooter: boolean;
askBreakingChange: boolean;
}

/** Represents a string value that can be valid or invalid */
Expand Down

0 comments on commit 4da8063

Please sign in to comment.