Skip to content

Fix/pound in text #78

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 3 commits into from
Apr 3, 2021
Merged
Changes from 1 commit
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
Next Next commit
lint
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Apr 3, 2021
commit 869c60459ab71e856d0139470bce41e79ba00703
172 changes: 86 additions & 86 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,171 +1,171 @@
import * as yamlParser from "js-yaml";
import * as path from "path";
import * as fs from "fs";
import * as util from "util";
import { parse } from "./utils/parse";
import { getArg } from "./utils/args";
import { getCommits, CommitLogObject } from "./utils/commits";
import skeletonSchema from "./schema/skeleton";
import tutorialSchema from "./schema/tutorial";
import { validateSchema } from "./utils/validateSchema";
import { validateMarkdown } from "./utils/validateMarkdown";
import * as T from "../typings/tutorial";

const write = util.promisify(fs.writeFile);
const read = util.promisify(fs.readFile);
import * as yamlParser from 'js-yaml'
import * as path from 'path'
import * as fs from 'fs'
import * as util from 'util'
import { parse } from './utils/parse'
import { getArg } from './utils/args'
import { getCommits, CommitLogObject } from './utils/commits'
import skeletonSchema from './schema/skeleton'
import tutorialSchema from './schema/tutorial'
import { validateSchema } from './utils/validateSchema'
import { validateMarkdown } from './utils/validateMarkdown'
import * as T from '../typings/tutorial'

const write = util.promisify(fs.writeFile)
const read = util.promisify(fs.readFile)

export type BuildConfigOptions = {
text: string; // text document from markdown
config: T.Tutorial; // yaml config file converted to json
commits: CommitLogObject; // an object of tutorial positions with a list of commit hashes
};
text: string // text document from markdown
config: T.Tutorial // yaml config file converted to json
commits: CommitLogObject // an object of tutorial positions with a list of commit hashes
}

type BuildArgs = {
dir: string;
markdown: string;
yaml: string;
output: string;
validate: boolean;
};
dir: string
markdown: string
yaml: string
output: string
validate: boolean
}

async function build(args: string[]) {
let options: BuildArgs;
async function build (args: string[]) {
let options: BuildArgs

try {
// dir - default .
const dir = !args.length || args[0].match(/^-/) ? "." : args[0];
const dir = !args.length || args[0].match(/^-/) ? '.' : args[0]
// -m --markdown - default TUTORIAL.md
const markdown =
getArg(args, { name: "markdown", alias: "m" }) || "TUTORIAL.md";
getArg(args, { name: 'markdown', alias: 'm' }) || 'TUTORIAL.md'
// -y --yaml - default coderoad-config.yml
const yaml = getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml";
const yaml = getArg(args, { name: 'yaml', alias: 'y' }) || 'coderoad.yaml'
// -o --output - default coderoad.json
const output =
getArg(args, { name: "output", alias: "o" }) || "tutorial.json";
const validate = getArg(args, { name: "validate", alias: "v" }) !== "false";
getArg(args, { name: 'output', alias: 'o' }) || 'tutorial.json'
const validate = getArg(args, { name: 'validate', alias: 'v' }) !== 'false'

console.log(`Building CodeRoad ${output}...`);
console.log(`Building CodeRoad ${output}...`)

options = {
dir,
output,
markdown,
yaml,
validate,
};
validate
}
} catch (e) {
console.error("Error parsing build logs");
console.error(e.message);
return;
console.error('Error parsing build logs')
console.error(e.message)
return
}

// path to run build from
const localPath = path.join(process.cwd(), options.dir);
const localPath = path.join(process.cwd(), options.dir)

// load markdown and files
let _markdown: string;
let _yaml: string;
let _markdown: string
let _yaml: string
try {
[_markdown, _yaml] = await Promise.all([
read(path.join(localPath, options.markdown), "utf8"),
read(path.join(localPath, options.yaml), "utf8"),
]);
;[_markdown, _yaml] = await Promise.all([
read(path.join(localPath, options.markdown), 'utf8'),
read(path.join(localPath, options.yaml), 'utf8')
])
} catch (e) {
console.error("Error reading file:");
console.error(e.message);
return;
console.error('Error reading file:')
console.error(e.message)
return
}

// validate markdown loosely
try {
const isValid = validateMarkdown(_markdown);
const isValid = validateMarkdown(_markdown)
if (!isValid) {
console.warn("Invalid markdown");
console.warn('Invalid markdown')
}
} catch (e) {
console.error("Error validating markdown:");
console.error(e.message);
return;
console.error('Error validating markdown:')
console.error(e.message)
return
}

// parse yaml skeleton config
let skeleton;
let skeleton
try {
skeleton = yamlParser.load(_yaml);
skeleton = yamlParser.load(_yaml)
if (!skeleton || !Object.keys(skeleton).length) {
throw new Error(`Skeleton at "${options.yaml}" is invalid`);
throw new Error(`Skeleton at "${options.yaml}" is invalid`)
}
} catch (e) {
console.error("Error parsing yaml");
console.error(e.message);
return;
console.error('Error parsing yaml')
console.error(e.message)
return
}

// validate skeleton based on skeleton json schema
try {
const valid = validateSchema(skeletonSchema, skeleton);
const valid = validateSchema(skeletonSchema, skeleton)
if (!valid) {
console.error("Skeleton validation failed. See above to see what to fix");
return;
console.error('Skeleton validation failed. See above to see what to fix')
return
}
} catch (e) {
console.error("Error validating tutorial schema:");
console.error(e.message);
console.error('Error validating tutorial schema:')
console.error(e.message)
}

// load git commits to use in parse step
let commits: CommitLogObject;
let commits: CommitLogObject
try {
commits = await getCommits({
localDir: localPath,
codeBranch: skeleton.config.repo.branch,
});
codeBranch: skeleton.config.repo.branch
})
} catch (e) {
console.error("Error loading commits:");
console.error(e.message);
return;
console.error('Error loading commits:')
console.error(e.message)
return
}

