microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.16

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

80lines · 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";
8
61c4db14Meena Kunnathur Balakrishnan10 years ago9interface IPackageDependencyDict {
10[packageName: string]: string;
11}
12
3fb37ad5unknown10 years ago13export interface IPackageInformation {
14name: string;
61c4db14Meena Kunnathur Balakrishnan10 years ago15version: string;
16dependencies?: IPackageDependencyDict;
17main?: string;
bd539474digeff10 years ago18[key: string]: any;
3fb37ad5unknown10 years ago19}
20
21export class Package {
22private INFORMATION_PACKAGE_FILENAME = "package.json";
bd539474digeff10 years ago23private DEPENDENCIES_SUBFOLDER = "node_modules";
3fb37ad5unknown10 years ago24
aab2095edigeff10 years ago25private fileSystem: FileSystem;
26
27private _path: string;
28
29constructor(path: string, { fileSystem = new FileSystem() } = {}) {
3fb37ad5unknown10 years ago30this._path = path;
aab2095edigeff10 years ago31this.fileSystem = fileSystem;
3fb37ad5unknown10 years ago32}
33
e639e7a4Daniel10 years ago34public parsePackageInformation(): Q.Promise<IPackageInformation> {
aab2095edigeff10 years ago35return this.fileSystem.readFile(this.informationJsonFilePath(), "utf8")
3fb37ad5unknown10 years ago36.then(data =>
37<IPackageInformation>JSON.parse(data));
38}
39
40public name(): Q.Promise<string> {
bd539474digeff10 years ago41return this.parseProperty("name");
3fb37ad5unknown10 years ago42}
43
61c4db14Meena Kunnathur Balakrishnan10 years ago44public dependencies(): Q.Promise<IPackageDependencyDict> {
bd539474digeff10 years ago45return this.parseProperty("dependencies");
46}
47
48public version(): Q.Promise<string> {
49return this.parseProperty("version").then(version =>
50typeof version === "string"
51? version
52: Q.reject<string>(`Couldn't parse the version component of the package at ${this.informationJsonFilePath()}: version = ${version}`));
3fb37ad5unknown10 years ago53}
3736c251dlebu10 years ago54
61c4db14Meena Kunnathur Balakrishnan10 years ago55public setMainFile(value: string): Q.Promise<void> {
56return this.parsePackageInformation()
b031edc7digeff10 years ago57.then(packageInformation => {
58packageInformation.main = value;
aab2095edigeff10 years ago59return this.fileSystem.writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation));
b031edc7digeff10 years ago60});
61c4db14Meena Kunnathur Balakrishnan10 years ago61}
aa27d69aMeena Kunnathur Balakrishnan10 years ago62
bd539474digeff10 years ago63public dependencyPath(dependencyName: string) {
64return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
65}
66
67public dependencyPackage(dependencyName: string): Package {
aab2095edigeff10 years ago68return new Package(this.dependencyPath(dependencyName), { fileSystem: this.fileSystem});
bd539474digeff10 years ago69}
70
aab2095edigeff10 years ago71public informationJsonFilePath(): string {
3736c251dlebu10 years ago72return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
73}
bd539474digeff10 years ago74
75private parseProperty(name: string): Q.Promise<any> {
76return this.parsePackageInformation()
aab2095edigeff10 years ago77.then(packageInformation =>
78packageInformation[name]);
bd539474digeff10 years ago79}
3d97c2a3Meena Kunnathur Balakrishnan10 years ago80}