Skip to content

Convert/typescript #3

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 5 commits into from
May 30, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
node_modules
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"source.organizeImports": false,
"source.fixAll": true
},
"eslint.validate": ["javascript"],
"eslint.validate": ["javascript", "typescript"],
"files.exclude": {},
"git.alwaysSignOff": true
"git.alwaysSignOff": true,
"typescript.tsdk": "./node_modules/typescript/lib"
}
4 changes: 2 additions & 2 deletions bin/coderoad
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
require = require("esm")(module /*, options*/);
require("../build/cli").cli(process.argv);
6,291 changes: 6,068 additions & 223 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"access": "public"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "del-cli --force ./build && npm run compile",
"compile": "tsc",
"test": "jest"
},
"keywords": [],
"author": "Argemiro Neto",
Expand All @@ -26,5 +28,21 @@
"ncp": "^2.0.0",
"npc": "0.0.1",
"simple-git": "^2.5.0"
},
"devDependencies": {
"@babel/preset-typescript": "^7.10.1",
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.3",
"@types/js-yaml": "^3.12.4",
"@types/lodash": "^4.14.154",
"@types/ncp": "^2.0.4",
"del-cli": "^3.0.1",
"jest": "^26.0.1",
"ts-jest": "^26.0.0",
"typescript": "^3.9.3"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
}
}
140 changes: 85 additions & 55 deletions src/parse.js → src/build.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import simpleGit from "simple-git/promise";
import yamlParser from "js-yaml";
import path from "path";
import _ from "lodash";
import fs from "fs";
import * as yamlParser from "js-yaml";
import * as path from "path";
import * as _ from "lodash";
import * as fs from "fs";
import * as T from "../typings/tutorial";
// import validate from './validator';

// import not working
const simpleGit = require("simple-git/promise");

const workingDir = "tmp";

