From 285466bfb4103691001eaad03b324a41d9dd542c Mon Sep 17 00:00:00 2001 From: Mario Nebl Date: Fri, 31 May 2019 09:34:40 +0200 Subject: [PATCH] port execute-rule to ts (#660) * build: simplify repo cleaning * build: port execute-rule to ts --- .vscode/settings.json | 3 ++ @commitlint/execute-rule/index.d.ts | 1 + @commitlint/execute-rule/index.js | 1 + @commitlint/execute-rule/jest.config.js | 4 ++ @commitlint/execute-rule/package.json | 43 +++++----------------- @commitlint/execute-rule/src/index.js | 9 ----- @commitlint/execute-rule/src/index.test.js | 27 -------------- @commitlint/execute-rule/src/index.test.ts | 26 +++++++++++++ @commitlint/execute-rule/src/index.ts | 25 +++++++++++++ @commitlint/execute-rule/tsconfig.json | 22 +++++++++++ @commitlint/format/package.json | 10 ++--- 11 files changed, 96 insertions(+), 75 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 @commitlint/execute-rule/index.d.ts create mode 100644 @commitlint/execute-rule/index.js create mode 100644 @commitlint/execute-rule/jest.config.js delete mode 100644 @commitlint/execute-rule/src/index.js delete mode 100644 @commitlint/execute-rule/src/index.test.js create mode 100644 @commitlint/execute-rule/src/index.test.ts create mode 100644 @commitlint/execute-rule/src/index.ts create mode 100644 @commitlint/execute-rule/tsconfig.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..55712c19f1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/@commitlint/execute-rule/index.d.ts b/@commitlint/execute-rule/index.d.ts new file mode 100644 index 0000000000..8cd5167d1c --- /dev/null +++ b/@commitlint/execute-rule/index.d.ts @@ -0,0 +1 @@ +export * from "./lib"; diff --git a/@commitlint/execute-rule/index.js b/@commitlint/execute-rule/index.js new file mode 100644 index 0000000000..bb0a047c4f --- /dev/null +++ b/@commitlint/execute-rule/index.js @@ -0,0 +1 @@ +module.exports = require('./lib'); diff --git a/@commitlint/execute-rule/jest.config.js b/@commitlint/execute-rule/jest.config.js new file mode 100644 index 0000000000..20d2ea60f5 --- /dev/null +++ b/@commitlint/execute-rule/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node' +}; diff --git a/@commitlint/execute-rule/package.json b/@commitlint/execute-rule/package.json index fd3ad68283..33645d70c1 100644 --- a/@commitlint/execute-rule/package.json +++ b/@commitlint/execute-rule/package.json @@ -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" @@ -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" } -} +} \ No newline at end of file diff --git a/@commitlint/execute-rule/src/index.js b/@commitlint/execute-rule/src/index.js deleted file mode 100644 index 5deda83237..0000000000 --- a/@commitlint/execute-rule/src/index.js +++ /dev/null @@ -1,9 +0,0 @@ -export default async entry => { - if (!Array.isArray(entry)) { - return null; - } - const [name, config] = entry; - return typeof config === 'function' - ? [name, await config()] - : [name, await config]; -}; diff --git a/@commitlint/execute-rule/src/index.test.js b/@commitlint/execute-rule/src/index.test.js deleted file mode 100644 index 836265740c..0000000000 --- a/@commitlint/execute-rule/src/index.test.js +++ /dev/null @@ -1,27 +0,0 @@ -import test from 'ava'; -import execute from '.'; - -test('does nothing without params', async t => { - const actual = await execute(); - t.is(actual, null); -}); - -test('returns plain config', async t => { - const actual = await execute(['name', 'config']); - t.deepEqual(actual, ['name', 'config']); -}); - -test('unwraps promised config', async t => { - const actual = await execute(['name', Promise.resolve('config')]); - t.deepEqual(actual, ['name', 'config']); -}); - -test('executes config functions', async t => { - const actual = await execute(['name', () => 'config']); - t.deepEqual(actual, ['name', 'config']); -}); - -test('executes async config functions', async t => { - const actual = await execute(['name', async () => 'config']); - t.deepEqual(actual, ['name', 'config']); -}); diff --git a/@commitlint/execute-rule/src/index.test.ts b/@commitlint/execute-rule/src/index.test.ts new file mode 100644 index 0000000000..2c9698de18 --- /dev/null +++ b/@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']); +}); diff --git a/@commitlint/execute-rule/src/index.ts b/@commitlint/execute-rule/src/index.ts new file mode 100644 index 0000000000..4eb0fa7f88 --- /dev/null +++ b/@commitlint/execute-rule/src/index.ts @@ -0,0 +1,25 @@ +type Rule = readonly [string, Config]; +type Config = T | Promise | ExectableConfig; +type ExectableConfig = (() => T) | (() => Promise); + +type ExecutedRule = readonly [string, T]; + +export default execute; + +export async function execute(rule: Rule): Promise | null> { + if (!Array.isArray(rule)) { + return null; + } + + const [name, config] = rule; + + const fn = executable(config) + ? config + : async () => config; + + return [name, await fn()]; +}; + +function executable(config: Config): config is ExectableConfig { + return typeof config === 'function'; +} \ No newline at end of file diff --git a/@commitlint/execute-rule/tsconfig.json b/@commitlint/execute-rule/tsconfig.json new file mode 100644 index 0000000000..7ed644a5a1 --- /dev/null +++ b/@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" + ] +} \ No newline at end of file diff --git a/@commitlint/format/package.json b/@commitlint/format/package.json index e266a3b0be..50acd55b67 100644 --- a/@commitlint/format/package.json +++ b/@commitlint/format/package.json @@ -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"