@govela/app 0.1.1 → 0.2.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 CHANGED
@@ -3,7 +3,22 @@
3
3
  The client router of Vela App, the application layer above the Vela compiler
4
4
  with a Go server. The contract is [docs/APP.md](../../docs/APP.md).
5
5
 
6
- This package is not published yet. `@govela/app/match` has the built-in
6
+ It also installs the `vela-app` command, which generates the app sources,
7
+ runs the development loop and makes production builds:
8
+
9
+ ```sh
10
+ npm install @govela/app @govela/runtime
11
+ npx vela-app dev
12
+ npx vela-app build
13
+ ```
14
+
15
+ `bin/vela-app.js` runs the binary of the optional dependency
16
+ `@govela/app-<os>-<cpu>` for this platform (linux x64/arm64, macOS
17
+ x64/arm64, Windows x64), or `$VELA_APP_BINARY`. Do not install with
18
+ `--omit=optional`. The guide is
19
+ [docs/VELA_APP.md](../../docs/VELA_APP.md).
20
+
21
+ `@govela/app/match` has the built-in
7
22
  matchers (`int`, `uuid`, `slug`) and `matchPath`, which the route table that
8
23
  `vela-app gen` writes (`src/_app/routes.js`) uses; the Go package
9
24
  `github.com/elauven/vela/app` matches with the same rules.
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env node
2
+ // The `vela-app` command of @govela/app: runs the vela-app binary of this
3
+ // platform (the optional dependency @govela/app-<os>-<cpu>), or
4
+ // $VELA_APP_BINARY, with the arguments, standard streams and exit code of
5
+ // the command.
6
+ import { spawnSync } from 'node:child_process';
7
+ import { readFileSync } from 'node:fs';
8
+ import { createRequire } from 'node:module';
9
+
10
+ const require = createRequire(import.meta.url);
11
+ const manifest = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
12
+ const platforms = ['linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64', 'win32-x64'];
13
+
14
+ function binaryPath() {
15
+ if (process.env.VELA_APP_BINARY) {
16
+ return process.env.VELA_APP_BINARY;
17
+ }
18
+ const platform = `${process.platform}-${process.arch}`;
19
+ const name = `${manifest.name}-${platform}`;
20
+ if (!platforms.includes(platform)) {
21
+ throw new Error(`vela-app: no prebuilt binary for ${platform}; supported: ${platforms.join(', ')}. Install it with \`go install github.com/elauven/vela/app/cmd/vela-app@v${manifest.version}\` and set VELA_APP_BINARY to it.`);
22
+ }
23
+ const file = `bin/vela-app${process.platform === 'win32' ? '.exe' : ''}`;
24
+ try {
25
+ return require.resolve(`${name}/${file}`);
26
+ } catch {
27
+ throw new Error(`vela-app: the package ${name} is not installed. It is an optional dependency of ${manifest.name}; reinstall without --no-optional / --omit=optional, or set VELA_APP_BINARY to a vela-app binary.`);
28
+ }
29
+ }
30
+
31
+ let binary;
32
+ try {
33
+ binary = binaryPath();
34
+ } catch (error) {
35
+ console.error(error.message);
36
+ process.exit(1);
37
+ }
38
+ const result = spawnSync(binary, process.argv.slice(2), { stdio: 'inherit', windowsHide: true });
39
+ if (result.error) {
40
+ console.error(`vela-app: cannot run ${binary}: ${result.error.message}`);
41
+ process.exit(1);
42
+ }
43
+ if (result.signal) {
44
+ process.kill(process.pid, result.signal);
45
+ }
46
+ process.exit(result.status ?? 1);
package/client.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- // Types of @govela/app/client for editors (docs/APP.md).
1
+ // Types of @govela/app/client (docs/APP.md).
2
+
3
+ import type { Component, ComponentInstance } from '@govela/runtime';
4
+ import type { MismatchReport } from '@govela/runtime/hydrate';
2
5
 
3
6
  /** The route table of src/_app/routes.js, which vela-app generates. */
