-
Notifications
You must be signed in to change notification settings - Fork 117
/
natal.coffee
611 lines (490 loc) · 16.6 KB
/
natal.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# Natal
# Bootstrap ClojureScript React Native apps
# Dan Motzenbecker
# http://oxism.com
# MIT License
fs = require 'fs'
net = require 'net'
http = require 'http'
crypto = require 'crypto'
child = require 'child_process'
cli = require 'commander'
chalk = require 'chalk'
semver = require 'semver'
{Tail} = require 'tail'
pkgJson = require __dirname + '/package.json'
globalVerbose = false
verboseFlag = '-v, --verbose'
verboseText = 'verbose output'
nodeVersion = pkgJson.engines.node
resources = __dirname + '/resources/'
validNameRx = /^[A-Z][0-9A-Z]*$/i
camelRx = /([a-z])([A-Z])/g
projNameRx = /\$PROJECT_NAME\$/g
projNameHyphRx = /\$PROJECT_NAME_HYPHENATED\$/g
rnVersion = '0.19.0'
rnPackagerPort = 8081
podMinVersion = '0.38.2'
rnCliMinVersion = '0.1.10'
process.title = 'natal'
reactInterfaces =
om: 'org.omcljs/om "0.9.0"'
'om-next': 'org.omcljs/om "1.0.0-alpha28"'
interfaceNames = Object.keys reactInterfaces
defaultInterface = 'om'
sampleCommands =
om: '(swap! app-state assoc :text "Hello Native World")'
'om-next': '(swap! app-state assoc :app/msg "Hello Native World")'
log = (s, color = 'green') ->
console.log chalk[color] s
logErr = (err, color = 'red') ->
console.error chalk[color] err
process.exit 1
verboseDec = (fn) ->
(..., cmd) ->
globalVerbose = cmd.verbose
fn.apply cli, arguments
exec = (cmd, keepOutput) ->
if globalVerbose and !keepOutput
return child.execSync cmd, stdio: 'inherit'
if keepOutput
child.execSync cmd
else
child.execSync cmd, stdio: 'ignore'
readFile = (path) ->
fs.readFileSync path, encoding: 'ascii'
edit = (path, pairs) ->
fs.writeFileSync path, pairs.reduce (contents, [rx, replacement]) ->
contents.replace rx, replacement
, readFile path
pluckUuid = (line) ->
line.match(/\[(.+)\]/)[1]
getUuidForDevice = (deviceName) ->
device = getDeviceList().find (line) -> line.match deviceName
unless device
logErr "Cannot find device `#{deviceName}`"
pluckUuid device
toUnderscored = (s) ->
s.replace(camelRx, '$1_$2').toLowerCase()
checkPort = (port, cb) ->
sock = net.connect {port}, ->
sock.end()
http.get "http://localhost:#{port}/status", (res) ->
data = ''
res.on 'data', (chunk) -> data += chunk
res.on 'end', ->
cb data.toString() isnt 'packager-status:running'
.on 'error', -> cb true
.setTimeout 3000
sock.on 'error', ->
sock.end()
cb false
ensureFreePort = (cb) ->
checkPort rnPackagerPort, (inUse) ->
if inUse
logErr "
Port #{rnPackagerPort} is currently in use by another process
and is needed by the React Native packager.
"
cb()
generateConfig = (name) ->
log 'Creating Natal config'
config =
name: name
device: getUuidForDevice 'iPhone 6'
writeConfig config
config
writeConfig = (config) ->
try
fs.writeFileSync '.natal', JSON.stringify config, null, 2
catch {message}
logErr \
if message.match /EACCES/i
'Invalid write permissions for creating .natal config file'
else
message
readConfig = ->
try
JSON.parse readFile '.natal'
catch {message}
logErr \
if message.match /ENOENT/i
'No Natal config was found in this directory (.natal)'
else if message.match /EACCES/i
'No read permissions for .natal'
else if message.match /Unexpected/i
'.natal contains malformed JSON'
else
message
getBundleId = (name) ->
try
if line = readFile "native/ios/#{name}.xcodeproj/project.pbxproj"
.match /PRODUCT_BUNDLE_IDENTIFIER = (.+);/
line[1]
else if line = readFile "native/ios/#{name}/Info.plist"
.match /\<key\>CFBundleIdentifier\<\/key\>\n?\s*\<string\>(.+)\<\/string\>/
rfcIdRx = /\$\(PRODUCT_NAME\:rfc1034identifier\)/
if line[1].match rfcIdRx
line[1].replace rfcIdRx, name
else
line[1]
else
throw new Error 'Cannot find bundle identifier in project.pbxproj or Info.plist'
catch {message}
logErr message
installRnCli = ->
try
exec "npm i -g react-native-cli@#{rnCliMinVersion}"
catch
logErr """
react-native-cli@#{rnCliMinVersion} is required
Run `[sudo] npm i -g react-native-cli@#{rnCliMinVersion}` then try again
"""
init = (projName, interfaceName) ->
log "Creating #{projName}", 'bgMagenta'
log ''
if projName.toLowerCase() is 'react' or !projName.match validNameRx
logErr 'Invalid project name. Use an alphanumeric CamelCase name.'
try
cliVersion = exec('react-native --version', true).toString().trim()
unless semver.satisfies rnCliMinVersion, ">=#{rnCliMinVersion}"
installRnCli()
catch
installRnCli()
projNameHyph = projName.replace(camelRx, '$1-$2').toLowerCase()
projNameUs = toUnderscored projName
try
if fs.existsSync projNameHyph
throw new Error "Directory #{projNameHyph} already exists"
exec 'type lein'
exec 'type pod'
exec 'type watchman'
exec 'type xcodebuild'
podVersion = exec('pod --version', true).toString().trim().replace /\.beta.+$/, ''
unless semver.satisfies podVersion, ">=#{podMinVersion}"
throw new Error """
Natal requires CocoaPods #{podMinVersion} or higher (you have #{podVersion}).
Run [sudo] gem update cocoapods and try again.
"""
log 'Creating Leiningen project'
exec "lein new #{projNameHyph}"
log 'Updating Leiningen project'
process.chdir projNameHyph
exec "cp #{resources}project.clj project.clj"
edit \
'project.clj',
[
[projNameHyphRx, projNameHyph]
[/\$REACT_INTERFACE\$/, reactInterfaces[interfaceName]]
]
corePath = "src/#{projNameUs}/core.clj"
fs.unlinkSync corePath
corePath += 's'
exec "cp #{resources}#{interfaceName}.cljs #{corePath}"
edit corePath, [[projNameHyphRx, projNameHyph], [projNameRx, projName]]
log 'Creating React Native skeleton'
fs.mkdirSync 'native'
process.chdir 'native'
fs.writeFileSync 'package.json', JSON.stringify
name: projName
version: '0.0.1'
private: true
scripts:
start: 'node_modules/react-native/packager/packager.sh'
dependencies:
'react-native': rnVersion
, null, 2
exec 'npm i'
exec "
node -e
\"process.argv[3]='#{projName}';
require('react-native/local-cli/cli').init('.', '#{projName}')\"
"
exec 'rm -rf android'
fs.unlinkSync 'index.android.js'
fs.appendFileSync '.gitignore', '\n# CocoaPods\n#\nios/Pods\n'
log 'Installing Pod dependencies'
process.chdir 'ios'
exec "cp #{resources}Podfile ."
edit 'Podfile', [[projNameRx, projName]]
exec 'pod install'
log 'Updating Xcode project'
for ext in ['m', 'h']
path = "#{projName}/AppDelegate.#{ext}"
exec "cp #{resources}AppDelegate.#{ext} #{path}"
edit path, [[projNameRx, projName], [projNameHyphRx, projNameHyph]]
uuid1 = crypto
.createHash 'md5'
.update projName, 'utf8'
.digest('hex')[...24]
.toUpperCase()
uuid2 = uuid1.split ''
uuid2.splice 7, 1, ((parseInt(uuid1[7], 16) + 1) % 16).toString(16).toUpperCase()
uuid2 = uuid2.join ''
edit \
"#{projName}.xcodeproj/project.pbxproj",
[
[
/OTHER_LDFLAGS = "-ObjC";/g
'OTHER_LDFLAGS = "${inherited}";'
]
[
/\/\* End PBXBuildFile section \*\//
"\t\t#{uuid2} /* out in Resources */ =
{isa = PBXBuildFile; fileRef = #{uuid1} /* out */; };
\n/* End PBXBuildFile section */"
]
[
/\/\* End PBXFileReference section \*\//
"\t\t#{uuid1} /* out */ = {isa = PBXFileReference; lastKnownFileType
= folder; name = out; path = ../../target/out;
sourceTree = \"<group>\"; };\n/* End PBXFileReference section */"
]
[
/main.jsbundle \*\/\,/
"main.jsbundle */,\n\t\t\t\t#{uuid1} /* out */,"
]
[
/\/\* LaunchScreen.xib in Resources \*\/\,/
"/* LaunchScreen.xib in Resources */,
\n\t\t\t\t#{uuid2} /* out in Resources */,"
]
]
testId = readFile("#{projName}.xcodeproj/project.pbxproj")
.match(new RegExp "([0-9A-F]+) \/\\* #{projName}Tests \\*\/ = \\{")[1]
edit \
"#{projName}.xcodeproj/xcshareddata/xcschemes/#{projName}.xcscheme",
[
[
/\<Testables\>\n\s*\<\/Testables\>/
"""
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "#{testId}"
BuildableName = "#{projName}Tests.xctest"
BlueprintName = "#{projName}Tests"
ReferencedContainer = "container:#{projName}.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
"""
]
]
process.chdir '../..'
launch generateConfig projName
log ''
log 'To get started with your new app, first cd into its directory:', 'yellow'
log "cd #{projNameHyph}", 'inverse'
log ''
log 'Boot the REPL by typing:', 'yellow'
log 'natal repl', 'inverse'
log ''
log 'At the REPL prompt type this:', 'yellow'
log "(in-ns '#{projNameHyph}.core)", 'inverse'
log ''
log 'Changes you make via the REPL or by changing your .cljs files should appear live.', 'yellow'
log ''
log 'Try this command as an example:', 'yellow'
log sampleCommands[interfaceName], 'inverse'
log ''
log '✔ Done', 'bgMagenta'
log ''
catch {message}
logErr \
if message.match /type.+lein/i
'Leiningen is required (http://leiningen.org)'
else if message.match /type.+pod/i
'CocoaPods is required (https://cocoapods.org)'
else if message.match /type.+watchman/i
'Watchman is required (https://facebook.github.io/watchman)'
else if message.match /type.+xcodebuild/i
'Xcode Command Line Tools are required'
else if message.match /npm/i
"npm install failed. This may be a network issue. Check #{projNameHyph}/native/npm-debug.log for details."
else
message
launch = ({name, device}) ->
unless device in getDeviceUuids()
log 'Device ID not available, defaulting to iPhone 6 simulator', 'yellow'
{device} = generateConfig name
try
fs.statSync 'native/node_modules'
fs.statSync 'native/ios/Pods'
catch
logErr 'Dependencies are missing. Run natal deps to install them.'
log 'Compiling ClojureScript'
exec 'lein cljsbuild once dev'
log 'Compiling Xcode project'
try
exec "
xcodebuild
-workspace native/ios/#{name}.xcworkspace
-scheme #{name}
-destination platform='iOS Simulator',OS=latest,id='#{device}'
test
"
log 'Launching simulator'
exec "xcrun simctl launch #{device} #{getBundleId name}"
catch {message}
logErr message
openXcode = (name) ->
try
exec "open native/ios/#{name}.xcworkspace"
catch {message}
logErr \
if message.match /ENOENT/i
"""
Cannot find #{name}.xcworkspace in native/ios.
Run this command from your project's root directory.
"""
else if message.match /EACCES/i
"Invalid permissions for opening #{name}.xcworkspace in native/ios"
else
message
getDeviceList = ->
try
exec 'xcrun instruments -s devices', true
.toString()
.split '\n'
.filter (line) -> /^i/.test line
catch {message}
logErr 'Device listing failed: ' + message
getDeviceUuids = ->
getDeviceList().map (line) -> line.match(/\[(.+)\]/)[1]
startRepl = (name, autoChoose) ->
try
exec 'type rlwrap'
catch
log 'Note: rlwrap is not installed but is recommended for REPL use.', 'yellow'
log 'You can optionally install it and run `rlwrap natal repl` for proper arrow key support in the REPL.\n', 'gray'
log 'Starting REPL'
try
lein = child.spawn 'lein', 'trampoline run -m clojure.main -e'.split(' ').concat(
"""
(require '[cljs.repl :as repl])
(require '[ambly.core :as ambly])
(let [repl-env (ambly/repl-env#{if autoChoose then ' :choose-first-discovered true' else ''})]
(repl/repl repl-env
:watch \"src\"
:watch-fn
(fn []
(repl/load-file repl-env \"src/#{toUnderscored name}/core.cljs\"))
:analyze-path \"src\"))
"""),
cwd: process.cwd()
env: process.env
onLeinOut = (chunk) ->
if path = chunk.toString().match /Watch compilation log available at:\s(.+)/
lein.stdout.removeListener 'data', onLeinOut
setTimeout ->
tail = new Tail path[1]
tail.on 'line', (line) ->
if line.match /^WARNING/ or line.match /failed compiling/
log line, 'red'
lein.stdin.write '\n'
else if line.match /done\. Elapsed/
log line, 'green'
lein.stdin.write '\n'
else if line.match /^Change detected/
log '\n' + line, 'white'
else if line.match /^Watching paths/
log '\n' + line, 'white'
lein.stdin.write '\n'
else if line.match /^Compiling/
log line, 'white'
else
log line, 'gray'
, 1000
lein.stdout.on 'data', onLeinOut
lein.stdout.pipe process.stdout
process.stdin.pipe lein.stdin
catch {message}
logErr message
cli._name = 'natal'
cli.version pkgJson.version
cli.command 'init <name>'
.description 'create a new ClojureScript React Native project'
.option verboseFlag, verboseText
.option "-i, --interface [#{interfaceNames.join ' '}]", 'specify React interface'
.action verboseDec (name, cmd) ->
if cmd
interfaceName = cmd['interface'] or defaultInterface
else
interfaceName = defaultInterface
unless reactInterfaces[interfaceName]
logErr "Unsupported React interface: #{interfaceName}"
if typeof name isnt 'string'
logErr '''
natal init requires a project name as the first argument.
e.g.
natal init HelloWorld
'''
ensureFreePort -> init name, interfaceName
cli.command 'launch'
.description 'compile project and run in simulator'
.option verboseFlag, verboseText
.action verboseDec ->
ensureFreePort -> launch readConfig()
cli.command 'repl'
.description 'launch a ClojureScript REPL with background compilation'
.option verboseFlag, verboseText
.option '-c, --choose', 'choose target device from list'
.action verboseDec (cmd) ->
startRepl readConfig().name, !cmd.choose
cli.command 'listdevices'
.description 'list available simulator devices by index'
.option verboseFlag, verboseText
.action verboseDec ->
console.log (getDeviceList()
.map (line, i) -> "#{i}\t#{line.replace /\[.+\]/, ''}"
.join '\n')
cli.command 'setdevice <index>'
.description 'choose simulator device by index'
.option verboseFlag, verboseText
.action verboseDec (index) ->
unless device = getDeviceList()[parseInt index, 10]
logErr 'Invalid device index. Run natal listdevices for valid indexes.'
config = readConfig()
config.device = pluckUuid device
writeConfig config
cli.command 'xcode'
.description 'open Xcode project'
.option verboseFlag, verboseText
.action verboseDec ->
openXcode readConfig().name
cli.command 'deps'
.description 'install all dependencies for the project'
.option verboseFlag, verboseText
.option '-l, --lein', 'Leiningen jars only'
.option '-n, --npm', 'npm packages only'
.option '-p, --pods', 'pods only'
.action verboseDec (cmd) ->
all = ['lein', 'npm', 'pods'].every (o) -> !cmd[o]
try
if all or cmd.lein
log 'Installing Leiningen jars'
exec 'lein deps'
if all or cmd.npm
process.chdir 'native'
log 'Installing npm packages'
exec 'npm i'
if all or cmd.pods
log 'Installing pods'
process.chdir if all or cmd.npm then 'ios' else 'native/ios'
exec 'pod install'
catch {message}
logErr message
cli.on '*', (command) ->
logErr "unknown command #{command[0]}. See natal --help for valid commands"
unless semver.satisfies process.version[1...], nodeVersion
logErr """
Natal requires Node.js version #{nodeVersion}
You have #{process.version[1...]}
"""
if process.argv.length <= 2
cli.outputHelp()
else
cli.parse process.argv