microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efbe1ba61ca71887b3fa9a2d1ae3afb9a78cb87e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

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