Skip to content

Commit 36e1a22

Browse files
committed
refactor(travis-cli): port travis-cli to typescript
1 parent f379dcc commit 36e1a22

File tree

7 files changed

+47
-54
lines changed

7 files changed

+47
-54
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

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@
66
"lib/"
77
],
88
"bin": {
9-
"commitlint-travis": "./lib/cli.js"
9+
"commitlint-travis": "./cli.js"
1010
},
1111
"scripts": {
12-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
1312
"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-
]
13+
"pkg": "pkg-check --skip-main"
2514
},
2615
"engines": {
2716
"node": ">=8"
@@ -45,12 +34,8 @@
4534
},
4635
"license": "MIT",
4736
"devDependencies": {
48-
"@babel/cli": "7.8.4",
49-
"@babel/core": "7.8.4",
5037
"@commitlint/test": "8.2.0",
51-
"@commitlint/utils": "^8.3.4",
52-
"babel-preset-commitlint": "^8.2.0",
53-
"cross-env": "7.0.0"
38+
"@commitlint/utils": "^8.3.4"
5439
},
5540
"dependencies": {
5641
"@commitlint/cli": "^8.3.5",

@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

+18-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,21 @@ 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]);
8984
return result.stdout;
9085
}
9186

9287
async function stash() {
9388
if (await isClean()) {
94-
return async () => {};
89+
return () => Promise.resolve();
9590
}
9691
await git(['stash', '-k', '-u', '--quiet']);
9792
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
@@ -18,6 +18,7 @@
1818
{"path": "@commitlint/read"},
1919
{"path": "@commitlint/rules"},
2020
{"path": "@commitlint/lint"},
21-
{"path": "@commitlint/core"}
21+
{"path": "@commitlint/core"},
22+
{"path": "@commitlint/travis-cli"}
2223
]
2324
}

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# yarn lockfile v1
33

44

5-
"@babel/cli@7.8.4", "@babel/cli@^7.7.7":
5+
"@babel/cli@^7.7.7":
66
version "7.8.4"
77
resolved "https://registry.npmjs.org/@babel/cli/-/cli-7.8.4.tgz#505fb053721a98777b2b175323ea4f090b7d3c1c"
88
integrity sha512-XXLgAm6LBbaNxaGhMAznXXaxtCWfuv6PIDJ9Alsy9JYTOh+j2jJz+L/162kkfU1j/pTSxK1xGmlwI4pdIMkoag==
@@ -34,7 +34,7 @@
3434
invariant "^2.2.4"
3535
semver "^5.5.0"
3636

37-
"@babel/core@7.8.4", "@babel/core@^7.1.0", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
37+
"@babel/core@^7.1.0", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
3838
version "7.8.4"
3939
resolved "https://registry.npmjs.org/@babel/core/-/core-7.8.4.tgz#d496799e5c12195b3602d0fddd77294e3e38e80e"
4040
integrity sha512-0LiLrB2PwrVI+a2/IEskBopDYSd8BCb3rOvH7D5tzoWd696TBEduBvuLVm4Nx6rltrLZqvI3MCalB2K2aVzQjA==
@@ -8576,7 +8576,7 @@ [email protected]:
85768576
resolved "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
85778577
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=
85788578

8579-
[email protected], resolve@^1.1.6, resolve@^1.10.0, resolve@^1.3.2, resolve@^1.8.1, resolve@^1.12.0, resolve@^1.13.1:
8579+
[email protected], resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.2, resolve@^1.8.1:
85808580
version "1.15.1"
85818581
resolved "https://registry.npmjs.org/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8"
85828582
integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w==

0 commit comments

Comments
 (0)