Skip to content

Commit c9bcc36

Browse files
authored
Feat: change options to commands closes(#13) (#14)
* Feat: use commandDir * Feat: refactor for commandDir and change from option -r to command r * Test: update test for new feat * Docs: update readme * Refactor: remove not used option * Test: update tests * Chore: mv default to release * Test: update tests * Docs: update readme BREAKING CHANGE: option (-r, -v) removed, add commands (r, v)
1 parent 75b0ee6 commit c9bcc36

File tree

33 files changed

+378
-197
lines changed

33 files changed

+378
-197
lines changed

README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ $ yarn add global semantic-git-release-cli
2828

2929
## Usage
3030

31-
- [Release a new version](#release-a-new-version)
32-
- [Recover the complete changelog](#recover-the-complete-changelog)
33-
34-
### Release a new version
35-
36-
If you start using `semantic-git-release-cli` with a brand new project simply start in this section.
37-
38-
If you already released some verions of your project, you might consider to [recover the complete changelog first](#recover-the-complete-changelog).
39-
4031
Forget the times when you had to manually write changelogs, update versions, tag commits. Now just type:
4132

4233
```sh
@@ -49,6 +40,19 @@ or if you already have an alias for sgr, use following instead:
4940
$ semantic-git-release
5041
```
5142

43+
## Commands
44+
45+
`semantic-git-release-cli` was build to be as simple as possible, so there are just a few commands you need to know.
46+
47+
* [sgr](#sgr)
48+
* [sgr recover](#sgr-recover)
49+
* [sgr version](#sgr-version)
50+
* [sgr --help](#sgr---help)
51+
52+
### sgr
53+
54+
With `sgr` you can release a new version of your project.
55+
5256
#### Tasks
5357

5458
So there are a few tasks `semantic-git-release-cli` will do for you:
@@ -59,15 +63,36 @@ So there are a few tasks `semantic-git-release-cli` will do for you:
5963
- creates or updates the `CHANGELOG.md`
6064
- commits and tags the new `version`
6165

62-
### Recover the complete changelog
66+
```sh
67+
# release a new version
68+
$ sgr
69+
```
70+
71+
### sgr recover
72+
73+
With `sgr recover [backup]` you can recover your complete CHANGELOG.md if you just started to use `semantic-git-release-cli` but already released (and tagged) versions.
74+
75+
```sh
76+
# generates the complete CHANGELOG.md
77+
$ sgr recover
78+
# generates the complete CHANGELOG.md and creates a backup of the current CHANGELOG.md in .sgr_backup
79+
$ sgr recover backup
80+
```
81+
82+
### sgr version
83+
84+
With `sgr version` you can display the current version of `semantic-git-release-cli`.
85+
86+
```sh
87+
# current version
88+
$ sgr version
89+
```
6390

64-
If you are start to use `semantic-git-release-cli` but already released (and tagged) versions. Simply use the recover mode.
91+
### sgr --help
6592

66-
If `--recover` is set to `backup` it will generate a backup file of your current `CHANGELOG.md` inside of the `.sgr_backup` directory, which gets generated the first time you use this command inside of your current working directory.
93+
With `sgr --help` you can display usage of `semantic-git-release-cli`.
6794

6895
```sh
69-
# generates the complete CHANGELOG.md without backup
70-
$ sgr --recover # or short `sgr -r`
71-
# generates the complete CHANGELOG.md with backup
72-
$ sgr --recover backup # or short `sgr -r b`
96+
# usage of cli
97+
$ sgr --help
7398
```

lib/cli.js

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,13 @@
11
#!/usr/bin/env node
22

3-
import chalk from 'chalk';
4-
import inquirer from 'inquirer';
5-
import isAdded from 'is-git-added';
6-
import isGit from 'is-git-repository';
7-
import getGitRemotes from 'get-git-remotes';
8-
import getCommitRange from 'git-commit-range';
9-
import taggedCommits from 'tagged-git-commits';
10-
import updateNotifier from 'update-notifier';
113
import yargs from 'yargs';
12-
13-
import generateVersions from './helpers/generateVersions';
14-
import getLatestVersion from './helpers/getLatestVersion';
4+
import updateNotifier from 'update-notifier';
155
import pkg from '../package.json';
16-
import questions from './questions';
17-
import tasks from './tasks';
18-
import recoverTasks from './recover-tasks';
196

20-
const argv = yargs
21-
.usage('Usage: $0')
22-
.alias('v', 'version')
23-
.describe('v', 'Version number')
24-
.help('h')
25-
.alias('h', 'help')
26-
.alias('r', 'recover')
27-
.describe('r', 'Recover the complete CHANGELOG.md')
7+
yargs // eslint-disable-line
8+
.commandDir('cmds')
9+
.demandCommand()
10+
.help()
2811
.argv;
2912

3013
updateNotifier({ pkg }).notify();
31-
32-
const cli = () => {
33-
const cwd = process.cwd();
34-
const latestTaggedCommits = taggedCommits({ path: cwd });
35-
const latestTaggedCommit = latestTaggedCommits.length === 0 ? '' : latestTaggedCommits[0].commit;
36-
const commits = getCommitRange({ path: cwd, from: latestTaggedCommit });
37-
const latestVersion = getLatestVersion();
38-
const newVersions = generateVersions(latestVersion);
39-
const questionsList = questions(newVersions);
40-
41-
if (!isGit(cwd)) {
42-
return console.warn(chalk.red('Error: this is not a git repository... make sure you are in the right directory'));
43-
} else if (!isAdded(cwd) && commits.length === 0) {
44-
return console.warn(chalk.red('Error: no changes... try to git add <files>'));
45-
} else if (commits.length === 0) {
46-
return console.warn(chalk.red('Error: no commits yet... try to git commit -m <message>'));
47-
} else if (!getGitRemotes(cwd)) {
48-
return console.warn(chalk.red('Error: it seems you do not have a remote repository set... try to git remote add origin <remote-url>'));
49-
} else if (latestVersion === '') {
50-
return console.warn(chalk.red('Error: it seems you do not have a package.json... try npm init'));
51-
}
52-
53-
return inquirer
54-
.prompt(questionsList)
55-
.then((answers) => {
56-
if (answers.ownVersion) {
57-
return tasks(commits, answers.ownVersion)
58-
.run()
59-
.catch(() => console.warn(chalk.red('Error: whoops, try to solve the problem mentioned above...')));
60-
}
61-
62-
return tasks(commits, answers.version)
63-
.run()
64-
.catch(() => console.warn(chalk.red('Error: whoops, try to solve the problem mentioned above...')));
65-
})
66-
.catch((err) => console.warn(chalk.red(err)));
67-
};
68-
69-
if (argv.v) {
70-
console.info(`v${pkg.version}`);
71-
} else if (argv.recover) {
72-
if (argv.recover === 'b' || argv.recover === 'backup') {
73-
recoverTasks(true).run();
74-
} else {
75-
recoverTasks(false).run();
76-
}
77-
} else {
78-
cli();
79-
}
80-

lib/cmds/recover.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import recoverTasks from '../tasks/recover-tasks';
2+
3+
const command = 'recover [backup]';
4+
5+
const builder = {
6+
backup: {
7+
default: false,
8+
},
9+
};
10+
11+
const aliases = ['r'];
12+
13+
const desc = 'Recover the complete CHANGELOG.md';
14+
15+
/* istanbul ignore next */
16+
const handler = (argv) => {
17+
if (argv.backup || argv.b) {
18+
return recoverTasks(true).run();
19+
}
20+
21+
return recoverTasks(false).run();
22+
};
23+
24+
export {
25+
command,
26+
builder,
27+
aliases,
28+
desc,
29+
handler,
30+
};

lib/cmds/release.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import release from '../tasks/release-tasks';
2+
3+
const command = '*';
4+
5+
const desc = 'Release a new version (run tests, write changelog, tag version, push release)';
6+
7+
/* istanbul ignore next */
8+
const handler = () => release();
9+
10+
export {
11+
command,
12+
desc,
13+
handler,
14+
};

lib/cmds/version.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pkg from '../../package.json';
2+
3+
const command = 'version';
4+
5+
const aliases = ['v'];
6+
7+
const desc = 'Show the current version number';
8+
9+
/* istanbul ignore next */
10+
const handler = () => console.info(`sgr version ${pkg.version}`);
11+
12+
export {
13+
command,
14+
aliases,
15+
desc,
16+
handler,
17+
};

lib/helpers/changelogParts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const body = (commits, version) => {
2828
return;
2929
}
3030

31-
changelogData = `${changelogData} ${oneCommit(commitInfo)}\n`;
31+
changelogData = `${changelogData}${oneCommit(commitInfo)}\n`;
3232

3333
if (commits.length - 1 === i) {
3434
changelogData = `${changelogData}\n`;

lib/helpers/generateCompleteChangelog.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ const generateCompleteChangelog = (backup) => {
9494
fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), '');
9595
}
9696

97-
return writeToFile(getAllTags(), exists, backup);
98-
} catch (err) {
97+
writeToFile(getAllTags(), exists, backup);
98+
99+
return true;
100+
} catch (err) /* istanbul ignore next */ {
99101
return false;
100102
}
101103
};

lib/helpers/updateChangelog.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ const updateChangelog = (commits = [], version) => {
2323
fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), '');
2424
}
2525

26-
return writeToFile(commits, version, exists);
27-
} catch (err) {
26+
writeToFile(commits, version, exists);
27+
28+
return true;
29+
} catch (err) /* istanbul ignore next */ {
2830
return false;
2931
}
3032
};

lib/tasks.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)