microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/package.ts
54lines · 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 | |
| 4 | import {Node} from "./node"; |
| 5 | import * as pathModule from "path"; |
| 6 | |
| 7 | interface IPackageDependencyDict { |
| 8 | [packageName: string]: string; |
| 9 | } |
| 10 | |
| 11 | export interface IPackageInformation { |
| 12 | name: string; |
| 13 | version: string; |
| 14 | dependencies?: IPackageDependencyDict; |
| 15 | main?: string; |
| 16 | } |
| 17 | |
| 18 | export class Package { |
| 19 | private _path: string; |
| 20 | private INFORMATION_PACKAGE_FILENAME = "package.json"; |
| 21 | |
| 22 | constructor(path: string) { |
| 23 | this._path = path; |
| 24 | } |
| 25 | |
| 26 | public parsePackageInformation(): Q.Promise<IPackageInformation> { |
| 27 | return new Node.FileSystem().readFile(this.informationJsonFilePath(), "utf8") |
| 28 | .then(data => |
| 29 | <IPackageInformation>JSON.parse(data)); |
| 30 | } |
| 31 | |
| 32 | public name(): Q.Promise<string> { |
| 33 | return this.parsePackageInformation() |
| 34 | .then(packageInformation => |
| 35 | packageInformation.name); |
| 36 | } |
| 37 | |
| 38 | public dependencies(): Q.Promise<IPackageDependencyDict> { |
| 39 | return this.parsePackageInformation() |
| 40 | .then(packageInformation => packageInformation.dependencies); |
| 41 | } |
| 42 | |
| 43 | public setMainFile(value: string): Q.Promise<void> { |
| 44 | return this.parsePackageInformation() |
| 45 | .then(packageInformation => { |
| 46 | packageInformation.main = value; |
| 47 | return new Node.FileSystem().writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation)); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | private informationJsonFilePath(): string { |
| 52 | return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME); |
| 53 | } |
| 54 | } |
| 55 | |