microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bcb44c4d4e94b00d16fc644eee9a1d64a2ef86b7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/package.ts

96lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
3fb37ad5unknown10 years ago4import * as pathModule from "path";
5
ce5e88eeYuri Skorokhodov5 years ago6import { FileSystem } from "./fileSystem";
fc602bb6Yuri Skorokhodov7 years ago7import { ErrorHelper } from "../error/errorHelper";
8import { InternalErrorCode } from "../error/internalErrorCode";
aab2095edigeff10 years ago9
61c4db14Meena Kunnathur Balakrishnan10 years ago10interface IPackageDependencyDict {
11[packageName: string]: string;
12}
13
3fb37ad5unknown10 years ago14export interface IPackageInformation {
15name: string;
61c4db14Meena Kunnathur Balakrishnan10 years ago16version: string;
17dependencies?: IPackageDependencyDict;
18main?: string;
bd539474digeff10 years ago19[key: string]: any;
3fb37ad5unknown10 years ago20}
21
22export class Package {
23private INFORMATION_PACKAGE_FILENAME = "package.json";
bd539474digeff10 years ago24private DEPENDENCIES_SUBFOLDER = "node_modules";
3fb37ad5unknown10 years ago25
aab2095edigeff10 years ago26private fileSystem: FileSystem;
27
28private _path: string;
29
30constructor(path: string, { fileSystem = new FileSystem() } = {}) {
3fb37ad5unknown10 years ago31this._path = path;
aab2095edigeff10 years ago32this.fileSystem = fileSystem;
3fb37ad5unknown10 years ago33}
34
ce5e88eeYuri Skorokhodov5 years ago35public getPackageVersionFromNodeModules(packageName: string): Promise<string> {
7fa90b3bRedMickey6 years ago36return this.dependencyPackage(packageName).version();
37}
38
ce5e88eeYuri Skorokhodov5 years ago39public parsePackageInformation(): Promise<IPackageInformation> {
34472878RedMickey5 years ago40return this.fileSystem
41.readFile(this.informationJsonFilePath(), "utf8")
42.then(data => <IPackageInformation>JSON.parse(data.toString()));
3fb37ad5unknown10 years ago43}
44
ce5e88eeYuri Skorokhodov5 years ago45public name(): Promise<string> {
bd539474digeff10 years ago46return this.parseProperty("name");
3fb37ad5unknown10 years ago47}
48
ce5e88eeYuri Skorokhodov5 years ago49public dependencies(): Promise<IPackageDependencyDict> {
bd539474digeff10 years ago50return this.parseProperty("dependencies");
51}
52
ce5e88eeYuri Skorokhodov5 years ago53public devDependencies(): Promise<IPackageDependencyDict> {
af1474acRedMickey6 years ago54return this.parseProperty("devDependencies");
55}
56
ce5e88eeYuri Skorokhodov5 years ago57public version(): Promise<string> {
bd539474digeff10 years ago58return this.parseProperty("version").then(version =>
59typeof version === "string"
60? version
34472878RedMickey5 years ago61: Promise.reject<string>(
62ErrorHelper.getInternalError(
63InternalErrorCode.CouldNotParsePackageVersion,
64this.informationJsonFilePath(),
65version,
66),
67),
68);
3fb37ad5unknown10 years ago69}
3736c251dlebu10 years ago70
ce5e88eeYuri Skorokhodov5 years ago71public setMainFile(value: string): Promise<void> {
34472878RedMickey5 years ago72return this.parsePackageInformation().then(packageInformation => {
73packageInformation.main = value;
74return this.fileSystem.writeFile(
75this.informationJsonFilePath(),
76JSON.stringify(<Record<string, any>>packageInformation),
77);
78});
61c4db14Meena Kunnathur Balakrishnan10 years ago79}
aa27d69aMeena Kunnathur Balakrishnan10 years ago80
34472878RedMickey5 years ago81public dependencyPath(dependencyName: string): string {
bd539474digeff10 years ago82return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName);
83}
84
85public dependencyPackage(dependencyName: string): Package {
34472878RedMickey5 years ago86return new Package(this.dependencyPath(dependencyName), { fileSystem: this.fileSystem });
bd539474digeff10 years ago87}
88
aab2095edigeff10 years ago89public informationJsonFilePath(): string {
3736c251dlebu10 years ago90return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME);
91}
bd539474digeff10 years ago92
ce5e88eeYuri Skorokhodov5 years ago93private parseProperty(name: string): Promise<any> {
34472878RedMickey5 years ago94return this.parsePackageInformation().then(packageInformation => packageInformation[name]);
bd539474digeff10 years ago95}
3d97c2a3Meena Kunnathur Balakrishnan10 years ago96}