Skip to content

Commit 89a072c

Browse files
committed
refactor(cli): Completely rewrite commitizen
In this release, commitizen was completely rewritten to clean up the code base, add tests, and fix several long outstanding bugs. Additionally, there is additional functionality that has been added to attempt to polish the user experience and address certain important edge cases. Closes #25, closes #16, closes #13, closes #12, closes #7, closes commitizen/cz-conventional-changelog#8, closes commitizen/cz-conventional-changelog#9
1 parent 0be1657 commit 89a072c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1446
-216
lines changed

.cz.json

-3
This file was deleted.

.gitignore

+4-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
# Logs
2-
logs
3-
*.log
4-
5-
# Runtime data
6-
pids
7-
*.pid
8-
*.seed
9-
10-
# Directory for instrumented libs generated by jscoverage/JSCover
11-
lib-cov
12-
13-
# Coverage directory used by tools like istanbul
14-
coverage
15-
16-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17-
.grunt
18-
19-
# node-waf configuration
20-
.lock-wscript
21-
22-
# Compiled binary addons (http://nodejs.org/api/addons.html)
23-
build/Release
24-
25-
# Dependency directory
26-
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27-
node_modules
1+
node_modules/
2+
.tmp/
3+
artifacts/
4+
npm-debug.log

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,21 @@ As a project maintainer, making your repo Commitizen friendly allows you to sele
2424

