From 9e14a62f4e71162ebf0e7eaff761910f05b2ffdb Mon Sep 17 00:00:00 2001 From: Artur Girenko Date: Sun, 8 Oct 2017 18:56:30 +0200 Subject: [PATCH] move figwheel url configuration into generated namespace --- package.json | 5 ++-- re-natal.coffee | 39 +++++++++++++-------------- resources/cljs-om-next/main_dev.cljs | 5 ++-- resources/cljs-reagent/main_dev.cljs | 5 ++-- resources/cljs-reagent6/main_dev.cljs | 5 ++-- resources/cljs-rum/main_dev.cljs | 5 ++-- resources/config.cljs | 7 +++++ 7 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 resources/config.cljs diff --git a/package.json b/package.json index 5a64b6f..15cf5eb 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/re-natal.coffee b/re-natal.coffee index 46ff8c9..35d9868 100644 --- a/re-natal.coffee +++ b/re-natal.coffee @@ -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 @@ -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 @@ -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 @@ -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") @@ -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") @@ -560,7 +563,8 @@ init = (interfaceName, projName) -> updateGitIgnore() - generateConfig(interfaceName, projName) + config = generateConfig(interfaceName, projName) + generateConfigNs(config); copyFigwheelBridge(projNameUs) @@ -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' @@ -647,6 +651,7 @@ addPlatform = (platform) -> config.platforms[platform] = host: "localhost" modules: [] + generateConfigNs(config) writeConfig(config) @@ -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}\";"]] @@ -737,7 +737,6 @@ generateDevScripts = () -> config = readLocalConfig() platforms = Object.keys config.platforms projName = config.name - devEnvRoot = config.envRoots.dev if isSomeDepsMissing() installDeps(verbose: true) @@ -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]}" @@ -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) diff --git a/resources/cljs-om-next/main_dev.cljs b/resources/cljs-om-next/main_dev.cljs index c548aa0..384eafa 100644 --- a/resources/cljs-om-next/main_dev.cljs +++ b/resources/cljs-om-next/main_dev.cljs @@ -2,7 +2,8 @@ (: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!) @@ -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?") (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)}) diff --git a/resources/cljs-reagent/main_dev.cljs b/resources/cljs-reagent/main_dev.cljs index e0afa8a..c5a6408 100644 --- a/resources/cljs-reagent/main_dev.cljs +++ b/resources/cljs-reagent/main_dev.cljs @@ -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!) @@ -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)}) diff --git a/resources/cljs-reagent6/main_dev.cljs b/resources/cljs-reagent6/main_dev.cljs index 571ac77..23a5a15 100644 --- a/resources/cljs-reagent6/main_dev.cljs +++ b/resources/cljs-reagent6/main_dev.cljs @@ -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!) @@ -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!}) diff --git a/resources/cljs-rum/main_dev.cljs b/resources/cljs-rum/main_dev.cljs index ae84083..6b88980 100644 --- a/resources/cljs-rum/main_dev.cljs +++ b/resources/cljs-rum/main_dev.cljs @@ -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?") @@ -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)}) diff --git a/resources/config.cljs b/resources/config.cljs new file mode 100644 index 0000000..b6a84e0 --- /dev/null +++ b/resources/config.cljs @@ -0,0 +1,7 @@ +(ns env.config) + +(def figwheel-urls { + {{#each platforms}} + :{{@key}} "ws://{{this.host}}:3449/figwheel-ws" + {{/each}} + }) \ No newline at end of file