From 7c36b5b191731199c9a3e46a6d0bb38aca68a567 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Sat, 11 Dec 2021 21:06:25 +0100 Subject: [PATCH] feat: ktlint-all (cli), ktlintAll (api) --- package.json | 5 ++++- readme.md | 12 ++++++++++++ src/bin/ktlint-all.ts | 17 +++++++++++++++++ src/bin/run-ktlint.ts | 18 ++---------------- src/exec.util.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 7 +++++++ tsconfig.prod.json | 2 +- 7 files changed, 87 insertions(+), 18 deletions(-) create mode 100755 src/bin/ktlint-all.ts create mode 100644 src/exec.util.ts create mode 100644 src/index.ts diff --git a/package.json b/package.json index 25235b9..34559de 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "@types/node": "^16.11.12", "got": "^11.0.0" }, + "main": "dist/index.js", + "types": "dist/index.d.ts", "files": [ "dist", "resources", @@ -18,7 +20,8 @@ "!src/**/__exclude" ], "bin": { - "run-ktlint": "dist/bin/run-ktlint.js" + "run-ktlint": "dist/bin/run-ktlint.js", + "ktlint-all": "dist/bin/ktlint-all.js" }, "publishConfig": { "access": "public" diff --git a/readme.md b/readme.md index 01b5095..ca0ff3c 100644 --- a/readme.md +++ b/readme.md @@ -45,3 +45,15 @@ yarn run-ktlint "someFile.kt" ``` Test it with `yarn run-ktlint --version` + +## ktlint-all + +```shell +yarn ktlint-all +``` + +Equivalent of running `ktlint -F`. + +It also exposes a `ktlintAll` function in `index.js` for programmatic usage. + +It will throw an Error on non-zero return codes. diff --git a/src/bin/ktlint-all.ts b/src/bin/ktlint-all.ts new file mode 100755 index 0000000..d0ba7ac --- /dev/null +++ b/src/bin/ktlint-all.ts @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +/* + +yarn tsn ./src/bin/ktlint-all.ts + + */ + +import { runCommand } from '../exec.util' +import { ktlintPath } from '../paths' + +// argv[0] is /usr/local/bin/node +// argv[1] is ./src/bin/run-ktlint.ts (when ran with `yarn tsn`) +const args = process.argv.slice(2) +// console.log({argv, argv0, args}) + +runCommand(ktlintPath, ['-F', ...args]) diff --git a/src/bin/run-ktlint.ts b/src/bin/run-ktlint.ts index 80b10cb..e3c4251 100755 --- a/src/bin/run-ktlint.ts +++ b/src/bin/run-ktlint.ts @@ -6,7 +6,7 @@ yarn tsn ./src/bin/run-ktlint.ts --version */ -import { spawn } from 'child_process' +import { runCommand } from '../exec.util' import { ktlintPath } from '../paths' // argv[0] is /usr/local/bin/node @@ -14,18 +14,4 @@ import { ktlintPath } from '../paths' const args = process.argv.slice(2) // console.log({argv, argv0, args}) -console.log(['ktlint', ...args].join(' ')) - -const p = spawn(ktlintPath, [...args], { - stdio: 'inherit', - shell: true, -}) - -// out.on('error', (error) => { -// console.log(`error: ${error.message}`); -// }); - -p.on('close', code => { - // console.log(`child process exited with code ${code}`); - process.exit(code || 0) -}) +runCommand(ktlintPath, args) diff --git a/src/exec.util.ts b/src/exec.util.ts new file mode 100644 index 0000000..adf327c --- /dev/null +++ b/src/exec.util.ts @@ -0,0 +1,44 @@ +import { spawn, SpawnOptions } from 'child_process' + +export function runCommand(command: string, args: string[] = [], opt: SpawnOptions = {}): void { + console.log([command, ...args].join(' ')) + + const p = spawn(command, [...args], { + stdio: 'inherit', + shell: true, + ...opt, + }) + + p.on('close', code => { + if (code) { + console.log(`${command} exited with code ${code}`) + } + + process.exit(code || 0) + }) +} + +/** + * Throws error on failure. + */ +export async function runCommandSafe( + command: string, + args: string[] = [], + opt: SpawnOptions = {}, +): Promise { + console.log([command, ...args].join(' ')) + + return await new Promise((resolve, reject) => { + const p = spawn(command, [...args], { + stdio: 'inherit', + shell: true, + ...opt, + }) + + p.on('close', code => { + if (!code) return resolve() + + reject(new Error(`${command} exited with code ${code}`)) + }) + }) +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..4e8981e --- /dev/null +++ b/src/index.ts @@ -0,0 +1,7 @@ +import { SpawnOptions } from 'child_process' +import { runCommandSafe } from './exec.util' +import { ktlintPath } from './paths' + +export async function ktlintAll(args: string[] = [], opt: SpawnOptions): Promise { + await runCommandSafe(ktlintPath, ['-F', ...args], opt) +} diff --git a/tsconfig.prod.json b/tsconfig.prod.json index 025572f..b8f248d 100644 --- a/tsconfig.prod.json +++ b/tsconfig.prod.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "declaration": false, + "declaration": true, "sourceMap": false }, "exclude": ["**/__exclude", "src/test", "src/**/*.test.*"]