Skip to content

Commit

Permalink
Merge pull request #90 from yarastqt/yarastqt.extends-error
Browse files Browse the repository at this point in the history
Throw custom YamlParseError while yaml parse
  • Loading branch information
yarastqt authored Oct 26, 2020
2 parents 160bb47 + 736446e commit 8cc4eb7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
30 changes: 21 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
},
"dependencies": {
"@babel/code-frame": "7.10.4",
"@oclif/command": "1.5.19",
"chalk": "4.1.0",
"chokidar": "3.4.0",
Expand All @@ -48,6 +49,7 @@
"@commitlint/cli": "9.1.2",
"@commitlint/config-conventional": "9.1.2",
"@oclif/dev-cli": "1.22.2",
"@types/babel__code-frame": "7.0.2",
"@types/fs-extra": "8.1.0",
"@types/jest": "25.2.1",
"@typescript-eslint/eslint-plugin": "2.31.0",
Expand Down
11 changes: 8 additions & 3 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ export default class Build extends Command {
}

private async build(config: any) {
console.log(`----------------- ${chalk.yellow('Build started')} -----------------`)
await build(config)
console.log(`\n---------------- ${chalk.green('Build completed')} ----------------`)
console.log(`>---------------- ${chalk.yellow('Build started')} ----------------<`)
try {
await build(config)
console.log(`\n>--------------- ${chalk.green('Build completed')} ---------------<`)
} catch (error) {
console.log(error)
console.log(`\n>---------------- ${chalk.red('Build failed')} -----------------<`)
}
}

private emitWatching() {
Expand Down
29 changes: 27 additions & 2 deletions src/core/yaml-interop.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import Module from 'module'
import { readFileSync } from 'fs'
import YAML from 'yaml'
import { codeFrameColumns } from '@babel/code-frame'

class YamlParseError extends Error {
public location: any

constructor(fileName: string, source: string, message: string, location: any) {
super('')
// Override native stack with custom message.
this.stack = new Error(
[
// Take only message without code-frame.
`${message.split(/\n/)[0]} (${fileName}:${location.start.line}:${location.start.col})`,
codeFrameColumns(source, {
start: { line: location.start.line, column: location.start.col },
}),
].join('\n'),
).stack
this.location = location
}
}

/**
* Patched native `require` for importing module with `yaml` extensions.
*/
Module.prototype.require = new Proxy(Module.prototype.require, {
apply(target, thisArg, args) {
if (/\.ya?ml$/.test(args[0])) {
const file = readFileSync(args[0], 'utf8')
try {
return YAML.parse(file)
} catch (error) {}
return YAML.parse(file, { prettyErrors: true })
} catch (error) {
throw new YamlParseError(args[0], file, error.message, error.linePos)
}
}
return Reflect.apply(target, thisArg, args)
},
Expand Down

0 comments on commit 8cc4eb7

Please sign in to comment.