microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/settingsHelper.ts
83lines · modeblame
3d1881c7Joshua 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"; | |
| 5 | import * as vscode from "vscode"; | |
| 6 | import fs = require("fs"); | |
| 7 | import path = require("path"); | |
| 8 | import {FileSystem} from "../common/node/fileSystem"; | |
| 9 | | |
| 10 | export class SettingsHelper { | |
| 11 | | |
| 12 | public static get settingsJsonPath(): string { | |
| 13 | return path.join(vscode.workspace.rootPath, ".vscode", "settings.json"); | |
| 14 | } | |
| 15 | | |
acf08bc2dlebu10 years ago | 16 | public static get launchJsonPath(): string { |
| 17 | return path.join(vscode.workspace.rootPath, ".vscode", "launch.json"); | |
| 18 | } | |
| 19 | | |
3d1881c7Joshua Skelton10 years ago | 20 | /** |
| 21 | * Constructs a JSON object from tsconfig.json. Will create the file if needed. | |
| 22 | */ | |
| 23 | public static readSettingsJson(): Q.Promise<any> { | |
| 24 | let settingsJsonPath: string = SettingsHelper.settingsJsonPath; | |
| 25 | let fileSystem = new FileSystem(); | |
| 26 | | |
| 27 | return fileSystem.exists(settingsJsonPath) | |
acf08bc2dlebu10 years ago | 28 | .then(function(exists: boolean): Q.Promise<string> { |
| 29 | if (!exists) { | |
| 30 | return fileSystem.writeFile(settingsJsonPath, "{}") | |
| 31 | .then(() => { return "{}"; }); | |
| 32 | } | |
cdbc4389Joshua Skelton10 years ago | 33 | |
acf08bc2dlebu10 years ago | 34 | return fileSystem.readFile(settingsJsonPath, "utf-8"); |
| 35 | }) | |
| 36 | .then(function(jsonContents: string): Q.Promise<any> { | |
| 37 | return JSON.parse(jsonContents); | |
| 38 | }); | |
3d1881c7Joshua Skelton10 years ago | 39 | } |
| 40 | | |
| 41 | /** | |
| 42 | * Writes out a JSON configuration object to the tsconfig.json file. | |
| 43 | */ | |
| 44 | public static writeSettingsJson(settingsJson: any): Q.Promise<void> { | |
| 45 | let settingsJsonPath: string = SettingsHelper.settingsJsonPath; | |
| 46 | | |
| 47 | return Q.nfcall<void>(fs.writeFile, settingsJsonPath, JSON.stringify(settingsJson, null, 4)); | |
| 48 | } | |
| 49 | | |
| 50 | /** | |
| 51 | * Enable javascript intellisense via typescript. | |
| 52 | */ | |
8a9925eeEric Hamilton10 years ago | 53 | public static setTypeScriptTsdk(path: string): Q.Promise<void> { |
3d1881c7Joshua Skelton10 years ago | 54 | return SettingsHelper.readSettingsJson() |
5e53b45aJoshua Skelton10 years ago | 55 | .then(function(settingsJson: any): Q.Promise<void> { |
| 56 | if (settingsJson["typescript.tsdk"] !== path) { | |
| 57 | settingsJson["typescript.tsdk"] = path; | |
3d1881c7Joshua Skelton10 years ago | 58 | |
5e53b45aJoshua Skelton10 years ago | 59 | return SettingsHelper.writeSettingsJson(settingsJson); |
| 60 | } | |
| 61 | }); | |
3d1881c7Joshua Skelton10 years ago | 62 | } |
bed90dbdJoshua Skelton10 years ago | 63 | |
8a9925eeEric Hamilton10 years ago | 64 | /** |
| 65 | * Removes javascript intellisense via typescript. | |
| 66 | */ | |
| 67 | public static removeTypeScriptTsdk(): Q.Promise<void> { | |
| 68 | return SettingsHelper.readSettingsJson() | |
| 69 | .then(function(settingsJson: any): Q.Promise<void> { | |
| 70 | if (settingsJson["typescript.tsdk"] !== undefined) { | |
| 71 | delete settingsJson["typescript.tsdk"]; | |
| 72 | return SettingsHelper.writeSettingsJson(settingsJson); | |
| 73 | } | |
| 74 | }); | |
| 75 | } | |
| 76 | | |
| 77 | public static getTypeScriptTsdk(): Q.Promise<string> { | |
bed90dbdJoshua Skelton10 years ago | 78 | return SettingsHelper.readSettingsJson() |
| 79 | .then(function(settingsJson: any): Q.Promise<string> { | |
8a9925eeEric Hamilton10 years ago | 80 | return settingsJson["typescript.tsdk"] || null; |
bed90dbdJoshua Skelton10 years ago | 81 | }); |
| 82 | } | |
3d1881c7Joshua Skelton10 years ago | 83 | } |