|
| 1 | +import*asTfrom"../../typings/tutorial"; |
| 2 | +import{execascpExec}from"child_process"; |
| 3 | +import*aspathfrom"path"; |
| 4 | +import{promisify}from"util"; |
| 5 | + |
| 6 | +constasyncExec=promisify(cpExec); |
| 7 | + |
| 8 | +exportfunctioncreateExec(cwd:string){ |
| 9 | +returnasyncfunctionexec( |
| 10 | +command:string |
| 11 | +):Promise<{stdout:string|null;stderr:string}>{ |
| 12 | +try{ |
| 13 | +constresult=awaitasyncExec(command,{ cwd}); |
| 14 | +returnresult; |
| 15 | +}catch(e){ |
| 16 | +return{stdout:null,stderr:e.message}; |
| 17 | +} |
| 18 | +}; |
| 19 | +} |
| 20 | + |
| 21 | +exportfunctioncreateCherryPick(cwd:string){ |
| 22 | +returnasyncfunctioncherryPick(commits:string[]):Promise<void>{ |
| 23 | +for(constcommitofcommits){ |
| 24 | +try{ |
| 25 | +const{ stdout}=awaitcreateExec(cwd)( |
| 26 | +`git cherry-pick -X theirs${commit}` |
| 27 | +); |
| 28 | +if(!stdout){ |
| 29 | +console.warn(`No cherry-pick output for${commit}`); |
| 30 | +} |
| 31 | +}catch(e){ |
| 32 | +console.warn(`Cherry-pick failed for${commit}`); |
| 33 | +} |
| 34 | +} |
| 35 | +}; |
| 36 | +} |
| 37 | + |
| 38 | +exportfunctioncreateCommandRunner(cwd:string){ |
| 39 | +returnasyncfunctionrunCommands( |
| 40 | +commands:string[], |
| 41 | +dir?:string |
| 42 | +):Promise<boolean>{ |
| 43 | +leterrors=[]; |
| 44 | +for(constcommandofcommands){ |
| 45 | +try{ |
| 46 | +console.log(`-->${command}`); |
| 47 | +letcwdDir=cwd; |
| 48 | +if(dir){ |
| 49 | +cwdDir=path.join(cwd,dir); |
| 50 | +} |
| 51 | +const{ stdout, stderr}=awaitcreateExec(cwdDir)(command); |
| 52 | + |
| 53 | +console.warn(stderr); |
| 54 | +}catch(e){ |
| 55 | +console.error(`Command failed: "${command}"`); |
| 56 | +console.warn(e.message); |
| 57 | +errors.push(e.message); |
| 58 | +} |
| 59 | +} |
| 60 | +return!!errors.length; |
| 61 | +}; |
| 62 | +} |
| 63 | + |
| 64 | +// function isAbsolute(p: string) { |
| 65 | +// return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/"); |
| 66 | +// } |
| 67 | + |
| 68 | +exportfunctioncreateTestRunner(cwd:string,config:T.TestRunnerConfig){ |
| 69 | +const{ command, args, directory}=config; |
| 70 | + |
| 71 | +// const commandIsAbsolute = isAbsolute(command); |
| 72 | + |
| 73 | +letwd=cwd; |
| 74 | +if(directory){ |
| 75 | +wd=path.join(cwd,directory); |
| 76 | +} |
| 77 | + |
| 78 | +constcommandWithArgs=`${command}${args.tap}`; |
| 79 | + |
| 80 | +returnasyncfunctionrunTest():Promise<{ |
| 81 | +stdout:string|null; |
| 82 | +stderr:string|null; |
| 83 | +}>{ |
| 84 | +try{ |
| 85 | +// console.log(await createExec(wd)("ls -a node_modules/.bin")); |
| 86 | +returnawaitcreateExec(wd)(commandWithArgs); |
| 87 | +}catch(e){ |
| 88 | +returnPromise.resolve({stdout:null,stderr:e.message}); |
| 89 | +} |
| 90 | +}; |
| 91 | +} |