microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/package.ts
80lines · modeblame
a31b007cunknown10 years ago | 1 | // 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 ago | 4 | import * as pathModule from "path"; |
bd539474digeff10 years ago | 5 | import * as Q from "q"; |
3fb37ad5unknown10 years ago | 6 | |
aab2095edigeff10 years ago | 7 | import {FileSystem} from "./fileSystem"; |
| 8 | | |
61c4db14Meena Kunnathur Balakrishnan10 years ago | 9 | interface IPackageDependencyDict { |
| 10 | [packageName: string]: string; | |
| 11 | } | |
| 12 | | |
3fb37ad5unknown10 years ago | 13 | export interface IPackageInformation { |
| 14 | name: string; | |
61c4db14Meena Kunnathur Balakrishnan10 years ago | 15 | version: string; |
| 16 | dependencies?: IPackageDependencyDict; | |
| 17 | main?: string; | |
bd539474digeff10 years ago | 18 | [key: string]: any; |
3fb37ad5unknown10 years ago | 19 | } |
| 20 | | |
| 21 | export class Package { | |
| 22 | private INFORMATION_PACKAGE_FILENAME = "package.json"; | |
bd539474digeff10 years ago | 23 | private DEPENDENCIES_SUBFOLDER = "node_modules"; |
3fb37ad5unknown10 years ago | 24 | |
aab2095edigeff10 years ago | 25 | private fileSystem: FileSystem; |
| 26 | | |
| 27 | private _path: string; | |
| 28 | | |
| 29 | constructor(path: string, { fileSystem = new FileSystem() } = {}) { | |
3fb37ad5unknown10 years ago | 30 | this._path = path; |
aab2095edigeff10 years ago | 31 | this.fileSystem = fileSystem; |
3fb37ad5unknown10 years ago | 32 | } |
| 33 | | |
e639e7a4Daniel10 years ago | 34 | public parsePackageInformation(): Q.Promise<IPackageInformation> { |
aab2095edigeff10 years ago | 35 | return this.fileSystem.readFile(this.informationJsonFilePath(), "utf8") |
3fb37ad5unknown10 years ago | 36 | .then(data => |
| 37 | <IPackageInformation>JSON.parse(data)); | |
| 38 | } | |
| 39 | | |
| 40 | public name(): Q.Promise<string> { | |
bd539474digeff10 years ago | 41 | return this.parseProperty("name"); |
3fb37ad5unknown10 years ago | 42 | } |
| 43 | | |
61c4db14Meena Kunnathur Balakrishnan10 years ago | 44 | public dependencies(): Q.Promise<IPackageDependencyDict> { |
bd539474digeff10 years ago | 45 | return this.parseProperty("dependencies"); |
| 46 | } | |
| 47 | | |
| 48 | public version(): Q.Promise<string> { | |
| 49 | return this.parseProperty("version").then(version => | |
| 50 | typeof version === "string" | |
| 51 | ? version | |
| 52 | : Q.reject<string>(`Couldn't parse the version component of the package at ${this.informationJsonFilePath()}: version = ${version}`)); | |
3fb37ad5unknown10 years ago | 53 | } |
3736c251dlebu10 years ago | 54 | |
61c4db14Meena Kunnathur Balakrishnan10 years ago | 55 | public setMainFile(value: string): Q.Promise<void> { |
| 56 | return this.parsePackageInformation() | |
b031edc7digeff10 years ago | 57 | .then(packageInformation => { |
| 58 | packageInformation.main = value; | |
aab2095edigeff10 years ago | 59 | return this.fileSystem.writeFile(this.informationJsonFilePath(), JSON.stringify(<Object>packageInformation)); |
b031edc7digeff10 years ago | 60 | }); |
61c4db14Meena Kunnathur Balakrishnan10 years ago | 61 | } |
aa27d69aMeena Kunnathur Balakrishnan10 years ago | 62 | |
bd539474digeff10 years ago | 63 | public dependencyPath(dependencyName: string) { |
| 64 | return pathModule.resolve(this._path, this.DEPENDENCIES_SUBFOLDER, dependencyName); | |
| 65 | } | |
| 66 | | |
| 67 | public dependencyPackage(dependencyName: string): Package { | |
aab2095edigeff10 years ago | 68 | return new Package(this.dependencyPath(dependencyName), { fileSystem: this.fileSystem}); |
bd539474digeff10 years ago | 69 | } |
| 70 | | |
aab2095edigeff10 years ago | 71 | public informationJsonFilePath(): string { |
3736c251dlebu10 years ago | 72 | return pathModule.resolve(this._path, this.INFORMATION_PACKAGE_FILENAME); |
| 73 | } | |
bd539474digeff10 years ago | 74 | |
| 75 | private parseProperty(name: string): Q.Promise<any> { | |
| 76 | return this.parsePackageInformation() | |
aab2095edigeff10 years ago | 77 | .then(packageInformation => |
| 78 | packageInformation[name]); | |
bd539474digeff10 years ago | 79 | } |
3d97c2a3Meena Kunnathur Balakrishnan10 years ago | 80 | } |