@craft-native/ios 0.0.100 → 0.0.101
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
- package/templates/CraftApp.swift +77 -4
package/dist/cli.js
CHANGED
|
@@ -390,12 +390,13 @@ async function init(options) {
|
|
|
390
390
|
}
|
|
391
391
|
const finalBundleId = bundleId || `com.craft.${name.toLowerCase()}`;
|
|
392
392
|
const bundleIdPrefix = finalBundleId.split(".").slice(0, -1).join(".");
|
|
393
|
+
const given = Object.fromEntries(Object.entries(options.config ?? {}).filter(([, value]) => value !== undefined));
|
|
393
394
|
const config = {
|
|
394
395
|
...DEFAULT_CONFIG,
|
|
395
396
|
appName: displayName,
|
|
396
397
|
bundleId: finalBundleId,
|
|
397
398
|
teamId: teamId || "",
|
|
398
|
-
...
|
|
399
|
+
...given
|
|
399
400
|
};
|
|
400
401
|
if (config.enableBackgroundLocation)
|
|
401
402
|
config.enableGeolocation = true;
|
package/dist/index.js
CHANGED
|
@@ -385,12 +385,13 @@ async function init(options) {
|
|
|
385
385
|
}
|
|
386
386
|
const finalBundleId = bundleId || `com.craft.${name.toLowerCase()}`;
|
|
387
387
|
const bundleIdPrefix = finalBundleId.split(".").slice(0, -1).join(".");
|
|
388
|
+
const given = Object.fromEntries(Object.entries(options.config ?? {}).filter(([, value]) => value !== undefined));
|
|
388
389
|
const config = {
|
|
389
390
|
...DEFAULT_CONFIG,
|
|
390
391
|
appName: displayName,
|
|
391
392
|
bundleId: finalBundleId,
|
|
392
393
|
teamId: teamId || "",
|
|
393
|
-
...
|
|
394
|
+
...given
|
|
394
395
|
};
|
|
395
396
|
if (config.enableBackgroundLocation)
|
|
396
397
|
config.enableGeolocation = true;
|
package/package.json
CHANGED
package/templates/CraftApp.swift
CHANGED
|
@@ -337,17 +337,40 @@ class AppState: ObservableObject {
|
|
|
337
337
|
@Published var config: CraftConfig
|
|
338
338
|
|
|
339
339
|
init() {
|
|
340
|
-
// Load config from craft.config.json if available
|
|
341
340
|
if let configURL = Bundle.main.url(forResource: "craft.config", withExtension: "json"),
|
|
342
|
-
let data = try? Data(contentsOf: configURL)
|
|
343
|
-
|
|
344
|
-
self.config = config
|
|
341
|
+
let data = try? Data(contentsOf: configURL) {
|
|
342
|
+
self.config = CraftConfig.load(from: data)
|
|
345
343
|
} else {
|
|
346
344
|
self.config = CraftConfig()
|
|
347
345
|
}
|
|
348
346
|
}
|
|
349
347
|
}
|
|
350
348
|
|
|
349
|
+
extension CraftConfig {
|
|
350
|
+
/// The bundled config laid over the defaults, key by key.
|
|
351
|
+
///
|
|
352
|
+
/// Decoded whole, one missing or mistyped key failed the entire file and
|
|
353
|
+
/// the app ran on `CraftConfig()` — every capability off, silently: the
|
|
354
|
+
/// network bridge refused, the page read that as offline, and nothing said
|
|
355
|
+
/// why. A key the file leaves out now keeps its default, and a file that
|
|
356
|
+
/// still cannot be read says so in the log.
|
|
357
|
+
static func load(from data: Data) -> CraftConfig {
|
|
358
|
+
let defaults = (try? JSONEncoder().encode(CraftConfig()))
|
|
359
|
+
.flatMap { try? JSONSerialization.jsonObject(with: $0) as? [String: Any] } ?? [:]
|
|
360
|
+
guard let given = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
|
|
361
|
+
NSLog("[craft] craft.config.json is not a JSON object; running on defaults")
|
|
362
|
+
return CraftConfig()
|
|
363
|
+
}
|
|
364
|
+
let merged = defaults.merging(given.filter { !($0.value is NSNull) }) { _, bundled in bundled }
|
|
365
|
+
do {
|
|
366
|
+
return try JSONDecoder().decode(CraftConfig.self, from: JSONSerialization.data(withJSONObject: merged))
|
|
367
|
+
} catch {
|
|
368
|
+
NSLog("[craft] craft.config.json could not be read, running on defaults: %@", String(describing: error))
|
|
369
|
+
return CraftConfig()
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
351
374
|
// MARK: - Configuration
|
|
352
375
|
struct CraftConfig: Codable {
|
|
353
376
|
var appName: String = "Craft App"
|
|
@@ -514,6 +537,48 @@ final class BundledAssetSchemeHandler: NSObject, WKURLSchemeHandler {
|
|
|
514
537
|
}
|
|
515
538
|
}
|
|
516
539
|
|
|
540
|
+
#if DEBUG
|
|
541
|
+
/// Relays the page's console and uncaught errors to NSLog in debug builds.
|
|
542
|
+
final class PageConsoleRelay: NSObject, WKScriptMessageHandler {
|
|
543
|
+
static let script = """
|
|
544
|
+
(function() {
|
|
545
|
+
var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.craftLog;
|
|
546
|
+
if (!handler) return;
|
|
547
|
+
function text(value) {
|
|
548
|
+
try { return typeof value === 'string' ? value : (value && value.stack) || JSON.stringify(value); }
|
|
549
|
+
catch (e) { return String(value); }
|
|
550
|
+
}
|
|
551
|
+
function send(level, message) { try { handler.postMessage({ level: level, message: String(message).slice(0, 4000) }); } catch (e) {} }
|
|
552
|
+
['error', 'warn', 'log', 'info'].forEach(function(level) {
|
|
553
|
+
var original = console[level];
|
|
554
|
+
console[level] = function() {
|
|
555
|
+
send(level, Array.prototype.map.call(arguments, text).join(' '));
|
|
556
|
+
return original.apply(console, arguments);
|
|
557
|
+
};
|
|
558
|
+
});
|
|
559
|
+
// Capture phase: a script or stylesheet that fails to load fires its
|
|
560
|
+
// error on the element, which never bubbles to window.
|
|
561
|
+
window.addEventListener('error', function(event) {
|
|
562
|
+
var target = event.target;
|
|
563
|
+
if (target && target !== window && (target.src || target.href)) {
|
|
564
|
+
send('error', 'failed to load ' + (target.src || target.href));
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
send('error', 'uncaught ' + event.message + ' at ' + event.filename + ':' + event.lineno + ':' + event.colno);
|
|
568
|
+
}, true);
|
|
569
|
+
window.addEventListener('unhandledrejection', function(event) {
|
|
570
|
+
send('error', 'unhandled rejection ' + text(event.reason));
|
|
571
|
+
});
|
|
572
|
+
})();
|
|
573
|
+
"""
|
|
574
|
+
|
|
575
|
+
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
576
|
+
guard let body = message.body as? [String: Any] else { return }
|
|
577
|
+
NSLog("[craft page] %@: %@", body["level"] as? String ?? "log", body["message"] as? String ?? "")
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
#endif
|
|
581
|
+
|
|
517
582
|
struct CraftWebView: UIViewRepresentable {
|
|
518
583
|
let config: CraftConfig
|
|
519
584
|
|
|
@@ -528,6 +593,14 @@ struct CraftWebView: UIViewRepresentable {
|
|
|
528
593
|
let contentController = WKUserContentController()
|
|
529
594
|
contentController.add(context.coordinator, name: "craft")
|
|
530
595
|
webConfig.userContentController = contentController
|
|
596
|
+
#if DEBUG
|
|
597
|
+
// The page's errors and console, in the device log, so a debug build
|
|
598
|
+
// can be diagnosed from `log stream` or `simctl spawn … log show`
|
|
599
|
+
// without attaching Safari. A separate handler, outside the bridge's
|
|
600
|
+
// action dispatch. Never in a release build.
|
|
601
|
+
contentController.add(PageConsoleRelay(), name: "craftLog")
|
|
602
|
+
contentController.addUserScript(WKUserScript(source: PageConsoleRelay.script, injectionTime: .atDocumentStart, forMainFrameOnly: true))
|
|
603
|
+
#endif
|
|
531
604
|
|
|
532
605
|
let webView = WKWebView(frame: .zero, configuration: webConfig)
|
|
533
606
|
webView.navigationDelegate = context.coordinator
|