Skip to content

Commit

Permalink
refactor(travis-cli): port travis-cli to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 10, 2020
1 parent f379dcc commit 3657790
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 55 deletions.
2 changes: 2 additions & 0 deletions @commitlint/travis-cli/cli.js
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('./lib/cli');
21 changes: 3 additions & 18 deletions @commitlint/travis-cli/package.json
Expand Up @@ -6,22 +6,11 @@
"lib/"
],
"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": ">=8"
Expand All @@ -45,12 +34,8 @@
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.8.4",
"@commitlint/test": "8.2.0",
"@commitlint/utils": "^8.3.4",
"babel-preset-commitlint": "^8.2.0",
"cross-env": "7.0.0"
"@commitlint/utils": "^8.3.4"
},
"dependencies": {
"@commitlint/cli": "^8.3.5",
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
45 changes: 21 additions & 24 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,44 +53,42 @@ 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']);
}

async function isClean() {
const result = await git(['status', '--porcelain'], {
stdio: ['pipe', 'pipe', 'pipe']
stdio: 'pipe'
});
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 @@ -18,6 +18,7 @@
{"path": "@commitlint/read"},
{"path": "@commitlint/rules"},
{"path": "@commitlint/lint"},
{"path": "@commitlint/core"}
{"path": "@commitlint/core"},
{"path": "@commitlint/travis-cli"}
]
}
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -2,7 +2,7 @@
# yarn lockfile v1


"@babel/cli@7.8.4", "@babel/cli@^7.7.7":
"@babel/cli@^7.7.7":
version "7.8.4"
resolved "https://registry.npmjs.org/@babel/cli/-/cli-7.8.4.tgz#505fb053721a98777b2b175323ea4f090b7d3c1c"
integrity sha512-XXLgAm6LBbaNxaGhMAznXXaxtCWfuv6PIDJ9Alsy9JYTOh+j2jJz+L/162kkfU1j/pTSxK1xGmlwI4pdIMkoag==
Expand Down Expand Up @@ -34,7 +34,7 @@
invariant "^2.2.4"
semver "^5.5.0"

"@babel/core@7.8.4", "@babel/core@^7.1.0", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
"@babel/core@^7.1.0", "@babel/core@^7.7.5", "@babel/core@^7.7.7":
version "7.8.4"
resolved "https://registry.npmjs.org/@babel/core/-/core-7.8.4.tgz#d496799e5c12195b3602d0fddd77294e3e38e80e"
integrity sha512-0LiLrB2PwrVI+a2/IEskBopDYSd8BCb3rOvH7D5tzoWd696TBEduBvuLVm4Nx6rltrLZqvI3MCalB2K2aVzQjA==
Expand Down Expand Up @@ -8576,7 +8576,7 @@ resolve@1.1.7:
resolved "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=

resolve@1.x, resolve@^1.1.6, resolve@^1.10.0, resolve@^1.3.2, resolve@^1.8.1, resolve@^1.12.0, resolve@^1.13.1:
resolve@1.x, resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.2, resolve@^1.8.1:
version "1.15.1"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8"
integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w==
Expand Down

0 comments on commit 3657790

Please sign in to comment.