microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/tsconfigHelper.ts
86lines · modeblame
d24fea86Joshua Skelton10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as Q from "q"; | |
d8231ba2Joshua Skelton10 years ago | 5 | import * as vscode from "vscode"; |
d24fea86Joshua Skelton10 years ago | 6 | import fs = require("fs"); |
| 7 | import path = require("path"); | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 8 | import {FileSystem} from "../common/node/fileSystem"; |
d24fea86Joshua Skelton10 years ago | 9 | |
| 10 | export 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 | */ | |
2b1090f9Joshua Skelton10 years ago | 19 | public static readConfigJson(): Q.Promise<any> { |
d8231ba2Joshua Skelton10 years ago | 20 | let tsConfigPath: string = TsConfigHelper.tsConfigPath; |
| 21 | let fileSystem = new FileSystem(); | |
d24fea86Joshua Skelton10 years ago | 22 | |
2b1090f9Joshua Skelton10 years ago | 23 | return fileSystem.exists(tsConfigPath) |
b031edc7digeff10 years ago | 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 | }); | |
d24fea86Joshua Skelton10 years ago | 35 | } |
| 36 | | |
| 37 | /** | |
| 38 | * Writes out a JSON configuration object to the tsconfig.json file. | |
| 39 | */ | |
d8231ba2Joshua Skelton10 years ago | 40 | public static writeConfigJson(configJson: any): Q.Promise<void> { |
| 41 | let tsConfigPath: string = TsConfigHelper.tsConfigPath; | |
2b1090f9Joshua Skelton10 years ago | 42 | |
| 43 | return Q.nfcall<void>(fs.writeFile, tsConfigPath, JSON.stringify(configJson, null, 4)); | |
d24fea86Joshua Skelton10 years ago | 44 | } |
| 45 | | |
| 46 | /** | |
| 47 | * Enable javascript intellisense via typescript. | |
| 48 | */ | |
d8231ba2Joshua Skelton10 years ago | 49 | public static allowJs(enabled: boolean): Q.Promise<void> { |
2b1090f9Joshua Skelton10 years ago | 50 | return TsConfigHelper.readConfigJson() |
b031edc7digeff10 years ago | 51 | .then(function(tsConfigJson: any): Q.Promise<void> { |
| 52 | tsConfigJson.compilerOptions = tsConfigJson.compilerOptions || {}; | |
b4683d60Joshua Skelton10 years ago | 53 | |
d10cf2b6Joshua Skelton10 years ago | 54 | if (!tsConfigJson.compilerOptions.hasOwnProperty("allowJs")) { |
6e35ee57Joshua Skelton10 years ago | 55 | tsConfigJson.compilerOptions.allowJs = enabled; |
| 56 | return TsConfigHelper.writeConfigJson(tsConfigJson); | |
b031edc7digeff10 years ago | 57 | } |
b4683d60Joshua Skelton10 years ago | 58 | |
6e35ee57Joshua Skelton10 years ago | 59 | return Q.resolve<void>(void 0); |
b031edc7digeff10 years ago | 60 | }); |
d24fea86Joshua Skelton10 years ago | 61 | } |
17f4855fJoshua Skelton10 years ago | 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() | |
b031edc7digeff10 years ago | 68 | .then(function(tsConfigJson: any) { |
| 69 | let currentExcludes: string[] = tsConfigJson.exclude || []; | |
| 70 | let isDirty: boolean = false; | |
17f4855fJoshua Skelton10 years ago | 71 | |
b031edc7digeff10 years ago | 72 | excludePaths.forEach(function(exclude: string) { |
| 73 | if (currentExcludes.indexOf(exclude) < 0) { | |
| 74 | currentExcludes.push(exclude); | |
| 75 | isDirty = true; | |
| 76 | } | |
| 77 | }); | |
17f4855fJoshua Skelton10 years ago | 78 | |
b031edc7digeff10 years ago | 79 | if (isDirty) { |
| 80 | tsConfigJson.exclude = currentExcludes; | |
17f4855fJoshua Skelton10 years ago | 81 | |
b031edc7digeff10 years ago | 82 | return TsConfigHelper.writeConfigJson(tsConfigJson); |
| 83 | } | |
| 84 | }); | |
17f4855fJoshua Skelton10 years ago | 85 | } |
d24fea86Joshua Skelton10 years ago | 86 | } |