UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var ts = require("typescript");
4var fs = require("fs");
5var path = require("path");
6var TSCONFIG_CACHE = {};
7function 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}
32exports.createDefaultFormatCodeSettings = createDefaultFormatCodeSettings;
33function 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}
43exports.getConfigFileName = getConfigFileName;
44function readFilesFromTsconfig(configPath) {
45 return readTsconfig(configPath).fileNames;
46}
47exports.readFilesFromTsconfig = readFilesFromTsconfig;
48function readTsconfig(configPath) {
49 if (TSCONFIG_CACHE[configPath]) {
50 return TSCONFIG_CACHE[configPath];
51 }
52 // for `extends` support. It supported from TypeScript 2.1.1.
53 // `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
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}
68exports.readTsconfig = readTsconfig;
69function 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}
76exports.parseJSON = parseJSON;
77//# sourceMappingURL=utils.js.map
\No newline at end of file