microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
43e6ccc3781e2fec953ee0059638ffa0e5d403ab

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

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