Skip to content

Commit c0e7cd0

Browse files
committed
validate yaml setup
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 7a1ebe0 commit c0e7cd0

File tree

8 files changed

+40
-13
lines changed

8 files changed

+40
-13
lines changed

src/build.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ async function build(args: string[]) {
7474
let config;
7575
try {
7676
config = yamlParser.load(_yaml);
77+
// TODO: validate yaml
78+
if (!config || !config.length) {
79+
throw new Error("Invalid yaml file contents");
80+
}
7781
} catch (e) {
7882
console.error("Error parsing yaml");
7983
console.error(e.message);

src/utils/commits.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import util from "util";
33
import * as path from "path";
44
import gitP, { SimpleGit } from "simple-git/promise";
5-
import { addToCommitOrder, validateCommitOrder } from "./commitOrder";
5+
import { validateCommitOrder } from "./validateCommits";
66

77
const mkdir = util.promisify(fs.mkdir);
88
const exists = util.promisify(fs.exists);

src/utils/parse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export function parse(params: ParseParams): any {
106106

107107
// add init commits
108108
if (params.commits.INIT && params.commits.INIT.length) {
109-
console.log(JSON.stringify(parsed.config?.testRunner));
110109
// @ts-ignore
111110
parsed.config.testRunner.setup = {
112111
...(parsed.config?.testRunner?.setup || {}),

src/utils/commitOrder.ts renamed to src/utils/validateCommits.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// should flag commits that are out of order based on the previous commit
22
// position is a string like 'INIT', 'L1', 'L1S1'
3-
export function addToCommitOrder(position: string) {
4-
// add position to list
5-
}
6-
73
export function validateCommitOrder(positions: string[]): boolean {
84
// loop over positions
95
const errors: number[] = [];
@@ -12,7 +8,6 @@ export function validateCommitOrder(positions: string[]): boolean {
128
positions.forEach((position: string, index: number) => {
139
if (position === "INIT") {
1410
if (previous.level !== 0 && previous.step !== 0) {
15-
console.log("ERROR HERE");
1611
errors.push(index);
1712
}
1813
current = { level: 0, step: 0 };
@@ -46,7 +41,7 @@ export function validateCommitOrder(positions: string[]): boolean {
4641
previous = current;
4742
});
4843

49-
if (errors.length) {
44+
if (errors.length && process.env.NODE_ENV !== "test") {
5045
console.warn("Found commit positions out of order");
5146
positions.forEach((position, index) => {
5247
if (errors.includes(index)) {

src/utils/validateYaml.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function validateYaml(yaml: string) {
2+
return true;
3+
}

src/validate.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
import * as path from "path";
22
import * as fs from "fs";
33
import util from "util";
4+
import * as yamlParser from "js-yaml";
5+
import { getArg } from "./utils/args";
46
import gitP, { SimpleGit } from "simple-git/promise";
57
import { getCommits, CommitLogObject } from "./utils/commits";
68

79
const mkdir = util.promisify(fs.mkdir);
810
const exists = util.promisify(fs.exists);
911
const rmdir = util.promisify(fs.rmdir);
12+
const read = util.promisify(fs.readFile);
1013

1114
async function validate(args: string[]) {
1215
// dir - default .
1316
const dir = !args.length || args[0].match(/^-/) ? "." : args[0];
14-
console.warn("Not yet implemented. Coming soon");
15-
1617
const localDir = path.join(process.cwd(), dir);
17-
const codeBranch = "";
1818

19-
const commits = getCommits({ localDir, codeBranch });
19+
// -y --yaml - default coderoad-config.yml
20+
const options = {
21+
yaml: getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml";
22+
}
23+
24+
const _yaml = await read(path.join(localDir, options.yaml), "utf8");
25+
26+
// parse yaml config
27+
let config;
28+
try {
29+
config = yamlParser.load(_yaml)
30+
// TODO: validate yaml
31+
if (!config || !config.length) {
32+
throw new Error('Invalid yaml file contents')
33+
}
34+
} catch (e) {
35+
console.error("Error parsing yaml");
36+
console.error(e.message);
37+
}
38+
39+
const codeBranch: string = config.config.repo.branch;
40+
2041
// VALIDATE SKELETON WITH COMMITS
42+
const commits = getCommits({ localDir, codeBranch });
43+
2144
// parse tutorial skeleton for order and commands
2245

2346
// on error, warn missing level/step

tests/commitOrder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateCommitOrder } from "../src/utils/commitOrder";
1+
import { validateCommitOrder } from "../src/utils/validateCommits";
22

33
describe("commitOrder", () => {
44
it("should return true if order is valid", () => {

tests/yaml.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
describe("yaml", () => {
2+
it.todo("should parse a valid yaml file");
3+
});

0 commit comments

Comments
 (0)