2525
For this example, we'll be setting up our repo to use [AngularJS's commit message convention](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#-git-commit-guidelines) also known as [conventional-changelog](https://github.com/ajoslin/conventional-changelog).
2626

27-
First, install the Commitizen conventional-changelog adapter into your project.
27+
First, install the Commitizen cli tools:
2828

2929
```
30-
npm install cz-conventional-changelog
30+
npm install commitizen -g
3131
```
3232

33-
Then just add **.cz.json** to the root of your repository with the following contents:
33+
Next, initialize your project to use the cz-conventional-changelog adapter by typing:
3434

35-
```json
36-
{
37-
"path": "node_modules/cz-conventional-changelog/"
38-
}
35+
```
36+
commitizen init cz-conventional-changelog --save --save-exact
3937
```
4038

41-
This just tells Commitizen which adapter we actually want our contributors to use when they try to commit to this repo.
39+
Note that if you want to force install over the top of an old adapter, you can apply the `--force` argument. For more information on this, just run `commitizen help`.
4240

43-
Alternatively, you can skip adding an additional dot file to your root directory by making **.cz.json** a part of **package.json**... just add the `czConfig` field:
41+
Then just add the `czConfig` field to the root of your **package.json** with the following contents:
4442

4543
```json
4644
...
@@ -49,6 +47,8 @@ Alternatively, you can skip adding an additional dot file to your root directory
4947
}
5048
```
5149

50+
This just tells Commitizen which adapter we actually want our contributors to use when they try to commit to this repo.
51+
5252
#### Congratulations your repo is Commitizen-friendly. Time to flaunt it!
5353

5454
Add the Commitizen-friendly badge to your README using the following markdown:
@@ -73,4 +73,10 @@ Both! Commitizen is not meant to be a replacement for git commit hooks. Rather,
7373
We accomplish this by letting you define which adapter you'd like to use in your project. Adapters just allow multiple projects to share the same commit message conventions. A good example of an adapter is the cz-conventional-commit adapter.
7474

7575
### Authors and Contributors
76-
Jim Cummins (@JimTheDev)
76+
@JimTheDev (Jim Cummins, author)
77+
@kentcdodds
78+
@accraze
79+
@kytwb
80+
@Den-dp
81+
82+
Special thanks to @stevelacy, whose [gulp-git](https://www.npmjs.com/package/gulp-git) project makes commitizen possible.

bin/commitizen

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
require("babel/register");
4+
require('../src/cli/commitizen.js').bootstrap();
File renamed without changes.

bin/git-cz

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#!/usr/bin/env node
22

3-
var app = require('../src/app.js');
4-
app.bootstrap();
5-
6-
// PRIORITY HIGH
7-
// TODO: Tests
8-
// TODO: Examples
9-
10-
// PRIORITY MEDIUM
11-
// TODO: 3. Allow users to use their original messages using
12-
// some kind of template {{m1}} {{m2}} so that they
13-
// aren't lost.
3+
require("babel/register");
4+
var path = require('path');
5+
require('../src/cli/git-cz.js').bootstrap({
6+
cliPath: path.join(__dirname, '../')
7+
});

bin/git-cz-debug

-6
This file was deleted.

bin/git-cz.cmd

100755100644
File mode changed.

common/util.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export {
5+
executeShellCommand,
6+
getParsedJsonFromFile,
7+
getParsedPackageJsonFromPath,
8+
isArray,
9+
isFunction,
10+
isString
11+
}
12+
13+
/**
14+
* Executes the command passed to it at the path requested
15+
* using the instance of shelljs passed in
16+
*/
17+
function executeShellCommand(sh, path, installCommand) {
18+
sh.cd(path);
19+
sh.exec(installCommand);
20+
}
21+
22+
/**
23+
* Gets the parsed contents of a json file
24+
*/
25+
function getParsedJsonFromFile(filePath, fileName, encoding = 'utf8') {
26+
try {
27+
var packageJsonContents = fs.readFileSync(path.join(filePath, fileName), encoding);
28+
return JSON.parse(packageJsonContents);
29+
} catch (e) {
30+
console.error(e);
31+
}
32+
}
33+
34+
/**
35+
* A helper method for getting the contents of package.json at a given path
36+
*/
37+
function getParsedPackageJsonFromPath(path) {
38+
return getParsedJsonFromFile(path, 'package.json');
39+
}
40+
41+
/**
42+
* Test if the passed argument is an array
43+
*/
44+
function isArray(arr) {
45+
return arr.constructor === Array;
46+
}
47+
48+
/**
49+
* Test if the passed argument is a function
50+
*/
51+
function isFunction(functionToCheck) {
52+
var getType = {};
53+
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
54+
}
55+
56+
/**
57+
* Test if the passed argument is a string
58+
*/
59+
function isString(str) {
60+
return Object.prototype.toString.call(str) == '[object String]';
61+
}
62+

jsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "commonjs",
5+
"experimentalDecorators" : true
6+
}
7+
}

package.json

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
22
"name": "commitizen",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Git commit, but play nice with conventions.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"start": "npm run test:watch",
8+
"test": "mocha test/tests/index.js --compilers js:babel/register",
9+
"test:watch": "nodemon -q --exec \"$(npm bin)/mocha test/tests/index.js --watch --compilers js:babel/register\" --"
10+
},
11+
"czConfig": {
12+
"path": "./node_modules/cz-conventional-changelog"
813
},
914
"homepage": "https://github.com/commitizen/cz-cli",
1015
"repository": {
@@ -13,22 +18,33 @@
1318
},
1419
"bin": {
1520
"git-cz": "./bin/git-cz",
16-
"git-cz-debug": "./bin/git-cz-debug",
17-
"commitizen": "./bin/git-cz",
18-
"commitizen-debug": "./bin/git-cz-debug"
21+
"commitizen": "./bin/commitizen"
1922
},
2023
"author": "Jim Cummins <[email protected]>",
2124
"license": "MIT",
25+
"devDependencies": {
26+
"babel": "5.8.23",
27+
"chai": "3.3.0",
28+
"lodash": "3.10.1",
29+
"mocha": "2.3.3",
30+
"shelljs": "0.5.3"
31+
},
2232
"dependencies": {
33+
"babel": "5.8.23",
2334
"chalk": "1.1.1",
24-
"cz-conventional-changelog": "1.1.1",
2535
"dedent": "0.4.0",
26-
"glob": "5.0.14",
36+
"find-node-modules": "1.0.1",
37+
"glob": "5.0.15",
2738
"gulp": "3.9.0",
28-
"gulp-git": "1.4.0",
39+
"gulp-git": "1.5.0",
2940
"inquirer": "0.10.1",
41+
"jsontool": "7.0.2",
3042
"minimist": "1.2.0",
43+
"node-uuid": "1.4.3",
44+
"nodemon": "1.7.1",
45+
"rimraf": "2.4.3",
46+
"semver": "5.0.3",
3147
"strip-json-comments": "1.0.4",
32-
"systemjs": "0.16.11"
48+
"cz-conventional-changelog": "1.1.4"
3349
}
3450
}

src/app.js

-16
This file was deleted.

src/cli/commitizen.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {configLoader} from '../commitizen';
2+
import {init} from '../commitizen';
3+
import {commitizen as commitizenParser} from './parsers';
4+
import * as sh from 'shelljs';
5+
6+
let {parse} = commitizenParser;
7+
8+
export {
9+
bootstrap
10+
};
11+
12+
/**
13+
* This is the main cli entry point.
14+
* environment may be used for debugging.
15+
*/
16+
function bootstrap(environment = {}) {
17+
18+
// Get cli args
19+
let rawGitArgs = process.argv.slice(2, process.argv.length);
20+
21+
// Parse the args
22+
let parsedArgs = parse(rawGitArgs);
23+
let command = parsedArgs._[0];
24+
25+
// Do actions based on commands
26+
if (command === "init") {
27+
let adapterNpmName = parsedArgs._[1];
28+
if (adapterNpmName) {
29+
console.log(`Attempting to initialize using the npm package ${adapterNpmName}`);
30+
try {
31+
init(sh, process.cwd(), adapterNpmName, parsedArgs);
32+
} catch(e) {
33+
console.error(`Error: ${e}`);
34+
}
35+
} else {
36+
console.error('Error: You must provide an adapter name as the second argument.');
37+
}
38+
} else {
39+
console.log(`
40+
41+
Commitizen has two command line tools:
42+
43+
1) commitizen -- used for installing adapters into your project
44+
2) git-cz -- used for making commits according to convention
45+
note: you can run 'git cz' if installed with -g
46+
47+
Generally if you're using someone else's repo and they've already set up an
48+
adapter, you're going to just be running:
49+
50+
git-cz
51+
52+
However, if you create a new repo and you want to make it easier for future
53+
contributors to follow your commit message conventions using commitizen then
54+
you'll need to run a command like this one to add this adapter to your config:
55+
56+
commitizen init cz-convetional-changelog --save
57+
58+
You should swap out cz-conventional-changelog for the NPM package name of the
59+
adapter you wish you install in your project's package.json.
60+
61+
Detailed usage:
62+
63+
1) commitizen <sub-command>
64+
65+
init <adapter-npm-name> [args]
66+
67+
description: Install a commitizen adapter from npm and adds it to your
68+
czConfig in your package.json file.
69+
70+
args:
71+
--save Install the adapter to package.json dependencies
72+
--save-dev Install the adapter to devDependencies
73+
--save-exact Install an exact version instead of a range
74+
--force Force install the adapter, even if a previous one exists.
75+
76+
2) git-cz <any regular git commit arguments>
77+
78+
description: Runs the commitizen prompter, asking you questions so that you
79+
follow the commit conventions of the repository of the current
80+
directory.
81+
82+
note: git-cz may even be run as 'git cz' if installed with -g.
83+
84+
`);
85+
}
86+
87+
}

0 commit comments

Comments
 (0)