Skip to content

Commit 1861195

Browse files
authored
Merge pull request #34 from coderoad/feature/validate
Feature/validate
2 parents 46b64db + 2fc1113 commit 1861195

File tree

6 files changed

+263
-42
lines changed

6 files changed

+263
-42
lines changed

Diff for: package-lock.json

+41-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"dependencies": {
5050
"ajv": "^6.12.2",
5151
"esm": "^3.2.25",
52+
"fs-extra": "^9.0.1",
5253
"js-yaml": "^3.14.0",
5354
"kleur": "^3.0.3",
5455
"lodash": "^4.17.15",
@@ -58,6 +59,7 @@
5859
"devDependencies": {
5960
"@babel/preset-typescript": "^7.10.1",
6061
"@types/ajv": "^1.0.0",
62+
"@types/fs-extra": "^9.0.1",
6163
"@types/inquirer": "^6.5.0",
6264
"@types/jest": "^25.2.3",
6365
"@types/js-yaml": "^3.12.4",

Diff for: src/utils/exec.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)