move figwheel url configuration into generated namespace

This commit is contained in:
Artur Girenko 2017-10-08 18:56:30 +02:00
parent 181a208058
commit 9e14a62f4e
7 changed files with 41 additions and 30 deletions

View File

@ -10,10 +10,11 @@
"check-dependencies": "^1.0.1", "check-dependencies": "^1.0.1",
"coffee-script": "^1.9.3", "coffee-script": "^1.9.3",
"commander": "^2.8.1", "commander": "^2.8.1",
"deepmerge": "^1.5.2",
"fs-extra": "^0.26.5", "fs-extra": "^0.26.5",
"handlebars": "^4.0.10",
"klaw-sync": "^2.1.0", "klaw-sync": "^2.1.0",
"semver": "^5.0.1", "semver": "^5.0.1"
"deepmerge": "^1.5.2"
}, },
"engines": { "engines": {
"node": ">=4.0.0" "node": ">=4.0.0"

View File

@ -16,6 +16,7 @@ chalk = require 'chalk'
semver = require 'semver' semver = require 'semver'
ckDeps = require 'check-dependencies' ckDeps = require 'check-dependencies'
merge = require 'deepmerge' merge = require 'deepmerge'
hb = require 'handlebars'
pkgJson = require __dirname + '/package.json' pkgJson = require __dirname + '/package.json'
nodeVersion = pkgJson.engines.node nodeVersion = pkgJson.engines.node
@ -29,10 +30,7 @@ interfaceDepsRx = /\$INTERFACE_DEPS\$/g
platformRx = /\$PLATFORM\$/g platformRx = /\$PLATFORM\$/g
platformCleanRx = /#_\(\$PLATFORM_CLEAN\$\)/g platformCleanRx = /#_\(\$PLATFORM_CLEAN\$\)/g
platformCleanId = "#_($PLATFORM_CLEAN$)" platformCleanId = "#_($PLATFORM_CLEAN$)"
devHostRx = /\$DEV_HOST\$/g
ipAddressRx = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/i ipAddressRx = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/i
figwheelUrlRx = /ws:\/\/[0-9a-zA-Z\.]*:/g
appDelegateRx = /http:\/\/[^:]+/g
debugHostRx = /host]\s+\?:\s+@".*";/g debugHostRx = /host]\s+\?:\s+@".*";/g
namespaceRx = /\(ns\s+([A-Za-z0-9.-]+)/g namespaceRx = /\(ns\s+([A-Za-z0-9.-]+)/g
jsRequireRx = /js\/require "(.+)"/g jsRequireRx = /js\/require "(.+)"/g
@ -337,19 +335,23 @@ deviceTypeIsIpAddress = (deviceType, allowedTypes) ->
log("Value '#{deviceType}' is not a valid IP address, still configured it as development host. Did you mean one of: [#{allowedTypes}] ?", 'yellow') log("Value '#{deviceType}' is not a valid IP address, still configured it as development host. Did you mean one of: [#{allowedTypes}] ?", 'yellow')
deviceType deviceType
copyDevEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, devEnvRoot, devHost) -> copyDevEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, devEnvRoot) ->
cljsDir = interfaceConf[interfaceName].cljsDir cljsDir = interfaceConf[interfaceName].cljsDir
fs.mkdirpSync "#{devEnvRoot}/env/#{platform}" fs.mkdirpSync "#{devEnvRoot}/env/#{platform}"
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs" mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
fs.copySync("#{resources}/#{cljsDir}/main_dev.cljs", mainDevPath) fs.copySync("#{resources}/#{cljsDir}/main_dev.cljs", mainDevPath)
edit mainDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, platform], [devHostRx, devHost]] edit mainDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, platform]]
copyDevEnvironmentFiles = (interfaceName, projNameHyph, projName, devEnvRoot, devHost) -> generateConfigNs = (config) ->
template = hb.compile(readFile "#{resources}/config.cljs")
fs.writeFileSync("#{config.envRoots.dev}/env/config.cljs", template(config))
copyDevEnvironmentFiles = (interfaceName, projNameHyph, projName, devEnvRoot) ->
userNsPath = "#{devEnvRoot}/user.clj" userNsPath = "#{devEnvRoot}/user.clj"
fs.copySync("#{resources}/user.clj", userNsPath) fs.copySync("#{resources}/user.clj", userNsPath)
for platform in platforms for platform in platforms
copyDevEnvironmentFilesForPlatform platform, interfaceName, projNameHyph, projName, devEnvRoot, devHost copyDevEnvironmentFilesForPlatform platform, interfaceName, projNameHyph, projName, devEnvRoot
copyProdEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, prodEnvRoot) -> copyProdEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, prodEnvRoot) ->
cljsDir = interfaceConf[interfaceName].cljsDir cljsDir = interfaceConf[interfaceName].cljsDir
@ -372,7 +374,8 @@ updateGitIgnore = () ->
indexFiles = platforms.map (platform) -> "index.#{platform}.js" indexFiles = platforms.map (platform) -> "index.#{platform}.js"
fs.appendFileSync(".gitignore", indexFiles.join("\n")) fs.appendFileSync(".gitignore", indexFiles.join("\n"))
fs.appendFileSync(".gitignore", "\ntarget/") fs.appendFileSync(".gitignore", "\ntarget/")
fs.appendFileSync(".gitignore", "\n.re-natal.local\n") fs.appendFileSync(".gitignore", "\n.re-natal.local")
fs.appendFileSync(".gitignore", "\nenv/dev/env/config.cljs\n")
fs.appendFileSync(".gitignore", "\n# Figwheel\n#\nfigwheel_server.log") fs.appendFileSync(".gitignore", "\n# Figwheel\n#\nfigwheel_server.log")
@ -516,7 +519,7 @@ init = (interfaceName, projName) ->
copySrcFiles(interfaceName, projName, projNameUs, projNameHyph) copySrcFiles(interfaceName, projName, projNameUs, projNameHyph)
copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.dev, "localhost") copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.dev)
copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.prod) copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.prod)
fs.copySync("#{resources}/images", "./images") fs.copySync("#{resources}/images", "./images")
@ -560,7 +563,8 @@ init = (interfaceName, projName) ->
updateGitIgnore() updateGitIgnore()
generateConfig(interfaceName, projName) config = generateConfig(interfaceName, projName)
generateConfigNs(config);
copyFigwheelBridge(projNameUs) copyFigwheelBridge(projNameUs)
@ -620,7 +624,7 @@ addPlatform = (platform) ->
updateProjectClj(platform) updateProjectClj(platform)
copySrcFilesForPlatform(platform, interfaceName, projName, projNameUs, projNameHyph) copySrcFilesForPlatform(platform, interfaceName, projName, projNameUs, projNameHyph)
copyDevEnvironmentFilesForPlatform(platform, interfaceName, projNameHyph, projName, defaultEnvRoots.dev, "localhost") copyDevEnvironmentFilesForPlatform(platform, interfaceName, projNameHyph, projName, defaultEnvRoots.dev)
copyProdEnvironmentFilesForPlatform(platform, interfaceName, projNameHyph, projName, defaultEnvRoots.prod) copyProdEnvironmentFilesForPlatform(platform, interfaceName, projNameHyph, projName, defaultEnvRoots.prod)
pkg = JSON.parse readFile 'package.json' pkg = JSON.parse readFile 'package.json'
@ -647,6 +651,7 @@ addPlatform = (platform) ->
config.platforms[platform] = config.platforms[platform] =
host: "localhost" host: "localhost"
modules: [] modules: []
generateConfigNs(config)
writeConfig(config) writeConfig(config)
@ -676,11 +681,6 @@ generateRequireModulesCode = (modules) ->
jsCode += "modules['#{m}']=require('#{m}');"; jsCode += "modules['#{m}']=require('#{m}');";
jsCode += '\n' jsCode += '\n'
updateFigwheelUrls = (devEnvRoot, devHost) ->
for platform in platforms
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
edit mainDevPath, [[figwheelUrlRx, "ws://#{devHost[platform]}:"]]
updateIosRCTWebSocketExecutor = (iosHost) -> updateIosRCTWebSocketExecutor = (iosHost) ->
RCTWebSocketExecutorPath = "node_modules/react-native/Libraries/WebSocket/RCTWebSocketExecutor.m" RCTWebSocketExecutorPath = "node_modules/react-native/Libraries/WebSocket/RCTWebSocketExecutor.m"
edit RCTWebSocketExecutorPath, [[debugHostRx, "host] ?: @\"#{iosHost}\";"]] edit RCTWebSocketExecutorPath, [[debugHostRx, "host] ?: @\"#{iosHost}\";"]]
@ -737,7 +737,6 @@ generateDevScripts = () ->
config = readLocalConfig() config = readLocalConfig()
platforms = Object.keys config.platforms platforms = Object.keys config.platforms
projName = config.name projName = config.name
devEnvRoot = config.envRoots.dev
if isSomeDepsMissing() if isSomeDepsMissing()
installDeps(verbose: true) installDeps(verbose: true)
@ -757,11 +756,10 @@ generateDevScripts = () ->
fs.writeFileSync "index.#{platform}.js", "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','#{platform}','#{devHost[platform]}');" fs.writeFileSync "index.#{platform}.js", "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','#{platform}','#{devHost[platform]}');"
log "index.#{platform}.js was regenerated" log "index.#{platform}.js was regenerated"
#updateIosAppDelegate(projName, devHost.ios)
updateIosRCTWebSocketExecutor(devHost.ios) updateIosRCTWebSocketExecutor(devHost.ios)
log "Host in RCTWebSocketExecutor.m was updated" log "Host in RCTWebSocketExecutor.m was updated"
updateFigwheelUrls(devEnvRoot, devHost) generateConfigNs(config);
for platform in platforms for platform in platforms
log "Dev server host for #{platformMeta[platform].name}: #{devHost[platform]}" log "Dev server host for #{platformMeta[platform].name}: #{devHost[platform]}"
@ -821,8 +819,9 @@ doUpgrade = (config) ->
interfaceName = config.interface interfaceName = config.interface
envRoots = config.envRoots envRoots = config.envRoots
copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.dev, "localhost") copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.dev)
copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.prod) copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.prod)
generateConfigNs(config);
log "upgraded files in #{envRoots.dev} and #{envRoots.prod} " log "upgraded files in #{envRoots.dev} and #{envRoots.prod} "
copyFigwheelBridge(projNameUs) copyFigwheelBridge(projNameUs)

