microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
79da24e0a4fdf36141d2f4886b403bdf622224c9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

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