1
1
import axios from 'axios' ;
2
2
import fs from 'fs-extra' ;
3
- import execa from 'execa' ;
4
3
import { File } from 'models' ;
5
4
import removeMarkdown from 'remove-markdown' ;
5
+ import * as child_process from 'child_process' ;
6
+ import { ExecOptions } from 'child_process' ;
6
7
7
8
export function download ( url : string , localPath : string ) {
8
9
return axios ( { url, method : 'GET' , responseType : 'stream' } )
@@ -16,11 +17,11 @@ export function download(url: string, localPath: string) {
16
17
17
18
export async function pull ( dir : string , repo : string , commit = 'origin/master' ) {
18
19
if ( fs . pathExistsSync ( dir ) ) {
19
- await execa . shell ( `git fetch` , { cwd : dir } ) ;
20
+ await execute ( `git fetch` , { cwd : dir } ) ;
20
21
} else {
21
- await execa . shell ( `git clone https://github.com/algorithm-visualizer/${ repo } .git ${ dir } ` ) ;
22
+ await execute ( `git clone https://github.com/algorithm-visualizer/${ repo } .git ${ dir } ` ) ;
22
23
}
23
- await execa . shell ( `git reset --hard ${ commit } ` , { cwd : dir } ) ;
24
+ await execute ( `git reset --hard ${ commit } ` , { cwd : dir } ) ;
24
25
}
25
26
26
27
export function getDescription ( files : File [ ] ) {
@@ -34,6 +35,18 @@ export function getDescription(files: File[]) {
34
35
return removeMarkdown ( descriptionLines . join ( ' ' ) ) ;
35
36
}
36
37
37
- export function rethrow ( err : any ) {
38
- throw err ;
38
+ type ExecuteOptions = ExecOptions & {
39
+ stdout ?: NodeJS . WriteStream ;
40
+ stderr ?: NodeJS . WriteStream ;
41
+ }
42
+
43
+ export function execute ( command : string , { stdout, stderr, ...options } : ExecuteOptions = { } ) : Promise < string > {
44
+ return new Promise ( ( resolve , reject ) => {
45
+ const child = child_process . exec ( command , options , ( error , stdout , stderr ) => {
46
+ if ( error ) return reject ( error . code ? new Error ( stderr ) : error ) ;
47
+ resolve ( stdout ) ;
48
+ } ) ;
49
+ if ( child . stdout && stdout ) child . stdout . pipe ( stdout ) ;
50
+ if ( child . stderr && stderr ) child . stderr . pipe ( stderr ) ;
51
+ } ) ;
39
52
}
0 commit comments