Skip to content

Commit

Permalink
refactor(travis-cli): port travis-cli to typescript (#990)
Browse files Browse the repository at this point in the history
* refactor(travis-cli): port travis-cli to typescript

* chore: fix formatting
  • Loading branch information
armano2 committed Dec 11, 2020
1 parent 389419b commit bb5d22b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 288 deletions.
2 changes: 2 additions & 0 deletions @commitlint/travis-cli/cli.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./lib/cli');
25 changes: 5 additions & 20 deletions @commitlint/travis-cli/package.json
Expand Up @@ -3,25 +3,15 @@
"version": "11.0.0",
"description": "Lint all relevant commits for a change or PR on Travis CI",
"files": [
"lib/"
"lib/",
"cli.js"
],
"bin": {
"commitlint-travis": "./lib/cli.js"
"commitlint-travis": "./cli.js"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"deps": "dep-check",
"pkg": "pkg-check --skip-main",
"start": "yarn run watch",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"babel": {
"presets": [
"babel-preset-commitlint"
],
"ignore": [
"**/*.test.js"
]
"pkg": "pkg-check --skip-main"
},
"engines": {
"node": ">=v10.22.1"
Expand All @@ -45,15 +35,10 @@
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "7.12.10",
"@babel/core": "7.12.10",
"@commitlint/test": "^11.0.0",
"@commitlint/utils": "^11.0.0",
"babel-preset-commitlint": "^11.0.0",
"cross-env": "7.0.3"
"@commitlint/utils": "^11.0.0"
},
"dependencies": {
"@babel/runtime": "^7.11.2",
"@commitlint/cli": "^11.0.0",
"execa": "^5.0.0"
},
Expand Down
@@ -1,14 +1,14 @@
import execa from 'execa';
import {git} from '@commitlint/test';

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

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

const validBaseEnv = {
TRAVIS: true,
CI: true,
TRAVIS: 'true',
CI: 'true',
TRAVIS_COMMIT: 'TRAVIS_COMMIT',
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN,
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN,
Expand All @@ -18,7 +18,7 @@ const validBaseEnv = {
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG',
};

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

test('should throw when not on travis ci', async () => {
const env = {
CI: false,
TRAVIS: false,
CI: 'false',
TRAVIS: 'false',
};

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

test('should throw when on travis ci, but env vars are missing', async () => {
const env = {
TRAVIS: true,
CI: true,
TRAVIS: 'true',
CI: 'true',
};

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

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

Expand Down
43 changes: 20 additions & 23 deletions @commitlint/travis-cli/src/cli.js → @commitlint/travis-cli/src/cli.ts 100755 → 100644
@@ -1,10 +1,9 @@
#!/usr/bin/env node
import execa from 'execa';
import commitlint from '@commitlint/cli';

// Allow to override used bins for testing purposes
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
const COMMITLINT = process.env.TRAVIS_COMMITLINT_BIN;
const COMMITLINT =
process.env.TRAVIS_COMMITLINT_BIN || require('@commitlint/cli');

const REQUIRED = [
'TRAVIS_COMMIT',
Expand All @@ -14,7 +13,7 @@ const REQUIRED = [
'TRAVIS_PULL_REQUEST_SLUG',
];

const COMMIT = process.env.TRAVIS_COMMIT;
const COMMIT = process.env.TRAVIS_COMMIT || '';
const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
const RANGE = process.env.TRAVIS_COMMIT_RANGE;
Expand All @@ -36,7 +35,7 @@ async function main() {
() => fetch({name: 'base', url: `https://github.com/${REPO_SLUG}.git`}),
IS_PR
? () => fetch({name: 'source', url: `https://github.com/${PR_SLUG}.git`})
: async () => {},
: () => Promise.resolve(),
]);

// Restore stashed changes if any
Expand All @@ -54,11 +53,14 @@ async function main() {
}
}

async function git(args, options) {
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
async function git(args: string[], options: execa.Options = {}) {
return execa(GIT, args, {
stdio: 'inherit',
...options,
});
}

async function fetch({name, url}) {
async function fetch({name, url}: {name: string; url: string}) {
await git(['remote', 'add', name, url]);
await git(['fetch', name, '--quiet']);
}
Expand All @@ -70,28 +72,23 @@ async function isClean() {
return !(result.stdout && result.stdout.trim());
}

async function lint(args, options) {
return execa(
COMMITLINT || commitlint,
args,
Object.assign({}, {stdio: ['pipe', 'inherit', 'inherit']}, options)
);
async function lint(args: string[], options: execa.Options = {}) {
return execa(COMMITLINT, args, {
stdio: ['pipe', 'inherit', 'inherit'],
...options,
});
}

async function log(hash) {
const result = await execa(GIT, [
'log',
'-n',
'1',
'--pretty=format:%B',
hash,
]);
async function log(hash: string) {
const result = await git(['log', '-n', '1', '--pretty=format:%B', hash], {
stdio: 'pipe',
});
return result.stdout;
}

async function stash() {
if (await isClean()) {
return async () => {};
return () => Promise.resolve();
}
await git(['stash', '-k', '-u', '--quiet']);
return () => git(['stash', 'pop', '--quiet']);
Expand Down
10 changes: 10 additions & 0 deletions @commitlint/travis-cli/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"composite": true,
"rootDir": "./src",
"outDir": "./lib"
},
"include": ["./src"],
"exclude": ["./src/**/*.test.ts", "./lib/**/*"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -19,6 +19,7 @@
{"path": "@commitlint/rules"},
{"path": "@commitlint/lint"},
{"path": "@commitlint/core"},
{"path": "@commitlint/cli"}
{"path": "@commitlint/cli"},
{"path": "@commitlint/travis-cli"}
]
}

0 comments on commit bb5d22b

Please sign in to comment.