-
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.
Add better logging and error handling
- Loading branch information
1 parent
2d270c1
commit 57de628
Showing
7 changed files
with
76 additions
and
29 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
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
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
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,10 @@ | ||
export const logger = { | ||
info: (message: string) => { | ||
const now = new Date() | ||
console.log(`${now.toLocaleTimeString()} ${message}`) | ||
}, | ||
error: (message: string) => { | ||
const now = new Date() | ||
console.error(`${now.toLocaleTimeString()} ${message}`) | ||
} | ||
} |
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
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,14 +1,34 @@ | ||
import { ModuleKind, ScriptTarget, transpileModule } from "typescript" | ||
import { ModuleKind, ScriptTarget, transpileModule, flattenDiagnosticMessageText } from "typescript" | ||
import { VtsFile } from "../types/file" | ||
import { logger } from "../log" | ||
|
||
export const transpileFromTypeScript = (source: string): string => { | ||
const result = transpileModule(source, { | ||
export const transpileFromTypeScript = (file: VtsFile): { output: string; success: boolean } => { | ||
const result = transpileModule(file.content, { | ||
compilerOptions: { | ||
module: ModuleKind.ESNext, | ||
target: ScriptTarget.ES2015, | ||
removeComments: true, | ||
esModuleInterop: false, | ||
allowSyntheticDefaultImports: false | ||
} | ||
}, | ||
reportDiagnostics: true | ||
}) | ||
return result.outputText | ||
|
||
if (result.diagnostics && result.diagnostics.length > 0) { | ||
result.diagnostics.forEach(diagnostic => { | ||
const message = flattenDiagnosticMessageText(diagnostic.messageText, "\n") | ||
const now = new Date() | ||
if (diagnostic.file && diagnostic.start !== undefined) { | ||
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start) | ||
logger.error(`Error in ${file.path} (${line + 1},${character + 1}): ${message}`) | ||
} else { | ||
logger.error(`Error in ${file.path}: ${message}`) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
output: result.outputText, | ||
success: !result.diagnostics || result.diagnostics.length === 0 | ||
} | ||
} |
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