Removed hardcoded usage of ios and android where possible

This commit is contained in:
amorokh 2017-04-12 11:03:33 +02:00
parent 31b3509f1f
commit 76c4bc78e4
1 changed files with 49 additions and 47 deletions

View File

@ -167,12 +167,13 @@ generateConfig = (interfaceName, projName) ->
config =
name: projName
interface: interfaceName
androidHost: "localhost"
iosHost: "localhost"
envRoots: defaultEnvRoots
modules: []
imageDirs: ["images"]
for platform in platforms
config["#{platform}Host"] = "localhost"
writeConfig config
config
@ -188,8 +189,16 @@ writeConfig = (config) ->
message
verifyConfig = (config) ->
if !config.androidHost? || !config.modules? || !config.imageDirs? || !config.interface? || !config.iosHost? || !config.envRoots?
throw new Error 're-natal project needs to be upgraded, please run: re-natal upgrade'
upgradeMessage = 're-natal project needs to be upgraded, please run: re-natal upgrade'
if !config.modules? || !config.imageDirs? || !config.interface? || !config.envRoots?
throw new Error upgradeMessage
for platform in platforms
platformHost = "#{platform}Host"
if !config[platformHost]?
throw new Error upgradeMessage
config
readConfig = (verify = true)->
@ -216,7 +225,7 @@ scanImageDir = (dir) ->
.filter (path) -> fs.statSync(path).isFile()
.filter (path) -> removeExcludeFiles(path)
.map (path) -> path.replace /@2x|@3x/i, ''
.map (path) -> path.replace new RegExp(".(android|ios)" + fpath.extname(path) + "$", "i"), fpath.extname(path)
.map (path) -> path.replace new RegExp(".(#{platforms.join('|')})" + fpath.extname(path) + "$", "i"), fpath.extname(path)
.filter (v, idx, slf) -> slf.indexOf(v) == idx
dirs = fs.readdirSync(dir)
@ -284,40 +293,36 @@ deviceTypeIsIpAddress = (deviceType, allowedTypes) ->
deviceType
copyDevEnvironmentFiles = (interfaceName, projNameHyph, projName, devEnvRoot, devHost) ->
fs.mkdirpSync "#{devEnvRoot}/env/ios"
fs.mkdirpSync "#{devEnvRoot}/env/android"
userNsPath = "#{devEnvRoot}/user.clj"
fs.copySync("#{resources}/user.clj", userNsPath)
mainIosDevPath = "#{devEnvRoot}/env/ios/main.cljs"
mainAndroidDevPath = "#{devEnvRoot}/env/android/main.cljs"
cljsDir = interfaceConf[interfaceName].cljsDir
fs.copySync("#{resources}/#{cljsDir}/main_dev.cljs", mainIosDevPath)
edit mainIosDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "ios"], [devHostRx, devHost] ]
fs.copySync("#{resources}/#{cljsDir}/main_dev.cljs", mainAndroidDevPath)
edit mainAndroidDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "android"], [devHostRx, devHost]]
for platform in platforms
fs.mkdirpSync "#{devEnvRoot}/env/#{platform}"
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
fs.copySync("#{resources}/#{cljsDir}/main_dev.cljs", mainDevPath)
edit mainDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, platform], [devHostRx, devHost]]
copyProdEnvironmentFiles = (interfaceName, projNameHyph, projName, prodEnvRoot) ->
fs.mkdirpSync "#{prodEnvRoot}/env/ios"
fs.mkdirpSync "#{prodEnvRoot}/env/android"
mainIosProdPath = "#{prodEnvRoot}/env/ios/main.cljs"
mainAndroidProdPath = "#{prodEnvRoot}/env/android/main.cljs"
cljsDir = interfaceConf[interfaceName].cljsDir
fs.copySync("#{resources}/#{cljsDir}/main_prod.cljs", mainIosProdPath)
edit mainIosProdPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "ios"]]
fs.copySync("#{resources}/#{cljsDir}/main_prod.cljs", mainAndroidProdPath)
edit mainAndroidProdPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "android"]]
for platform in platforms
fs.mkdirpSync "#{prodEnvRoot}/env/#{platform}"
mainProdPath = "#{prodEnvRoot}/env/#{platform}/main.cljs"
fs.copySync("#{resources}/#{cljsDir}/main_prod.cljs", mainProdPath)
edit mainProdPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, platform]]
copyFigwheelBridge = (projNameUs) ->
fs.copySync("#{resources}/figwheel-bridge.js", "./figwheel-bridge.js")
edit "figwheel-bridge.js", [[projNameUsRx, projNameUs]]
updateGitIgnore = () ->
fs.appendFileSync(".gitignore", "\n# Generated by re-natal\n#\nindex.android.js\nindex.ios.js\ntarget/\n")
fs.appendFileSync(".gitignore", "\n# Generated by re-natal\n#\n")
for platform in platforms
fs.appendFileSync(".gitignore", "index.#{platform}.js\n")
fs.appendFileSync(".gitignore", "target\n")
fs.appendFileSync(".gitignore", "\n# Figwheel\n#\nfigwheel_server.log")
patchReactNativePackager = () ->
@ -481,12 +486,10 @@ generateRequireModulesCode = (modules) ->
jsCode += "modules['#{m}']=require('#{m}');";
jsCode += '\n'
updateFigwheelUrls = (devEnvRoot, androidHost, iosHost) ->
mainAndroidDevPath = "#{devEnvRoot}/env/android/main.cljs"
edit mainAndroidDevPath, [[figwheelUrlRx, "ws://#{androidHost}:"]]
mainIosDevPath = "#{devEnvRoot}/env/ios/main.cljs"
edit mainIosDevPath, [[figwheelUrlRx, "ws://#{iosHost}:"]]
updateFigwheelUrls = (devEnvRoot, devHost) ->
for platform in platforms
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
edit mainDevPath, [[figwheelUrlRx, "ws://#{devHost[platform]}:"]]
# Current RN version (0.29.2) has no host in AppDelegate.m maybe docs are outdated?
updateIosAppDelegate = (projName, iosHost) ->
@ -520,22 +523,22 @@ generateDevScripts = () ->
log 'Cleaning...'
exec 'lein clean'
androidDevHost = config.androidHost
iosDevHost = config.iosHost
devHost = {'android' : androidDevHost, 'ios' : iosDevHost}
devHost = {}
for platform in platforms
devHost[platform] = config["#{platform}Host"]
for platform in platforms
moduleMap = generateRequireModulesCode(platformModulesAndImages(config, platform))
fs.writeFileSync "index.#{platform}.js", "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','#{platform}','#{devHost[platform]}');"
log "index.#{platform}.js was regenerated"
#updateIosAppDelegate(projName, iosDevHost)
updateIosRCTWebSocketExecutor(iosDevHost)
#updateIosAppDelegate(projName, devHost.ios)
updateIosRCTWebSocketExecutor(devHost.ios)
log "Host in RCTWebSocketExecutor.m was updated"
updateFigwheelUrls(devEnvRoot, androidDevHost, iosDevHost)
log 'Dev server host for iOS: ' + iosDevHost
log 'Dev server host for Android: ' + androidDevHost
updateFigwheelUrls(devEnvRoot, devHost)
for platform in platforms
log "Dev server host for #{platform}: #{devHost[platform]}"
catch {message}
logErr \
@ -558,15 +561,14 @@ doUpgrade = (config) ->
unless config.imageDirs
config.imageDirs = ["images"]
unless config.androidHost
config.androidHost = "localhost"
unless config.iosHost
config.iosHost = "localhost"
unless config.envRoots
config.envRoots = defaultEnvRoots
for platform in platforms
platformHost = "#{platform}Host"
unless config[platformHost]
config[platformHost] = "localhost"
writeConfig(config)
log 'upgraded .re-natal'
@ -635,7 +637,7 @@ cli.command 'deps'
ckDeps.sync {install: true, verbose: true}
cli.command 'use-figwheel'
.description 'generate index.ios.js and index.android.js for development with figwheel'
.description 'generate index.*.js for development with figwheel'
.action () ->
generateDevScripts()