Skip to content

Commit e0e2258

Browse files
committed
✨ Feat: fail on non git
1 parent 316bcbe commit e0e2258

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import chalk from 'chalk';
33
import execa from 'execa';
44
import yargs from 'yargs';
5+
import git from 'git-utils';
56
import inquirer from 'inquirer';
67
import updateNotifier from 'update-notifier';
78

89
import pkg from '../package.json';
910
import getConfig from './getConfig';
1011
import { choices, questions } from './promptConfig';
1112

13+
const repository = git.open(process.cwd());
1214
const configuration = getConfig();
1315
const choicesList = choices(configuration);
1416
const questionsList = questions(choicesList);
@@ -24,6 +26,8 @@ updateNotifier({ pkg }).notify();
2426

2527
if (argv.v) {
2628
console.log(`v${pkg.version}`);
29+
} else if (!repository) {
30+
console.error('fatal: Not a git repository (or any of the parent directories): .git');
2731
} else {
2832
inquirer.prompt(questionsList).then((answers) => {
2933
const message = answers.moreInfo ? `${answers.editor}` : `${answers.type} ${answers.description}`;

test/cli.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'path';
2+
import test from 'ava';
3+
import execa from 'execa';
4+
import { homedir } from 'os';
5+
6+
import pkg from '../package.json';
7+
8+
const cli = 'dest/cli.js';
9+
10+
test('should print the right version', async (t) => {
11+
const { stdout } = await execa(cli, ['--version']);
12+
13+
t.is(stdout, `v${pkg.version}`);
14+
});
15+
16+
test('should fail on non git repository', async (t) => {
17+
// assume that the homedir is not a git repo
18+
process.chdir(homedir());
19+
20+
const { stderr } = await execa(path.join(__dirname, '..', cli));
21+
22+
process.chdir(path.join(__dirname, '..'));
23+
24+
t.is(stderr, 'fatal: Not a git repository (or any of the parent directories): .git');
25+
});
26+

0 commit comments

Comments
 (0)