microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/settingsHelper.ts
66lines · 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 | |
| 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 | |
| 16 | /** |
| 17 | * Constructs a JSON object from tsconfig.json. Will create the file if needed. |
| 18 | */ |
| 19 | public static readSettingsJson(): Q.Promise<any> { |
| 20 | let settingsJsonPath: string = SettingsHelper.settingsJsonPath; |
| 21 | let fileSystem = new FileSystem(); |
| 22 | |
| 23 | return fileSystem.exists(settingsJsonPath) |
| 24 | .then(function(exists: boolean): Q.Promise<void> { |
| 25 | if (!exists) { |
| 26 | return fileSystem.writeFile(settingsJsonPath, "{}"); |
| 27 | } |
| 28 | }) |
| 29 | .then(function(): Q.Promise<string> { |
| 30 | return fileSystem.readFile(settingsJsonPath, "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 writeSettingsJson(settingsJson: any): Q.Promise<void> { |
| 41 | let settingsJsonPath: string = SettingsHelper.settingsJsonPath; |
| 42 | |
| 43 | return Q.nfcall<void>(fs.writeFile, settingsJsonPath, JSON.stringify(settingsJson, null, 4)); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Enable javascript intellisense via typescript. |
| 48 | */ |
| 49 | public static typescriptTsdk(path: string): Q.Promise<void> { |
| 50 | return SettingsHelper.readSettingsJson() |
| 51 | .then(function(settingsJson: any): Q.Promise<void> { |
| 52 | if (settingsJson["typescript.tsdk"] !== path) { |
| 53 | settingsJson["typescript.tsdk"] = path; |
| 54 | |
| 55 | return SettingsHelper.writeSettingsJson(settingsJson); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | public static getTypescriptTsdk(): Q.Promise<string> { |
| 61 | return SettingsHelper.readSettingsJson() |
| 62 | .then(function(settingsJson: any): Q.Promise<string> { |
| 63 | return settingsJson["typescript.tsdk"] || ""; |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | |