microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
52bf0a75506c1b3b1cecc9ad89937e596d1bc22a

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/tsconfigHelper.ts

64lines · 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 "./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 // Return if the setting is already correctly set.
55 if (tsConfigJson.compilerOptions.allowJs === enabled) {
56 return Q.resolve<void>(void 0);
57 }
58
59 tsConfigJson.compilerOptions.allowJs = enabled;
60
61 return TsConfigHelper.writeConfigJson(tsConfigJson);
62 });
63 }
64}
65