microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/exponent/exponentHelper.ts
307lines · modeblame
1c32fe84Patricio Beltran9 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 | | |
94cd5149Artem Egorov8 years ago | 4 | /// <reference path="exponentHelper.d.ts" /> |
| 5 | | |
1c32fe84Patricio Beltran9 years ago | 6 | import * as path from "path"; |
| 7 | import * as Q from "q"; | |
7059d307Patricio Beltran9 years ago | 8 | import * as XDL from "./xdlInterface"; |
0a68f8dbArtem Egorov8 years ago | 9 | import { Package } from "../../common/node/package"; |
| 10 | import { ReactNativeProjectHelper } from "../../common/reactNativeProjectHelper"; | |
| 11 | import { FileSystem } from "../../common/node/fileSystem"; | |
| 12 | import {OutputChannelLogger} from "../log/OutputChannelLogger"; | |
94cd5149Artem Egorov8 years ago | 13 | import stripJSONComments = require("strip-json-comments"); |
1c32fe84Patricio Beltran9 years ago | 14 | |
94cd5149Artem Egorov8 years ago | 15 | const APP_JSON = "app.json"; |
| 16 | const EXP_JSON = "exp.json"; | |
1c32fe84Patricio Beltran9 years ago | 17 | |
| 18 | const EXPONENT_INDEX = "exponentIndex.js"; | |
94cd5149Artem Egorov8 years ago | 19 | const DEFAULT_EXPONENT_INDEX = "index.js"; |
1c32fe84Patricio Beltran9 years ago | 20 | const DEFAULT_IOS_INDEX = "index.ios.js"; |
| 21 | const DEFAULT_ANDROID_INDEX = "index.android.js"; | |
| 22 | | |
94cd5149Artem Egorov8 years ago | 23 | const DBL_SLASHES = /\\/g; |
1c32fe84Patricio Beltran9 years ago | 24 | |
| 25 | export class ExponentHelper { | |
38edb09eAlexander Sorokin9 years ago | 26 | private workspaceRootPath: string; |
94cd5149Artem Egorov8 years ago | 27 | private projectRootPath: string; |
db6fd42aRuslan Bikkinin7 years ago | 28 | private fs: FileSystem = new FileSystem(); |
a57e740bPatricio Beltran9 years ago | 29 | private hasInitialized: boolean; |
0a68f8dbArtem Egorov8 years ago | 30 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
1c32fe84Patricio Beltran9 years ago | 31 | |
38edb09eAlexander Sorokin9 years ago | 32 | public constructor(workspaceRootPath: string, projectRootPath: string) { |
| 33 | this.workspaceRootPath = workspaceRootPath; | |
| 34 | this.projectRootPath = projectRootPath; | |
a57e740bPatricio Beltran9 years ago | 35 | this.hasInitialized = false; |
| 36 | // Constructor is slim by design. This is to add as less computation as possible | |
| 37 | // to the initialization of the extension. If a public method is added, make sure | |
b0af599cJimmy Thomson9 years ago | 38 | // to call this.lazilyInitialize() at the begining of the code to be sure all variables |
a57e740bPatricio Beltran9 years ago | 39 | // are correctly initialized. |
1c32fe84Patricio Beltran9 years ago | 40 | } |
| 41 | | |
d1d77244Jimmy Thomson9 years ago | 42 | public configureExponentEnvironment(): Q.Promise<void> { |
| 43 | this.lazilyInitialize(); | |
0a68f8dbArtem Egorov8 years ago | 44 | this.logger.info("Making sure your project uses the correct dependencies for exponent. This may take a while..."); |
db6fd42aRuslan Bikkinin7 years ago | 45 | this.logger.logStream("Checking if this is Expo app."); |
94cd5149Artem Egorov8 years ago | 46 | return this.isExpoApp(true) |
| 47 | .then(isExpo => { | |
0a68f8dbArtem Egorov8 years ago | 48 | this.logger.logStream(".\n"); |
d1d77244Jimmy Thomson9 years ago | 49 | |
94cd5149Artem Egorov8 years ago | 50 | return this.patchAppJson(isExpo); |
| 51 | }); | |
d1d77244Jimmy Thomson9 years ago | 52 | } |
| 53 | | |
| 54 | /** | |
| 55 | * Returns the current user. If there is none, asks user for username and password and logins to exponent servers. | |
| 56 | */ | |
5c8365a6Artem Egorov8 years ago | 57 | public loginToExponent( |
| 58 | promptForInformation: (message: string, password: boolean) => Q.Promise<string>, | |
| 59 | showMessage: (message: string) => Q.Promise<string> | |
| 60 | ): Q.Promise<XDL.IUser> { | |
d1d77244Jimmy Thomson9 years ago | 61 | this.lazilyInitialize(); |
| 62 | return XDL.currentUser() | |
| 63 | .then((user) => { | |
| 64 | if (!user) { | |
| 65 | let username = ""; | |
| 66 | return showMessage("You need to login to exponent. Please provide username and password to login. If you don't have an account we will create one for you.") | |
| 67 | .then(() => | |
| 68 | promptForInformation("Exponent username", false) | |
5c8365a6Artem Egorov8 years ago | 69 | ).then((name: string) => { |
d1d77244Jimmy Thomson9 years ago | 70 | username = name; |
| 71 | return promptForInformation("Exponent password", true); | |
| 72 | }) | |
5c8365a6Artem Egorov8 years ago | 73 | .then((password: string) => |
d1d77244Jimmy Thomson9 years ago | 74 | XDL.login(username, password)); |
| 75 | } | |
| 76 | return user; | |
| 77 | }) | |
| 78 | .catch(error => { | |
| 79 | return Q.reject<XDL.IUser>(error); | |
| 80 | }); | |
| 81 | } | |
| 82 | | |
94cd5149Artem Egorov8 years ago | 83 | public getExpPackagerOptions(): Q.Promise<ExpConfigPackager> { |
6458f408Nikita Matrosov9 years ago | 84 | this.lazilyInitialize(); |
94cd5149Artem Egorov8 years ago | 85 | return this.getFromExpConfig("packagerOpts") |
| 86 | .then(opts => opts || {}); | |
6458f408Nikita Matrosov9 years ago | 87 | } |
| 88 | | |
db6fd42aRuslan Bikkinin7 years ago | 89 | public isExpoApp(showProgress: boolean = false): Q.Promise<boolean> { |
| 90 | if (showProgress) { | |
| 91 | this.logger.logStream("..."); | |
| 92 | } | |
| 93 | | |
| 94 | const packageJsonPath = this.pathToFileInWorkspace("package.json"); | |
| 95 | return this.fs.readFile(packageJsonPath) | |
| 96 | .then(content => { | |
| 97 | const packageJson = JSON.parse(content); | |
| 98 | const isExp = packageJson.dependencies && !!packageJson.dependencies.expo || false; | |
| 99 | if (showProgress) this.logger.logStream("."); | |
| 100 | return isExp; | |
| 101 | }).catch(() => { | |
| 102 | if (showProgress) { | |
| 103 | this.logger.logStream("."); | |
| 104 | } | |
| 105 | // Not in a react-native project | |
| 106 | return false; | |
| 107 | }); | |
| 108 | } | |
| 109 | | |
1c32fe84Patricio Beltran9 years ago | 110 | /** |
94cd5149Artem Egorov8 years ago | 111 | * Path to a given file inside the .vscode directory |
1c32fe84Patricio Beltran9 years ago | 112 | */ |
94cd5149Artem Egorov8 years ago | 113 | private dotvscodePath(filename: string): string { |
| 114 | return path.join(this.workspaceRootPath, ".vscode", filename); | |
| 115 | } | |
| 116 | | |
| 117 | private createExpoEntry(name: string): Q.Promise<void> { | |
b0af599cJimmy Thomson9 years ago | 118 | this.lazilyInitialize(); |
94cd5149Artem Egorov8 years ago | 119 | return this.detectEntry() |
| 120 | .then((entryPoint: string) => { | |
| 121 | const content = this.generateFileContent(name, entryPoint); | |
| 122 | return this.fs.writeFile(this.dotvscodePath(EXPONENT_INDEX), content); | |
| 123 | }); | |
1c32fe84Patricio Beltran9 years ago | 124 | |
94cd5149Artem Egorov8 years ago | 125 | } |
1c32fe84Patricio Beltran9 years ago | 126 | |
94cd5149Artem Egorov8 years ago | 127 | private detectEntry(): Q.Promise<string> { |
| 128 | this.lazilyInitialize(); | |
| 129 | return Q.all([ | |
| 130 | this.fs.exists(this.pathToFileInWorkspace(DEFAULT_EXPONENT_INDEX)), | |
| 131 | this.fs.exists(this.pathToFileInWorkspace(DEFAULT_IOS_INDEX)), | |
| 132 | this.fs.exists(this.pathToFileInWorkspace(DEFAULT_ANDROID_INDEX)), | |
| 133 | ]) | |
| 134 | .spread((expo: boolean, ios: boolean): string => { | |
| 135 | return expo ? this.pathToFileInWorkspace(DEFAULT_EXPONENT_INDEX) : | |
| 136 | ios ? this.pathToFileInWorkspace(DEFAULT_IOS_INDEX) : | |
| 137 | this.pathToFileInWorkspace(DEFAULT_ANDROID_INDEX); | |
| 138 | }); | |
| 139 | } | |
1c32fe84Patricio Beltran9 years ago | 140 | |
94cd5149Artem Egorov8 years ago | 141 | private generateFileContent(name: string, entryPoint: string): string { |
| 142 | return `// This file is automatically generated by VS Code | |
| 143 | // Please do not modify it manually. All changes will be lost. | |
| 144 | var React = require('${this.pathToFileInWorkspace("/node_modules/react")}'); | |
| 145 | var { Component } = React; | |
| 146 | var ReactNative = require('${this.pathToFileInWorkspace("/node_modules/react-native")}'); | |
| 147 | var { AppRegistry } = ReactNative; | |
| 148 | var entryPoint = require('${entryPoint}'); | |
f6b41bbfAlexander Sorokin9 years ago | 149 | AppRegistry.registerRunnable('main', function(appParameters) { |
94cd5149Artem Egorov8 years ago | 150 | AppRegistry.runApplication('${name}', appParameters); |
1ae29ddcJimmy Thomson9 years ago | 151 | });`; |
1c32fe84Patricio Beltran9 years ago | 152 | } |
| 153 | | |
94cd5149Artem Egorov8 years ago | 154 | private patchAppJson(isExpo: boolean = true): Q.Promise<void> { |
| 155 | return this.readAppJson() | |
| 156 | .catch(() => { | |
| 157 | // if app.json doesn't exist but it's ok, we will create it | |
| 158 | return {}; | |
| 159 | }) | |
| 160 | .then((config: AppJson) => { | |
| 161 | let expoConfig = <ExpConfig>(config.expo || {}); | |
| 162 | if (!expoConfig.name || !expoConfig.slug) { | |
b0af599cJimmy Thomson9 years ago | 163 | return this.getPackageName() |
94cd5149Artem Egorov8 years ago | 164 | .then((name: string) => { |
| 165 | expoConfig.slug = expoConfig.slug || config.name || name.replace(" ", "-"); | |
| 166 | expoConfig.name = expoConfig.name || config.name || name; | |
| 167 | config.expo = expoConfig; | |
| 168 | return config; | |
b0af599cJimmy Thomson9 years ago | 169 | }); |
| 170 | } | |
| 171 | | |
94cd5149Artem Egorov8 years ago | 172 | return config; |
| 173 | }) | |
| 174 | .then((config: AppJson) => { | |
| 175 | if (!config.expo.sdkVersion) { | |
| 176 | return this.exponentSdk(true) | |
| 177 | .then(sdkVersion => { | |
| 178 | config.expo.sdkVersion = sdkVersion; | |
| 179 | return config; | |
| 180 | }); | |
1c32fe84Patricio Beltran9 years ago | 181 | } |
94cd5149Artem Egorov8 years ago | 182 | return config; |
1c32fe84Patricio Beltran9 years ago | 183 | }) |
94cd5149Artem Egorov8 years ago | 184 | .then((config: AppJson) => { |
| 185 | if (!isExpo) { | |
| 186 | config.expo.entryPoint = this.dotvscodePath(EXPONENT_INDEX); | |
| 187 | } | |
1c32fe84Patricio Beltran9 years ago | 188 | |
94cd5149Artem Egorov8 years ago | 189 | return config; |
| 190 | }) | |
| 191 | .then((config: AppJson) => { | |
5c8365a6Artem Egorov8 years ago | 192 | return config ? this.writeAppJson(config) : config; |
1c32fe84Patricio Beltran9 years ago | 193 | }) |
94cd5149Artem Egorov8 years ago | 194 | .then((config: AppJson) => { |
5c8365a6Artem Egorov8 years ago | 195 | return isExpo ? Q.resolve(void 0) : this.createExpoEntry(config.expo.name); |
1c32fe84Patricio Beltran9 years ago | 196 | }); |
27710197Vladimir Kotikov8 years ago | 197 | } |
1c32fe84Patricio Beltran9 years ago | 198 | |
| 199 | /** | |
| 200 | * Exponent sdk version that maps to the current react-native version | |
| 201 | * If react native version is not supported it returns null. | |
| 202 | */ | |
831f4a85Patricio Beltran9 years ago | 203 | private exponentSdk(showProgress: boolean = false): Q.Promise<string> { |
94cd5149Artem Egorov8 years ago | 204 | if (showProgress) { |
0a68f8dbArtem Egorov8 years ago | 205 | this.logger.logStream("..."); |
1c32fe84Patricio Beltran9 years ago | 206 | } |
94cd5149Artem Egorov8 years ago | 207 | |
2e432a9eArtem Egorov8 years ago | 208 | return ReactNativeProjectHelper.getReactNativeVersion(this.projectRootPath) |
94cd5149Artem Egorov8 years ago | 209 | .then(version => { |
0a68f8dbArtem Egorov8 years ago | 210 | if (showProgress) this.logger.logStream("."); |
94cd5149Artem Egorov8 years ago | 211 | return XDL.mapVersion(version) |
| 212 | .then(sdkVersion => { | |
| 213 | if (!sdkVersion) { | |
| 214 | return XDL.supportedVersions() | |
| 215 | .then((versions) => { | |
| 216 | return Q.reject<string>(new Error(`React Native version not supported by exponent. Major versions supported: ${versions.join(", ")}`)); | |
| 217 | }); | |
| 218 | } | |
| 219 | return sdkVersion; | |
1c32fe84Patricio Beltran9 years ago | 220 | }); |
| 221 | }); | |
| 222 | } | |
| 223 | | |
94cd5149Artem Egorov8 years ago | 224 | |
1c32fe84Patricio Beltran9 years ago | 225 | /** |
94cd5149Artem Egorov8 years ago | 226 | * Name specified on user's package.json |
1c32fe84Patricio Beltran9 years ago | 227 | */ |
94cd5149Artem Egorov8 years ago | 228 | private getPackageName(): Q.Promise<string> { |
| 229 | return new Package(this.projectRootPath, { fileSystem: this.fs }).name(); | |
| 230 | } | |
| 231 | | |
| 232 | private getExpConfig(): Q.Promise<ExpConfig> { | |
| 233 | return this.readExpJson() | |
| 234 | .catch(err => { | |
| 235 | if (err.code === "ENOENT") { | |
| 236 | return this.readAppJson() | |
| 237 | .then((config: AppJson) => { | |
| 238 | return config.expo || {}; | |
| 239 | }); | |
1c32fe84Patricio Beltran9 years ago | 240 | } |
94cd5149Artem Egorov8 years ago | 241 | |
| 242 | return err; | |
1c32fe84Patricio Beltran9 years ago | 243 | }); |
| 244 | } | |
| 245 | | |
94cd5149Artem Egorov8 years ago | 246 | private getFromExpConfig(key: string): Q.Promise<any> { |
| 247 | return this.getExpConfig() | |
| 248 | .then((config: ExpConfig) => config[key]); | |
1c32fe84Patricio Beltran9 years ago | 249 | } |
| 250 | | |
| 251 | /** | |
94cd5149Artem Egorov8 years ago | 252 | * Returns the specified setting from exp.json if it exists |
1c32fe84Patricio Beltran9 years ago | 253 | */ |
94cd5149Artem Egorov8 years ago | 254 | private readExpJson(): Q.Promise<ExpConfig> { |
| 255 | const expJsonPath = this.pathToFileInWorkspace(EXP_JSON); | |
| 256 | return this.fs.readFile(expJsonPath) | |
| 257 | .then(content => { | |
| 258 | return JSON.parse(stripJSONComments(content)); | |
1c32fe84Patricio Beltran9 years ago | 259 | }); |
| 260 | } | |
| 261 | | |
94cd5149Artem Egorov8 years ago | 262 | private readAppJson(): Q.Promise<AppJson> { |
| 263 | const appJsonPath = this.pathToFileInWorkspace(APP_JSON); | |
| 264 | return this.fs.readFile(appJsonPath) | |
| 265 | .then(content => { | |
| 266 | return JSON.parse(stripJSONComments(content)); | |
1c32fe84Patricio Beltran9 years ago | 267 | }); |
| 268 | } | |
| 269 | | |
94cd5149Artem Egorov8 years ago | 270 | private writeAppJson(config: AppJson): Q.Promise<AppJson> { |
| 271 | const appJsonPath = this.pathToFileInWorkspace(APP_JSON); | |
| 272 | return this.fs.writeFile(appJsonPath, JSON.stringify(config, null, 2)) | |
| 273 | .then(() => config); | |
1c32fe84Patricio Beltran9 years ago | 274 | } |
| 275 | | |
| 276 | /** | |
38edb09eAlexander Sorokin9 years ago | 277 | * Path to a given file from the workspace root |
1c32fe84Patricio Beltran9 years ago | 278 | */ |
| 279 | private pathToFileInWorkspace(filename: string): string { | |
94cd5149Artem Egorov8 years ago | 280 | return path.join(this.projectRootPath, filename).replace(DBL_SLASHES, "/"); |
1c32fe84Patricio Beltran9 years ago | 281 | } |
| 282 | | |
a57e740bPatricio Beltran9 years ago | 283 | /** |
| 284 | * Works as a constructor but only initiliazes when it's actually needed. | |
| 285 | */ | |
b0af599cJimmy Thomson9 years ago | 286 | private lazilyInitialize(): void { |
a57e740bPatricio Beltran9 years ago | 287 | if (!this.hasInitialized) { |
| 288 | this.hasInitialized = true; | |
| 289 | | |
| 290 | XDL.configReactNativeVersionWargnings(); | |
38edb09eAlexander Sorokin9 years ago | 291 | XDL.attachLoggerStream(this.projectRootPath, { |
a57e740bPatricio Beltran9 years ago | 292 | stream: { |
| 293 | write: (chunk: any) => { | |
| 294 | if (chunk.level <= 30) { | |
0a68f8dbArtem Egorov8 years ago | 295 | this.logger.logStream(chunk.msg); |
a57e740bPatricio Beltran9 years ago | 296 | } else if (chunk.level === 40) { |
0a68f8dbArtem Egorov8 years ago | 297 | this.logger.warning(chunk.msg); |
a57e740bPatricio Beltran9 years ago | 298 | } else { |
0a68f8dbArtem Egorov8 years ago | 299 | this.logger.error(chunk.msg); |
a57e740bPatricio Beltran9 years ago | 300 | } |
| 301 | }, | |
| 302 | }, | |
| 303 | type: "raw", | |
| 304 | }); | |
| 305 | } | |
| 306 | } | |
5c8365a6Artem Egorov8 years ago | 307 | } |