Skip to content

Commit

Permalink
Merge pull request #27 from JPeer264/feature/scope
Browse files Browse the repository at this point in the history
Breaking Changes
  • Loading branch information
JPeer264 authored May 6, 2017
2 parents 781bba3 + c3baf46 commit 7b938c0
Show file tree
Hide file tree
Showing 22 changed files with 428 additions and 213 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"presets": ["env"]
"presets": ["env"],
"plugins": [
["transform-object-rest-spread", { "useBuiltIns": true }]
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ coverage

# macOS
.DS_STORE

# IDE / Editors
.vscode
8 changes: 3 additions & 5 deletions .sgcrc_default → .sgcrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"scope": false,
"body": true,
"emoji": false,
"types": [
{
"emoji": ":wrench:",
Expand Down Expand Up @@ -44,11 +47,6 @@
"emoji": ":white_check_mark:",
"type": "Test:",
"description": "Adding missing tests or correcting existing tests"
},
{
"emoji": ":tada:",
"type": "Init:",
"description": "The very first commit"
}
],
"rules": {
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ node_js:
- "4"
- "6"
install:
- npm i -g npm
- npm install
- yarn
script: npm test
notifications:
email:
on_failure: change
on_success: change
after_success:
- npm run coveralls
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,61 @@ $ sgc
You can even create a global config. Just go to your users home and create a `.sgcrc`. The global config will be triggered if no project configurations are present.

**Options:**
- [emojies](#emojies)
- [body](#body)
- [scope](#scope)
- [emoji](#emoji)
- [types](#types)
- [rules](#rules)

### emojies
### body

**Type:** `boolean`

**Default:** `true`
**Default**: `true`

Asks if more info (body) should be added. This will open your default editor.

Example:
```json
{
"body": false
}
```

### scope

**Type:** `boolean`

**Default:** `false`

A boolean to enable emojies at the beginning of a commit message
Asks for the scope in parentheses of the commit.

Example:
```json
{
"emojies": true
"scope": true
}
```

### emoji

**Type:** `boolean`

**Default:** `false`

A boolean to enable emoji at the beginning of a commit message

Example:
```json
{
"emoji": true
}
```

### types

> Types will define your git commits.
> Types will define your git commits. If `types` is not set in your own `.sgcrc`, the `types` of the global [.sgcrc](.sgcrc)

**Keys**

Expand Down Expand Up @@ -114,15 +147,15 @@ Available rules:

**Type:** `number`

**Default:** `undefined`
**Default:** `72`

If a number is set, it will not allow to commit messages **more than** the given number
If a number is set, it will not allow to commit messages **more than** the given number. If it is set to `-1` the rule is deactivated

Example:
```json
{
"rules": {
"max-char": 10
"max-char": -1
}
}
```
Expand All @@ -131,15 +164,15 @@ Example:

**Type:** `number`

**Default:** `undefined`
**Default:** `10`

If a number is set, it will not allow to commit messages **less than** the given number
If a number is set, it will not allow to commit messages **less than** the given number. If it is set to `-1` the rule is deactivated

Example:
```json
{
"rules": {
"min-char": 10
"min-char": -1
}
}
```
Expand Down
13 changes: 7 additions & 6 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

import chalk from 'chalk';
import execa from 'execa';
import yargs from 'yargs';
Expand All @@ -9,11 +10,11 @@ import updateNotifier from 'update-notifier';

import pkg from '../package.json';
import getConfig from './getConfig';
import { choices, questions } from './promptConfig';
import questions from './questions';
import combineTypeScope from './helpers/combineTypeScope';

const configuration = getConfig();
const choicesList = choices(configuration);
const questionsList = questions(choicesList, configuration);
const config = getConfig();
const questionsList = questions(config);
const argv = yargs
.usage('Usage: $0')
.alias('v', 'version')
Expand All @@ -32,7 +33,8 @@ if (argv.v) {
console.error(chalk.red('Please', chalk.bold('git add'), 'some files first before you commit.'));
} else {
inquirer.prompt(questionsList).then((answers) => {
const message = answers.moreInfo ? `${answers.editor}` : `${answers.type} ${answers.description}`;
const typeScope = combineTypeScope(answers.type, answers.scope);
const message = answers.body ? `${answers.editor}` : `${typeScope} ${answers.description}`;

return execa('git', ['commit', '-m', message], { stdio: 'inherit' })
.catch(() => {
Expand All @@ -41,4 +43,3 @@ if (argv.v) {
});
});
}

27 changes: 23 additions & 4 deletions lib/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os from 'os';
import path from 'path';
import json from 'json-extra';
import merge from 'lodash.merge';

const cwd = process.cwd();
const homedir = os.homedir();
Expand All @@ -10,17 +11,35 @@ const getConfig = (altPath) => {
const configObject = json.readToObjSync(pathString);
const globalConfig = json.readToObjSync(path.join(homedir, '.sgcrc'));
const packageConfig = json.readToObjSync(path.join(cwd, 'package.json')).sgc;
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc'));
const sgcrcTestDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));

const sgcrcDefault = sgcrcDefaultConfig || sgcrcTestDefaultConfig;

// priority order (1. highest priority):
// 1. local config
// - 1. .sgcrc
// - 2. (package.json).sgc
// 2. global config
// 3. default config from ../.sgcrc_default
const config = configObject || packageConfig || globalConfig || sgcrcDefaultConfig;
// 3. default config
// - 1. from ../.sgcrc
// - 2. test case ../.sgcrc is renamed to ../.sgcrc_default
const config = configObject || packageConfig || globalConfig || sgcrcDefault;

// set defaults which are necessary
const tempConfig = merge({}, sgcrcDefault, config);

// do not merge types
// so return them to their set default
if (config.types) {
tempConfig.types = config.types;
}

// next will remove "inherit" from the config
// eslint-disable-next-line
const { inherit, ...copiedConfig } = tempConfig;

return config;
return copiedConfig;
};

export default getConfig;
19 changes: 19 additions & 0 deletions lib/helpers/combineTypeScope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const combineTypeScope = (type, scope) => {
let thisType = type;

const thisScope = scope || '';

// add scope correctly if ':' is at the end
if (thisScope.length > 0) {
if (thisType.charAt(thisType.length - 1) === ':') {
thisType = thisType.slice(0, thisType.length - 1);
thisType = `${thisType}${thisScope}:`;
} else {
thisType += thisScope;
}
}

return thisType;
};

export default combineTypeScope;
58 changes: 0 additions & 58 deletions lib/promptConfig.js

This file was deleted.

73 changes: 73 additions & 0 deletions lib/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import chalk from 'chalk';
import ruleWarningMessages from './rules/ruleWarningMessages';
import combineTypeScope from './helpers/combineTypeScope';

const choices = (config) => {
const choicesList = [];

config.types.forEach((type) => {
const emoji = config.emoji && type.emoji ? `${type.emoji} ` : '';
const configType = type.type;
const description = type.description || '';

choicesList.push({
value: emoji + configType,
name: `${chalk.bold(configType)} ${description}`,
});
});

return choicesList;
};

const questions = (config) => {
const choicesList = choices(config);
const questionsList = [
{
type: 'list',
name: 'type',
message: 'Select the type of your commit:',
choices: choicesList,
},
{
type: 'input',
name: 'scope',
message: 'Enter your scope (no whitespaces allowed):',
when: () => config.scope,
validate: input => (input.match(/\s/) !== null ? 'No whitespaces allowed' : true),
filter: input => (input ? `(${input})` : input),
},
{
type: 'input',
name: 'description',
message: 'Enter your commit message:',
validate: (input) => {
const warnings = ruleWarningMessages(input, config);

return warnings || true;
},
},
{
type: 'confirm',
name: 'body',
message: 'Do you want to add a body?',
when: () => config.body,
default: false,
},
{
type: 'editor',
name: 'editor',
message: 'This will let you add more information',
when: answers => answers.body,
default: (answers) => {
const type = combineTypeScope(answers.type, answers.scope);

return `${type} ${answers.description}\n\n\n`;
},
},
];

return questionsList;
};

export default questions;
export { choices };
Loading

0 comments on commit 7b938c0

Please sign in to comment.