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