1 | import * as fs from 'fs';
|
2 | import * as path from 'path';
|
3 | import * as ts from 'typescript';
|
4 | import { OutputFile } from './interfaces';
|
5 |
|
6 | const double = /\/\//;
|
7 | export function toUnix(fileName: string): string {
|
8 | let res: string = fileName.replace(/\\/g, '/');
|
9 | while (res.match(double)) {
|
10 | res = res.replace(double, '/');
|
11 | }
|
12 |
|
13 | return res;
|
14 | }
|
15 |
|
16 | function withoutExt(fileName: string): string {
|
17 | return path.join(path.dirname(fileName), path.basename(fileName).split('.')[0]);
|
18 | }
|
19 |
|
20 | function isFileEmit(fileName, outputFileName, sourceFileName) {
|
21 | return sourceFileName === fileName
|
22 |
|
23 | && (outputFileName.substr(-3) === '.js' || outputFileName.substr(-4) === '.jsx');
|
24 | }
|
25 |
|
26 | function isSourceMapEmit(fileName, outputFileName, sourceFileName) {
|
27 | return sourceFileName === fileName
|
28 |
|
29 | && (outputFileName.substr(-7) === '.js.map' || outputFileName.substr(-8) === '.jsx.map');
|
30 | }
|
31 |
|
32 | function isDeclarationEmit(fileName, outputFileName, sourceFileName) {
|
33 | return sourceFileName === fileName
|
34 | && (outputFileName.substr(-5) === '.d.ts');
|
35 | }
|
36 |
|
37 | export function findResultFor(fileName: string, output: ts.EmitOutput): OutputFile {
|
38 | let text;
|
39 | let sourceMap;
|
40 | let declaration: ts.OutputFile;
|
41 | fileName = withoutExt(fileName);
|
42 |
|
43 | for (let i = 0; i < output.outputFiles.length; i++) {
|
44 | let o = output.outputFiles[i];
|
45 | let outputFileName = o.name;
|
46 | let sourceFileName = withoutExt(o.name);
|
47 | if (isFileEmit(fileName, outputFileName, sourceFileName)) {
|
48 | text = o.text;
|
49 | }
|
50 | if (isSourceMapEmit(fileName, outputFileName, sourceFileName)) {
|
51 | sourceMap = o.text;
|
52 | }
|
53 | if (isDeclarationEmit(fileName, outputFileName, sourceFileName)) {
|
54 | declaration = o;
|
55 | }
|
56 | }
|
57 |
|
58 | return {
|
59 | text: text,
|
60 | sourceMap: sourceMap,
|
61 | declaration
|
62 | };
|
63 | }
|
64 |
|
65 | export function codegenErrorReport(errors) {
|
66 | return errors
|
67 | .map(function (error) {
|
68 | return 'console.error(' + JSON.stringify(error) + ');';
|
69 | })
|
70 | .join('\n');
|
71 | }
|
72 |
|
73 | export function formatError(diagnostic) {
|
74 | let lineChar;
|
75 | if (diagnostic.file) {
|
76 | lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
77 | }
|
78 | return (
|
79 | (diagnostic.file ? path.normalize(diagnostic.file.fileName) : '')
|
80 | + (lineChar ? formatLineChar(lineChar) + ' ' : '') + "\n"
|
81 | + (typeof diagnostic.messageText == "string" ?
|
82 | diagnostic.messageText :
|
83 | formatMessageChain(<ts.DiagnosticMessageChain>diagnostic.messageText))
|
84 | );
|
85 | }
|
86 |
|
87 | export function formatMessageChain(chain: ts.DiagnosticMessageChain) {
|
88 | let result = "";
|
89 | let separator = "\n ";
|
90 | let current = chain;
|
91 |
|
92 | while (current) {
|
93 | result += current.messageText;
|
94 |
|
95 | if (!!current.next) {
|
96 | result += separator;
|
97 | separator += " ";
|
98 | }
|
99 |
|
100 | current = current.next;
|
101 | }
|
102 |
|
103 | return result;
|
104 | }
|
105 |
|
106 | export function formatLineChar(lineChar) {
|
107 | return ':' + (lineChar.line + 1) + ':' + lineChar.character;
|
108 | }
|
109 |
|
110 | export function loadLib(moduleId) {
|
111 | let fileName = require.resolve(moduleId);
|
112 | let text = fs.readFileSync(fileName, 'utf8');
|
113 | return {
|
114 | fileName: fileName,
|
115 | text: text
|
116 | };
|
117 | }
|
118 |
|
119 | const TYPESCRIPT_EXTENSION = /\.(d\.)?(t|j)s$/;
|
120 |
|
121 | export function withoutTypeScriptExtension(fileName: string): string {
|
122 | return fileName.replace(TYPESCRIPT_EXTENSION, '');
|
123 | }
|