1
- import * as ShellJS from "shelljs" ;
2
1
import * as Path from "path" ;
2
+ import { spawnSync } from "child_process" ;
3
3
4
- import { SourceReference } from "../../models/sources/file" ;
4
+ import type { SourceReference } from "../../models/sources/file" ;
5
5
import { Component , ConverterComponent } from "../components" ;
6
6
import { BasePath } from "../utils/base-path" ;
7
7
import { Converter } from "../converter" ;
8
- import { Context } from "../context" ;
8
+ import type { Context } from "../context" ;
9
9
import { BindOption } from "../../utils" ;
10
10
11
+ function git ( ...args : string [ ] ) {
12
+ return spawnSync ( "git" , args , { encoding : "utf-8" , windowsHide : true } ) ;
13
+ }
14
+
11
15
/**
12
16
* Stores data of a repository.
13
17
*/
@@ -55,7 +59,6 @@ export class Repository {
55
59
constructor ( path : string , gitRevision : string , repoLinks : string [ ] ) {
56
60
this . path = path ;
57
61
this . branch = gitRevision || "master" ;
58
- ShellJS . pushd ( path ) ;
59
62
60
63
for ( let i = 0 , c = repoLinks . length ; i < c ; i ++ ) {
61
64
const url = / ( g i t h u b (?: \. [ a - z ] + ) * \. [ a - z ] { 2 , } ) [: / ] ( [ ^ / ] + ) \/ ( .* ) / . exec (
@@ -76,10 +79,8 @@ export class Repository {
76
79
}
77
80
}
78
81
79
- let out = < ShellJS . ExecOutputReturnValue > (
80
- ShellJS . exec ( "git ls-files" , { silent : true } )
81
- ) ;
82
- if ( out . code === 0 ) {
82
+ let out = git ( "-C" , path , "ls-files" ) ;
83
+ if ( out . status === 0 ) {
83
84
out . stdout . split ( "\n" ) . forEach ( ( file ) => {
84
85
if ( file !== "" ) {
85
86
this . files . push ( BasePath . normalize ( path + "/" + file ) ) ;
@@ -88,15 +89,11 @@ export class Repository {
88
89
}
89
90
90
91
if ( ! gitRevision ) {
91
- out = < ShellJS . ExecOutputReturnValue > (
92
- ShellJS . exec ( "git rev-parse --short HEAD" , { silent : true } )
93
- ) ;
94
- if ( out . code === 0 ) {
92
+ out = git ( "-C" , path , "rev-parse" , "--short" , "HEAD" ) ;
93
+ if ( out . status === 0 ) {
95
94
this . branch = out . stdout . replace ( "\n" , "" ) ;
96
95
}
97
96
}
98
-
99
- ShellJS . popd ( ) ;
100
97
}
101
98
102
99
/**
@@ -148,31 +145,17 @@ export class Repository {
148
145
gitRevision : string ,
149
146
gitRemote : string
150
147
) : Repository | undefined {
151
- ShellJS . pushd ( path ) ;
152
- const out = < ShellJS . ExecOutputReturnValue > (
153
- ShellJS . exec ( "git rev-parse --show-toplevel" , { silent : true } )
154
- ) ;
155
- const remotesOutput = < ShellJS . ExecOutputReturnValue > (
156
- ShellJS . exec ( `git remote get-url ${ gitRemote } ` , { silent : true } )
157
- ) ;
158
- ShellJS . popd ( ) ;
148
+ const out = git ( "-C" , path , "rev-parse" , "--show-toplevel" ) ;
149
+ const remotesOutput = git ( "-C" , path , "remote" , "get-url" , gitRemote ) ;
159
150
160
- if (
161
- ! out ||
162
- out . code !== 0 ||
163
- ! remotesOutput ||
164
- remotesOutput . code !== 0
165
- ) {
151
+ if ( out . status !== 0 || remotesOutput . status !== 0 ) {
166
152
return ;
167
153
}
168
154
169
- const remotes : string [ ] =
170
- remotesOutput . code === 0 ? remotesOutput . stdout . split ( "\n" ) : [ ] ;
171
-
172
155
return new Repository (
173
156
BasePath . normalize ( out . stdout . replace ( "\n" , "" ) ) ,
174
157
gitRevision ,
175
- remotes
158
+ remotesOutput . stdout . split ( "\n" )
176
159
) ;
177
160
}
178
161
}
@@ -205,8 +188,7 @@ export class GitHubPlugin extends ConverterComponent {
205
188
* @param converter The converter this plugin should be attached to.
206
189
*/
207
190
initialize ( ) {
208
- ShellJS . config . silent = true ;
209
- if ( ShellJS . which ( "git" ) ) {
191
+ if ( git ( "--version" ) . status === 0 ) {
210
192
this . listenTo (
211
193
this . owner ,
212
194
Converter . EVENT_RESOLVE_END ,
0 commit comments