File tree Expand file tree Collapse file tree 4 files changed +39
-37
lines changed Expand file tree Collapse file tree 4 files changed +39
-37
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env node
2
- import { add } from './commands/add '
2
+ import { add } from './commands/set_add '
3
3
import { install } from './commands/install'
4
4
import { uninstall } from './commands/uninstall'
5
5
import fs from 'fs'
@@ -27,6 +27,7 @@ function help() {
27
27
husky init
28
28
husky install [dir] (default: .husky)
29
29
husky uninstall
30
+ husky set <file> [cmd]
30
31
husky add <file> [cmd]
31
32
32
33
Examples
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
import fs from 'fs'
2
2
import { PackageJson } from 'type-fest'
3
- import { add } from './add '
3
+ import { set } from './set_add '
4
4
import { install } from './install'
5
5
6
6
const regex = / ^ [ ] + | \t + / m
@@ -34,5 +34,5 @@ export function init(isYarn2: boolean): void {
34
34
install ( )
35
35
36
36
// Add pre-commit sample
37
- add ( '.husky/pre-commit' , 'npm test' )
37
+ set ( '.husky/pre-commit' , 'npm test' )
38
38
}
Original file line number Diff line number Diff line change
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+
4
+ function data ( cmd : string ) {
5
+ return `#!/bin/sh
6
+ . "$(dirname "$0")/_/husky.sh"
7
+
8
+ ${ cmd }
9
+ `
10
+ }
11
+
12
+ function format ( file : string ) : string {
13
+ return `${ path . dirname ( file ) } ${ path . sep } ${ path . basename ( file ) } `
14
+ }
15
+
16
+ export function set ( file : string , cmd : string ) : void {
17
+ const dir = path . dirname ( file )
18
+ if ( ! fs . existsSync ( dir ) ) {
19
+ throw new Error ( `can't create hook, ${ dir } directory doesn't exist` )
20
+ }
21
+
22
+ fs . writeFileSync ( file , data ( cmd ) , { mode : 0o0755 } )
23
+
24
+ // Show "./file" instead of just "file"
25
+ console . log ( `husky - created ${ format ( file ) } ` )
26
+ }
27
+
28
+ export function add ( file : string , cmd : string ) : void {
29
+ if ( fs . existsSync ( file ) ) {
30
+ fs . appendFileSync ( file , `${ cmd } \n` )
31
+ console . log ( `husky - updated ${ format ( file ) } ` )
32
+ } else {
33
+ set ( file , cmd )
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments