Merge pull request #119 from carocad/infer

Infer components using a regex
This commit is contained in:
Artūr Girenko 2017-06-21 20:56:51 +02:00 committed by GitHub
commit 7bfbcf832a
2 changed files with 36 additions and 3 deletions

View File

@ -7,11 +7,12 @@
"license": "MIT",
"dependencies": {
"chalk": "^1.1.1",
"check-dependencies": "^1.0.1",
"coffee-script": "^1.9.3",
"commander": "^2.8.1",
"fs-extra": "^0.26.5",
"semver": "^5.0.1",
"check-dependencies":"^1.0.1"
"klaw-sync": "^2.1.0",
"semver": "^5.0.1"
},
"engines": {
"node": ">=4.0.0"

View File

@ -5,6 +5,7 @@
# MIT License
fs = require 'fs-extra'
klawSync = require 'klaw-sync'
fpath = require 'path'
net = require 'net'
http = require 'http'
@ -215,8 +216,9 @@ generateConfig = (interfaceName, projName) ->
writeConfig = (config) ->
try
fs.writeFileSync '.re-natal', JSON.stringify config, null, 2
fs.writeFileSync './.re-natal', JSON.stringify config, null, 2
catch {message}
logErr message
logErr \
if message.match /EACCES/i
'Invalid write permissions for creating .re-natal config file'
@ -774,6 +776,31 @@ useComponent = (name, platform) ->
catch {message}
logErr message
inferComponents = () ->
onlyUserCljs = (item) -> fpath.extname(item.path) == '.cljs' and
item.path.indexOf('/target/') < 0 # ignore target dir
jsRequire = /js\/require \"(.+)\"/g
files = klawSync process.cwd(),
nodir: true
filter: onlyUserCljs
filenames = files.map((o) -> o.path)
contents = filenames.map((path) -> fs.readFileSync(path, encoding: 'utf8'))
config = readConfig() # re-natal file
requires = new Set()
contents.forEach((text) ->
while match = jsRequire.exec(text)
requires.add(match[1]) if match[1].indexOf(config.imageDirs) < 0)
modules = new Set(config.modules)
difference = new Set(Array.from(requires).filter((m) -> !modules.has(m)))
if(difference.size isnt 0)
log "new component import found #{Array.from(difference)}"
config.modules = Array.from(requires)
writeConfig(config)
else
log "no new component was imported, defaulting to #{Array.from(modules)}"
cli._name = 're-natal'
cli.version pkgJson.version
@ -841,6 +868,11 @@ cli.command 'use-component <name> [<platform>]'
.action (name, platform) ->
useComponent(name, platform)
cli.command 'infer-components'
.description 'parses all cljs files in this project, extracts all (js/require) components calls and uses them to populate the re-natal file'
.action () ->
inferComponents()
cli.command 'enable-source-maps'
.description 'patches RN packager to server *.map files from filesystem, so that chrome can download them.'
.action () ->