Skip to content

Commit

Permalink
Merge pull request #146 from drapanjanas/move-host-config-to-generate…
Browse files Browse the repository at this point in the history
…d-ns

Move figwheel url configuration into generated namespace
  • Loading branch information
drapanjanas authored Oct 8, 2017
2 parents 181a208 + 9e14a62 commit e67e7ae
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"check-dependencies": "^1.0.1",
"coffee-script": "^1.9.3",
"commander": "^2.8.1",
"deepmerge": "^1.5.2",
"fs-extra": "^0.26.5",
"handlebars": "^4.0.10",
"klaw-sync": "^2.1.0",
"semver": "^5.0.1",
"deepmerge": "^1.5.2"
"semver": "^5.0.1"
},
"engines": {
"node": ">=4.0.0"
Expand Down
39 changes: 19 additions & 20 deletions re-natal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ chalk = require 'chalk'
semver = require 'semver'
ckDeps = require 'check-dependencies'
merge = require 'deepmerge'
hb = require 'handlebars'
pkgJson = require __dirname + '/package.json'

nodeVersion = pkgJson.engines.node
Expand All @@ -29,10 +30,7 @@ interfaceDepsRx = /\$INTERFACE_DEPS\$/g
platformRx = /\$PLATFORM\$/g
platformCleanRx = /#_\(\$PLATFORM_CLEAN\$\)/g
platformCleanId = "#_($PLATFORM_CLEAN$)"
devHostRx = /\$DEV_HOST\$/g
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
namespaceRx = /\(ns\s+([A-Za-z0-9.-]+)/g
jsRequireRx = /js\/require "(.+)"/g
Expand Down Expand Up @@ -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')
deviceType

copyDevEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, devEnvRoot, devHost) ->
copyDevEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, devEnvRoot) ->
cljsDir = interfaceConf[interfaceName].cljsDir
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]]
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"
fs.copySync("#{resources}/user.clj", userNsPath)

for platform in platforms
copyDevEnvironmentFilesForPlatform platform, interfaceName, projNameHyph, projName, devEnvRoot, devHost
copyDevEnvironmentFilesForPlatform platform, interfaceName, projNameHyph, projName, devEnvRoot

copyProdEnvironmentFilesForPlatform = (platform, interfaceName, projNameHyph, projName, prodEnvRoot) ->
cljsDir = interfaceConf[interfaceName].cljsDir
Expand All @@ -372,7 +374,8 @@ updateGitIgnore = () ->
indexFiles = platforms.map (platform) -> "index.#{platform}.js"
fs.appendFileSync(".gitignore", indexFiles.join("\n"))
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")

Expand Down Expand Up @@ -516,7 +519,7 @@ init = (interfaceName, projName) ->

copySrcFiles(interfaceName, projName, projNameUs, projNameHyph)

copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.dev, "localhost")
copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.dev)
copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, defaultEnvRoots.prod)

fs.copySync("#{resources}/images", "./images")
Expand Down Expand Up @@ -560,7 +563,8 @@ init = (interfaceName, projName) ->

updateGitIgnore()

generateConfig(interfaceName, projName)
config = generateConfig(interfaceName, projName)
generateConfigNs(config);

copyFigwheelBridge(projNameUs)

Expand Down Expand Up @@ -620,7 +624,7 @@ addPlatform = (platform) ->

updateProjectClj(platform)
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)

pkg = JSON.parse readFile 'package.json'
Expand All @@ -647,6 +651,7 @@ addPlatform = (platform) ->
config.platforms[platform] =
host: "localhost"
modules: []
generateConfigNs(config)

writeConfig(config)

Expand Down Expand Up @@ -676,11 +681,6 @@ generateRequireModulesCode = (modules) ->
jsCode += "modules['#{m}']=require('#{m}');";
jsCode += '\n'

updateFigwheelUrls = (devEnvRoot, devHost) ->
for platform in platforms
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
edit mainDevPath, [[figwheelUrlRx, "ws://#{devHost[platform]}:"]]

