1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var fs = require("fs-extra");
|
4 | var path = require("path");
|
5 | var ts = require("typescript");
|
6 | var _ = require("lodash");
|
7 | var logger_1 = require("./logger");
|
8 | var utils_1 = require("./utils/utils");
|
9 | var carriageReturnLineFeed = '\r\n';
|
10 | var lineFeed = '\n';
|
11 | function cleanNameWithoutSpaceAndToLowerCase(name) {
|
12 | return name.toLowerCase().replace(/ /g, '-');
|
13 | }
|
14 | exports.cleanNameWithoutSpaceAndToLowerCase = cleanNameWithoutSpaceAndToLowerCase;
|
15 | function detectIndent(str, count, indent) {
|
16 | var stripIndent = function (str) {
|
17 | var match = str.match(/^[ \t]*(?=\S)/gm);
|
18 | if (!match) {
|
19 | return str;
|
20 | }
|
21 |
|
22 | var indent = Math.min.apply(Math, match.map(function (x) { return x.length; }));
|
23 | var re = new RegExp("^[ \\t]{" + indent + "}", 'gm');
|
24 | return indent > 0 ? str.replace(re, '') : str;
|
25 | };
|
26 | var repeating = function (n, str) {
|
27 | str = str === undefined ? ' ' : str;
|
28 | if (typeof str !== 'string') {
|
29 | throw new TypeError("Expected `input` to be a `string`, got `" + typeof str + "`");
|
30 | }
|
31 | if (n < 0) {
|
32 | throw new TypeError("Expected `count` to be a positive finite number, got `" + n + "`");
|
33 | }
|
34 | var ret = '';
|
35 | do {
|
36 | if (n & 1) {
|
37 | ret += str;
|
38 | }
|
39 | str += str;
|
40 | } while ((n >>= 1));
|
41 | return ret;
|
42 | };
|
43 | var indentString = function (str, count, indent) {
|
44 | indent = indent === undefined ? ' ' : indent;
|
45 | count = count === undefined ? 1 : count;
|
46 | if (typeof str !== 'string') {
|
47 | throw new TypeError("Expected `input` to be a `string`, got `" + typeof str + "`");
|
48 | }
|
49 | if (typeof count !== 'number') {
|
50 | throw new TypeError("Expected `count` to be a `number`, got `" + typeof count + "`");
|
51 | }
|
52 | if (typeof indent !== 'string') {
|
53 | throw new TypeError("Expected `indent` to be a `string`, got `" + typeof indent + "`");
|
54 | }
|
55 | if (count === 0) {
|
56 | return str;
|
57 | }
|
58 | indent = count > 1 ? repeating(count, indent) : indent;
|
59 | return str.replace(/^(?!\s*$)/mg, indent);
|
60 | };
|
61 | return indentString(stripIndent(str), count || 0, indent);
|
62 | }
|
63 | exports.detectIndent = detectIndent;
|
64 |
|
65 | function compilerHost(transpileOptions) {
|
66 | var inputFileName = transpileOptions.fileName || (transpileOptions.jsx ? 'module.tsx' : 'module.ts');
|
67 | var toReturn = {
|
68 | getSourceFile: function (fileName) {
|
69 | if (fileName.lastIndexOf('.ts') !== -1) {
|
70 | if (fileName === 'lib.d.ts') {
|
71 | return undefined;
|
72 | }
|
73 | if (fileName.substr(-5) === '.d.ts') {
|
74 | return undefined;
|
75 | }
|
76 | if (path.isAbsolute(fileName) === false) {
|
77 | fileName = path.join(transpileOptions.tsconfigDirectory, fileName);
|
78 | }
|
79 | if (!fs.existsSync(fileName)) {
|
80 | return undefined;
|
81 | }
|
82 | var libSource = '';
|
83 | try {
|
84 | libSource = fs.readFileSync(fileName).toString();
|
85 | if (utils_1.hasBom(libSource)) {
|
86 | libSource = utils_1.stripBom(libSource);
|
87 | }
|
88 | }
|
89 | catch (e) {
|
90 | logger_1.logger.debug(e, fileName);
|
91 | }
|
92 | return ts.createSourceFile(fileName, libSource, transpileOptions.target, false);
|
93 | }
|
94 | return undefined;
|
95 | },
|
96 | writeFile: function (name, text) { },
|
97 | getDefaultLibFileName: function () { return 'lib.d.ts'; },
|
98 | useCaseSensitiveFileNames: function () { return false; },
|
99 | getCanonicalFileName: function (fileName) { return fileName; },
|
100 | getCurrentDirectory: function () { return ''; },
|
101 | getNewLine: function () { return '\n'; },
|
102 | fileExists: function (fileName) { return fileName === inputFileName; },
|
103 | readFile: function () { return ''; },
|
104 | directoryExists: function () { return true; },
|
105 | getDirectories: function () { return []; }
|
106 | };
|
107 | return toReturn;
|
108 | }
|
109 | exports.compilerHost = compilerHost;
|
110 | function findMainSourceFolder(files) {
|
111 | var mainFolder = '';
|
112 | var mainFolderCount = 0;
|
113 | var rawFolders = files.map(function (filepath) {
|
114 | var shortPath = filepath.replace(process.cwd() + path.sep, '');
|
115 | return path.dirname(shortPath);
|
116 | });
|
117 | var folders = {};
|
118 | rawFolders = _.uniq(rawFolders);
|
119 | for (var i = 0; i < rawFolders.length; i++) {
|
120 | var sep = rawFolders[i].split(path.sep);
|
121 | sep.map(function (folder) {
|
122 | if (folders[folder]) {
|
123 | folders[folder] += 1;
|
124 | }
|
125 | else {
|
126 | folders[folder] = 1;
|
127 | }
|
128 | });
|
129 | }
|
130 | for (var f in folders) {
|
131 | if (folders[f] > mainFolderCount) {
|
132 | mainFolderCount = folders[f];
|
133 | mainFolder = f;
|
134 | }
|
135 | }
|
136 | return mainFolder;
|
137 | }
|
138 | exports.findMainSourceFolder = findMainSourceFolder;
|
139 |
|
\ | No newline at end of file |