also looking at this
feat: finish native customization and platform seams
#401
21 files
+707
-17
| @@ -68,6 +68,21 @@ const CRAFT_VERSION = resolveCraftVersion() | ||
| 68 | 68 | |
| 69 | 69 | export type CraftAppearance = 'light' | 'dark' | 'system' |
| 70 | 70 | |
| 71 | /** A declared command Craft runs after the native project has been generated. */ | |
| 72 | export interface CraftPostGenerateHook { | |
| 73 | command: string | |
| 74 | args?: string[] | |
| 75 | } | |
| 76 | ||
| 77 | export interface CraftTabBarConfig { | |
| 78 | selection?: { | |
| 79 | /** The selected tab indicator's corner radius in points; omitted keeps a capsule. */ | |
| 80 | cornerRadius?: number | |
| 81 | /** The selected tab indicator fill as #RRGGBB. */ | |
| 82 | color?: string | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 71 | 86 | export interface CraftConfig { |
| 72 | 87 | appShortcuts?: CraftAppShortcut[] |
| 73 | 88 | /** Bundled font files keyed by their PostScript font name, used as native fontFamily. */ |
| @@ -86,6 +101,10 @@ export interface CraftConfig { | ||
| 86 | 101 | * and `dark` pin one. Unset keeps the older `darkMode` switch. |
| 87 | 102 | */ |
| 88 | 103 | appearance?: CraftAppearance |
| 104 | /** Static native tab-bar styling applied before the page's own tabBar.set call. */ | |
| 105 | tabBar?: CraftTabBarConfig | |
| 106 | /** Runs from the generated project root after `craft ios build` writes it. */ | |
| 107 | postGenerate?: CraftPostGenerateHook | |
| 89 | 108 | backgroundColor?: string |
| 90 | 109 | /** The launch and webview background while the phone is in Dark Mode. */ |
| 91 | 110 | backgroundColorDark?: string |
| @@ -639,6 +658,42 @@ export interface BuildOptions { | ||
| 639 | 658 | runtimeDir?: string | null |
| 640 | 659 | } |
| 641 | 660 | |
| 661 | /** | |
| 662 | * Run the app's declared generator hook without invoking a shell. | |
| 663 | * | |
| 664 | * A hook gets the generated project as its cwd and explicit metadata so a | |
| 665 | * patch can target the right platform and phase without guessing from paths. | |
| 666 | */ | |
| 667 | export function runPostGenerateHook( | |
| 668 | hook: CraftPostGenerateHook | undefined, | |
| 669 | output: string, | |
| 670 | phase: 'build' = 'build', | |
| 671 | ): void { | |
| 672 | if (!hook) return | |
| 673 | if (typeof hook.command !== 'string' || !hook.command.trim()) | |
| 674 | throw new Error('iOS postGenerate.command must be a non-empty executable') | |
| 675 | if (hook.args !== undefined && (!Array.isArray(hook.args) || hook.args.some(argument => typeof argument !== 'string'))) | |
| 676 | throw new Error('iOS postGenerate.args must be an array of strings') | |
| 677 | ||
| 678 | const command = [hook.command, ...(hook.args ?? [])] | |
| 679 | let result: ReturnType<typeof Bun.spawnSync> | |
| 680 | try { | |
| 681 | result = Bun.spawnSync(command, { | |
| 682 | cwd: output, | |
| 683 | env: { ...process.env, CRAFT_PROJECT_ROOT: output, CRAFT_PLATFORM: 'ios', CRAFT_GENERATE_PHASE: phase }, | |
| 684 | stdout: 'inherit', | |
| 685 | stderr: 'pipe', | |
| 686 | }) | |
| 687 | } | |
| 688 | catch (error) { | |
| 689 | throw new Error(`iOS postGenerate hook could not start in ${output}: ${hook.command}`, { cause: error }) | |
| 690 | } | |
| 691 | if (result.exitCode !== 0) { | |
| 692 | const stderr = result.stderr?.toString().trim() | |
| 693 | throw new Error(`iOS postGenerate hook failed in ${output} with exit code ${result.exitCode}${stderr ? `: ${stderr}` : ''}`) | |
| 694 | } | |
| 695 | } | |
| 696 | ||
| 642 | 697 | export interface OpenOptions { |
| 643 | 698 | output: string |
| 644 | 699 | } |
| @@ -1502,6 +1557,11 @@ export async function init(options: InitOptions): Promise<void> { | ||
| 1502 | 1557 | if (config.renderer !== 'web' && config.renderer !== 'native') { |
| 1503 | 1558 | throw new Error(`Unknown iOS renderer: ${config.renderer}. Expected web or native.`) |
| 1504 | 1559 | } |
| 1560 | const selection = config.tabBar?.selection | |
| 1561 | if (selection?.cornerRadius !== undefined && (!Number.isFinite(selection.cornerRadius) || selection.cornerRadius < 0)) | |
| 1562 | throw new Error('iOS tabBar.selection.cornerRadius must be a finite non-negative number') | |
| 1563 | if (selection?.color !== undefined && !/^#[0-9a-f]{6}$/i.test(selection.color)) | |
| 1564 | throw new Error('iOS tabBar.selection.color must be a #RRGGBB color') | |
| 1505 | 1565 | if (config.enableBackgroundLocation) config.enableGeolocation = true |
| 1506 | 1566 | const problems = hybridConfigProblems(config) |
| 1507 | 1567 | if (problems.length) throw new Error(problems.join('\n')) |
| @@ -1755,6 +1815,8 @@ catch (error) { | ||
| 1755 | 1815 | }) |
| 1756 | 1816 | } |
| 1757 | 1817 | |
| 1818 | runPostGenerateHook(config.postGenerate, output) | |
| 1819 | ||
| 1758 | 1820 | console.log('') |
| 1759 | 1821 | } |
| 1760 | 1822 | |