Skip to content

Commit

Permalink
Feat: init commit (#28)
Browse files Browse the repository at this point in the history
* Chore: add git-commit-count

* Chore: add npm-debug.log

* Feat: add support for initial commit

* Test: update test for initial commit

* Fix: update validation for first commit

* Docs: update README.md for new feature

* Docs: typo

* Fix: add type check
  • Loading branch information
aichbauer committed May 15, 2017
1 parent 6cc8d7d commit 3ca438c
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# modules
node_modules
npm-debug.log

# generated
dest
Expand Down
5 changes: 5 additions & 0 deletions .sgcrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"scope": false,
"body": true,
"emoji": false,
"initial-commit": {
"isEnabled": true,
"emoji": ":tada:",
"message": "Initial commit"
},
"types": [
{
"emoji": ":wrench:",
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can even create a global config. Just go to your users home and create a `.s
- [body](#body)
- [scope](#scope)
- [emoji](#emoji)
- [initial-commit](#initial-commit)
- [types](#types)
- [rules](#rules)

Expand Down Expand Up @@ -94,6 +95,28 @@ Example:
}
```

### initial-commit

**Type:** `object`

**Default:**

```json
{
"initial-commit": {
"isEnabled": true,
"emoji": ":tada:",
"message": "Initial commit"
}
}
```

**Keys:**

- `isEnabled` - Whether an explicit initial commit should be used for the very first commit
- `emoji` - An emoji which will be appended at the beginning of the commit ([Emoji Cheat Sheet](https://www.webpagefx.com/tools/emoji-cheat-sheet/))
- `message` - The commit message for the very first commit

### types

> Types will define your git commits. If `types` is not set in your own `.sgcrc`, the `types` of the global [.sgcrc](.sgcrc)
Expand Down
36 changes: 27 additions & 9 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#!/usr/bin/env node

import chalk from 'chalk';
import commitCount from 'git-commit-count';
import execa from 'execa';
import yargs from 'yargs';
import inquirer from 'inquirer';
import isGit from 'is-git-repository';
import isAdded from 'is-git-added';
import isGit from 'is-git-repository';
import updateNotifier from 'update-notifier';
import yargs from 'yargs';

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

const config = getConfig();
const questionsList = questions(config);
const question = initQuestion(config);

const gitCommitExeca = message => (
execa('git', ['commit', '-m', message], { stdio: 'inherit' })
.catch(() => {
console.error(chalk.red('\nAn error occured. Try to resolve the previous error and run following commit message again:'));
console.error(chalk.green(`git commit -m "${message}"`));
})
);

const argv = yargs
.usage('Usage: $0')
.alias('v', 'version')
Expand All @@ -31,15 +45,19 @@ if (argv.v) {
console.error('fatal: Not a git repository (or any of the parent directories): .git');
} else if (!isAdded()) {
console.error(chalk.red('Please', chalk.bold('git add'), 'some files first before you commit.'));
} else if (commitCount() === 0 &&
typeof config['initial-commit'] === 'object' &&
config['initial-commit'].isEnabled) {
const message = initMessage(config);

inquirer.prompt(question).then(answers => (
answers.initCommit ? gitCommitExeca(message) : undefined
));
} else {
inquirer.prompt(questionsList).then((answers) => {
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(() => {
console.error(chalk.red('\nAn error occured. Try to resolve the previous error and run following commit message again:'));
console.error(chalk.green(`git commit -m "${message}"`));
});
return gitCommitExeca(message);
});
}
8 changes: 6 additions & 2 deletions lib/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from 'os';
import path from 'path';
import json from 'json-extra';
import merge from 'lodash.merge';
import os from 'os';
import path from 'path';

const cwd = process.cwd();
const homedir = os.homedir();
Expand Down Expand Up @@ -35,6 +35,10 @@ const getConfig = (altPath) => {
tempConfig.types = config.types;
}

if (config['initial-commit']) {
tempConfig['initial-commit'] = config['initial-commit'];
}

// next will remove "inherit" from the config
// eslint-disable-next-line
const { inherit, ...copiedConfig } = tempConfig;
Expand Down
33 changes: 31 additions & 2 deletions lib/questions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import ruleWarningMessages from './rules/ruleWarningMessages';
import combineTypeScope from './helpers/combineTypeScope';
import ruleWarningMessages from './rules/ruleWarningMessages';

const choices = (config) => {
const choicesList = [];
Expand All @@ -19,6 +19,31 @@ const choices = (config) => {
return choicesList;
};

const initMessage = (config) => {
let message = '';

if (config.emoji &&
typeof config['initial-commit'] === 'object' &&
config['initial-commit'].isEnabled) {
message = `${config['initial-commit'].emoji} ${config['initial-commit'].message}`;
} else {
message = config['initial-commit'].message;
}

return message;
};

const initQuestion = (config) => {
const message = initMessage(config);

return {
type: 'confirm',
name: 'initCommit',
message: `Confirm as first commit message: "${message}"`,
default: true,
};
};

const questions = (config) => {
const choicesList = choices(config);
const questionsList = [
Expand Down Expand Up @@ -70,4 +95,8 @@ const questions = (config) => {
};

export default questions;
export { choices };
export {
choices,
initMessage,
initQuestion,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"dependencies": {
"chalk": "^1.1.3",
"execa": "^0.6.1",
"git-commit-count": "^1.1.2",
"inquirer": "^3.0.6",
"is-git-added": "^1.0.1",
"is-git-repository": "^1.0.1",
Expand Down
4 changes: 2 additions & 2 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { homedir } from 'os';
import test from 'ava';
import path from 'path';
import execa from 'execa';
import { homedir } from 'os';
import path from 'path';

import pkg from '../package.json';

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/.sgcrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"scope": false,
"body": true,
"emoji": false,
"initial-commit": {
"isEnabled": true,
"emoji": ":tada:",
"message": "Initial commit"
},
"types": [
{
"emoji": ":emo:",
Expand Down
4 changes: 2 additions & 2 deletions test/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os from 'os';
import test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import json from 'json-extra';
import os from 'os';
import path from 'path';

import getConfig from '../lib/getConfig';

Expand Down
46 changes: 42 additions & 4 deletions test/questions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os from 'os';
import test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';

import getConfig from '../lib/getConfig';
import questions, { choices } from '../lib/questions';
import { withEmoji, withoutEmoji } from './fixtures/questions';
import getConfig from '../lib/getConfig';
import questions, {
choices,
initMessage,
initQuestion,
} from '../lib/questions';

const cwd = process.cwd();
const date = new Date();
Expand Down Expand Up @@ -108,3 +112,37 @@ test('CONFIRM EDITOR | check if it shows if it has to', (t) => {

t.is(questionsList[3].when(), config.body);
});

test('INIT COMMIT | check message without emoji', (t) => {
const config = getConfig();
const message = initMessage(config);

t.is(message, config['initial-commit'].message);
});

test('INIT COMMIT | check message with emoji', (t) => {
const config = getConfig();

config.emoji = true;

const message = initMessage(config);

t.is(message, `${config['initial-commit'].emoji} ${config['initial-commit'].message}`);
});

test('INIT QUESTION | check message without emoji', (t) => {
const config = getConfig();
const question = initQuestion(config);

t.is(question.message, `Confirm as first commit message: "${config['initial-commit'].message}"`);
});

test('INIT QUESTION | check message with emoji', (t) => {
const config = getConfig();

config.emoji = true;

const question = initQuestion(config);

t.is(question.message, `Confirm as first commit message: "${config['initial-commit'].emoji} ${config['initial-commit'].message}"`);
});
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,14 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

git-commit-count@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/git-commit-count/-/git-commit-count-1.1.2.tgz#6c1e4a8d7daf5444a9ea501da605d180ee995fb6"
dependencies:
execa "^0.6.1"
is-git-repository "^1.0.1"
path-is-absolute "^1.0.1"

glob-base@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4"
Expand Down Expand Up @@ -3104,7 +3112,7 @@ path-exists@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"

path-is-absolute@^1.0.0:
path-is-absolute@^1.0.0, path-is-absolute@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"

Expand Down

0 comments on commit 3ca438c

Please sign in to comment.