1
1
import { execa } from 'execa' ;
2
2
import { readFileSync } from 'fs' ;
3
3
import ignore , { Ignore } from 'ignore' ;
4
-
4
+ import { join } from 'path' ;
5
5
import { outro , spinner } from '@clack/prompts' ;
6
6
7
7
export const assertGitRepo = async ( ) => {
@@ -16,41 +16,44 @@ export const assertGitRepo = async () => {
16
16
// (file) => `:(exclude)${file}`
17
17
// );
18
18
19
- export const getOpenCommitIgnore = ( ) : Ignore => {
19
+ export const getOpenCommitIgnore = async ( ) : Promise < Ignore > => {
20
+ const gitDir = await getGitDir ( ) ;
21
+
20
22
const ig = ignore ( ) ;
21
23
22
24
try {
23
- ig . add ( readFileSync ( '.opencommitignore' ) . toString ( ) . split ( '\n' ) ) ;
25
+ ig . add (
26
+ readFileSync ( join ( gitDir , '.opencommitignore' ) ) . toString ( ) . split ( '\n' )
27
+ ) ;
24
28
} catch ( e ) { }
25
29
26
30
return ig ;
27
31
} ;
28
32
29
33
export const getCoreHooksPath = async ( ) : Promise < string > => {
30
- const { stdout } = await execa ( 'git' , [ 'config' , 'core.hooksPath' ] ) ;
34
+ const gitDir = await getGitDir ( ) ;
35
+
36
+ const { stdout } = await execa ( 'git' , [ 'config' , 'core.hooksPath' ] , {
37
+ cwd : gitDir
38
+ } ) ;
31
39
32
40
return stdout ;
33
41
} ;
34
42
35
43
export const getStagedFiles = async ( ) : Promise < string [ ] > => {
36
- const { stdout : gitDir } = await execa ( 'git' , [
37
- 'rev-parse' ,
38
- '--show-toplevel'
39
- ] ) ;
44
+ const gitDir = await getGitDir ( ) ;
40
45
41
- const { stdout : files } = await execa ( 'git' , [
42
- 'diff' ,
43
- '--name-only' ,
44
- '--cached' ,
45
- '--relative' ,
46
- gitDir
47
- ] ) ;
46
+ const { stdout : files } = await execa (
47
+ 'git' ,
48
+ [ 'diff' , '--name-only' , '--cached' , '--relative' ] ,
49
+ { cwd : gitDir }
50
+ ) ;
48
51
49
52
if ( ! files ) return [ ] ;
50
53
51
54
const filesList = files . split ( '\n' ) ;
52
55
53
- const ig = getOpenCommitIgnore ( ) ;
56
+ const ig = await getOpenCommitIgnore ( ) ;
54
57
const allowedFiles = filesList . filter ( ( file ) => ! ig . ignores ( file ) ) ;
55
58
56
59
if ( ! allowedFiles ) return [ ] ;
@@ -59,12 +62,17 @@ export const getStagedFiles = async (): Promise<string[]> => {
59
62
} ;
60
63
61
64
export const getChangedFiles = async ( ) : Promise < string [ ] > => {
62
- const { stdout : modified } = await execa ( 'git' , [ 'ls-files' , '--modified' ] ) ;
63
- const { stdout : others } = await execa ( 'git' , [
64
- 'ls-files' ,
65
- '--others' ,
66
- '--exclude-standard'
67
- ] ) ;
65
+ const gitDir = await getGitDir ( ) ;
66
+
67
+ const { stdout : modified } = await execa ( 'git' , [ 'ls-files' , '--modified' ] , {
68
+ cwd : gitDir
69
+ } ) ;
70
+
71
+ const { stdout : others } = await execa (
72
+ 'git' ,
73
+ [ 'ls-files' , '--others' , '--exclude-standard' ] ,
74
+ { cwd : gitDir }
75
+ ) ;
68
76
69
77
const files = [ ...modified . split ( '\n' ) , ...others . split ( '\n' ) ] . filter (
70
78
( file ) => ! ! file
@@ -74,16 +82,20 @@ export const getChangedFiles = async (): Promise<string[]> => {
74
82
} ;
75
83
76
84
export const gitAdd = async ( { files } : { files : string [ ] } ) => {
85
+ const gitDir = await getGitDir ( ) ;
86
+
77
87
const gitAddSpinner = spinner ( ) ;
78
88
79
89
gitAddSpinner . start ( 'Adding files to commit' ) ;
80
90
81
- await execa ( 'git' , [ 'add' , ...files ] ) ;
91
+ await execa ( 'git' , [ 'add' , ...files ] , { cwd : gitDir } ) ;
82
92
83
93
gitAddSpinner . stop ( `Staged ${ files . length } files` ) ;
84
94
} ;
85
95
86
96
export const getDiff = async ( { files } : { files : string [ ] } ) => {
97
+ const gitDir = await getGitDir ( ) ;
98
+
87
99
const lockFiles = files . filter (
88
100
( file ) =>
89
101
file . includes ( '.lock' ) ||
@@ -108,12 +120,20 @@ export const getDiff = async ({ files }: { files: string[] }) => {
108
120
( file ) => ! file . includes ( '.lock' ) && ! file . includes ( '-lock.' )
109
121
) ;
110
122
111
- const { stdout : diff } = await execa ( 'git' , [
112
- 'diff' ,
113
- '--staged' ,
114
- '--' ,
115
- ...filesWithoutLocks
116
- ] ) ;
123
+ const { stdout : diff } = await execa (
124
+ 'git' ,
125
+ [ 'diff' , '--staged' , '--' , ...filesWithoutLocks ] ,
126
+ { cwd : gitDir }
127
+ ) ;
117
128
118
129
return diff ;
119
130
} ;
131
+
132
+ export const getGitDir = async ( ) : Promise < string > => {
133
+ const { stdout : gitDir } = await execa ( 'git' , [
134
+ 'rev-parse' ,
135
+ '--show-toplevel'
136
+ ] ) ;
137
+
138
+ return gitDir ;
139
+ } ;
0 commit comments