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