microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
df4bce4041caa61af1460ef87f2380820508a455

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

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