4
7
  export interface RouteTable {
@@ -7,12 +10,15 @@ export interface RouteTable {
7
10
  match(path: string): { route: { id: string }; params: Record<string, string> } | null;
8
11
  }
9
12
 
10
- /** The route prop of route components (R-1). */
11
- export interface Route {
13
+ /**
14
+ * The route prop of route components (R-1). P are the parameters of the
15
+ * path: `Route<Params>` of the route's `$types` (docs/TYPESCRIPT.md TS-8.2).
16
+ */
17
+ export interface Route<P extends Record<string, string | undefined> = Record<string, string>> {
12
18
  id: string;
13
19
  url: string;
14
20
  path: string;
15
- params: Record<string, string>;
21
+ params: P;
16
22
  query: Record<string, string>;
17
23
  }
18
24
 
@@ -37,19 +43,19 @@ export interface BeforeNavigation extends Navigation {
37
43
 
38
44
  export interface StartOptions {
39
45
  /** The generated app root, src/_app/VelaApp.vela. */
40
- VelaApp: Function;
46
+ VelaApp: Component<any, any, any>;
41
47
  /** The generated route table, src/_app/routes.js. Without it links load documents. */
42
48
  routes?: RouteTable;
43
49
  /** The element that holds the app root, or its id. */
44
50
  root?: string | Element;
45
51
  /** Receives hydration mismatch reports. */
46
- onMismatch?: (report: unknown) => void;
52
+ onMismatch?: (report: MismatchReport) => void;
47
53
  /** The default prefetch of links; data-vela-preload on a link or an ancestor overrides it. */
48
54
  preload?: 'hover' | 'viewport' | 'off';
49
55
  }
50
56
 
51
57
  /** Hydrates the server document and installs the router. */
52
- export function start(options: StartOptions): Promise<{ app: unknown; routes?: RouteTable }>;
58
+ export function start(options: StartOptions): Promise<{ app: ComponentInstance; routes?: RouteTable }>;
53
59
 
54
60
  export interface GotoOptions {
55
61
  /** Replace the current history entry instead of adding one. */
@@ -82,3 +88,22 @@ export const protocolVersion: 1;
82
88
 
83
89
  /** The request headers of a navigation (N-2). */
84
90
  export const headers: Readonly<{ navigate: string; have: string; build: string }>;
91
+
92
+ /** The detail of `vela:form`: a failed form action (`app.Fail`). */
93
+ export interface FormFailure {
94
+ /** The HTTP status of the failure, 400–499. */
95
+ status: number;
96
+ /** The value the action failed with: the page's `form` prop. */
97
+ form: unknown;
98
+ }
99
+
100
+ declare global {
101
+ interface HTMLElementEventMap {
102
+ /** The app root hydrated (start()). */
103
+ 'vela:hydrated': CustomEvent<{ app: ComponentInstance }>;
104
+ /** A navigation of the router ended. */
105
+ 'vela:navigated': CustomEvent<Navigation>;
106
+ /** A form action failed; the page's `form` prop changed. */
107
+ 'vela:form': CustomEvent<FormFailure>;
108
+ }
109
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@govela/app",
3
- "version": "0.1.1",
4
- "description": "Client router of Vela App (docs/APP.md): hydration, data navigation and route matching.",
3
+ "version": "0.2.0",
4
+ "description": "Vela App (docs/APP.md): the client router (hydration, data navigation, route matching) and the vela-app command.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -9,6 +9,9 @@
9
9
  "directory": "packages/app"
10
10
  },
11
11
  "type": "module",
12
+ "bin": {
13
+ "vela-app": "bin/vela-app.js"
14
+ },
12
15
  "sideEffects": false,
13
16
  "exports": {
14
17
  "./client": {
@@ -21,6 +24,7 @@
21
24
  }
22
25
  },
23
26
  "files": [
27
+ "bin/vela-app.js",
24
28
  "client.js",
25
29
  "client.d.ts",
26
30
  "match.js",
@@ -32,6 +36,16 @@
32
36
  "protocol": 1
33
37
  },
34
38
  "peerDependencies": {
35
- "@govela/runtime": ">=0.4.8"
39
+ "@govela/runtime": ">=0.5.0"
40
+ },
41
+ "engines": {
42
+ "node": ">=18"
43
+ },
44
+ "optionalDependencies": {
45
+ "@govela/app-darwin-arm64": "0.2.0",
46
+ "@govela/app-darwin-x64": "0.2.0",
47
+ "@govela/app-linux-arm64": "0.2.0",
48
+ "@govela/app-linux-x64": "0.2.0",
49
+ "@govela/app-win32-x64": "0.2.0"
36
50
  }
37
51
  }