-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ktlint-all (cli), ktlintAll (api)
- Loading branch information
1 parent
b7a7623
commit 7c36b5b
Showing
7 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters