add a scan for images to 'use-figwheel' CLI command #11

- use-figwheel command scans for all files in image dirs (configured in .re-natal) and adds a require() call to each image index.*.js
 - removed require-img macros
 - core.cljs now uses normal 'js/require' function to load static image.
This commit is contained in:
Artur Girenko 2016-01-17 13:32:42 +01:00
parent f5eae7973e
commit b4e94aa039
7 changed files with 26 additions and 23 deletions

View File

@ -77,7 +77,6 @@ getUuidForDevice = (deviceName) ->
toUnderscored = (s) ->
s.replace(camelRx, '$1_$2').toLowerCase()
checkPort = (port, cb) ->
sock = net.connect {port}, ->
sock.end()
@ -121,6 +120,7 @@ generateConfig = (name) ->
config =
name: name
modules: []
imageDirs: ["images"]
writeConfig config
config
@ -175,6 +175,24 @@ getBundleId = (name) ->
catch {message}
logErr message
scanImageDir = (dir) ->
fnames = fs.readdirSync(dir)
.map (fname) -> "#{dir}/#{fname}"
.filter (path) -> fs.statSync(path).isFile()
.map (path) -> path.replace /@2x|@3x/i, ''
.filter (v, idx, slf) -> slf.indexOf(v) == idx
dirs = fs.readdirSync(dir)
.map (fname) -> "#{dir}/#{fname}"
.filter (path) -> fs.statSync(path).isDirectory()
fnames.concat scanImages(dirs)
scanImages = (dirs) ->
imgs = []
for dir in dirs
imgs = imgs.concat(scanImageDir(dir));
imgs
copyDevEnvironmentFiles = (projNameHyph, projName, devHost) ->
mkdirSync "env/dev"
@ -193,10 +211,6 @@ copyDevEnvironmentFiles = (projNameHyph, projName, devHost) ->
exec "cp #{resources}cljs/main_dev.cljs #{mainAndroidDevPath}"
edit mainAndroidDevPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "android"], [devHostRx, devHost]]
requestImgMacroDevPath = "env/dev/env/require_img.clj"
exec "cp #{resources}require_img_dev.clj #{requestImgMacroDevPath}"
edit requestImgMacroDevPath, [[devHostRx, devHost]]
copyProdEnvironmentFiles = (projNameHyph, projName) ->
mkdirSync "env/prod"
mkdirSync "env/prod/env"
@ -211,9 +225,6 @@ copyProdEnvironmentFiles = (projNameHyph, projName) ->
exec "cp #{resources}cljs/main_prod.cljs #{mainAndroidProdPath}"
edit mainAndroidProdPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName], [platformRx, "android"]]
requestImgMacroProdPath = "env/prod/env/require_img.clj"
exec "cp #{resources}require_img_prod.clj #{requestImgMacroProdPath}"
copyFigwheelBridge = (projNameUs) ->
exec "cp #{resources}figwheel-bridge.js ."
edit "figwheel-bridge.js", [[projNameUsRx, projNameUs]]
@ -424,7 +435,9 @@ generateDevScripts = (devHost) ->
log 'Cleaning...'
exec 'lein clean'
moduleMap = generateRequireModulesCode config.modules
images = scanImages(config.imageDirs).map (fname) -> './' + fname;
modulesAndImages = config.modules.concat images;
moduleMap = generateRequireModulesCode modulesAndImages
fs.writeFileSync 'index.ios.js', "#{moduleMap}require('figwheel-bridge').withModules(modules).start('#{projName}','ios','#{devHost}');"
log 'index.ios.js was regenerated'
@ -455,6 +468,9 @@ doUpgrade = (config) ->
if (!config.modules)
config.modules = []
if (!config.imageDirs)
config.imageDirs = ["images"]
writeConfig(config)
log 'upgraded .re-natal'

View File

@ -1,5 +1,4 @@
(ns ^:figwheel-load $PROJECT_NAME_HYPHENATED$.$PLATFORM$.core
(:require-macros [env.require-img :refer [require-img]])
(:require [reagent.core :as r :refer [atom]]
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
[$PROJECT_NAME_HYPHENATED$.handlers]
@ -13,7 +12,7 @@
(def image (r/adapt-react-class (.-Image js/React)))
(def touchable-highlight (r/adapt-react-class (.-TouchableHighlight js/React)))
(def logo-img (require-img "./images/cljs.png"))
(def logo-img (js/require "./images/cljs.png"))
(defn alert [title]
(.alert (.-Alert js/React) title))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -1,6 +0,0 @@
(ns env.require-img)
(defmacro require-img
"Load image from local packager service"
[src]
{:uri (str "http://$DEV_HOST$:8081/" src)})

View File

@ -1,6 +0,0 @@
(ns env.require-img)
(defmacro require-img
"Creates 'require' statements for images"
[src]
(list 'js/require src))