Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): port cli to typescript #978

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8142287
refactor(cli): port cli to typescript
armano2 Feb 8, 2020
8670c15
chore(cli): cleanup code
armano2 Feb 8, 2020
b58696a
fix(cli): cleanup code and add missing dependencies
armano2 Feb 8, 2020
50afa34
fix(cli): restore original options array
armano2 Feb 8, 2020
f819a4c
fix(cli): set cli as executable
armano2 Feb 8, 2020
bb12a02
fix(cli): attempt to fix permissions on linux
armano2 Feb 8, 2020
27b1ccd
fix(cli): improve help format and setup version command
armano2 Feb 8, 2020
3d889a0
fix(cli): restore condition after refactoring
armano2 Feb 8, 2020
93f1e63
chore(cli): add missing todo
armano2 Feb 8, 2020
63123de
chore(cli): add missing type
armano2 Feb 8, 2020
e4efb34
chore: add missing new line in help and update CI doc
armano2 Feb 8, 2020
3a17ad8
docs: update default of cwd
armano2 Feb 8, 2020
8b57a0c
fix(cli): update published files
armano2 Feb 8, 2020
422b833
refactor(cli): rename cli-bin to cli
armano2 Feb 8, 2020
3f3d2e0
refactor(cli): add custom CliError and fix printing help
armano2 Feb 8, 2020
1f1e6a1
chore(cli): fix package.json
armano2 Feb 8, 2020
a0c4bb3
refactor(cli): use camelCase cli flag names
armano2 Feb 9, 2020
6fea4cf
fix: unify rule config types
marionebl Feb 9, 2020
4e12277
chore: drop no longer used package (issue after rebase)
armano2 Feb 9, 2020
17504f2
chore: remove todo comment as its already resolved
armano2 Feb 9, 2020
db186dd
fix: enable strict mode to reproduce previous behavior
armano2 Feb 9, 2020
4246081
chore: rollback some unnecessary changes
armano2 Feb 11, 2020
712f4cd
test(cli): add unit test for help and version commands
armano2 Feb 11, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions @commitlint/cli/cli.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./lib/cli.js');
2 changes: 1 addition & 1 deletion @commitlint/cli/index.js
@@ -1,3 +1,3 @@
const path = require('path'); const path = require('path');


module.exports = path.join(__dirname, 'lib/cli.js'); module.exports = path.join(__dirname, 'cli.js');
30 changes: 7 additions & 23 deletions @commitlint/cli/package.json
Expand Up @@ -4,26 +4,15 @@
"description": "Lint your commit messages", "description": "Lint your commit messages",
"files": [ "files": [
"index.js", "index.js",
"lib", "cli.js",
"!*.test.js*" "lib"
], ],
"bin": { "bin": {
"commitlint": "./lib/cli.js" "commitlint": "./cli.js"
}, },
"scripts": { "scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"deps": "dep-check", "deps": "dep-check",
"pkg": "pkg-check", "pkg": "pkg-check"
"start": "yarn run watch",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"babel": {
"presets": [
"babel-preset-commitlint"
],
"ignore": [
"**/*.test.js"
]
}, },
"engines": { "engines": {
"node": ">=8" "node": ">=8"
Expand All @@ -47,12 +36,9 @@
}, },
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.7.7", "@types/yargs": "^15.0.3",
"@babel/core": "^7.7.7",
"@commitlint/test": "8.2.0", "@commitlint/test": "8.2.0",
"@commitlint/utils": "^8.3.4", "@commitlint/utils": "^8.3.4",
"babel-preset-commitlint": "^8.2.0",
"cross-env": "7.0.0",
"execa": "^3.4.0", "execa": "^3.4.0",
"fs-extra": "^8.1.0" "fs-extra": "^8.1.0"
}, },
Expand All @@ -62,12 +48,10 @@
"@commitlint/load": "^8.3.5", "@commitlint/load": "^8.3.5",
"@commitlint/read": "^8.3.4", "@commitlint/read": "^8.3.4",
"chalk": "3.0.0", "chalk": "3.0.0",
"core-js": "^3.6.1",
"get-stdin": "7.0.0", "get-stdin": "7.0.0",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"meow": "5.0.0",
"regenerator-runtime": "0.13.3",
"resolve-from": "5.0.0", "resolve-from": "5.0.0",
"resolve-global": "1.0.0" "resolve-global": "1.0.0",
escapedcat marked this conversation as resolved.
Show resolved Hide resolved
"yargs": "^15.1.0"
} }
} }
13 changes: 13 additions & 0 deletions @commitlint/cli/src/cli-error.ts
@@ -0,0 +1,13 @@
export class CliError extends Error {
__proto__ = Error;

public type: string;

constructor(message: string, type: string) {
super(message);

this.type = type;

Object.setPrototypeOf(this, CliError.prototype);
}
}
Expand Up @@ -4,9 +4,14 @@ import execa from 'execa';
import merge from 'lodash/merge'; import merge from 'lodash/merge';
import fs from 'fs-extra'; import fs from 'fs-extra';


