microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bb77358c8dc7ea46fae9d6aa601a11fde8eed0fd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/tsconfigHelper.ts

86lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import * as vscode from "vscode";
6import fs = require("fs");
7import path = require("path");
8import {FileSystem} from "../common/node/fileSystem";
9
10export class TsConfigHelper {
11
12 public static get tsConfigPath(): string {
13 return path.join(vscode.workspace.rootPath, "tsconfig.json");
14 }
15
16 /**
17 * Constructs a JSON object from tsconfig.json. Will create the file if needed.
18 */
19 public static readConfigJson(): Q.Promise<any> {
20 let tsConfigPath: string = TsConfigHelper.tsConfigPath;
21 let fileSystem = new FileSystem();
22
23 return fileSystem.exists(tsConfigPath)
24 .then(function(exists: boolean): Q.Promise<void> {
25 if (!exists) {
26 return fileSystem.writeFile(tsConfigPath, "{}");
27 }
28 })
29 .then(function(): Q.Promise<string> {
30 return fileSystem.readFile(tsConfigPath, "utf-8");
31 })
32 .then(function(jsonContents: string): Q.Promise<any> {
33 return JSON.parse(jsonContents);
34 });
35 }
36
37 /**
38 * Writes out a JSON configuration object to the tsconfig.json file.
39 */
40 public static writeConfigJson(configJson: any): Q.Promise<void> {
41 let tsConfigPath: string = TsConfigHelper.tsConfigPath;
42
43 return Q.nfcall<void>(fs.writeFile, tsConfigPath, JSON.stringify(configJson, null, 4));
44 }
45
46 /**
47 * Enable javascript intellisense via typescript.
48 */
49 public static allowJs(enabled: boolean): Q.Promise<void> {
50 return TsConfigHelper.readConfigJson()
51 .then(function(tsConfigJson: any): Q.Promise<void> {
52 tsConfigJson.compilerOptions = tsConfigJson.compilerOptions || {};
53
54 if (!tsConfigJson.compilerOptions.hasOwnProperty("allowJs")) {
55 tsConfigJson.compilerOptions.allowJs = enabled;
56 return TsConfigHelper.writeConfigJson(tsConfigJson);
57 }
58
59 return Q.resolve<void>(void 0);
60 });
61 }
62
63 /**
64 * Add directories to be excluded by the Typescript compiler.
65 */
66 public static addExcludePaths(excludePaths: string[]): Q.Promise<void> {
67 return TsConfigHelper.readConfigJson()
68 .then(function(tsConfigJson: any) {
69 let currentExcludes: string[] = tsConfigJson.exclude || [];
70 let isDirty: boolean = false;
71
72 excludePaths.forEach(function(exclude: string) {
73 if (currentExcludes.indexOf(exclude) < 0) {
74 currentExcludes.push(exclude);
75 isDirty = true;
76 }
77 });
78
79 if (isDirty) {
80 tsConfigJson.exclude = currentExcludes;
81
82 return TsConfigHelper.writeConfigJson(tsConfigJson);
83 }
84 });
85 }
86}
87