11import axios from 'axios' ;
22import fs from 'fs-extra' ;
3- import execa from 'execa' ;
43import { File } from 'models' ;
54import removeMarkdown from 'remove-markdown' ;
5+ import * as child_process from 'child_process' ;
6+ import { ExecOptions } from 'child_process' ;
67
78export function download ( url : string , localPath : string ) {
89 return axios ( { url, method : 'GET' , responseType : 'stream' } )
@@ -16,11 +17,11 @@ export function download(url: string, localPath: string) {
1617
1718export async function pull ( dir : string , repo : string , commit = 'origin/master' ) {
1819 if ( fs . pathExistsSync ( dir ) ) {
19- await execa . shell ( `git fetch` , { cwd : dir } ) ;
20+ await execute ( `git fetch` , { cwd : dir } ) ;
2021 } 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 } ` ) ;
2223 }
23- await execa . shell ( `git reset --hard ${ commit } ` , { cwd : dir } ) ;
24+ await execute ( `git reset --hard ${ commit } ` , { cwd : dir } ) ;
2425}
2526
2627export function getDescription ( files : File [ ] ) {
@@ -34,6 +35,18 @@ export function getDescription(files: File[]) {
3435 return removeMarkdown ( descriptionLines . join ( ' ' ) ) ;
3536}
3637
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+ } ) ;
3952}
0 commit comments