1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var ts = require("typescript");
|
4 | var fs = require("fs");
|
5 | var path = require("path");
|
6 | var TSCONFIG_CACHE = {};
|
7 | function createDefaultFormatCodeSettings() {
|
8 | return {
|
9 | baseIndentSize: 0,
|
10 | indentSize: 4,
|
11 | tabSize: 4,
|
12 | indentStyle: ts.IndentStyle.Smart,
|
13 | newLineCharacter: "\r\n",
|
14 | convertTabsToSpaces: true,
|
15 | insertSpaceAfterCommaDelimiter: true,
|
16 | insertSpaceAfterSemicolonInForStatements: true,
|
17 | insertSpaceBeforeAndAfterBinaryOperators: true,
|
18 | insertSpaceAfterConstructor: false,
|
19 | insertSpaceAfterKeywordsInControlFlowStatements: true,
|
20 | insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
21 | insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
22 | insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
23 | insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
|
24 | insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
|
25 | insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
|
26 | insertSpaceAfterTypeAssertion: false,
|
27 | insertSpaceBeforeFunctionParenthesis: false,
|
28 | placeOpenBraceOnNewLineForFunctions: false,
|
29 | placeOpenBraceOnNewLineForControlBlocks: false,
|
30 | };
|
31 | }
|
32 | exports.createDefaultFormatCodeSettings = createDefaultFormatCodeSettings;
|
33 | function getConfigFileName(baseDir, configFileName) {
|
34 | var configFilePath = path.resolve(baseDir, configFileName);
|
35 | if (fs.existsSync(configFilePath)) {
|
36 | return configFilePath;
|
37 | }
|
38 | if (baseDir.length === path.dirname(baseDir).length) {
|
39 | return null;
|
40 | }
|
41 | return getConfigFileName(path.resolve(baseDir, "../"), configFileName);
|
42 | }
|
43 | exports.getConfigFileName = getConfigFileName;
|
44 | function readFilesFromTsconfig(configPath) {
|
45 | return readTsconfig(configPath).fileNames;
|
46 | }
|
47 | exports.readFilesFromTsconfig = readFilesFromTsconfig;
|
48 | function readTsconfig(configPath) {
|
49 | if (TSCONFIG_CACHE[configPath]) {
|
50 | return TSCONFIG_CACHE[configPath];
|
51 | }
|
52 |
|
53 |
|
54 | var host = {
|
55 | useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
|
56 | readDirectory: ts.sys.readDirectory,
|
57 | fileExists: function (path) { return fs.existsSync(path); },
|
58 | readFile: function (path) { return fs.readFileSync(path, "utf-8"); },
|
59 | };
|
60 | var rootConfig = parseJSON(fs.readFileSync(configPath, "utf-8"));
|
61 | var parsed = ts.parseJsonConfigFileContent(rootConfig, host, path.dirname(configPath));
|
62 | if (parsed.errors && parsed.errors.length !== 0) {
|
63 | throw new Error(parsed.errors.map(function (e) { return e.messageText; }).join("\n"));
|
64 | }
|
65 | TSCONFIG_CACHE[configPath] = parsed;
|
66 | return parsed;
|
67 | }
|
68 | exports.readTsconfig = readTsconfig;
|
69 | function parseJSON(jsonText) {
|
70 | var result = ts.parseConfigFileTextToJson("tmp.json", jsonText);
|
71 | if (result.error) {
|
72 | throw new Error("JSON parse error");
|
73 | }
|
74 | return result.config;
|
75 | }
|
76 | exports.parseJSON = parseJSON;
|
77 |
|
\ | No newline at end of file |