Skip to content

Feature/validate #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"ajv": "^6.12.2",
"esm": "^3.2.25",
"fs-extra": "^9.0.1",
"js-yaml": "^3.14.0",
"kleur": "^3.0.3",
"lodash": "^4.17.15",
Expand All @@ -58,6 +59,7 @@
"devDependencies": {
"@babel/preset-typescript": "^7.10.1",
"@types/ajv": "^1.0.0",
"@types/fs-extra": "^9.0.1",
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.3",
"@types/js-yaml": "^3.12.4",
Expand Down
91 changes: 91 additions & 0 deletions src/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as T from "../../typings/tutorial";
import { exec as cpExec } from "child_process";
import * as path from "path";
import { promisify } from "util";

const asyncExec = promisify(cpExec);

export function createExec(cwd: string) {
return async function exec(
command: string
): Promise<{ stdout: string | null; stderr: string }> {
try {
const result = await asyncExec(command, { cwd });
return result;
} catch (e) {
return { stdout: null, stderr: e.message };
}
};
}

export function createCherryPick(cwd: string) {
return async function cherryPick(commits: string[]): Promise<void> {
for (const commit of commits) {
try {
const { stdout } = await createExec(cwd)(
`git cherry-pick -X theirs ${commit}`
);
if (!stdout) {
console.warn(`No cherry-pick output for ${commit}`);
}
} catch (e) {
console.warn(`Cherry-pick failed for ${commit}`);
}
}
};
}

export function createCommandRunner(cwd: string) {
return async function runCommands(
commands: string[],
dir?: string
): Promise<boolean> {
let errors = [];
for (const command of commands) {
try {
console.log(`--> ${command}`);
let cwdDir = cwd;
if (dir) {
cwdDir = path.join(cwd, dir);
}
const { stdout, stderr } = await createExec(cwdDir)(command);

console.warn(stderr);
} catch (e) {
console.error(`Command failed: "${command}"`);
console.warn(e.message);
errors.push(e.message);
}
}
return !!errors.length;
};
}

// function isAbsolute(p: string) {
// return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
// }

export function createTestRunner(cwd: string, config: T.TestRunnerConfig) {
const { command, args, directory } = config;

// const commandIsAbsolute = isAbsolute(command);

let wd = cwd;
if (directory) {
wd = path.join(cwd, directory);
}

const commandWithArgs = `${command} ${args.tap}`;

return async function runTest(): Promise<{
stdout: string | null;
stderr: string | null;
}> {
try {
// console.log(await createExec(wd)("ls -a node_modules/.bin"));
return await createExec(wd)(commandWithArgs);
} catch (e) {
return Promise.resolve({ stdout: null, stderr: e.message });
}
};
}
Loading