updateIosRCTWebSocketExecutor = (iosHost) ->
RCTWebSocketExecutorPath = "node_modules/react-native/Libraries/WebSocket/RCTWebSocketExecutor.m"
edit RCTWebSocketExecutorPath, [[debugHostRx, "host] ?: @\"#{iosHost}\";"]]
Expand Down Expand Up @@ -737,7 +737,6 @@ generateDevScripts = () ->
config = readLocalConfig()
platforms = Object.keys config.platforms
projName = config.name
devEnvRoot = config.envRoots.dev

if isSomeDepsMissing()
installDeps(verbose: true)
Expand All @@ -757,11 +756,10 @@ generateDevScripts = () ->
fs.writeFileSync "index.#{platform}.js", "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','#{platform}','#{devHost[platform]}');"
log "index.#{platform}.js was regenerated"

#updateIosAppDelegate(projName, devHost.ios)
updateIosRCTWebSocketExecutor(devHost.ios)
log "Host in RCTWebSocketExecutor.m was updated"

updateFigwheelUrls(devEnvRoot, devHost)
generateConfigNs(config);
for platform in platforms
log "Dev server host for #{platformMeta[platform].name}: #{devHost[platform]}"

Expand Down Expand Up @@ -821,8 +819,9 @@ doUpgrade = (config) ->
interfaceName = config.interface
envRoots = config.envRoots

copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.dev, "localhost")
copyDevEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.dev)
copyProdEnvironmentFiles(interfaceName, projNameHyph, projName, envRoots.prod)
generateConfigNs(config);
log "upgraded files in #{envRoots.dev} and #{envRoots.prod} "

copyFigwheelBridge(projNameUs)
Expand Down
5 changes: 3 additions & 2 deletions resources/cljs-om-next/main_dev.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
(:require [om.next :as om]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[$PROJECT_NAME_HYPHENATED$.state :as state]
[figwheel.client :as fw]))
[figwheel.client :as fw]
[env.config :as conf]))

(enable-console-print!)

(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/app-root) "Fatal Error - Your core.cljs file doesn't define an 'app-root' function!!! - Perhaps there was a compilation failure?")

(fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws"
:websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false
:jsload-callback #(om/add-root! state/reconciler core/AppRoot 1)})

Expand Down
5 changes: 3 additions & 2 deletions resources/cljs-reagent/main_dev.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns ^:figwheel-no-load env.$PLATFORM$.main
(:require [reagent.core :as r]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[figwheel.client :as fw]))
[figwheel.client :as fw]
[env.config :as conf]))

(enable-console-print!)

Expand All @@ -15,7 +16,7 @@
(def root-el (r/as-element [reloader]))

(fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws"
:websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false
:jsload-callback #(swap! cnt inc)})

Expand Down
5 changes: 3 additions & 2 deletions resources/cljs-reagent6/main_dev.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [reagent.core :as r]
[re-frame.core :refer [clear-subscription-cache!]]
[$PROJECT_NAME_HYPHENATED$.$PLATFORM$.core :as core]
[figwheel.client :as fw]))
[figwheel.client :as fw]
[env.config :as conf]))

(enable-console-print!)

Expand All @@ -20,7 +21,7 @@
(swap! cnt inc))

(fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws"
:websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false
:jsload-callback force-reload!})

Expand Down
5 changes: 3 additions & 2 deletions resources/cljs-rum/main_dev.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns ^:figwheel-no-load env.$PLATFORM$.main
(: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/root-component-factory) "Fatal Error - Your core.cljs file doesn't define an 'root-component-factory' function!!! - Perhaps there was a compilation failure?")
Expand All @@ -9,7 +10,7 @@
(enable-console-print!)

(fw/start {
:websocket-url "ws://$DEV_HOST$:3449/figwheel-ws"
:websocket-url (:$PLATFORM$ conf/figwheel-urls)
:heads-up-display false
;; TODO make this Rum something
:jsload-callback #(#'core/mount-app)})
Expand Down
7 changes: 7 additions & 0 deletions resources/config.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns env.config)

(def figwheel-urls {
{{#each platforms}}
:{{@key}} "ws://{{this.host}}:3449/figwheel-ws"
{{/each}}
})

0 comments on commit e67e7ae

Please sign in to comment.