-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): adds PublicError and generateErrors
- Loading branch information
Showing
7 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ErrorCode } from '~/types'; | ||
|
||
export default class PublicError extends Error { | ||
public id: string; | ||
public code: ErrorCode; | ||
public source?: Error; | ||
public constructor( | ||
id: string, | ||
code: ErrorCode, | ||
source?: Error | null, | ||
message?: string | ||
) { | ||
super(message); | ||
this.id = id; | ||
this.code = code; | ||
this.source = source || undefined; | ||
} | ||
public get name(): string { | ||
return 'PublicError'; | ||
} | ||
public get root(): PublicError { | ||
return this.source instanceof PublicError && this.source !== this | ||
? this.source.root | ||
: this; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs'; | ||
import { CollectionTreeApplication } from '~/types'; | ||
|
||
export async function writeErrors( | ||
dest: string, | ||
collection: CollectionTreeApplication | ||
): Promise<void> { | ||
fs.writeFileSync(dest, await generateErrors(collection)); | ||
} | ||
|
||
export async function generateErrors( | ||
collection: CollectionTreeApplication | ||
): Promise<string> { | ||
let content = | ||
'/* eslint-disable */\n' + | ||
'/* tslint:disable */\n' + | ||
'/* This file was automatically generated. DO NOT MODIFY IT BY HAND. */\n\n' + | ||
`import { PublicError } from '@karmic/core';\n\n`; | ||
|
||
const entries = Object.entries(collection.types); | ||
|
||
const names: string[] = []; | ||
for (const [name, value] of entries) { | ||
if (value.kind === 'error') { | ||
names.push(name); | ||
content += | ||
`export const ${name} = new PublicError(\n` + | ||
` '${name}',\n` + | ||
` '${value.code}',\n` + | ||
` null,\n` + | ||
` ${value.description ? `'${value.description}'` : 'undefined'}\n` + | ||
`);\n\n`; | ||
} | ||
} | ||
|
||
content += `export default {`; | ||
for (const name of names) { | ||
content += `\n ${name},`; | ||
} | ||
if (entries.length) content = content.slice(0, -1) + '\n'; | ||
|
||
return content + '};\n'; | ||
} |
This file contains 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 +1,2 @@ | ||
export * from './typings'; | ||
export * from './errors'; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs from 'fs'; | ||
import { CollectionTreeApplication } from '~/types'; | ||
import { compile } from 'json-schema-to-typescript'; | ||
import camelcase from 'camelcase'; | ||
|
||
export async function writeTypings( | ||
dest: string, | ||
collection: CollectionTreeApplication | ||
): Promise<void> { | ||
fs.writeFileSync(dest, await generateTypings(collection)); | ||
} | ||
|
||
export async function generateTypings( | ||
collection: CollectionTreeApplication | ||
): Promise<string> { | ||
let content = | ||
'/* eslint-disable */\n' + | ||
'/* tslint:disable */\n' + | ||
'/* This file was automatically generated. DO NOT MODIFY IT BY HAND. */\n\n'; | ||
|
||
for (const [key, value] of Object.entries(collection.types)) { | ||
if (value.kind !== 'error') { | ||
content += await compile( | ||
value.schema, | ||
camelcase(key, { pascalCase: true }), | ||
{ bannerComment: '' } | ||
); | ||
content += '\n'; | ||
} | ||
} | ||
|
||
return content.trim() + '\n'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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