View File

@ -2,7 +2,8 @@
(:require [om.next :as om] (:require [om.next :as om]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core] [$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[$PROJECT_NAME_HYPHENATED$.state :as state] [$PROJECT_NAME_HYPHENATED$.state :as state]
[figwheel.client :as fw])) [figwheel.client :as fw]
[env.config :as conf]))
(enable-console-print!) (enable-console-print!)
@ -10,7 +11,7 @@
(assert (exists? core/app-root) "Fatal Error - Your core.cljs file doesn't define an 'app-root' function!!! - Perhaps there was a compilation failure?") (assert (exists? core/app-root) "Fatal Error - Your core.cljs file doesn't define an 'app-root' function!!! - Perhaps there was a compilation failure?")
(fw/start { (fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws" :websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false :heads-up-display false
:jsload-callback #(om/add-root! state/reconciler core/AppRoot 1)}) :jsload-callback #(om/add-root! state/reconciler core/AppRoot 1)})

View File

@ -1,7 +1,8 @@
(ns ^:figwheel-no-load env.$PLATFORM$.main (ns ^:figwheel-no-load env.$PLATFORM$.main
(:require [reagent.core :as r] (:require [reagent.core :as r]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core] [$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[figwheel.client :as fw])) [figwheel.client :as fw]
[env.config :as conf]))
(enable-console-print!) (enable-console-print!)
@ -15,7 +16,7 @@
(def root-el (r/as-element [reloader])) (def root-el (r/as-element [reloader]))
(fw/start { (fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws" :websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false :heads-up-display false
:jsload-callback #(swap! cnt inc)}) :jsload-callback #(swap! cnt inc)})

View File

@ -2,7 +2,8 @@
(:require [reagent.core :as r] (:require [reagent.core :as r]
[re-frame.core :refer [clear-subscription-cache!]] [re-frame.core :refer [clear-subscription-cache!]]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core] [$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[figwheel.client :as fw])) [figwheel.client :as fw]
[env.config :as conf]))
(enable-console-print!) (enable-console-print!)
@ -20,7 +21,7 @@
(swap! cnt inc)) (swap! cnt inc))
(fw/start { (fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws" :websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false :heads-up-display false
:jsload-callback force-reload!}) :jsload-callback force-reload!})

View File

@ -1,6 +1,7 @@
(ns ^:figwheel-no-load env.$PLATFORM$.main (ns ^:figwheel-no-load env.$PLATFORM$.main
(:require [$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core] (:require [$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[figwheel.client :as fw])) [figwheel.client :as fw]
[env.config :as conf]))
(assert (exists? core/init) "Fatal Error - Your core.cljs file doesn't define an 'init' function!!! - Perhaps there was a compilation failure?") (assert (exists? core/init) "Fatal Error - Your core.cljs file doesn't define an 'init' function!!! - Perhaps there was a compilation failure?")
(assert (exists? core/root-component-factory) "Fatal Error - Your core.cljs file doesn't define an 'root-component-factory' function!!! - Perhaps there was a compilation failure?") (assert (exists? core/root-component-factory) "Fatal Error - Your core.cljs file doesn't define an 'root-component-factory' function!!! - Perhaps there was a compilation failure?")
@ -9,7 +10,7 @@
(enable-console-print!) (enable-console-print!)
(fw/start { (fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws" :websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false :heads-up-display false
;; TODO make this Rum something ;; TODO make this Rum something
:jsload-callback #(#'core/mount-app)}) :jsload-callback #(#'core/mount-app)})

7
resources/config.cljs Normal file
View File

@ -0,0 +1,7 @@
(ns env.config)
(def figwheel-urls {
{{#each platforms}}
:{{@key}} "ws://{{this.host}}:3449/figwheel-ws"
{{/each}}
})