microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0502b7a86030ea91bf3320d0f2ad32aff86fa6db

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

74lines · 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
b0061ac6Meena Kunnathur Balakrishnan10 years ago4import {Node} from "./node";
3fb37ad5unknown10 years ago5import * as pathModule from "path";
bd539474digeff10 years ago6import * as Q from "q";
3fb37ad5unknown10 years ago7
61c4db14Meena Kunnathur Balakrishnan10 years ago8interface IPackageDependencyDict {
9[packageName: string]: string;
10}
11
3fb37ad5unknown10 years ago12export interface IPackageInformation {
13name: string;
61c4db14Meena Kunnathur Balakrishnan10 years ago14version: string;
15dependencies?: IPackageDependencyDict;
16main?: string;
bd539474digeff10 years ago17[key: string]: any;
3fb37ad5unknown10 years ago18}
19
20export class Package {
21private _path: string;
22private INFORMATION_PACKAGE_FILENAME = "package.json";
bd539474digeff10 years ago23private DEPENDENCIES_SUBFOLDER = "node_modules";
3fb37ad5unknown10 years ago24
25constructor(path: string) {
26this._path = path;
27}
28
e639e7a4Daniel10 years ago29public parsePackageInformation(): Q.Promise<IPackageInformation> {
3fb37ad5unknown10 years ago30return new Node.FileSystem().readFile(this.informationJsonFilePath(), "utf8")
31.then(data =>
32<IPackageInformation>JSON.parse(data));
33}
34
35public name(): Q.Promise<string> {
bd539474digeff10 years ago36return this.parseProperty("name");
3fb37ad5unknown10 years ago37}
38
61c4db14Meena Kunnathur Balakrishnan10 years ago39public dependencies(): Q.Promise<IPackageDependencyDict> {
bd539474digeff10 years ago40return this.parseProperty("dependencies");
41}
42
43public version(): Q.Promise<string> {
44return this.parseProperty("version").then(version =>
45typeof version === "string"
46? version
47: Q.reject<string>(`Couldn't parse the version component of the package at ${this.informationJsonFilePath()}: version = ${version}`));
3fb37ad5unknown10 years ago48}
3736c251dlebu10 years ago49
61c4db14Meena Kunnathur Balakrishnan10 years ago50public setMainFile(value: string): Q.Promise<void> {
51return this.parsePackageInformation()
b031edc7digeff10 years ago52.then(packageInformation => {
53packageInformation.main = value;
54return new Node.FileSystem().writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation));
55});
61c4db14Meena Kunnathur Balakrishnan10 years ago56}
aa27d69aMeena Kunnathur Balakrishnan10 years ago57
bd539474digeff10 years ago58public dependencyPath(dependencyName: string) {
59return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
60}
61
62public dependencyPackage(dependencyName: string): Package {
63return new Package(this.dependencyPath(dependencyName));
64}
65
3736c251dlebu10 years ago66private informationJsonFilePath(): string {
67return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
68}
bd539474digeff10 years ago69
70private parseProperty(name: string): Q.Promise<any> {
71return this.parsePackageInformation()
72.then(packageInformation => packageInformation[name]);
73}
3d97c2a3Meena Kunnathur Balakrishnan10 years ago74}