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