Skip to content

Commit 285466b

Browse files
authored
port execute-rule to ts (#660)
* build: simplify repo cleaning * build: port execute-rule to ts
1 parent 6c4eedf commit 285466b

File tree

11 files changed

+96
-75
lines changed

11 files changed

+96
-75
lines changed

Diff for: .vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

Diff for: @commitlint/execute-rule/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./lib";

Diff for: @commitlint/execute-rule/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib');

Diff for: @commitlint/execute-rule/jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node'
4+
};

Diff for: @commitlint/execute-rule/package.json

+10-33
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,12 @@
77
"lib/"
88
],
99
"scripts": {
10-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
10+
"build": "tsc",
1111
"deps": "dep-check",
1212
"pkg": "pkg-check",
13-
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
14-
"test": "ava -c 4 --verbose",
15-
"watch": "babel src --out-dir lib --watch --source-maps"
16-
},
17-
"ava": {
18-
"files": [
19-
"src/**/*.test.js",
20-
"!lib/**/*"
21-
],
22-
"source": [
23-
"src/**/*.js",
24-
"!lib/**/*"
25-
],
26-
"babel": "inherit",
27-
"require": [
28-
"babel-register"
29-
]
30-
},
31-
"babel": {
32-
"presets": [
33-
"babel-preset-commitlint"
34-
]
13+
"start": "concurrently \"yarn test --watchAll\" \"yarn run watch\"",
14+
"test": "jest",
15+
"watch": "tsc -w"
3516
},
3617
"engines": {
3718
"node": ">=4"
@@ -57,16 +38,12 @@
5738
"license": "MIT",
5839
"devDependencies": {
5940
"@commitlint/parse": "^8.0.0",
60-
"@commitlint/test": "^8.0.0",
6141
"@commitlint/utils": "^8.0.0",
62-
"ava": "0.22.0",
63-
"babel-cli": "6.26.0",
64-
"babel-preset-commitlint": "^8.0.0",
65-
"babel-register": "6.26.0",
42+
"@types/jest": "24.0.13",
43+
"@types/lodash": "4.14.130",
6644
"concurrently": "3.5.1",
67-
"cross-env": "5.1.1"
68-
},
69-
"dependencies": {
70-
"babel-runtime": "6.26.0"
45+
"jest": "24.8.0",
46+
"ts-jest": "24.0.2",
47+
"typescript": "3.4.5"
7148
}
72-
}
49+
}

Diff for: @commitlint/execute-rule/src/index.js

-9
This file was deleted.

Diff for: @commitlint/execute-rule/src/index.test.js

-27
This file was deleted.

Diff for: @commitlint/execute-rule/src/index.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import execute from '.';
2+
3+
test('does nothing without params', async () => {
4+
const exec = execute as any;
5+
expect(await exec()).toBeNull();
6+
});
7+
8+
test('returns plain config', async () => {
9+
const actual = await execute(['name', 'config']);
10+
expect(actual).toEqual(['name', 'config']);
11+
});
12+
13+
test('unwraps promised config', async () => {
14+
const actual = await execute(['name', Promise.resolve('config')]);
15+
expect(actual).toEqual(['name', 'config']);
16+
});
17+
18+
test('executes config functions', async () => {
19+
const actual = await execute(['name', () => 'config']);
20+
expect(actual).toEqual(['name', 'config']);
21+
});
22+
23+
test('executes async config functions', async () => {
24+
const actual = await execute(['name', async () => 'config']);
25+
expect(actual).toEqual(['name', 'config']);
26+
});

Diff for: @commitlint/execute-rule/src/index.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type Rule<T> = readonly [string, Config<T>];
2+
type Config<T> = T | Promise<T> | ExectableConfig<T>;
3+
type ExectableConfig<T> = (() => T) | (() => Promise<T>);
4+
5+
type ExecutedRule<T> = readonly [string, T];
6+
7+
export default execute;
8+
9+
export async function execute<T = unknown>(rule: Rule<T>): Promise<ExecutedRule<T> | null> {
10+
if (!Array.isArray(rule)) {
11+
return null;
12+
}
13+
14+
const [name, config] = rule;
15+
16+
const fn = executable(config)
17+
? config
18+
: async () => config;
19+
20+
return [name, await fn()];
21+
};
22+
23+
function executable<T>(config: Config<T>): config is ExectableConfig<T> {
24+
return typeof config === 'function';
25+
}

Diff for: @commitlint/execute-rule/tsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"dom",
5+
"es2015"
6+
],
7+
"rootDir": "src",
8+
"outDir": "lib",
9+
"declaration": true,
10+
"declarationMap": true,
11+
"sourceMap": true,
12+
"esModuleInterop": true,
13+
"allowSyntheticDefaultImports": true,
14+
"strict": true
15+
},
16+
"include": [
17+
"./src"
18+
],
19+
"exclude": [
20+
"./src/**/*.test.ts"
21+
]
22+
}

Diff for: @commitlint/format/package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@
3838
"license": "MIT",
3939
"devDependencies": {
4040
"@commitlint/test": "^8.0.0",
41-
"@commitlint/utils": "^8.0.0",
42-
"@types/jest": "24.0.12",
41+
"@types/jest": "24.0.13",
4342
"@types/lodash": "4.14.133",
4443
"concurrently": "3.5.1",
45-
"cross-env": "5.1.1",
46-
"jest": "24.7.1",
47-
"rimraf": "2.6.1",
44+
"jest": "24.8.0",
4845
"ts-jest": "24.0.2",
49-
"typescript": "3.5.1"
46+
"typescript": "3.5.1",
47+
"lodash": "4.17.11"
5048
},
5149
"dependencies": {
5250
"chalk": "^2.0.1"

0 commit comments

Comments
 (0)