Skip to content

Commit

Permalink
Feat: change options to commands closes(#13) (#14)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
aichbauer committed Oct 12, 2017
1 parent 75b0ee6 commit c9bcc36
Show file tree
Hide file tree
Showing 33 changed files with 378 additions and 197 deletions.
57 changes: 41 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ $ yarn add global semantic-git-release-cli

## Usage

- [Release a new version](#release-a-new-version)
- [Recover the complete changelog](#recover-the-complete-changelog)

### Release a new version

If you start using `semantic-git-release-cli` with a brand new project simply start in this section.

If you already released some verions of your project, you might consider to [recover the complete changelog first](#recover-the-complete-changelog).

Forget the times when you had to manually write changelogs, update versions, tag commits. Now just type:

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

## Commands

`semantic-git-release-cli` was build to be as simple as possible, so there are just a few commands you need to know.

* [sgr](#sgr)
* [sgr recover](#sgr-recover)
* [sgr version](#sgr-version)
* [sgr --help](#sgr---help)

### sgr

With `sgr` you can release a new version of your project.

#### Tasks

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

### Recover the complete changelog
```sh
# release a new version
$ sgr
```

### sgr recover

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.

```sh
# generates the complete CHANGELOG.md
$ sgr recover
# generates the complete CHANGELOG.md and creates a backup of the current CHANGELOG.md in .sgr_backup
$ sgr recover backup
```

### sgr version

With `sgr version` you can display the current version of `semantic-git-release-cli`.

```sh
# current version
$ sgr version
```

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

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.
With `sgr --help` you can display usage of `semantic-git-release-cli`.

```sh
# generates the complete CHANGELOG.md without backup
$ sgr --recover # or short `sgr -r`
# generates the complete CHANGELOG.md with backup
$ sgr --recover backup # or short `sgr -r b`
# usage of cli
$ sgr --help
```
77 changes: 5 additions & 72 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,13 @@
#!/usr/bin/env node

import chalk from 'chalk';
import inquirer from 'inquirer';
import isAdded from 'is-git-added';
import isGit from 'is-git-repository';
import getGitRemotes from 'get-git-remotes';
import getCommitRange from 'git-commit-range';
import taggedCommits from 'tagged-git-commits';
import updateNotifier from 'update-notifier';
import yargs from 'yargs';

import generateVersions from './helpers/generateVersions';
import getLatestVersion from './helpers/getLatestVersion';
import updateNotifier from 'update-notifier';
import pkg from '../package.json';
import questions from './questions';
import tasks from './tasks';
import recoverTasks from './recover-tasks';

const argv = yargs
.usage('Usage: $0')
.alias('v', 'version')
.describe('v', 'Version number')
.help('h')
.alias('h', 'help')
.alias('r', 'recover')
.describe('r', 'Recover the complete CHANGELOG.md')
yargs // eslint-disable-line
.commandDir('cmds')
.demandCommand()
.help()
.argv;

updateNotifier({ pkg }).notify();

const cli = () => {
const cwd = process.cwd();
const latestTaggedCommits = taggedCommits({ path: cwd });
const latestTaggedCommit = latestTaggedCommits.length === 0 ? '' : latestTaggedCommits[0].commit;
const commits = getCommitRange({ path: cwd, from: latestTaggedCommit });
const latestVersion = getLatestVersion();
const newVersions = generateVersions(latestVersion);
const questionsList = questions(newVersions);

if (!isGit(cwd)) {
return console.warn(chalk.red('Error: this is not a git repository... make sure you are in the right directory'));
} else if (!isAdded(cwd) && commits.length === 0) {
return console.warn(chalk.red('Error: no changes... try to git add <files>'));
} else if (commits.length === 0) {
return console.warn(chalk.red('Error: no commits yet... try to git commit -m <message>'));
} else if (!getGitRemotes(cwd)) {
return console.warn(chalk.red('Error: it seems you do not have a remote repository set... try to git remote add origin <remote-url>'));
} else if (latestVersion === '') {
return console.warn(chalk.red('Error: it seems you do not have a package.json... try npm init'));
}

return inquirer
.prompt(questionsList)
.then((answers) => {
if (answers.ownVersion) {
return tasks(commits, answers.ownVersion)
.run()
.catch(() => console.warn(chalk.red('Error: whoops, try to solve the problem mentioned above...')));
}

return tasks(commits, answers.version)
.run()
.catch(() => console.warn(chalk.red('Error: whoops, try to solve the problem mentioned above...')));
})
.catch((err) => console.warn(chalk.red(err)));
};

if (argv.v) {
console.info(`v${pkg.version}`);
} else if (argv.recover) {
if (argv.recover === 'b' || argv.recover === 'backup') {
recoverTasks(true).run();
} else {
recoverTasks(false).run();
}
} else {
cli();
}

30 changes: 30 additions & 0 deletions lib/cmds/recover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import recoverTasks from '../tasks/recover-tasks';

const command = 'recover [backup]';

const builder = {
backup: {
default: false,
},
};

const aliases = ['r'];

const desc = 'Recover the complete CHANGELOG.md';

/* istanbul ignore next */
const handler = (argv) => {
if (argv.backup || argv.b) {
return recoverTasks(true).run();
}

return recoverTasks(false).run();
};

export {
command,
builder,
aliases,
desc,
handler,
};
14 changes: 14 additions & 0 deletions lib/cmds/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import release from '../tasks/release-tasks';

const command = '*';

const desc = 'Release a new version (run tests, write changelog, tag version, push release)';

/* istanbul ignore next */
const handler = () => release();

export {
command,
desc,
handler,
};
17 changes: 17 additions & 0 deletions lib/cmds/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pkg from '../../package.json';

const command = 'version';

const aliases = ['v'];

const desc = 'Show the current version number';

/* istanbul ignore next */
const handler = () => console.info(`sgr version ${pkg.version}`);

export {
command,
aliases,
desc,
handler,
};
2 changes: 1 addition & 1 deletion lib/helpers/changelogParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const body = (commits, version) => {
return;
}

changelogData = `${changelogData} ${oneCommit(commitInfo)}\n`;
changelogData = `${changelogData}${oneCommit(commitInfo)}\n`;

if (commits.length - 1 === i) {
changelogData = `${changelogData}\n`;
Expand Down
6 changes: 4 additions & 2 deletions lib/helpers/generateCompleteChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ const generateCompleteChangelog = (backup) => {
fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), '');
}

return writeToFile(getAllTags(), exists, backup);
} catch (err) {
writeToFile(getAllTags(), exists, backup);

return true;
} catch (err) /* istanbul ignore next */ {
return false;
}
};
Expand Down
6 changes: 4 additions & 2 deletions lib/helpers/updateChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ const updateChangelog = (commits = [], version) => {
fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), '');
}

return writeToFile(commits, version, exists);
} catch (err) {
writeToFile(commits, version, exists);

return true;
} catch (err) /* istanbul ignore next */ {
return false;
}
};
Expand Down
File renamed without changes.
34 changes: 0 additions & 34 deletions lib/tasks.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Listr from 'listr';

import generateCompleteChangelog from '../helpers/generateCompleteChangelog';
import generateCompleteChangelog from '../../helpers/generateCompleteChangelog';

const recoverTasks = (backup) => (
new Listr([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Listr from 'listr';

import updateChangelog from '../helpers/updateChangelog';
import updateChangelog from '../../helpers/updateChangelog';

const changelogTasks = (commits, version) => (
new Listr([
Expand All @@ -9,7 +9,7 @@ const changelogTasks = (commits, version) => (
task: () => {
const updated = updateChangelog(commits, version);

if (updated === 'could not write') {
if (!updated) {
throw new Error('could not write CHANGELOG.md... make sure you have write and read access to it');
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
import {
deleteNodeModules,
installNodeModules,
} from '../helpers/cleanNodeModules';
} from '../../helpers/cleanNodeModules';

const cleanup = () => {
const cwd = process.cwd();
Expand Down

0 comments on commit c9bcc36

Please sign in to comment.