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

port execute-rule to ts #660

Merged
merged 4 commits into from May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
1 change: 1 addition & 0 deletions @commitlint/execute-rule/index.d.ts
@@ -0,0 +1 @@
export * from "./lib";
1 change: 1 addition & 0 deletions @commitlint/execute-rule/index.js
@@ -0,0 +1 @@
module.exports = require('./lib');
4 changes: 4 additions & 0 deletions @commitlint/execute-rule/jest.config.js
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
43 changes: 10 additions & 33 deletions @commitlint/execute-rule/package.json
Expand Up @@ -7,31 +7,12 @@
"lib/"
],
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"build": "tsc",
"deps": "dep-check",
"pkg": "pkg-check",
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"test": "ava -c 4 --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
"src/**/*.test.js",
"!lib/**/*"
],
"source": [
"src/**/*.js",
"!lib/**/*"
],
"babel": "inherit",
"require": [
"babel-register"
]
},
"babel": {
"presets": [
"babel-preset-commitlint"
]
"start": "concurrently \"yarn test --watchAll\" \"yarn run watch\"",
"test": "jest",
"watch": "tsc -w"
},
"engines": {
"node": ">=4"
Expand All @@ -57,16 +38,12 @@
"license": "MIT",
"devDependencies": {
"@commitlint/parse": "^8.0.0",
"@commitlint/test": "^8.0.0",
"@commitlint/utils": "^8.0.0",
"ava": "0.22.0",
"babel-cli": "6.26.0",
"babel-preset-commitlint": "^8.0.0",
"babel-register": "6.26.0",
"@types/jest": "24.0.13",
"@types/lodash": "4.14.130",
"concurrently": "3.5.1",
"cross-env": "5.1.1"
},
"dependencies": {
"babel-runtime": "6.26.0"
"jest": "24.8.0",
"ts-jest": "24.0.2",
"typescript": "3.4.5"
}
}
}
9 changes: 0 additions & 9 deletions @commitlint/execute-rule/src/index.js

This file was deleted.

27 changes: 0 additions & 27 deletions @commitlint/execute-rule/src/index.test.js

This file was deleted.

26 changes: 26 additions & 0 deletions @commitlint/execute-rule/src/index.test.ts
@@ -0,0 +1,26 @@
import execute from '.';

test('does nothing without params', async () => {
const exec = execute as any;
expect(await exec()).toBeNull();
});

test('returns plain config', async () => {
const actual = await execute(['name', 'config']);
expect(actual).toEqual(['name', 'config']);
});

test('unwraps promised config', async () => {
const actual = await execute(['name', Promise.resolve('config')]);
expect(actual).toEqual(['name', 'config']);
});

test('executes config functions', async () => {
const actual = await execute(['name', () => 'config']);
expect(actual).toEqual(['name', 'config']);
});

test('executes async config functions', async () => {
const actual = await execute(['name', async () => 'config']);
expect(actual).toEqual(['name', 'config']);
});
25 changes: 25 additions & 0 deletions @commitlint/execute-rule/src/index.ts
@@ -0,0 +1,25 @@
type Rule<T> = readonly [string, Config<T>];
type Config<T> = T | Promise<T> | ExectableConfig<T>;
type ExectableConfig<T> = (() => T) | (() => Promise<T>);

type ExecutedRule<T> = readonly [string, T];

export default execute;

export async function execute<T = unknown>(rule: Rule<T>): Promise<ExecutedRule<T> | null> {
if (!Array.isArray(rule)) {
return null;
}

const [name, config] = rule;

const fn = executable(config)
? config
: async () => config;

return [name, await fn()];
};

function executable<T>(config: Config<T>): config is ExectableConfig<T> {
return typeof config === 'function';
}
22 changes: 22 additions & 0 deletions @commitlint/execute-rule/tsconfig.json
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": [
"dom",
"es2015"
],
"rootDir": "src",
"outDir": "lib",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": [
"./src"
],
"exclude": [
"./src/**/*.test.ts"
]
}
10 changes: 4 additions & 6 deletions @commitlint/format/package.json
Expand Up @@ -38,15 +38,13 @@
"license": "MIT",
"devDependencies": {
"@commitlint/test": "^8.0.0",
"@commitlint/utils": "^8.0.0",
"@types/jest": "24.0.12",
"@types/jest": "24.0.13",
"@types/lodash": "4.14.133",
"concurrently": "3.5.1",
"cross-env": "5.1.1",
"jest": "24.7.1",
"rimraf": "2.6.1",
"jest": "24.8.0",
"ts-jest": "24.0.2",
"typescript": "3.5.1"
"typescript": "3.5.1",
"lodash": "4.17.11"
},
"dependencies": {
"chalk": "^2.0.1"
Expand Down