const bin = require.resolve('../lib/cli.js'); const bin = require.resolve('../cli.js');


const cli = (args, options) => { interface TestOptions {
cwd: string;
env?: Record<string, string>;
}

const cli = (args: string[], options: TestOptions) => {
return (input = '') => { return (input = '') => {
return execa(bin, args, { return execa(bin, args, {
cwd: options.cwd, cwd: options.cwd,
Expand All @@ -17,8 +22,8 @@ const cli = (args, options) => {
}; };
}; };


const gitBootstrap = fixture => git.bootstrap(fixture, __dirname); const gitBootstrap = (fixture: string) => git.bootstrap(fixture, __dirname);
const fixBootstrap = fixture => fix.bootstrap(fixture, __dirname); const fixBootstrap = (fixture: string) => fix.bootstrap(fixture, __dirname);


test('should throw when called without [input]', async () => { test('should throw when called without [input]', async () => {
const cwd = await gitBootstrap('fixtures/default'); const cwd = await gitBootstrap('fixtures/default');
Expand Down Expand Up @@ -423,7 +428,48 @@ test('should work with relative formatter path', async () => {
expect(actual.exitCode).toBe(0); expect(actual.exitCode).toBe(0);
}); });


async function writePkg(payload, options) { test('should print help', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--help'], {cwd})();
expect(actual.stdout).toMatchInlineSnapshot(`
"@commitlint/cli@8.3.5 - Lint your commit messages

[input] reads from stdin if --edit, --env, --from and --to are omitted

Options:
--color, -c toggle colored output [boolean] [default: true]
--config, -g path to the config file [string]
--cwd, -d directory to execute in
[string] [default: (Working Directory)]
--edit, -e read last commit message from the specified file or
fallbacks to ./.git/COMMIT_EDITMSG
[string] [default: false]
--env, -E check message in the file at path given by environment
variable value [string]
--extends, -x array of shareable configurations to extend [array]
--help-url, -H help url in error message [string]
--from, -f lower end of the commit range to lint; applies if
edit=false [string]
--format, -o output format of the results [string]
--parser-preset, -p configuration preset to use for
conventional-commits-parser [string]
--quiet, -q toggle console output [boolean] [default: false]
--to, -t upper end of the commit range to lint; applies if
edit=false [string]
--verbose, -V enable verbose output for reports without problems
[boolean]
-v, --version display version information [boolean]
-h, --help Show help [boolean]"
`);
});
armano2 marked this conversation as resolved.
Show resolved Hide resolved

test('should print version', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--version'], {cwd})();
expect(actual.stdout).toMatch('@commitlint/cli@');
});

async function writePkg(payload: unknown, options: TestOptions) {
const pkgPath = path.join(options.cwd, 'package.json'); const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8')); const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
const result = merge(pkg, payload); const result = merge(pkg, payload);
Expand Down