// parse tutorial from markdown and yaml
let tutorial: T.Tutorial;
let tutorial: T.Tutorial
try {
tutorial = await parse({
text: _markdown,
skeleton,
commits,
});
commits
})
} catch (e) {
console.error("Error parsing tutorial:");
console.error(e.message);
return;
console.error('Error parsing tutorial:')
console.error(e.message)
return
}

// validate tutorial based on tutorial json schema
try {
if (options.validate) {
const valid = validateSchema(tutorialSchema, tutorial);
const valid = validateSchema(tutorialSchema, tutorial)
if (!valid) {
console.error(
"Tutorial validation failed. See above to see what to fix"
);
'Tutorial validation failed. See above to see what to fix'
)
// continue rather than exiting early
}
}
} catch (e) {
console.error("Error validating tutorial schema:");
console.error(e.message);
console.error('Error validating tutorial schema:')
console.error(e.message)
}

// write tutorial
if (tutorial) {
try {
await write(options.output, JSON.stringify(tutorial, null, 2), "utf8");
console.info(`Success! See output at ${options.output}`);
await write(options.output, JSON.stringify(tutorial, null, 2), 'utf8')
console.info(`Success! See output at ${options.output}`)
} catch (e) {
console.error("Error writing tutorial json file:");
console.error(e.message);
console.error('Error writing tutorial json file:')
console.error(e.message)
}
}
}

export default build;
export default build
68 changes: 34 additions & 34 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import "./utils/logs";
import build from "./build";
import create from "./create";
import validate from "./validate";
import * as help from "./help";
import './utils/logs'
import build from './build'
import create from './create'
import validate from './validate'
import * as help from './help'

export async function cli(rawArgs: string[]): Promise<void> {
const command: string = rawArgs[2];
const args = rawArgs.slice(3);
export async function cli (rawArgs: string[]): Promise<void> {
const command: string = rawArgs[2]
const args = rawArgs.slice(3)

switch (command) {
case "--version":
case "-v":
const version = require("../package.json").version;
console.log(`v${version}`);
return;
case '--version':
case '-v':
const version = require('../package.json').version
console.log(`v${version}`)
return

case "build":
if (args.length && ["--help", "-h"].includes(args[0])) {
help.build();
return;
case 'build':
if (args.length && ['--help', '-h'].includes(args[0])) {
help.build()
return
}
build(args);
break;
build(args)
break

case "create":
if (args.length && ["--help", "-h"].includes(args[0])) {
help.create();
return;
case 'create':
if (args.length && ['--help', '-h'].includes(args[0])) {
help.create()
return
}
create(args);
break;
create(args)
break

case "validate":
if (args.length && ["--help", "-h"].includes(args[0])) {
help.validate();
return;
case 'validate':
if (args.length && ['--help', '-h'].includes(args[0])) {
help.validate()
return
}
validate(args);
break;
validate(args)
break

case "--help":
case "-h":
case '--help':
case '-h':
default:
help.main();
help.main()
}
}
Loading