microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70cfd5650973cc6872aaae01ea2db7a924e6b67b

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

86lines · 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 devDependencies(): Q.Promise<IPackageDependencyDict> {
51 return this.parseProperty("devDependencies");
52 }
53
54 public version(): Q.Promise<string> {
55 return this.parseProperty("version").then(version =>
56 typeof version === "string"
57 ? version
58 : Q.reject<string>(ErrorHelper.getInternalError(InternalErrorCode.CouldNotParsePackageVersion, this.informationJsonFilePath(), version)));
59 }
60
61 public setMainFile(value: string): Q.Promise<void> {
62 return this.parsePackageInformation()
63 .then(packageInformation => {
64 packageInformation.main = value;
65 return this.fileSystem.writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation));
66 });
67 }
68
69 public dependencyPath(dependencyName: string) {
70 return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
71 }
72
73 public dependencyPackage(dependencyName: string): Package {
74 return new Package(this.dependencyPath(dependencyName), { fileSystem: this.fileSystem});
75 }
76
77 public informationJsonFilePath(): string {
78 return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
79 }
80
81 private parseProperty(name: string): Q.Promise<any> {
82 return this.parsePackageInformation()
83 .then(packageInformation =>
84 packageInformation[name]);
85 }
86}
87