- Notifications
You must be signed in to change notification settings - Fork2
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
lint
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit869c60459ab71e856d0139470bce41e79ba00703
There are no files selected for viewing
172 changes: 86 additions & 86 deletionssrc/build.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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) | ||
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 | ||
} | ||
type BuildArgs = { | ||
dir: string | ||
markdown: string | ||
yaml: string | ||
output: string | ||
validate: boolean | ||
} | ||
async function build(args: string[]) { | ||
let options: BuildArgs | ||
try { | ||
// dir - default . | ||
const dir = !args.length || args[0].match(/^-/) ?'.' : args[0] | ||
// -m --markdown - default TUTORIAL.md | ||
const markdown = | ||
getArg(args, { name:'markdown', alias:'m' }) ||'TUTORIAL.md' | ||
// -y --yaml - default coderoad-config.yml | ||
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' | ||
console.log(`Building CodeRoad ${output}...`) | ||
options = { | ||
dir, | ||
output, | ||
markdown, | ||
yaml, | ||
validate | ||
} | ||
} catch (e) { | ||
console.error('Error parsing build logs') | ||
console.error(e.message) | ||
return | ||
} | ||
// path to run build from | ||
const localPath = path.join(process.cwd(), options.dir) | ||
// load markdown and files | ||
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') | ||
]) | ||
} catch (e) { | ||
console.error('Error reading file:') | ||
console.error(e.message) | ||
return | ||
} | ||
// validate markdown loosely | ||
try { | ||
const isValid = validateMarkdown(_markdown) | ||
if (!isValid) { | ||
console.warn('Invalid markdown') | ||
} | ||
} catch (e) { | ||
console.error('Error validating markdown:') | ||
console.error(e.message) | ||
return | ||
} | ||
// parse yaml skeleton config | ||
let skeleton | ||
try { | ||
skeleton = yamlParser.load(_yaml) | ||
if (!skeleton || !Object.keys(skeleton).length) { | ||
throw new Error(`Skeleton at "${options.yaml}" is invalid`) | ||
} | ||
} catch (e) { | ||
console.error('Error parsing yaml') | ||
console.error(e.message) | ||
return | ||
} | ||
// validate skeleton based on skeleton json schema | ||
try { | ||
const valid = validateSchema(skeletonSchema, skeleton) | ||
if (!valid) { | ||
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) | ||
} | ||
// load git commits to use in parse step | ||
let commits: CommitLogObject | ||
try { | ||
commits = await getCommits({ | ||
localDir: localPath, | ||
codeBranch: skeleton.config.repo.branch | ||
}) | ||
} catch (e) { | ||
console.error('Error loading commits:') | ||
console.error(e.message) | ||
return | ||
} | ||
// parse tutorial from markdown and yaml | ||
let tutorial: T.Tutorial | ||
try { | ||
tutorial = await parse({ | ||
text: _markdown, | ||
skeleton, | ||
commits | ||
}) | ||
} catch (e) { | ||
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) | ||
if (!valid) { | ||
console.error( | ||
'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) | ||
} | ||
// write tutorial | ||
if (tutorial) { | ||
try { | ||
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) | ||
} | ||
} | ||
} | ||
export default build |
68 changes: 34 additions & 34 deletionssrc/cli.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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' | ||
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'build': | ||
if (args.length && ['--help', '-h'].includes(args[0])) { | ||
help.build() | ||
return | ||
} | ||
build(args) | ||
break | ||
case'create': | ||
if (args.length && ['--help', '-h'].includes(args[0])) { | ||
help.create() | ||
return | ||
} | ||
create(args) | ||
break | ||
case'validate': | ||
if (args.length && ['--help', '-h'].includes(args[0])) { | ||
help.validate() | ||
return | ||
} | ||
validate(args) | ||
break | ||
case'--help': | ||
case'-h': | ||
default: | ||
help.main() | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.