Skip to content

Commit bb5d22b

Browse files
authored
refactor(travis-cli): port travis-cli to typescript (#990)
* refactor(travis-cli): port travis-cli to typescript * chore: fix formatting
1 parent 389419b commit bb5d22b

File tree

7 files changed

+49
-288
lines changed

7 files changed

+49
-288
lines changed

@commitlint/travis-cli/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('./lib/cli');

@commitlint/travis-cli/package.json

+5-20
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@
33
"version": "11.0.0",
44
"description": "Lint all relevant commits for a change or PR on Travis CI",
55
"files": [
6-
"lib/"
6+
"lib/",
7+
"cli.js"
78
],
89
"bin": {
9-
"commitlint-travis": "./lib/cli.js"
10+
"commitlint-travis": "./cli.js"
1011
},
1112
"scripts": {
12-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
1313
"deps": "dep-check",
14-
"pkg": "pkg-check --skip-main",
15-
"start": "yarn run watch",
16-
"watch": "babel src --out-dir lib --watch --source-maps"
17-
},
18-
"babel": {
19-
"presets": [
20-
"babel-preset-commitlint"
21-
],
22-
"ignore": [
23-
"**/*.test.js"
24-
]
14+
"pkg": "pkg-check --skip-main"
2515
},
2616
"engines": {
2717
"node": ">=v10.22.1"
@@ -45,15 +35,10 @@
4535
},
4636
"license": "MIT",
4737
"devDependencies": {
48-
"@babel/cli": "7.12.10",
49-
"@babel/core": "7.12.10",
5038
"@commitlint/test": "^11.0.0",
51-
"@commitlint/utils": "^11.0.0",
52-
"babel-preset-commitlint": "^11.0.0",
53-
"cross-env": "7.0.3"
39+
"@commitlint/utils": "^11.0.0"
5440
},
5541
"dependencies": {
56-
"@babel/runtime": "^7.11.2",
5742
"@commitlint/cli": "^11.0.0",
5843
"execa": "^5.0.0"
5944
},

@commitlint/travis-cli/src/cli.test.js renamed to @commitlint/travis-cli/src/cli.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import execa from 'execa';
22
import {git} from '@commitlint/test';
33

4-
const bin = require.resolve('../lib/cli.js');
4+
const bin = require.resolve('../cli.js');
55

66
const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
77
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
88

99
const validBaseEnv = {
10-
TRAVIS: true,
11-
CI: true,
10+
TRAVIS: 'true',
11+
CI: 'true',
1212
TRAVIS_COMMIT: 'TRAVIS_COMMIT',
1313
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN,
1414
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN,
@@ -18,7 +18,7 @@ const validBaseEnv = {
1818
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG',
1919
};
2020

21-
const cli = async (config = {}, args = []) => {
21+
const cli = async (config: execa.Options = {}, args: string[] = []) => {
2222
try {
2323
return await execa(bin, args, config);
2424
} catch (err) {
@@ -28,8 +28,8 @@ const cli = async (config = {}, args = []) => {
2828

2929
test('should throw when not on travis ci', async () => {
3030
const env = {
31-
CI: false,
32-
TRAVIS: false,
31+
CI: 'false',
32+
TRAVIS: 'false',
3333
};
3434

3535
await expect(cli({env})).rejects.toThrow(
@@ -39,8 +39,8 @@ test('should throw when not on travis ci', async () => {
3939

4040
test('should throw when on travis ci, but env vars are missing', async () => {
4141
const env = {
42-
TRAVIS: true,
43-
CI: true,
42+
TRAVIS: 'true',
43+
CI: 'true',
4444
};
4545

4646
await expect(cli({env})).rejects.toThrow(
@@ -131,7 +131,7 @@ test('should call git with extra expected args on pull_request', async () => {
131131
]);
132132
});
133133

134-
function getInvocations(stdout) {
134+
function getInvocations(stdout: string): string[][] {
135135
const matches = stdout.match(/[^[\]]+/g);
136136
const raw = Array.isArray(matches) ? matches : [];
137137

@commitlint/travis-cli/src/cli.js renamed to @commitlint/travis-cli/src/cli.ts

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#!/usr/bin/env node
21
import execa from 'execa';
3-
import commitlint from '@commitlint/cli';
42

53
// Allow to override used bins for testing purposes
64
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
7-
const COMMITLINT = process.env.TRAVIS_COMMITLINT_BIN;
5+
const COMMITLINT =
6+
process.env.TRAVIS_COMMITLINT_BIN || require('@commitlint/cli');
87

98
const REQUIRED = [
109
'TRAVIS_COMMIT',
@@ -14,7 +13,7 @@ const REQUIRED = [
1413
'TRAVIS_PULL_REQUEST_SLUG',
1514
];
1615

17-
const COMMIT = process.env.TRAVIS_COMMIT;
16+
const COMMIT = process.env.TRAVIS_COMMIT || '';
1817
const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
1918
const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
2019
const RANGE = process.env.TRAVIS_COMMIT_RANGE;
@@ -36,7 +35,7 @@ async function main() {
3635
() => fetch({name: 'base', url: `https://github.com/${REPO_SLUG}.git`}),
3736
IS_PR
3837
? () => fetch({name: 'source', url: `https://github.com/${PR_SLUG}.git`})
39-
: async () => {},
38+
: () => Promise.resolve(),
4039
]);
4140

4241
// Restore stashed changes if any
@@ -54,11 +53,14 @@ async function main() {
5453
}
5554
}
5655

57-
async function git(args, options) {
58-
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
56+
async function git(args: string[], options: execa.Options = {}) {
57+
return execa(GIT, args, {
58+
stdio: 'inherit',
59+
...options,
60+
});
5961
}
6062

61-
async function fetch({name, url}) {
63+
async function fetch({name, url}: {name: string; url: string}) {
6264
await git(['remote', 'add', name, url]);
6365
await git(['fetch', name, '--quiet']);
6466
}
@@ -70,28 +72,23 @@ async function isClean() {
7072
return !(result.stdout && result.stdout.trim());
7173
}
7274

73-
async function lint(args, options) {
74-
return execa(
75-
COMMITLINT || commitlint,
76-
args,
77-
Object.assign({}, {stdio: ['pipe', 'inherit', 'inherit']}, options)
78-
);
75+
async function lint(args: string[], options: execa.Options = {}) {
76+
return execa(COMMITLINT, args, {
77+
stdio: ['pipe', 'inherit', 'inherit'],
78+
...options,
79+
});
7980
}
8081

81-
async function log(hash) {
82-
const result = await execa(GIT, [
83-
'log',
84-
'-n',
85-
'1',
86-
'--pretty=format:%B',
87-
hash,
88-
]);
82+
async function log(hash: string) {
83+
const result = await git(['log', '-n', '1', '--pretty=format:%B', hash], {
84+
stdio: 'pipe',
85+
});
8986
return result.stdout;
9087
}
9188

9289
async function stash() {
9390
if (await isClean()) {
94-
return async () => {};
91+
return () => Promise.resolve();
9592
}
9693
await git(['stash', '-k', '-u', '--quiet']);
9794
return () => git(['stash', 'pop', '--quiet']);

@commitlint/travis-cli/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.shared.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"rootDir": "./src",
6+
"outDir": "./lib"
7+
},
8+
"include": ["./src"],
9+
"exclude": ["./src/**/*.test.ts", "./lib/**/*"]
10+
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
{"path": "@commitlint/rules"},
2020
{"path": "@commitlint/lint"},
2121
{"path": "@commitlint/core"},
22-
{"path": "@commitlint/cli"}
22+
{"path": "@commitlint/cli"},
23+
{"path": "@commitlint/travis-cli"}
2324
]
2425
}

0 commit comments

Comments
 (0)