@elumixor/devkit 0.8.0 → 0.9.0
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/README.md +79 -0
- package/package.json +1 -1
- package/src/bin/devkit.ts +32 -4
- package/src/build.ts +75 -0
- package/src/config.ts +61 -0
- package/src/deploy.ts +6 -2
- package/src/release.ts +26 -0
- package/src/sim.ts +51 -0
- package/src/version.ts +43 -0
package/README.md
CHANGED
|
@@ -81,6 +81,80 @@ you type on the command line.
|
|
|
81
81
|
| `needs` | targets that must finish first |
|
|
82
82
|
| `color` | colour of this target's line; one is picked for you otherwise |
|
|
83
83
|
|
|
84
|
+
## Build
|
|
85
|
+
|
|
86
|
+
`devkit build` packs a client-rendered web app and a Nitro API into one Vercel deployment: the app
|
|
87
|
+
as static files, the API as the one serverless function behind them. Same origin, so the web client
|
|
88
|
+
calls the API without CORS.
|
|
89
|
+
|
|
90
|
+
```jsonc
|
|
91
|
+
"devkit": {
|
|
92
|
+
"build": {
|
|
93
|
+
"frontendDir": "apps/frontend",
|
|
94
|
+
"backendDir": "apps/backend",
|
|
95
|
+
"apiPrefixes": ["mobile", "health"],
|
|
96
|
+
"clientCommand": "bunx nitro-client",
|
|
97
|
+
"include": { "dist/download": "download" }
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
| key | meaning |
|
|
103
|
+
| --------------- | ------------------------------------------------------------------------------ |
|
|
104
|
+
| `frontendDir` | web app dir, run with `bun run build` (default `apps/frontend`) |
|
|
105
|
+
| `backendDir` | Nitro dir (default `apps/backend`) |
|
|
106
|
+
| `webBuildDir` | where the web build lands inside `frontendDir` (default `build`) |
|
|
107
|
+
| `apiPrefixes` | paths the API owns; every other path answers with the app shell |
|
|
108
|
+
| `clientCommand` | run in `backendDir` first, unless `SKIP_CLIENT_GEN` is set |
|
|
109
|
+
| `include` | staged dirs to serve alongside the web files, as `{ "<source>": "<url path>" }` |
|
|
110
|
+
|
|
111
|
+
## Versions
|
|
112
|
+
|
|
113
|
+
`devkit version` reads one version and writes it into every other file that declares one, so the
|
|
114
|
+
App Store, the installer and the number at the bottom of settings can't drift apart.
|
|
115
|
+
|
|
116
|
+
```jsonc
|
|
117
|
+
"devkit": {
|
|
118
|
+
"version": {
|
|
119
|
+
"source": "apps/frontend/src-tauri/tauri.conf.json",
|
|
120
|
+
"packages": ["package.json", "apps/frontend/package.json"],
|
|
121
|
+
"cargo": ["apps/frontend/src-tauri/Cargo.toml"],
|
|
122
|
+
"xcodeProjects": ["apps/frontend/ios/App/App.xcodeproj/project.pbxproj"],
|
|
123
|
+
"marketingVersionPlists": ["apps/frontend/ios/App/App/Info.plist"],
|
|
124
|
+
"plists": ["apps/frontend/ios/App/WatchApp/Info.plist"]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Targets in an Xcode project read `$(MARKETING_VERSION)` from the project, so their plists are
|
|
130
|
+
pointed at it rather than at a number — `plists` is for targets wired in by a script, which have no
|
|
131
|
+
such setting and carry the number themselves.
|
|
132
|
+
|
|
133
|
+
`devkit release <prefix>` tags the current commit `<prefix>-v<timestamp>` and pushes the tag, which
|
|
134
|
+
is how a CI release is asked for.
|
|
135
|
+
|
|
136
|
+
## Simulator
|
|
137
|
+
|
|
138
|
+
`devkit sim` builds an Apple target and launches it on a simulator without opening Xcode. A watch
|
|
139
|
+
app has no sign-in of its own, so `previewEnv` is what lets it run on a simulator with no paired
|
|
140
|
+
phone; `--real` leaves it unset and talks to the real API.
|
|
141
|
+
|
|
142
|
+
```jsonc
|
|
143
|
+
"devkit": {
|
|
144
|
+
"sim": {
|
|
145
|
+
"project": "apps/frontend/ios/App/App.xcodeproj",
|
|
146
|
+
"scheme": "BalanceWatch",
|
|
147
|
+
"bundleId": "com.example.app.watchkitapp",
|
|
148
|
+
"device": "Apple Watch Series 11 (46mm)",
|
|
149
|
+
"previewEnv": "BALANCE_WATCH_PREVIEW"
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
`--device`, or `DEVKIT_SIM_DEVICE`, overrides the configured simulator — everyone's list of
|
|
155
|
+
installed ones differs. `sdk` (default `watchsimulator`) and `derivedDataDir` (default `build/sim`)
|
|
156
|
+
are there for a phone target.
|
|
157
|
+
|
|
84
158
|
## Setting up a machine
|
|
85
159
|
|
|
86
160
|
A repo usually needs a couple of gitignored files before it will run — `.env`, `infra/terraform.tfvars` — and those cannot be fetched back from a host. Vercel, for one, stores Terraform-managed variables write-only and hands them back from `vercel env pull` as *empty strings*, which looks like it worked and quietly leaves you with a `.env` full of blanks.
|
|
@@ -137,5 +211,10 @@ Changed a `.env`? Run `devkit secrets push`, or the next machine gets a stale co
|
|
|
137
211
|
- `devkit [root] [--open] [--dry] [--version]` — the orchestrator above. `root` is an optional path to the folder holding the `devkit` config (defaults to the current directory); `--dry` prints the resolved commands and URLs without running anything.
|
|
138
212
|
- `devkit clone <owner/repo> [dir]` — clone, decrypt secrets, run setup.
|
|
139
213
|
- `devkit setup [root] [--dry]` — validate secrets, install, `terraform init`, run steps.
|
|
214
|
+
- `devkit deploy [targets...] [--dry] [--verbose]` — run the release targets, in parallel.
|
|
215
|
+
- `devkit build` — build the web app and the API into one Vercel output.
|
|
216
|
+
- `devkit sim [--real] [--device <name>]` — build and launch the app on a simulator.
|
|
217
|
+
- `devkit version` — write the source version into every file that declares one.
|
|
218
|
+
- `devkit release <prefix>` — tag this commit `<prefix>-v<timestamp>` and push it.
|
|
140
219
|
- `devkit secrets push|pull [root]` — sync the encrypted secrets.
|
|
141
220
|
- `free-port <port>` — kill whatever is listening on a port (handy in a `predev`).
|
package/package.json
CHANGED
package/src/bin/devkit.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import { parseArgs } from "node:util";
|
|
3
2
|
import { resolve } from "node:path";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import pkg from "../../package.json" with { type: "json" };
|
|
5
|
+
import { runBuild } from "../build.ts";
|
|
6
|
+
import { runClone } from "../clone.ts";
|
|
4
7
|
import { loadConfig } from "../config.ts";
|
|
5
8
|
import { runDeploy } from "../deploy.ts";
|
|
6
9
|
import { deployFastlaneIos } from "../deploy-targets/fastlane-ios.ts";
|
|
7
10
|
import { deployTauriMacos } from "../deploy-targets/tauri-macos.ts";
|
|
8
11
|
import { deployVercel } from "../deploy-targets/vercel.ts";
|
|
9
12
|
import { runDev } from "../dev.ts";
|
|
10
|
-
import {
|
|
11
|
-
import { runClone } from "../clone.ts";
|
|
13
|
+
import { runRelease } from "../release.ts";
|
|
12
14
|
import { pullSecrets, pushSecrets } from "../secrets.ts";
|
|
13
|
-
import
|
|
15
|
+
import { runSetup } from "../setup.ts";
|
|
16
|
+
import { runSim } from "../sim.ts";
|
|
17
|
+
import { runVersion } from "../version.ts";
|
|
14
18
|
|
|
15
19
|
const { values, positionals } = parseArgs({
|
|
16
20
|
options: {
|
|
17
21
|
open: { type: "boolean", default: false },
|
|
22
|
+
real: { type: "boolean", default: false },
|
|
23
|
+
device: { type: "string" },
|
|
18
24
|
dry: { type: "boolean", default: false },
|
|
19
25
|
verbose: { type: "boolean", default: false },
|
|
20
26
|
version: { type: "boolean", short: "v", default: false },
|
|
@@ -32,12 +38,18 @@ const usage = `usage: devkit [root] [--open] [--dry] run the dev processes
|
|
|
32
38
|
devkit clone <owner/repo> [dir] clone, decrypt secrets, set up
|
|
33
39
|
devkit setup [root] [--dry] validate secrets, install, terraform init, run steps
|
|
34
40
|
devkit deploy [targets...] run the configured deploys, in parallel
|
|
41
|
+
devkit build build the web app and the API into one Vercel output
|
|
42
|
+
devkit sim [--real] [--device n] build and launch the app on a simulator
|
|
43
|
+
devkit version write the source version into every file that declares one
|
|
44
|
+
devkit release <prefix> tag this commit <prefix>-v<timestamp> and push it
|
|
35
45
|
devkit secrets push|pull [root] sync age-encrypted secrets
|
|
36
46
|
|
|
37
47
|
root path to the folder holding the devkit config (default: cwd)
|
|
38
48
|
--open open each app's URL in the browser once its port is live
|
|
39
49
|
--dry print what would run without running it
|
|
40
50
|
--verbose stream every line instead of drawing the live board (deploy)
|
|
51
|
+
--real run the simulator build against the real API instead of sample data
|
|
52
|
+
--device simulator to run on, overriding the configured one
|
|
41
53
|
--version print the devkit version`;
|
|
42
54
|
|
|
43
55
|
if (values.help) {
|
|
@@ -64,6 +76,22 @@ try {
|
|
|
64
76
|
await runSetup(values.dry);
|
|
65
77
|
break;
|
|
66
78
|
}
|
|
79
|
+
case "build": {
|
|
80
|
+
await runBuild();
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "sim": {
|
|
84
|
+
await runSim({ real: values.real, device: values.device });
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "version": {
|
|
88
|
+
await runVersion();
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case "release": {
|
|
92
|
+
await runRelease(rest[0] ?? "");
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
67
95
|
case "deploy": {
|
|
68
96
|
await runDeploy(rest, { dry: values.dry, verbose: values.verbose });
|
|
69
97
|
break;
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { cp, rename, rm } from "node:fs/promises";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { $ } from "bun";
|
|
5
|
+
import { loadConfig } from "./config.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Build a client-rendered web app and a Nitro API into a single Vercel deployment: the app as
|
|
9
|
+
* static files, the API as the one serverless function behind them. Both end up at the same
|
|
10
|
+
* origin, so the web client calls the API without CORS and there is one URL to remember.
|
|
11
|
+
*/
|
|
12
|
+
export async function runBuild() {
|
|
13
|
+
const { build } = loadConfig();
|
|
14
|
+
if (!build) throw new Error('No "devkit.build" config in package.json');
|
|
15
|
+
|
|
16
|
+
const root = process.cwd();
|
|
17
|
+
const frontend = resolve(root, build.frontendDir ?? "apps/frontend");
|
|
18
|
+
const backend = resolve(root, build.backendDir ?? "apps/backend");
|
|
19
|
+
const webDir = join(backend, "public");
|
|
20
|
+
const nitroOutput = join(backend, ".vercel/output");
|
|
21
|
+
const finalOutput = join(root, ".vercel/output");
|
|
22
|
+
|
|
23
|
+
// The typed client is generated from the route handlers, so it has to exist before the app
|
|
24
|
+
// compiles — unless a release already did it, in which case regenerating it now would be writing
|
|
25
|
+
// to files a build running alongside is reading.
|
|
26
|
+
if (build.clientCommand && !process.env.SKIP_CLIENT_GEN) await $`sh -c ${build.clientCommand}`.cwd(backend);
|
|
27
|
+
|
|
28
|
+
await $`bun run build`.cwd(frontend);
|
|
29
|
+
await rm(webDir, { recursive: true, force: true });
|
|
30
|
+
await cp(join(frontend, build.webBuildDir ?? "build"), webDir, { recursive: true });
|
|
31
|
+
|
|
32
|
+
// Anything a sibling target staged for the site to hand out — an installer, say — rides along
|
|
33
|
+
// with the web files rather than living in the frontend's static folder, which other builds
|
|
34
|
+
// copy wholesale.
|
|
35
|
+
for (const [from, to] of Object.entries(build.include ?? {})) {
|
|
36
|
+
const staged = resolve(root, from);
|
|
37
|
+
if (!existsSync(staged)) {
|
|
38
|
+
console.log(`Nothing staged at ${from} — the site will serve no ${to}`);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
await cp(staged, join(webDir, to), { recursive: true });
|
|
42
|
+
console.log(`Serving ${from} at /${to}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await rm(nitroOutput, { recursive: true, force: true });
|
|
46
|
+
await $`bunx nitro build --preset vercel`.cwd(backend);
|
|
47
|
+
|
|
48
|
+
await rm(finalOutput, { recursive: true, force: true });
|
|
49
|
+
await rename(nitroOutput, finalOutput);
|
|
50
|
+
|
|
51
|
+
await routeUnmatchedPathsToTheApp(finalOutput, build.apiPrefixes ?? []);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The app is one client-rendered bundle, so every path it owns has to answer with `index.html`
|
|
56
|
+
* and let the router sort it out. Nitro ends its routes with a catch-all into the function, which
|
|
57
|
+
* would otherwise answer an unknown web path with an API-shaped 404 JSON body.
|
|
58
|
+
*/
|
|
59
|
+
async function routeUnmatchedPathsToTheApp(output: string, apiPrefixes: readonly string[]) {
|
|
60
|
+
if (!apiPrefixes.length) throw new Error('"devkit.build.apiPrefixes" must list the paths the API owns');
|
|
61
|
+
|
|
62
|
+
const configPath = join(output, "config.json");
|
|
63
|
+
const config = await Bun.file(configPath).json();
|
|
64
|
+
const staticDir = join(output, "static");
|
|
65
|
+
if (!existsSync(staticDir)) throw new Error(`No static output at ${staticDir} — did the web build land?`);
|
|
66
|
+
|
|
67
|
+
const catchAll = config.routes.findIndex((route: { dest?: string }) => route.dest === "/__fallback");
|
|
68
|
+
config.routes.splice(catchAll < 0 ? config.routes.length : catchAll, 0, {
|
|
69
|
+
src: `/((?!${apiPrefixes.join("|")}).*)`,
|
|
70
|
+
dest: "/index.html",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await Bun.write(configPath, JSON.stringify(config, null, 2));
|
|
74
|
+
console.log("Unmatched web paths route to the app shell");
|
|
75
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -69,11 +69,66 @@ export interface DeployTarget {
|
|
|
69
69
|
color?: string;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* `devkit build`: a client-rendered web app and a Nitro API packed into one Vercel deployment.
|
|
74
|
+
*/
|
|
75
|
+
export interface BuildConfig {
|
|
76
|
+
/** Web app dir, run with `bun run build` (default `apps/frontend`). */
|
|
77
|
+
frontendDir?: string;
|
|
78
|
+
/** Nitro dir, built with `bunx nitro build --preset vercel` (default `apps/backend`). */
|
|
79
|
+
backendDir?: string;
|
|
80
|
+
/** Where the web build lands inside `frontendDir` (default `build`). */
|
|
81
|
+
webBuildDir?: string;
|
|
82
|
+
/** Path prefixes the API owns; everything else answers with the app shell. */
|
|
83
|
+
apiPrefixes: string[];
|
|
84
|
+
/** Run in `backendDir` before the web build, unless `SKIP_CLIENT_GEN` is set (e.g. `bunx nitro-client`). */
|
|
85
|
+
clientCommand?: string;
|
|
86
|
+
/** Extra staged directories to serve, as `{ "<source dir>": "<path under the site>" }`. */
|
|
87
|
+
include?: Record<string, string>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `devkit sim`: an Apple app built and launched on a simulator, without opening Xcode. */
|
|
91
|
+
export interface SimConfig {
|
|
92
|
+
/** Path to the `.xcodeproj`. */
|
|
93
|
+
project: string;
|
|
94
|
+
/** Scheme to build; also the name of the built `.app`. */
|
|
95
|
+
scheme: string;
|
|
96
|
+
/** Bundle id to install and launch. */
|
|
97
|
+
bundleId: string;
|
|
98
|
+
/** Simulator to run on — overridable with `--device` or `DEVKIT_SIM_DEVICE`. */
|
|
99
|
+
device: string;
|
|
100
|
+
/** Simulator SDK the products are built for (default `watchsimulator`). */
|
|
101
|
+
sdk?: string;
|
|
102
|
+
/** Derived data dir (default `build/sim`). */
|
|
103
|
+
derivedDataDir?: string;
|
|
104
|
+
/** Env var the app reads to show sample data; set unless `--real` is passed. */
|
|
105
|
+
previewEnv?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** `devkit version`: one version, declared once and written everywhere else it appears. */
|
|
109
|
+
export interface VersionConfig {
|
|
110
|
+
/** JSON file whose `version` field is the number the rest follow. */
|
|
111
|
+
source: string;
|
|
112
|
+
/** `package.json`-shaped files. */
|
|
113
|
+
packages?: string[];
|
|
114
|
+
/** `Cargo.toml` files. */
|
|
115
|
+
cargo?: string[];
|
|
116
|
+
/** `project.pbxproj` files, whose `MARKETING_VERSION` every target's plist reads. */
|
|
117
|
+
xcodeProjects?: string[];
|
|
118
|
+
/** Plists of targets in those projects — pointed at `$(MARKETING_VERSION)` rather than a number. */
|
|
119
|
+
marketingVersionPlists?: string[];
|
|
120
|
+
/** Plists of targets with no project setting of their own, which carry the number itself. */
|
|
121
|
+
plists?: string[];
|
|
122
|
+
}
|
|
123
|
+
|
|
72
124
|
/** Apps and deploy targets are written as name-keyed maps, so a name is never repeated or missing. */
|
|
73
125
|
export interface DevkitConfig {
|
|
74
126
|
apps?: Record<string, DevApp>;
|
|
75
127
|
setup?: SetupConfig;
|
|
76
128
|
deploy?: Record<string, DeployTarget>;
|
|
129
|
+
build?: BuildConfig;
|
|
130
|
+
sim?: SimConfig;
|
|
131
|
+
version?: VersionConfig;
|
|
77
132
|
}
|
|
78
133
|
|
|
79
134
|
/** A config entry with its map key attached — the name every command prints and refers to. */
|
|
@@ -84,6 +139,9 @@ export interface LoadedConfig {
|
|
|
84
139
|
apps: Named<DevApp>[];
|
|
85
140
|
setup?: SetupConfig;
|
|
86
141
|
deploy: Named<DeployTarget>[];
|
|
142
|
+
build?: BuildConfig;
|
|
143
|
+
sim?: SimConfig;
|
|
144
|
+
version?: VersionConfig;
|
|
87
145
|
}
|
|
88
146
|
|
|
89
147
|
/** Read the `devkit` config from the nearest package.json. */
|
|
@@ -98,6 +156,9 @@ export function loadConfig(dir: string = process.cwd()): LoadedConfig {
|
|
|
98
156
|
apps: named(cfg.apps, "apps", pkgPath),
|
|
99
157
|
setup: cfg.setup,
|
|
100
158
|
deploy: named(cfg.deploy, "deploy", pkgPath),
|
|
159
|
+
build: cfg.build,
|
|
160
|
+
sim: cfg.sim,
|
|
161
|
+
version: cfg.version,
|
|
101
162
|
};
|
|
102
163
|
}
|
|
103
164
|
|
package/src/deploy.ts
CHANGED
|
@@ -58,7 +58,8 @@ export async function runDeploy(only: string[], options: { dry?: boolean; verbos
|
|
|
58
58
|
|
|
59
59
|
for (const target of deploy) {
|
|
60
60
|
const missing = (target.needs ?? []).filter((need) => !deploy.some((other) => other.name === need));
|
|
61
|
-
if (missing.length)
|
|
61
|
+
if (missing.length)
|
|
62
|
+
throw new Error(`${target.name} needs unknown target${missing.length > 1 ? "s" : ""}: ${missing.join(", ")}`);
|
|
62
63
|
if (!target.command && !target.type) throw new Error(`${target.name} needs a "command" or a "type"`);
|
|
63
64
|
}
|
|
64
65
|
|
|
@@ -69,7 +70,10 @@ export async function runDeploy(only: string[], options: { dry?: boolean; verbos
|
|
|
69
70
|
mkdirSync(logDir, { recursive: true });
|
|
70
71
|
|
|
71
72
|
const live = Boolean(process.stdout.isTTY) && !options.verbose;
|
|
72
|
-
console.log(
|
|
73
|
+
console.log(
|
|
74
|
+
bold(`\ndeploying ${jobs.map((job) => job.label).join(", ")}`) +
|
|
75
|
+
dim(live ? ` · logs in ${"./.devkit/deploy"}` : ""),
|
|
76
|
+
);
|
|
73
77
|
console.log();
|
|
74
78
|
const board = live ? startBoard(jobs) : null;
|
|
75
79
|
|
package/src/release.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { $ } from "bun";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tag the current commit and push the tag, which is how a CI release is asked for: `ios` becomes
|
|
5
|
+
* `ios-v20260820203000`. The timestamp is what keeps two releases of the same commit apart.
|
|
6
|
+
*/
|
|
7
|
+
export async function runRelease(prefix: string) {
|
|
8
|
+
if (!prefix) throw new Error("usage: devkit release <prefix>");
|
|
9
|
+
|
|
10
|
+
const now = new Date();
|
|
11
|
+
const stamp = [
|
|
12
|
+
now.getFullYear(),
|
|
13
|
+
now.getMonth() + 1,
|
|
14
|
+
now.getDate(),
|
|
15
|
+
now.getHours(),
|
|
16
|
+
now.getMinutes(),
|
|
17
|
+
now.getSeconds(),
|
|
18
|
+
]
|
|
19
|
+
.map((part, index) => String(part).padStart(index === 0 ? 4 : 2, "0"))
|
|
20
|
+
.join("");
|
|
21
|
+
|
|
22
|
+
const tag = `${prefix}-v${stamp}`;
|
|
23
|
+
await $`git tag ${tag}`;
|
|
24
|
+
await $`git push origin ${tag}`;
|
|
25
|
+
console.log(`Pushed ${tag}`);
|
|
26
|
+
}
|
package/src/sim.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { $ } from "bun";
|
|
3
|
+
import { loadConfig } from "./config.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Build an Apple app and run it on a simulator, without going through Xcode.
|
|
7
|
+
*
|
|
8
|
+
* A watch app usually has no sign-in of its own, so on a simulator with no paired phone it can
|
|
9
|
+
* only show its signed-out screen. `preview` launches it with the app's own preview flag set
|
|
10
|
+
* instead, which is the point of running it here at all: gestures have to be felt.
|
|
11
|
+
*/
|
|
12
|
+
export async function runSim(options: { real?: boolean; device?: string } = {}) {
|
|
13
|
+
const { sim } = loadConfig();
|
|
14
|
+
if (!sim) throw new Error('No "devkit.sim" config in package.json');
|
|
15
|
+
|
|
16
|
+
const project = resolve(process.cwd(), sim.project);
|
|
17
|
+
const derived = resolve(process.cwd(), sim.derivedDataDir ?? "build/sim");
|
|
18
|
+
const sdk = sim.sdk ?? "watchsimulator";
|
|
19
|
+
const deviceName = options.device ?? process.env.DEVKIT_SIM_DEVICE ?? sim.device;
|
|
20
|
+
const preview = !options.real;
|
|
21
|
+
|
|
22
|
+
const udid = await boot(deviceName);
|
|
23
|
+
|
|
24
|
+
await $`xcodebuild -project ${project} -scheme ${sim.scheme} -configuration Debug \
|
|
25
|
+
-destination id=${udid} -derivedDataPath ${derived} build`.quiet();
|
|
26
|
+
|
|
27
|
+
await $`xcrun simctl install ${udid} ${derived}/Build/Products/Debug-${sdk}/${sim.scheme}.app`;
|
|
28
|
+
await $`xcrun simctl terminate ${udid} ${sim.bundleId}`.nothrow().quiet();
|
|
29
|
+
// `SIMCTL_CHILD_` is how simctl passes an environment on to the app it launches.
|
|
30
|
+
await $`xcrun simctl launch ${udid} ${sim.bundleId}`.env({
|
|
31
|
+
...process.env,
|
|
32
|
+
...(preview && sim.previewEnv ? { [`SIMCTL_CHILD_${sim.previewEnv}`]: "1" } : {}),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
console.log(`${sim.scheme} running on ${deviceName}${preview && sim.previewEnv ? " with sample data" : ""}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Boots the simulator if it isn't already up, and returns its device id. */
|
|
39
|
+
async function boot(deviceName: string) {
|
|
40
|
+
const { devices } = await $`xcrun simctl list devices available -j`.quiet().json();
|
|
41
|
+
// The same model exists once per installed runtime; the newest one is the one to use.
|
|
42
|
+
const runtimes = Object.keys(devices).sort().reverse();
|
|
43
|
+
const match = runtimes
|
|
44
|
+
.flatMap((runtime) => devices[runtime] as { name: string; udid: string; state: string }[])
|
|
45
|
+
.find((device) => device.name === deviceName);
|
|
46
|
+
|
|
47
|
+
if (!match) throw new Error(`No simulator called "${deviceName}" — see \`xcrun simctl list devices available\``);
|
|
48
|
+
if (match.state !== "Booted") await $`xcrun simctl boot ${match.udid}`;
|
|
49
|
+
await $`open -a Simulator`;
|
|
50
|
+
return match.udid;
|
|
51
|
+
}
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { loadConfig } from "./config.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* One version for the whole product. A single file holds it — the one the app already reads its
|
|
6
|
+
* version from at build time — and this writes that number everywhere else a version is declared,
|
|
7
|
+
* so the App Store, the installer and the line at the bottom of settings can't drift apart.
|
|
8
|
+
*/
|
|
9
|
+
export async function runVersion() {
|
|
10
|
+
const { version: config } = loadConfig();
|
|
11
|
+
if (!config) throw new Error('No "devkit.version" config in package.json');
|
|
12
|
+
|
|
13
|
+
const root = process.cwd();
|
|
14
|
+
const sourcePath = resolve(root, config.source);
|
|
15
|
+
const version = (await Bun.file(sourcePath).json()).version as string | undefined;
|
|
16
|
+
if (!version || !/^\d+\.\d+\.\d+$/.test(version))
|
|
17
|
+
throw new Error(`${sourcePath} has no plain x.y.z version: ${version}`);
|
|
18
|
+
|
|
19
|
+
const path = (file: string) => resolve(root, file);
|
|
20
|
+
|
|
21
|
+
await Promise.all([
|
|
22
|
+
...(config.packages ?? []).map((file) => edit(path(file), /("version":\s*")[^"]+/, version)),
|
|
23
|
+
...(config.cargo ?? []).map((file) => edit(path(file), /(^version = ")[^"]+/m, version)),
|
|
24
|
+
// The Xcode project is the only place the iOS marketing version is written: each Info.plist
|
|
25
|
+
// that belongs to a target reads `$(MARKETING_VERSION)` from it.
|
|
26
|
+
...(config.xcodeProjects ?? []).map((file) => edit(path(file), /(MARKETING_VERSION = )[^;]+/g, version)),
|
|
27
|
+
...(config.marketingVersionPlists ?? []).map((file) => edit(path(file), SHORT_VERSION, "$(MARKETING_VERSION)")),
|
|
28
|
+
// Targets wired into a project by a script rather than by Xcode's template have no
|
|
29
|
+
// `MARKETING_VERSION` setting of their own, so their plists carry the number itself.
|
|
30
|
+
...(config.plists ?? []).map((file) => edit(path(file), SHORT_VERSION, version)),
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
console.log(`Version ${version} across every declared file`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SHORT_VERSION = /(<key>CFBundleShortVersionString<\/key>\s*<string>)[^<]+/;
|
|
37
|
+
|
|
38
|
+
/** Rewrites the part of `pattern` after its first capture group, leaving the rest of the file alone. */
|
|
39
|
+
async function edit(path: string, pattern: RegExp, value: string) {
|
|
40
|
+
const before = await Bun.file(path).text();
|
|
41
|
+
const after = before.replace(pattern, (_, prefix) => `${prefix}${value}`);
|
|
42
|
+
if (after !== before) await Bun.write(path, after);
|
|
43
|
+
}
|