microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2c19da7f131d11b4265a94fe25139194a565116e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

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