Last active
March 17, 2020 17:26
-
-
Save dralletje/91aa0f1ec675c2bae545c0f4a5559c7c to your computer and use it in GitHub Desktop.
Pre commit hook for prettier
This file contains hidden or 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
| #!/usr/bin/env node | |
| const util = require('util'); | |
| const exec = util.promisify(require('child_process').exec); | |
| let run = fn => fn(); | |
| run(async () => { | |
| try { | |
| let { stdout: git_status_output } = await exec('git diff --cached --name-status'); | |
| let file_rows = git_status_output.split("\n") | |
| // Data collection and manipulation | |
| let files = file_rows.filter(x => x !== '').map( file_row => { | |
| const [modifier, path] = file_row.split("\t") | |
| return { modifier, path }; | |
| }); | |
| console.log('files:', files); | |
| // Side effect/result | |
| for (let file of files) { | |
| try { | |
| let prettier_output = await exec(`prettier --write "${file.path}"`); | |
| console.log('prettier_output:', prettier_output); | |
| } catch (error) { | |
| //in case the file type throws an error | |
| } | |
| } | |
| } catch (error) { | |
| console.log('error:', error.stack); | |
| process.exit(1); | |
| } | |
| process.exit(1); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment