microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.10.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

82lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
3fb37ad5unknown10 years ago4import * as pathModule from "path";
bd539474digeff10 years ago5import * as Q from "q";
3fb37ad5unknown10 years ago6
aab2095edigeff10 years ago7import {FileSystem} from "./fileSystem";
fc602bb6Yuri Skorokhodov7 years ago8import { ErrorHelper } from "../error/errorHelper";
9import { InternalErrorCode } from "../error/internalErrorCode";
aab2095edigeff10 years ago10
61c4db14Meena Kunnathur Balakrishnan10 years ago11interface IPackageDependencyDict {
12[packageName: string]: string;
13}
14
3fb37ad5unknown10 years ago15export interface IPackageInformation {
16name: string;
61c4db14Meena Kunnathur Balakrishnan10 years ago17version: string;
18dependencies?: IPackageDependencyDict;
19main?: string;
bd539474digeff10 years ago20[key: string]: any;
3fb37ad5unknown10 years ago21}
22
23export class Package {
24private INFORMATION_PACKAGE_FILENAME = "package.json";
bd539474digeff10 years ago25private DEPENDENCIES_SUBFOLDER = "node_modules";
3fb37ad5unknown10 years ago26
aab2095edigeff10 years ago27private fileSystem: FileSystem;
28
29private _path: string;
30
31constructor(path: string, { fileSystem = new FileSystem() } = {}) {
3fb37ad5unknown10 years ago32this._path = path;
aab2095edigeff10 years ago33this.fileSystem = fileSystem;
3fb37ad5unknown10 years ago34}
35
e639e7a4Daniel10 years ago36public parsePackageInformation(): Q.Promise<IPackageInformation> {
aab2095edigeff10 years ago37return this.fileSystem.readFile(this.informationJsonFilePath(), "utf8")
3fb37ad5unknown10 years ago38.then(data =>
39<IPackageInformation>JSON.parse(data));
40}
41
42public name(): Q.Promise<string> {
bd539474digeff10 years ago43return this.parseProperty("name");
3fb37ad5unknown10 years ago44}
45
61c4db14Meena Kunnathur Balakrishnan10 years ago46public dependencies(): Q.Promise<IPackageDependencyDict> {
bd539474digeff10 years ago47return this.parseProperty("dependencies");
48}
49
50public version(): Q.Promise<string> {
51return this.parseProperty("version").then(version =>
52typeof version === "string"
53? version
fc602bb6Yuri Skorokhodov7 years ago54: Q.reject<string>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotParsePackageVersion, this.informationJsonFilePath(), version)));
3fb37ad5unknown10 years ago55}
3736c251dlebu10 years ago56
61c4db14Meena Kunnathur Balakrishnan10 years ago57public setMainFile(value: string): Q.Promise<void> {
58return this.parsePackageInformation()
b031edc7digeff10 years ago59.then(packageInformation => {
60packageInformation.main = value;
aab2095edigeff10 years ago61return this.fileSystem.writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation));
b031edc7digeff10 years ago62});
61c4db14Meena Kunnathur Balakrishnan10 years ago63}
aa27d69aMeena Kunnathur Balakrishnan10 years ago64
bd539474digeff10 years ago65public dependencyPath(dependencyName: string) {
66return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
67}
68
69public dependencyPackage(dependencyName: string): Package {
aab2095edigeff10 years ago70return new Package(this.dependencyPath(dependencyName), { fileSystem: this.fileSystem});
bd539474digeff10 years ago71}
72
aab2095edigeff10 years ago73public informationJsonFilePath(): string {
3736c251dlebu10 years ago74return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
75}
bd539474digeff10 years ago76
77private parseProperty(name: string): Q.Promise<any> {
78return this.parsePackageInformation()
aab2095edigeff10 years ago79.then(packageInformation =>
80packageInformation[name]);
bd539474digeff10 years ago81}
3d97c2a3Meena Kunnathur Balakrishnan10 years ago82}