function parseContent(md) {
let start = -1;
const parts = [];
type TutorialContent = {};

function parseContent(md: string): TutorialContent {
let start: number = -1;
const parts: any[] = [];

const lines = md.split("\n");

Expand Down Expand Up @@ -76,7 +81,7 @@ function parseContent(md) {
return sections;
}

function rmDir(dir, rmSelf) {
function rmDir(dir: string, rmSelf = false) {
try {
let files;
rmSelf = rmSelf === undefined ? true : rmSelf;
Expand All @@ -89,11 +94,11 @@ function rmDir(dir, rmSelf) {
}

if (files.length > 0) {
files.forEach(function (x, i) {
if (fs.statSync(path.join(dir, x)).isDirectory()) {
rmDir(path.join(dir, x));
files.forEach(function (filePath: string) {
if (fs.statSync(path.join(dir, filePath)).isDirectory()) {
rmDir(path.join(dir, filePath));
} else {
fs.unlinkSync(path.join(dir, x));
fs.unlinkSync(path.join(dir, filePath));
}
});
}
Expand All @@ -107,7 +112,7 @@ function rmDir(dir, rmSelf) {
}
}

async function cleanupFiles(workingDir) {
async function cleanupFiles(workingDir: string) {
try {
const gitModule = simpleGit(process.cwd());

Expand All @@ -121,17 +126,18 @@ async function cleanupFiles(workingDir) {
}
}

/**
*
* @param {string} repo Git url to the repo. It should finish with .git
* @param {string} codeBranch The branch containing the tutorial code
* @param {string} setupBranch The branch containing the configuration files
* @param {string} isLocal define if the repo is local or remote
*/
async function build({ repo, codeBranch, setupBranch, isLocal }) {
let git;
export type BuildOptions = {
repo: string; // Git url to the repo. It should finish with .git
codeBranch: string; // The branch containing the tutorial code
setupBranch: string; // The branch containing the tutorialuration files
isLocal: boolean; // define if the repo is local or remote
output: string;
};

async function build({ repo, codeBranch, setupBranch, isLocal }: BuildOptions) {
let git: any;
let isSubModule = false;
let localPath;
let localPath: string;

if (isLocal) {
git = simpleGit(repo);
Expand All @@ -154,30 +160,33 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {

await git.fetch();

// checkout the branch to load configuration and content branch
// checkout the branch to load tutorialuration and content branch
await git.checkout(setupBranch);

// Load files
const _mdContent = fs.readFileSync(
path.join(localPath, "TUTORIAL.md"),
"utf8"
);
let _config = fs.readFileSync(path.join(localPath, "coderoad.yaml"), "utf8");
let _tutorial = fs.readFileSync(
path.join(localPath, "coderoad.yaml"),
"utf8"
);

// Add one more line to the content as per Shawn's request
const mdContent = parseContent(_mdContent);
const mdContent: any = parseContent(_mdContent);

// Parse config to JSON
const config = yamlParser.load(_config);
// Parse tutorial to JSON
const tutorial: T.Tutorial = yamlParser.load(_tutorial);

// Add the summary to the config file
config["summary"] = mdContent.summary;
// Add the summary to the tutorial file
tutorial.summary = mdContent.summary;

// merge content and config
config.levels.forEach((level) => {
// merge content and tutorial
tutorial.levels.forEach((level: T.Level) => {
const { steps, ...content } = mdContent[level.id];

level.steps.forEach((step) => _.merge(step, steps[step.id]));
level.steps.forEach((step: T.Step) => _.merge(step, steps[step.id]));

_.merge(level, content);
});
Expand All @@ -200,29 +209,50 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
// Uses a set to make sure only the latest commit is proccessed
parts.add(matches[0]);

// Add the content and git hash to the config
// Add the content and git hash to the tutorial
if (matches.groups.stepId) {
// If it's a step: add the content and the setup/solution hashes depending on the type
const theStep = config.levels
.find((level) => level.id === matches.groups.levelId)
.steps.find((step) => step.id === matches.groups.stepId);

if (matches.groups.stepType === "Q") {
theStep.setup.commits.push(commit.hash.substr(0, 7));
} else if (
matches.groups.stepType === "A" &&
theStep.solution.commits
) {
theStep.solution.commits.push(commit.hash.substr(0, 7));
const level: T.Level | null =
tutorial.levels.find(
(level: T.Level) => level.id === matches.groups.levelId
) || null;
if (!level) {
console.log(`Level ${matches.groups.levelId} not found`);
} else {
const theStep: T.Step | null =
level.steps.find(
(step: T.Step) => step.id === matches.groups.stepId
) || null;

if (!theStep) {
console.log(`Step ${matches.groups.stepId} not found`);
} else {
if (matches.groups.stepType === "Q") {
theStep.setup.commits.push(commit.hash.substr(0, 7));
} else if (
matches.groups.stepType === "A" &&
theStep.solution &&
theStep.solution.commits
) {
theStep.solution.commits.push(commit.hash.substr(0, 7));
}
}
}
} else {
// If it's level: add the commit hash (if the level has the commit key) and the content to the config
const theLevel = config.levels.find(
(level) => level.id === matches.groups.levelId
);

if (_.has(theLevel, "config.commits")) {
theLevel.setup.commits.push(commit.hash.substr(0, 7));
// If it's level: add the commit hash (if the level has the commit key) and the content to the tutorial
const theLevel: T.Level | null =
tutorial.levels.find(
(level: T.Level) => level.id === matches.groups.levelId
) || null;

if (!theLevel) {
console.log(`Level ${matches.groups.levelId} not found`);
} else {
if (_.has(theLevel, "tutorial.commits")) {
if (theLevel.setup) {
theLevel.setup.commits.push(commit.hash.substr(0, 7));
}
}
}
}
}
Expand All @@ -247,14 +277,14 @@ async function build({ repo, codeBranch, setupBranch, isLocal }) {
}
}

// const isValid = validate(config);
// const isValid = validate(tutorial);

// if (!isValid) {
// console.log(JSON.stringify(validate.errors, null, 2));
// return;
// }

return config;
return tutorial;
}

export default build;
Loading