microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
147d9cc549da9c4c4297f65e799484b78fe8de80

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

74lines · 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 {Node} from "./node";
5import * as pathModule from "path";
6import * as Q from "q";
7
8interface IPackageDependencyDict {
9 [packageName: string]: string;
10}
11
12export interface IPackageInformation {
13 name: string;
14 version: string;
15 dependencies?: IPackageDependencyDict;
16 main?: string;
17 [key: string]: any;
18}
19
20export class Package {
21 private _path: string;
22 private INFORMATION_PACKAGE_FILENAME = "package.json";
23 private DEPENDENCIES_SUBFOLDER = "node_modules";
24
25 constructor(path: string) {
26 this._path = path;
27 }
28
29 public parsePackageInformation(): Q.Promise<IPackageInformation> {
30 return new Node.FileSystem().readFile(this.informationJsonFilePath(), "utf8")
31 .then(data =>
32 <IPackageInformation>JSON.parse(data));
33 }
34
35 public name(): Q.Promise<string> {
36 return this.parseProperty("name");
37 }
38
39 public dependencies(): Q.Promise<IPackageDependencyDict> {
40 return this.parseProperty("dependencies");
41 }
42
43 public version(): Q.Promise<string> {
44 return this.parseProperty("version").then(version =>
45 typeof version === "string"
46 ? version
47 : Q.reject<string>(`Couldn't parse the version component of the package at ${this.informationJsonFilePath()}: version = ${version}`));
48 }
49
50 public setMainFile(value: string): Q.Promise<void> {
51 return this.parsePackageInformation()
52 .then(packageInformation => {
53 packageInformation.main = value;
54 return new Node.FileSystem().writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation));
55 });
56 }
57
58 public dependencyPath(dependencyName: string) {
59 return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
60 }
61
62 public dependencyPackage(dependencyName: string): Package {
63 return new Package(this.dependencyPath(dependencyName));
64 }
65
66 private informationJsonFilePath(): string {
67 return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
68 }
69
70 private parseProperty(name: string): Q.Promise<any> {
71 return this.parsePackageInformation()
72 .then(packageInformation => packageInformation[name]);
73 }
74}
75