Skip to content

Commit

Permalink
port execute-rule to ts (#660)
Browse files Browse the repository at this point in the history
* build: simplify repo cleaning

* build: port execute-rule to ts
  • Loading branch information
marionebl committed May 31, 2019
1 parent 6c4eedf commit 285466b
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 75 deletions.
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

0 comments on commit 285466b

Please sign in to comment.