microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a3fd5ee9abdb16e41e4f03938cc208cec580e077

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/settingsHelper.ts

83lines · 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
4import * as Q from "q";
5import * as vscode from "vscode";
6import fs = require("fs");
7import path = require("path");
8import {FileSystem} from "../common/node/fileSystem";
9
10export class SettingsHelper {
11
12 public static get settingsJsonPath(): string {
13 return path.join(vscode.workspace.rootPath, ".vscode", "settings.json");
14 }
15
16 public static get launchJsonPath(): string {
17 return path.join(vscode.workspace.rootPath, ".vscode", "launch.json");
18 }
19
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)
28 .then(function(exists: boolean): Q.Promise<string> {
29 if (!exists) {
30 return fileSystem.writeFile(settingsJsonPath, "{}")
31 .then(() => { return "{}"; });
32 }
33
34 return fileSystem.readFile(settingsJsonPath, "utf-8");
35 })
36 .then(function(jsonContents: string): Q.Promise<any> {
37 return JSON.parse(jsonContents);
38 });
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 */
53 public static setTypeScriptTsdk(path: string): Q.Promise<void> {
54 return SettingsHelper.readSettingsJson()
55 .then(function(settingsJson: any): Q.Promise<void> {
56 if (settingsJson["typescript.tsdk"] !== path) {
57 settingsJson["typescript.tsdk"] = path;
58
59 return SettingsHelper.writeSettingsJson(settingsJson);
60 }
61 });
62 }
63
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> {
78 return SettingsHelper.readSettingsJson()
79 .then(function(settingsJson: any): Q.Promise<string> {
80 return settingsJson["typescript.tsdk"] || null;
81 });
82 }
83}