Skip to content

Commit 8041a08

Browse files
authored
Merge pull request #16 from coderoad/fix/create
fix coderoad create
2 parents 12fa899 + 60367b2 commit 8041a08

File tree

10 files changed

+89
-45
lines changed

10 files changed

+89
-45
lines changed

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coderoad/cli",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "A CLI to build the configuration file for Coderoad Tutorials",
55
"keywords": [
66
"coderoad",

src/build.ts

+19-22
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,28 @@ type BuildArgs = {
2525
output: string;
2626
};
2727

28-
const parseArgs = (args: string[]): BuildArgs => {
29-
// default .
30-
const dir = args[0] || ".";
31-
32-
// -m --markdown - default TUTORIAL.md
33-
const markdown =
34-
getArg(args, { name: "markdown", alias: "m" }) || "TUTORIAL.md";
35-
// -y --yaml - default coderoad-config.yml
36-
const yaml = getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml";
37-
// -o --output - default coderoad.json
38-
const output =
39-
getArg(args, { name: "output", alias: "o" }) || "tutorial.json";
40-
41-
return {
42-
dir,
43-
output,
44-
markdown,
45-
yaml,
46-
};
47-
};
48-
4928
async function build(args: string[]) {
5029
let options: BuildArgs;
5130
try {
52-
options = parseArgs(args);
31+
// default .
32+
const dir = args[0].match(/^-/) ? "." : args[0];
33+
// -m --markdown - default TUTORIAL.md
34+
const markdown =
35+
getArg(args, { name: "markdown", alias: "m" }) || "TUTORIAL.md";
36+
// -y --yaml - default coderoad-config.yml
37+
const yaml = getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml";
38+
// -o --output - default coderoad.json
39+
const output =
40+
getArg(args, { name: "output", alias: "o" }) || "tutorial.json";
41+
42+
console.log(`Building CodeRoad ${output}...`);
43+
44+
options = {
45+
dir,
46+
output,
47+
markdown,
48+
yaml,
49+
};
5350
} catch (e) {
5451
console.error("Error parsing build logs");
5552
console.error(e.message);

src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function cli(rawArgs: string[]): Promise<void> {
1919
break;
2020

2121
case "create":
22-
create(process.cwd());
22+
create(args);
2323
break;
2424

2525
case "--help":

src/create.ts

+60-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,75 @@
11
import ncp from "ncp";
22
import * as path from "path";
33
import { promisify } from "util";
4+
import { getArg } from "./utils/args";
45

56
const copy = promisify(ncp);
67

7-
const copyFiles = async (filePath: string): Promise<void> => {
8+
type CreateArgs = {
9+
dir: string;
10+
lang: string;
11+
testRunner: string;
12+
};
13+
14+
async function create(args: string[]): Promise<void> {
15+
let options: CreateArgs;
16+
17+
// default .
18+
const dir = !args.length || args[0].match(/^-/) ? "." : args[0];
19+
const lang = getArg(args, { name: "lang", alias: "l" }) || "js";
20+
const testRunner =
21+
getArg(args, { name: "testRunner", alias: "t" }) || "mocha";
22+
23+
// validate lang
24+
if (!["js"].includes(lang)) {
25+
throw new Error(`Language ${lang} not supported yet in create`);
26+
}
27+
28+
// validate test runner
29+
if (!["mocha"].includes(testRunner)) {
30+
throw new Error(`Test Runner ${testRunner} not supported yet in create`);
31+
}
32+
33+
console.info(`Creating CodeRoad project for ${lang} ${testRunner}`);
34+
35+
options = {
36+
dir,
37+
lang,
38+
testRunner,
39+
};
40+
41+
const localPath = path.join(process.cwd(), options.dir);
42+
43+
// TODO: git init ?
44+
45+
// copy tutorial file
46+
const pathToSrc = path.join(__dirname, "..", "src");
47+
const templateDirectory = path.resolve(pathToSrc, "templates");
48+
49+
const markdownPath = path.join(templateDirectory, "TUTORIAL.md");
50+
const targetMarkdownPath = path.join(localPath, "TUTORIAL.md");
851
try {
9-
const pathToSrc = path.join(__dirname, "..", "src");
10-
const templateDirectory = path.resolve(pathToSrc, "templates");
11-
const targetDirectory = process.cwd();
52+
await copy(markdownPath, targetMarkdownPath, {
53+
clobber: false,
54+
});
55+
} catch (e) {
56+
console.error("Error on creating markdown file");
57+
console.error(e.message);
58+
}
1259

13-
await copy(templateDirectory, targetDirectory, {
60+
// TODO: copy master yaml
61+
const pathToYaml = path.join(templateDirectory, `${lang}-${testRunner}`);
62+
const targetYamlPath = path.join(localPath, "coderoad.yaml");
63+
try {
64+
await copy(pathToYaml, targetYamlPath, {
1465
clobber: false,
1566
});
1667
} catch (e) {
17-
console.log("Error on creating the files:");
18-
console.log(JSON.stringify(e, null, 1));
68+
console.error("Error on creating yaml file");
69+
console.error(e.message);
1970
}
20-
};
2171

22-
const create = copyFiles;
72+
// TODO: copy code files with commits
73+
}
2374

2475
export default create;
File renamed without changes.
File renamed without changes.

src/templates/TUTORIAL.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,37 @@
22

33
Short description to be shown as a tutorial's subtitle
44

5-
65
## L1 Put Level's title here
76

87
> Level's summary: a short description of the level's content in one line.
98
10-
The level is identified and distributed following the regex:
9+
The level is identified and distributed following the regex:
1110

1211
```js
13-
/^(##\s(?<levelId>L\d+)\s(?<levelTitle>.*)\n*(>\s*(?<levelSummary>.*))?\n+(?<levelContent>[^]*))/
12+
/^(##\s(?<levelId>L\d+)\s(?<levelTitle>.*)\n*(>\s*(?<levelSummary>.*))?\n+(?<levelContent>[^]*))/;
1413
```
1514

1615
The Level can be split into steps or have no steps. Levels without steps are meant to be used as only informative content, for example: use a Level without steps at the end of the tutorial to congratulate the student and provide some additional related resources.
1716

18-
Tutorial's content. It can span through multiple paragraphs and use headers `####` and `#####`.
17+
Tutorial's content. It can span through multiple paragraphs and use headers `####` and `#####`.
1918

2019
Steps are identified and their content described using the following regex:
2120

2221
```js
23-
/^(###\s(?<stepId>(?<levelId>L\d+)S\d+)\s(?<stepTitle>.*)\n+(?<stepContent>[^]*))/
22+
/^(###\s(?<stepId>(?<levelId>L\d+)S\d+)\s(?<stepTitle>.*)\n+(?<stepContent>[^]*))/;
2423
```
2524

2625
The numbers identifying the levels and steps are irrelevant but keep in mind that they will be executed in order. A level with id `10` will be executed before another one with id `20` and so on. These `ids` should have a match in the configuration file (`coderoad.yaml`).
2726

28-
2927
### L1S1 A step title (not being shown on the extension at this moment)
3028

3129
Short description of the step's purpose. Should be short and fit in one line
3230

3331
**Important**
32+
3433
1. Both level and step ids must have an entry with the same id on the configuration file;
3534
2. Step Ids are based on its level id. Any step from level `L234` must start with `L234S`, followed by the sequential digits.
3635

37-
3836
### L1S2 Another step
3937

40-
Step's short description.
38+
Step's short description.

src/templates/coderoad.yaml renamed to src/templates/js-mocha/coderoad.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ config:
2121
## Commits to load to setup the test runner. Optional.
2222
##
2323
setup:
24-
## A list of commits to load to setup the tutorial
25-
commits: []
2624
# - commit1
2725
# - commit2
2826
## A list of commands to run to configure the tutorial

src/utils/validate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import schema from "./schema";
1+
import schema from "../schema";
22

33
// https://www.npmjs.com/package/ajv
44
// @ts-ignore ajv typings not working

0 commit comments

Comments
 (0)