microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/settingsHelper.ts
66lines · 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 | | |
| 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) | |
cdbc4389Joshua Skelton10 years ago | 24 | .then(function(exists: boolean): Q.Promise<string> { |
3d1881c7Joshua Skelton10 years ago | 25 | if (!exists) { |
cdbc4389Joshua Skelton10 years ago | 26 | return fileSystem.writeFile(settingsJsonPath, "{}") |
8c2b3bebJoshua Skelton10 years ago | 27 | .then(() => { return "{}"; }); |
3d1881c7Joshua Skelton10 years ago | 28 | } |
cdbc4389Joshua Skelton10 years ago | 29 | |
3d1881c7Joshua Skelton10 years ago | 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() | |
5e53b45aJoshua Skelton10 years ago | 51 | .then(function(settingsJson: any): Q.Promise<void> { |
| 52 | if (settingsJson["typescript.tsdk"] !== path) { | |
| 53 | settingsJson["typescript.tsdk"] = path; | |
3d1881c7Joshua Skelton10 years ago | 54 | |
5e53b45aJoshua Skelton10 years ago | 55 | return SettingsHelper.writeSettingsJson(settingsJson); |
| 56 | } | |
| 57 | }); | |
3d1881c7Joshua Skelton10 years ago | 58 | } |
bed90dbdJoshua Skelton10 years ago | 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 | } | |
3d1881c7Joshua Skelton10 years ago | 66 | } |