Skip to content

Commit

Permalink
feat: ktlint-all (cli), ktlintAll (api)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Dec 11, 2021
1 parent b7a7623 commit 7c36b5b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 17 additions & 0 deletions src/bin/ktlint-all.ts
Original file line number Diff line number Diff line change
@@ -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])
18 changes: 2 additions & 16 deletions src/bin/run-ktlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,12 @@ 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
// argv[1] is ./src/bin/run-ktlint.ts (when ran with `yarn tsn`)
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)
44 changes: 44 additions & 0 deletions src/exec.util.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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}`))
})
})
}
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await runCommandSafe(ktlintPath, ['-F', ...args], opt)
}
2 changes: 1 addition & 1 deletion tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": false,
"declaration": true,
"sourceMap": false
},
"exclude": ["**/__exclude", "src/test", "src/**/*.test.*"]
Expand Down

0 comments on commit 7c36b5b

Please sign in to comment.