Merge pull request #142 from drapanjanas/local-config

Support local configuration
This commit is contained in:
Artūr Girenko 2017-10-03 20:19:32 +02:00 committed by GitHub
commit 6f1ef05e17
2 changed files with 55 additions and 48 deletions

View File

@ -12,7 +12,8 @@
"commander": "^2.8.1", "commander": "^2.8.1",
"fs-extra": "^0.26.5", "fs-extra": "^0.26.5",
"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

@ -15,6 +15,7 @@ cli = require 'commander'
chalk = require 'chalk' chalk = require 'chalk'
semver = require 'semver' semver = require 'semver'
ckDeps = require 'check-dependencies' ckDeps = require 'check-dependencies'
merge = require 'deepmerge'
pkgJson = require __dirname + '/package.json' pkgJson = require __dirname + '/package.json'
nodeVersion = pkgJson.engines.node nodeVersion = pkgJson.engines.node
@ -220,44 +221,49 @@ generateConfig = (interfaceName, projName) ->
modules: [] modules: []
writeConfig config writeConfig config
config
writeConfig = (config, file = ".re-natal") ->
writeConfig = (config) ->
try try
fs.writeFileSync './.re-natal', JSON.stringify config, null, 2 fs.writeFileSync "./#{file}", JSON.stringify config, null, 2
config
catch {message} catch {message}
logErr message logErr message
logErr \ logErr \
if message.match /EACCES/i if message.match /EACCES/i
'Invalid write permissions for creating .re-natal config file' "Invalid write permissions for creating #{file} config file"
else else
message message
verifyConfig = (config) -> verifyConfig = (config) ->
if !config.platforms? || !config.modules? || !config.imageDirs? || !config.interface? || !config.envRoots? if !config.platforms? || !config.modules? || !config.imageDirs? || !config.interface? || !config.envRoots?
throw new Error 're-natal project needs to be upgraded, please run: re-natal upgrade' throw new Error 're-natal project needs to be upgraded, please run: re-natal upgrade'
config config
readConfig = (verify = true)-> readConfig = (file = '.re-natal', mustExist = true, defaultValue = {}) ->
try try
config = JSON.parse readFile '.re-natal' if (mustExist || fs.existsSync(file))
if (verify) JSON.parse readFile file
verifyConfig(config)
else else
config defaultValue
catch {message} catch {message}
logErr \ logErr \
if message.match /ENOENT/i if message.match /ENOENT/i
'No Re-Natal config was found in this directory (.re-natal)' "No Re-Natal config was found in this directory (#{file})"
else if message.match /EACCES/i else if message.match /EACCES/i
'No read permissions for .re-natal' "No read permissions for #{file}"
else if message.match /Unexpected/i else if message.match /Unexpected/i
'.re-natal contains malformed JSON' "#{file} contains malformed JSON"
else else
message message
readAndVerifyConfig = (file) ->
verifyConfig readConfig file
readLocalConfig = () ->
global = readConfig '.re-natal'
local = readConfig '.re-natal.local', false
verifyConfig merge(global, local)
scanImageDir = (dir) -> scanImageDir = (dir) ->
fnames = fs.readdirSync(dir) fnames = fs.readdirSync(dir)
.map (fname) -> "#{dir}/#{fname}" .map (fname) -> "#{dir}/#{fname}"
@ -292,12 +298,12 @@ resolveAndroidDevHost = (deviceType) ->
else else
deviceTypeIsIpAddress(deviceType, Object.keys(allowedTypes)) deviceTypeIsIpAddress(deviceType, Object.keys(allowedTypes))
configureDevHostForAndroidDevice = (deviceType) -> configureDevHostForAndroidDevice = (deviceType, globally = false) ->
try try
configFile = if globally then '.re-natal' else '.re-natal.local'
devHost = resolveAndroidDevHost(deviceType) devHost = resolveAndroidDevHost(deviceType)
config = readConfig() config = merge(readConfig(configFile, false), platforms: android: host: devHost)
config.platforms.android.host = devHost writeConfig(config, configFile)
writeConfig(config)
log "Please run: re-natal use-figwheel to take effect." log "Please run: re-natal use-figwheel to take effect."
catch {message} catch {message}
logErr message logErr message
@ -313,12 +319,12 @@ resolveIosDevHost = (deviceType) ->
else else
deviceTypeIsIpAddress(deviceType, ['simulator', 'real']) deviceTypeIsIpAddress(deviceType, ['simulator', 'real'])
configureDevHostForIosDevice = (deviceType) -> configureDevHostForIosDevice = (deviceType, globally = false) ->
try try
configFile = if globally then '.re-natal' else '.re-natal.local'
devHost = resolveIosDevHost(deviceType) devHost = resolveIosDevHost(deviceType)
config = readConfig() config = merge(readConfig(configFile, false), platforms: ios: host: devHost)
config.platforms.ios.host = devHost writeConfig(config, configFile)
writeConfig(config)
log "Please run: re-natal use-figwheel to take effect." log "Please run: re-natal use-figwheel to take effect."
catch {message} catch {message}
logErr message logErr message
@ -365,7 +371,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/\n") fs.appendFileSync(".gitignore", "\ntarget/")
fs.appendFileSync(".gitignore", "\n.re-natal.local\n")
fs.appendFileSync(".gitignore", "\n# Figwheel\n#\nfigwheel_server.log") fs.appendFileSync(".gitignore", "\n# Figwheel\n#\nfigwheel_server.log")
@ -598,7 +605,7 @@ addPlatform = (platform) ->
if !(platform of platformMeta) if !(platform of platformMeta)
throw new Error "Unknown platform [#{platform}]" throw new Error "Unknown platform [#{platform}]"
config = readConfig() config = readAndVerifyConfig()
platforms = Object.keys config.platforms platforms = Object.keys config.platforms
if platform in platforms if platform in platforms
@ -674,11 +681,6 @@ updateFigwheelUrls = (devEnvRoot, devHost) ->
mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs" mainDevPath = "#{devEnvRoot}/env/#{platform}/main.cljs"
edit mainDevPath, [[figwheelUrlRx, "ws://#{devHost[platform]}:"]] 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) ->
appDelegatePath = "ios/#{projName}/AppDelegate.m"
edit appDelegatePath, [[appDelegateRx, "http://#{iosHost}"]]
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}\";"]]
@ -732,7 +734,7 @@ platformModulesAndImages = (config, platform) ->
generateDevScripts = () -> generateDevScripts = () ->
try try
config = readConfig() config = readLocalConfig()
platforms = Object.keys config.platforms platforms = Object.keys config.platforms
projName = config.name projName = config.name
devEnvRoot = config.envRoots.dev devEnvRoot = config.envRoots.dev
@ -829,7 +831,7 @@ doUpgrade = (config) ->
useComponent = (name, platform) -> useComponent = (name, platform) ->
try try
config = readConfig() config = readAndVerifyConfig()
platforms = Object.keys config.platforms platforms = Object.keys config.platforms
if typeof platform isnt 'string' if typeof platform isnt 'string'
config.modules.push name config.modules.push name
@ -861,7 +863,7 @@ logModuleDifferences = (platform, existingModules, newModules) ->
inferComponents = () -> inferComponents = () ->
requiresByPlatform = buildRequireByPlatformMap() requiresByPlatform = buildRequireByPlatformMap()
config = readConfig() # re-natal file config = readAndVerifyConfig() # re-natal file
logModuleDifferences('common', config.modules, requiresByPlatform.common) logModuleDifferences('common', config.modules, requiresByPlatform.common)
config.modules = requiresByPlatform.common config.modules = requiresByPlatform.common
@ -872,10 +874,10 @@ inferComponents = () ->
writeConfig(config) writeConfig(config)
autoRequire = (enabled) -> autoRequire = (enabled, globally = false) ->
config = readConfig() configFile = if globally then '.re-natal' else '.re-natal.local'
config.autoRequire = enabled config = merge(readConfig(configFile, false), autoRequire: enabled)
writeConfig(config) writeConfig(config, configFile)
if (enabled) if (enabled)
log "Auto-Require feature is enabled in use-figwheel command" log "Auto-Require feature is enabled in use-figwheel command"
else else
@ -909,7 +911,7 @@ cli.command 'init <name>'
cli.command 'upgrade' cli.command 'upgrade'
.description 'upgrades project files to current installed version of re-natal (the upgrade of re-natal itself is done via npm)' .description 'upgrades project files to current installed version of re-natal (the upgrade of re-natal itself is done via npm)'
.action -> .action ->
doUpgrade readConfig(false) doUpgrade readConfig()
cli.command 'add-platform <platform>' cli.command 'add-platform <platform>'
.description 'adds additional app platform: \'windows\' - UWP app, \'wpf\' - WPF app' .description 'adds additional app platform: \'windows\' - UWP app, \'wpf\' - WPF app'
@ -921,7 +923,7 @@ cli.command 'xcode'
.action -> .action ->
ensureOSX -> ensureOSX ->
ensureXcode -> ensureXcode ->
openXcode readConfig().name openXcode readAndVerifyConfig().name
cli.command 'deps' cli.command 'deps'
.description 'install all dependencies for the project' .description 'install all dependencies for the project'
@ -935,13 +937,15 @@ cli.command 'use-figwheel'
cli.command 'use-android-device <type>' cli.command 'use-android-device <type>'
.description 'sets up the host for android device type: \'real\' - localhost, \'avd\' - 10.0.2.2, \'genymotion\' - 10.0.3.2, IP' .description 'sets up the host for android device type: \'real\' - localhost, \'avd\' - 10.0.2.2, \'genymotion\' - 10.0.3.2, IP'
.action (type) -> .option '-g --global', 'use global .re-natal config instead of .re-natal.local'
configureDevHostForAndroidDevice type .action (type, cmd) ->
configureDevHostForAndroidDevice type, cmd.global
cli.command 'use-ios-device <type>' cli.command 'use-ios-device <type>'
.description 'sets up the host for ios device type: \'simulator\' - localhost, \'real\' - auto detect IP on eth0, IP' .description 'sets up the host for ios device type: \'simulator\' - localhost, \'real\' - auto detect IP on eth0, IP'
.action (type) -> .option '-g --global', 'use global .re-natal config instead of .re-natal.local'
configureDevHostForIosDevice type .action (type, cmd) ->
configureDevHostForIosDevice type, cmd.global
cli.command 'use-component <name> [<platform>]' cli.command 'use-component <name> [<platform>]'
.description 'configures a custom component to work with figwheel. Same as \'require\' command.' .description 'configures a custom component to work with figwheel. Same as \'require\' command.'
@ -970,18 +974,20 @@ cli.command 'enable-source-maps'
cli.command 'enable-auto-require' cli.command 'enable-auto-require'
.description 'enables source scanning for automatic required module resolution in use-figwheel command.' .description 'enables source scanning for automatic required module resolution in use-figwheel command.'
.action () -> .option '-g --global', 'use global .re-natal config instead of .re-natal.local'
autoRequire(true) .action (cmd) ->
autoRequire(true, cmd.global)
cli.command 'disable-auto-require' cli.command 'disable-auto-require'
.description 'disables auto-require feature in use-figwheel command' .description 'disables auto-require feature in use-figwheel command'
.action () -> .option '-g --global', 'use global .re-natal config instead of .re-natal.local'
autoRequire(false) .action (cmd) ->
autoRequire(false, cmd.global)
cli.command 'copy-figwheel-bridge' cli.command 'copy-figwheel-bridge'
.description 'copy figwheel-bridge.js into project' .description 'copy figwheel-bridge.js into project'
.action () -> .action () ->
copyFigwheelBridge(readConfig(false).name) copyFigwheelBridge(readConfig().name)
log "Copied figwheel-bridge.js" log "Copied figwheel-bridge.js"
cli.on '*', (command) -> cli.on '*', (command) ->