|
| 1 | +import * as T from "../../typings/tutorial"; |
| 2 | +import { exec as cpExec } from "child_process"; |
| 3 | +import * as path from "path"; |
| 4 | +import { promisify } from "util"; |
| 5 | + |
| 6 | +const asyncExec = promisify(cpExec); |
| 7 | + |
| 8 | +export function createExec(cwd: string) { |
| 9 | + return async function exec( |
| 10 | + command: string |
| 11 | + ): Promise<{ stdout: string | null; stderr: string }> { |
| 12 | + try { |
| 13 | + const result = await asyncExec(command, { cwd }); |
| 14 | + return result; |
| 15 | + } catch (e) { |
| 16 | + return { stdout: null, stderr: e.message }; |
| 17 | + } |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +export function createCherryPick(cwd: string) { |
| 22 | + return async function cherryPick(commits: string[]): Promise<void> { |
| 23 | + for (const commit of commits) { |
| 24 | + try { |
| 25 | + const { stdout } = await createExec(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 | +export function createCommandRunner(cwd: string) { |
| 39 | + return async function runCommands( |
| 40 | + commands: string[], |
| 41 | + dir?: string |
| 42 | + ): Promise<boolean> { |
| 43 | + let errors = []; |
| 44 | + for (const command of commands) { |
| 45 | + try { |
| 46 | + console.log(`--> ${command}`); |
| 47 | + let cwdDir = cwd; |
| 48 | + if (dir) { |
| 49 | + cwdDir = path.join(cwd, dir); |
| 50 | + } |
| 51 | + const { stdout, stderr } = await createExec(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 | +export function createTestRunner(cwd: string, config: T.TestRunnerConfig) { |
| 69 | + const { command, args, directory } = config; |
| 70 | + |
| 71 | + // const commandIsAbsolute = isAbsolute(command); |
| 72 | + |
| 73 | + let wd = cwd; |
| 74 | + if (directory) { |
| 75 | + wd = path.join(cwd, directory); |
| 76 | + } |
| 77 | + |
| 78 | + const commandWithArgs = `${command} ${args.tap}`; |
| 79 | + |
| 80 | + return async function runTest(): Promise<{ |
| 81 | + stdout: string | null; |
| 82 | + stderr: string | null; |
| 83 | + }> { |
| 84 | + try { |
| 85 | + // console.log(await createExec(wd)("ls -a node_modules/.bin")); |
| 86 | + return await createExec(wd)(commandWithArgs); |
| 87 | + } catch (e) { |
| 88 | + return Promise.resolve({ stdout: null, stderr: e.message }); |
| 89 | + } |
| 90 | + }; |
| 91 | +} |
0 commit comments