microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/tsconfigHelper.ts

86lines · modeblame

d24fea86Joshua Skelton10 years ago1// 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";
d8231ba2Joshua Skelton10 years ago5import * as vscode from "vscode";
d24fea86Joshua Skelton10 years ago6import fs = require("fs");
7import path = require("path");
b0061ac6Meena Kunnathur Balakrishnan10 years ago8import {FileSystem} from "../common/node/fileSystem";
d24fea86Joshua Skelton10 years ago9
10export class TsConfigHelper {
11
12public static get tsConfigPath(): string {
13return 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*/
2b1090f9Joshua Skelton10 years ago19public static readConfigJson(): Q.Promise<any> {
d8231ba2Joshua Skelton10 years ago20let tsConfigPath: string = TsConfigHelper.tsConfigPath;
21let fileSystem = new FileSystem();
d24fea86Joshua Skelton10 years ago22
2b1090f9Joshua Skelton10 years ago23return fileSystem.exists(tsConfigPath)
b031edc7digeff10 years ago24.then(function(exists: boolean): Q.Promise<void> {
25if (!exists) {
26return fileSystem.writeFile(tsConfigPath, "{}");
27}
28})
29.then(function(): Q.Promise<string> {
30return fileSystem.readFile(tsConfigPath, "utf-8");
31})
32.then(function(jsonContents: string): Q.Promise<any> {
33return JSON.parse(jsonContents);
34});
d24fea86Joshua Skelton10 years ago35}
36
37/**
38* Writes out a JSON configuration object to the tsconfig.json file.
39*/
d8231ba2Joshua Skelton10 years ago40public static writeConfigJson(configJson: any): Q.Promise<void> {
41let tsConfigPath: string = TsConfigHelper.tsConfigPath;
2b1090f9Joshua Skelton10 years ago42
43return Q.nfcall<void>(fs.writeFile, tsConfigPath, JSON.stringify(configJson, null, 4));
d24fea86Joshua Skelton10 years ago44}
45
46/**
47* Enable javascript intellisense via typescript.
48*/
d8231ba2Joshua Skelton10 years ago49public static allowJs(enabled: boolean): Q.Promise<void> {
2b1090f9Joshua Skelton10 years ago50return TsConfigHelper.readConfigJson()
b031edc7digeff10 years ago51.then(function(tsConfigJson: any): Q.Promise<void> {
52tsConfigJson.compilerOptions = tsConfigJson.compilerOptions || {};
b4683d60Joshua Skelton10 years ago53
d10cf2b6Joshua Skelton10 years ago54if (!tsConfigJson.compilerOptions.hasOwnProperty("allowJs")) {
6e35ee57Joshua Skelton10 years ago55tsConfigJson.compilerOptions.allowJs = enabled;
56return TsConfigHelper.writeConfigJson(tsConfigJson);
b031edc7digeff10 years ago57}
b4683d60Joshua Skelton10 years ago58
6e35ee57Joshua Skelton10 years ago59return Q.resolve<void>(void 0);
b031edc7digeff10 years ago60});
d24fea86Joshua Skelton10 years ago61}
17f4855fJoshua Skelton10 years ago62
63/**
64* Add directories to be excluded by the Typescript compiler.
65*/
66public static addExcludePaths(excludePaths: string[]): Q.Promise<void> {
67return TsConfigHelper.readConfigJson()
b031edc7digeff10 years ago68.then(function(tsConfigJson: any) {
69let currentExcludes: string[] = tsConfigJson.exclude || [];
70let isDirty: boolean = false;
17f4855fJoshua Skelton10 years ago71
b031edc7digeff10 years ago72excludePaths.forEach(function(exclude: string) {
73if (currentExcludes.indexOf(exclude) < 0) {
74currentExcludes.push(exclude);
75isDirty = true;
76}
77});
17f4855fJoshua Skelton10 years ago78
b031edc7digeff10 years ago79if (isDirty) {
80tsConfigJson.exclude = currentExcludes;
17f4855fJoshua Skelton10 years ago81
b031edc7digeff10 years ago82return TsConfigHelper.writeConfigJson(tsConfigJson);
83}
84});
17f4855fJoshua Skelton10 years ago85}
d24fea86Joshua Skelton10 years ago86}