@20baht/cli 0.2.0 → 0.3.1
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 +95 -41
- package/dist/application/create.js +8 -4
- package/dist/application/generate.js +27 -21
- package/dist/application/update.js +104 -0
- package/dist/args.js +3 -0
- package/dist/cli.js +56 -22
- package/dist/domain/create-plan.js +4 -3
- package/dist/domain/form-dsl.js +1 -1
- package/dist/domain/generate-plan.js +3 -3
- package/dist/domain/update-plan.js +93 -0
- package/dist/infrastructure/fs-reader.js +34 -2
- package/dist/infrastructure/npm-registry.js +28 -0
- package/package.json +10 -7
- package/templates/angular/apps/server/package.json +2 -0
- package/templates/angular/apps/server/src/app.ts +10 -0
- package/templates/angular/apps/server/src/forms/contact.form.ts +34 -0
- package/templates/angular/apps/server/test/app.test.ts +29 -0
- package/templates/angular/package.json.hbs +1 -1
- package/templates/create/apps/web/package.json.hbs +1 -1
- package/templates/{angular → federation}/README.md.hbs +2 -2
- package/templates/{angular → federation}/_dependency-cruiser.mjs +1 -1
- package/templates/{angular → federation}/apps/shell/package.json +3 -3
- package/templates/{angular → federation}/apps/shell/src/app/modules/home/home.component.ts.hbs +1 -1
- package/templates/{angular → federation}/apps/shell/src/environments/environment.development.ts +1 -1
- package/templates/{angular → federation}/eslint.config.ts.hbs +1 -1
- package/templates/new/README.md.hbs +1 -1
- package/templates/new/package.json.hbs +1 -1
- package/templates/remote/package.json.hbs +3 -3
- package/templates/standalone/README.md.hbs +44 -0
- package/templates/standalone/_dependency-cruiser.mjs +29 -0
- package/templates/standalone/apps/web/_editorconfig +17 -0
- package/templates/standalone/apps/web/angular.json +72 -0
- package/templates/standalone/apps/web/package.json +39 -0
- package/templates/standalone/apps/web/src/app/app.config.ts +14 -0
- package/templates/standalone/apps/web/src/app/app.css +4 -0
- package/templates/standalone/apps/web/src/app/app.html.hbs +16 -0
- package/templates/standalone/apps/web/src/app/app.routes.ts.hbs +23 -0
- package/templates/standalone/apps/web/src/app/app.ts +43 -0
- package/templates/standalone/apps/web/src/app/core/navigation/navigation.spec.ts +26 -0
- package/templates/standalone/apps/web/src/app/core/navigation/navigation.ts +14 -0
- package/templates/standalone/apps/web/src/app/modules/contact/contact.component.ts.hbs +87 -0
- package/templates/standalone/apps/web/src/app/modules/home/home.component.ts.hbs +37 -0
- package/templates/standalone/apps/web/src/app/modules/not-found/not-found.component.ts +36 -0
- package/templates/standalone/apps/web/src/app/services/contact.service.ts.hbs +19 -0
- package/templates/standalone/apps/web/src/app/services/hello.service.ts.hbs +14 -0
- package/templates/standalone/apps/web/src/index.html.hbs +12 -0
- package/templates/standalone/apps/web/src/main.ts +10 -0
- package/templates/standalone/apps/web/src/styles.css +19 -0
- package/templates/standalone/apps/web/tsconfig.app.json +10 -0
- package/templates/standalone/apps/web/tsconfig.json.hbs +39 -0
- package/templates/standalone/apps/web/tsconfig.spec.json +9 -0
- package/templates/standalone/bahtui.json +6 -0
- package/templates/standalone/eslint.config.ts.hbs +24 -0
- package/templates/standalone/knip.ts +23 -0
- /package/templates/{angular → federation}/apps/shell/_editorconfig +0 -0
- /package/templates/{angular → federation}/apps/shell/angular.json +0 -0
- /package/templates/{angular → federation}/apps/shell/federation.config.mjs +0 -0
- /package/templates/{angular → federation}/apps/shell/public/federation.manifest.json +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/app.config.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/app.css +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/app.html.hbs +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/app.routes.ts.hbs +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/app.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/core/federation/load-remote.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/core/federation/manifest.spec.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/core/federation/manifest.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/core/federation/remote-loader.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/core/federation/timeout.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/app/modules/unavailable/unavailable.component.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/bootstrap.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/environments/environment.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/index.html.hbs +0 -0
- /package/templates/{angular → federation}/apps/shell/src/main.ts +0 -0
- /package/templates/{angular → federation}/apps/shell/src/styles.css +0 -0
- /package/templates/{angular → federation}/apps/shell/tsconfig.app.json +0 -0
- /package/templates/{angular → federation}/apps/shell/tsconfig.federation.json +0 -0
- /package/templates/{angular → federation}/apps/shell/tsconfig.json.hbs +0 -0
- /package/templates/{angular → federation}/apps/shell/tsconfig.spec.json +0 -0
- /package/templates/{angular → federation}/bahtui.json +0 -0
- /package/templates/{angular → federation}/knip.ts +0 -0
package/dist/domain/form-dsl.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BUILT_IN_FIELD_TYPES, parseSchema } from '@20baht/validator';
|
|
2
2
|
/**
|
|
3
|
-
* Compact field DSL for `
|
|
3
|
+
* Compact field DSL for `20baht g form` (spec 04-adjacent, PRD P3):
|
|
4
4
|
*
|
|
5
5
|
* --fields "title:text:required,priority:dropdown:options=low|medium|high,qty:number"
|
|
6
6
|
*
|
|
@@ -11,7 +11,7 @@ export function validateDomainName(domain) {
|
|
|
11
11
|
? []
|
|
12
12
|
: [`"${domain}" is not a valid domain name — lowercase letters, digits, dashes.`];
|
|
13
13
|
}
|
|
14
|
-
/** `
|
|
14
|
+
/** `20baht init` — write bahtui.json into an existing repo (root = cwd). */
|
|
15
15
|
export function planInit(root, config) {
|
|
16
16
|
return {
|
|
17
17
|
root,
|
|
@@ -22,7 +22,7 @@ export function planInit(root, config) {
|
|
|
22
22
|
export function modelDir(config, domain) {
|
|
23
23
|
return `${config.libsDir}/domain/${domain}/model`;
|
|
24
24
|
}
|
|
25
|
-
/** `
|
|
25
|
+
/** `20baht g model <domain>` — the framework-free domain model lib (PRD). */
|
|
26
26
|
export function planModel(root, config, domain) {
|
|
27
27
|
const dir = modelDir(config, domain);
|
|
28
28
|
const pkg = {
|
|
@@ -44,7 +44,7 @@ export function planModel(root, config, domain) {
|
|
|
44
44
|
postCommands: [],
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
-
/** `
|
|
47
|
+
/** `20baht g form <domain>/<name>` — engine-validated FormDefinition JSON. */
|
|
48
48
|
export function planForm(root, config, domain, name, form) {
|
|
49
49
|
const dir = modelDir(config, domain);
|
|
50
50
|
return {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const SCOPE = '@20baht/';
|
|
2
|
+
const DEP_FIELDS = [
|
|
3
|
+
'dependencies',
|
|
4
|
+
'devDependencies',
|
|
5
|
+
'peerDependencies',
|
|
6
|
+
'optionalDependencies',
|
|
7
|
+
];
|
|
8
|
+
/** `1.2.3`, with or without a prerelease tag: what a chosen version must look like. */
|
|
9
|
+
export const VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
10
|
+
/** `^1.2.3`, `~1.2.3` or `1.2.3`; anything else (a range, a tag, `workspace:*`) is not ours to move. */
|
|
11
|
+
const SIMPLE_SPEC = /^([\^~]?)(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/;
|
|
12
|
+
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
|
|
13
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
|
+
export const isPackageJson = (path) => path === 'package.json' || path.endsWith('/package.json');
|
|
15
|
+
/** A version's three numbers, and whether it carries a prerelease tag. */
|
|
16
|
+
function parts(version) {
|
|
17
|
+
const dash = version.indexOf('-');
|
|
18
|
+
const core = dash === -1 ? version : version.slice(0, dash);
|
|
19
|
+
return { numbers: core.split('.').map(Number), prerelease: dash !== -1 };
|
|
20
|
+
}
|
|
21
|
+
/** Whether version `a` is below `b`, by their `x.y.z`; a prerelease is below its release. */
|
|
22
|
+
function below(a, b) {
|
|
23
|
+
const [pa, pb] = [parts(a), parts(b)];
|
|
24
|
+
for (let i = 0; i < 3; i += 1) {
|
|
25
|
+
const [x, y] = [pa.numbers[i] ?? 0, pb.numbers[i] ?? 0];
|
|
26
|
+
if (x !== y)
|
|
27
|
+
return x < y;
|
|
28
|
+
}
|
|
29
|
+
return pa.prerelease && !pb.prerelease;
|
|
30
|
+
}
|
|
31
|
+
/** The `@20baht/*` dependencies a package.json lists, by name, with the range as written. */
|
|
32
|
+
export function familyDependencies(contents) {
|
|
33
|
+
let json;
|
|
34
|
+
try {
|
|
35
|
+
json = JSON.parse(contents);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const found = new Map();
|
|
41
|
+
if (!isRecord(json))
|
|
42
|
+
return found;
|
|
43
|
+
for (const field of DEP_FIELDS) {
|
|
44
|
+
const deps = json[field];
|
|
45
|
+
if (!isRecord(deps))
|
|
46
|
+
continue;
|
|
47
|
+
for (const [name, spec] of Object.entries(deps)) {
|
|
48
|
+
if (name.startsWith(SCOPE) && typeof spec === 'string')
|
|
49
|
+
found.set(name, spec);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* One package.json. Its dependency lines are edited as text, so the file keeps its formatting
|
|
56
|
+
* (`JSON.stringify` would re-wrap arrays and fail the repo's own format check).
|
|
57
|
+
*/
|
|
58
|
+
function planFile(file, options, plan) {
|
|
59
|
+
const deps = familyDependencies(file.contents);
|
|
60
|
+
if (deps === null) {
|
|
61
|
+
plan.notes.push(`${file.path}: not valid JSON, left as it is.`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
let contents = file.contents;
|
|
65
|
+
for (const [name, spec] of deps) {
|
|
66
|
+
const target = options.targets.get(name);
|
|
67
|
+
if (target === undefined)
|
|
68
|
+
continue;
|
|
69
|
+
const simple = SIMPLE_SPEC.exec(spec);
|
|
70
|
+
if (simple === null) {
|
|
71
|
+
if (!spec.startsWith('workspace:')) {
|
|
72
|
+
plan.notes.push(`${file.path}: ${name} "${spec}" is not a plain version, left as it is.`);
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const [, prefix = '', current = ''] = simple;
|
|
77
|
+
if (current === target || (!options.lower && !below(current, target)))
|
|
78
|
+
continue;
|
|
79
|
+
const next = `${prefix}${target}`;
|
|
80
|
+
const line = new RegExp(`"${escapeRegExp(name)}"(\\s*:\\s*)"${escapeRegExp(spec)}"`, 'g');
|
|
81
|
+
contents = contents.replace(line, `"${name}"$1"${next}"`);
|
|
82
|
+
plan.changes.push(`${file.path}: ${name} ${spec} → ${next}`);
|
|
83
|
+
}
|
|
84
|
+
if (contents !== file.contents)
|
|
85
|
+
plan.files.push({ path: file.path, contents });
|
|
86
|
+
}
|
|
87
|
+
export function planUpdate(files, options) {
|
|
88
|
+
const plan = { files: [], changes: [], notes: [] };
|
|
89
|
+
for (const file of files)
|
|
90
|
+
if (isPackageJson(file.path))
|
|
91
|
+
planFile(file, options, plan);
|
|
92
|
+
return plan;
|
|
93
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
2
3
|
export class DiskFileReader {
|
|
3
4
|
exists(path) {
|
|
4
5
|
return existsSync(path);
|
|
@@ -9,7 +10,7 @@ export class DiskFileReader {
|
|
|
9
10
|
}
|
|
10
11
|
catch (err) {
|
|
11
12
|
// null strictly means "not there" — callers use it to decide the file is
|
|
12
|
-
// absent (e.g. "run
|
|
13
|
+
// absent (e.g. "run 20baht init first"). A permission or IO error must
|
|
13
14
|
// surface, not masquerade as a missing file.
|
|
14
15
|
if (err.code === 'ENOENT')
|
|
15
16
|
return null;
|
|
@@ -17,3 +18,34 @@ export class DiskFileReader {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
}
|
|
21
|
+
/** Folders that hold what was installed or built, never what a person wrote. */
|
|
22
|
+
const SKIPPED_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', 'out']);
|
|
23
|
+
/** Source and config files: what a command that edits across a repo may read. */
|
|
24
|
+
const TEXT_FILE = /\.(?:[cm]?[jt]sx?|json|s?css|html|astro|vue|svelte)$/;
|
|
25
|
+
const SKIPPED_FILES = new Set(['package-lock.json']);
|
|
26
|
+
/** Past this a file is data or a bundle, not source. */
|
|
27
|
+
const MAX_BYTES = 1_000_000;
|
|
28
|
+
export class DiskFileWalker {
|
|
29
|
+
list(root) {
|
|
30
|
+
const found = [];
|
|
31
|
+
const walk = (dir, prefix) => {
|
|
32
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
33
|
+
const rel = `${prefix}${entry.name}`;
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
// Dot folders are caches, VCS and tool state; dot files (`.dependency-cruiser.mjs`) are config.
|
|
36
|
+
if (!entry.name.startsWith('.') && !SKIPPED_DIRS.has(entry.name)) {
|
|
37
|
+
walk(join(dir, entry.name), `${rel}/`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (entry.isFile() &&
|
|
41
|
+
TEXT_FILE.test(entry.name) &&
|
|
42
|
+
!SKIPPED_FILES.has(entry.name) &&
|
|
43
|
+
statSync(join(dir, entry.name)).size <= MAX_BYTES) {
|
|
44
|
+
found.push(rel);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
walk(root, '');
|
|
49
|
+
return found.sort((a, b) => (a < b ? -1 : 1));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const REGISTRY = 'https://registry.npmjs.org';
|
|
2
|
+
const TIMEOUT_MS = 15_000;
|
|
3
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
+
/** The public npm registry, read over HTTP: no npm on PATH needed, and only a package name is sent. */
|
|
5
|
+
export class NpmRegistry {
|
|
6
|
+
constructor(request = fetch) {
|
|
7
|
+
this.request = request;
|
|
8
|
+
}
|
|
9
|
+
async info(name) {
|
|
10
|
+
const response = await this.request(`${REGISTRY}/${name.replace('/', '%2F')}`, {
|
|
11
|
+
// The abbreviated document: versions and dist-tags without every readme.
|
|
12
|
+
headers: { accept: 'application/vnd.npm.install-v1+json' },
|
|
13
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
14
|
+
});
|
|
15
|
+
if (response.status === 404)
|
|
16
|
+
return null;
|
|
17
|
+
if (!response.ok)
|
|
18
|
+
throw new Error(`${name}: HTTP ${String(response.status)}`);
|
|
19
|
+
const body = await response.json();
|
|
20
|
+
const tags = isRecord(body) ? body['dist-tags'] : undefined;
|
|
21
|
+
const latest = isRecord(tags) ? tags.latest : undefined;
|
|
22
|
+
const versions = isRecord(body) ? body.versions : undefined;
|
|
23
|
+
if (typeof latest !== 'string' || !isRecord(versions)) {
|
|
24
|
+
throw new Error(`${name}: the registry's answer has no latest version`);
|
|
25
|
+
}
|
|
26
|
+
return { latest, versions: Object.keys(versions) };
|
|
27
|
+
}
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20baht/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Scaffold
|
|
5
|
+
"description": "Scaffold apps on the @20baht libraries: Angular + Elysia (20baht create), optionally as Native Federation microfrontends, a bare bun workspace (20baht new), plus bahtui generators.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Nattapong Promthong",
|
|
8
8
|
"repository": {
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
"bun",
|
|
23
23
|
"elysia",
|
|
24
24
|
"eden",
|
|
25
|
-
"react"
|
|
25
|
+
"react",
|
|
26
|
+
"angular",
|
|
27
|
+
"native-federation"
|
|
26
28
|
],
|
|
27
29
|
"publishConfig": {
|
|
28
30
|
"access": "public"
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
"node": ">=20.12"
|
|
32
34
|
},
|
|
33
35
|
"bin": {
|
|
34
|
-
"
|
|
36
|
+
"20baht": "./dist/cli.js"
|
|
35
37
|
},
|
|
36
38
|
"files": [
|
|
37
39
|
"dist",
|
|
@@ -42,14 +44,15 @@
|
|
|
42
44
|
"test": "vitest run",
|
|
43
45
|
"test:watch": "vitest",
|
|
44
46
|
"test:coverage": "vitest run --coverage",
|
|
45
|
-
"accept:angular": "bun scripts/accept-angular.ts"
|
|
47
|
+
"accept:angular": "bun scripts/accept-angular.ts",
|
|
48
|
+
"accept:federation": "bun scripts/accept-angular.ts federation"
|
|
46
49
|
},
|
|
47
50
|
"dependencies": {
|
|
48
|
-
"@20baht/validator": ">=0.
|
|
51
|
+
"@20baht/validator": ">=0.3.0 <0.4.0"
|
|
49
52
|
},
|
|
50
53
|
"devDependencies": {
|
|
51
54
|
"@types/node": "^22",
|
|
52
|
-
"typescript": "^5.
|
|
55
|
+
"typescript": "^5.9.3",
|
|
53
56
|
"vitest": "^3.2.4"
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { cors } from '@elysiajs/cors'
|
|
2
|
+
import { validateSubmission } from '@20baht/validator'
|
|
2
3
|
import { Elysia } from 'elysia'
|
|
4
|
+
import { contactForm } from './forms/contact.form'
|
|
3
5
|
|
|
4
6
|
/** In dev the host and the remotes run on their own localhost ports; deployed, all share one origin. */
|
|
5
7
|
const DEV_ORIGIN = /^http:\/\/localhost:\d+$/
|
|
@@ -12,3 +14,11 @@ export const buildApp = () =>
|
|
|
12
14
|
new Elysia({ prefix: '/api' })
|
|
13
15
|
.use(cors({ origin: DEV_ORIGIN, credentials: true }))
|
|
14
16
|
.get('/hello', () => ({ message: 'Hello from the server' }))
|
|
17
|
+
// A form: the schema the browser renders, and the same schema checking what comes back.
|
|
18
|
+
.get('/contact/form', () => contactForm)
|
|
19
|
+
.post('/contact', ({ body, status }) => {
|
|
20
|
+
const result = validateSubmission(contactForm, body)
|
|
21
|
+
if (!result.valid) return status(422, { message: 'Check the form', issues: result.issues })
|
|
22
|
+
// `result.data` is what to keep: defaults filled, anything the rules hide cleared.
|
|
23
|
+
return { received: result.data }
|
|
24
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FormDefinition } from '@20baht/engine'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One form, as JSON-shaped data: the web app renders it with `<baht-form>` and `POST /api/contact`
|
|
5
|
+
* validates a submission against it with the same rules. Add a field here and both sides have it.
|
|
6
|
+
*/
|
|
7
|
+
export const contactForm: FormDefinition = {
|
|
8
|
+
form_id: 'contact',
|
|
9
|
+
form_name: 'Contact',
|
|
10
|
+
fields: [
|
|
11
|
+
{
|
|
12
|
+
order: 1,
|
|
13
|
+
type: 'text',
|
|
14
|
+
key: 'name',
|
|
15
|
+
label: 'Name',
|
|
16
|
+
validators: [{ type: 'required' }],
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
order: 2,
|
|
20
|
+
type: 'email',
|
|
21
|
+
key: 'email',
|
|
22
|
+
label: 'Email',
|
|
23
|
+
validators: [{ type: 'required' }],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
order: 3,
|
|
27
|
+
type: 'textarea',
|
|
28
|
+
key: 'message',
|
|
29
|
+
label: 'Message',
|
|
30
|
+
max_length: 500,
|
|
31
|
+
validators: [{ type: 'required' }],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
}
|
|
@@ -2,6 +2,15 @@ import { describe, expect, test } from 'bun:test'
|
|
|
2
2
|
|
|
3
3
|
import { buildApp } from '../src/app'
|
|
4
4
|
|
|
5
|
+
const post = (body: unknown) =>
|
|
6
|
+
buildApp().handle(
|
|
7
|
+
new Request('http://localhost/api/contact', {
|
|
8
|
+
method: 'POST',
|
|
9
|
+
headers: { 'content-type': 'application/json' },
|
|
10
|
+
body: JSON.stringify(body),
|
|
11
|
+
}),
|
|
12
|
+
)
|
|
13
|
+
|
|
5
14
|
describe('api', () => {
|
|
6
15
|
test('says hello', async () => {
|
|
7
16
|
const response = await buildApp().handle(new Request('http://localhost/api/hello'))
|
|
@@ -9,4 +18,24 @@ describe('api', () => {
|
|
|
9
18
|
expect(response.status).toBe(200)
|
|
10
19
|
expect(await response.json()).toEqual({ message: 'Hello from the server' })
|
|
11
20
|
})
|
|
21
|
+
|
|
22
|
+
test('serves the contact form the browser renders', async () => {
|
|
23
|
+
const response = await buildApp().handle(new Request('http://localhost/api/contact/form'))
|
|
24
|
+
|
|
25
|
+
expect(response.status).toBe(200)
|
|
26
|
+
expect(await response.json()).toMatchObject({ form_name: 'Contact' })
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('keeps a valid submission and refuses an invalid one with the field at fault', async () => {
|
|
30
|
+
const good = await post({ name: 'Ada', email: 'ada@example.com', message: 'Hello' })
|
|
31
|
+
expect(good.status).toBe(200)
|
|
32
|
+
expect(await good.json()).toEqual({
|
|
33
|
+
received: { name: 'Ada', email: 'ada@example.com', message: 'Hello' },
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const bad = await post({ name: 'Ada', email: 'not-an-email' })
|
|
37
|
+
expect(bad.status).toBe(422)
|
|
38
|
+
const refused = (await bad.json()) as { issues: { ref: string }[] }
|
|
39
|
+
expect(refused.issues.map((issue) => issue.ref).sort()).toEqual(['email', 'message'])
|
|
40
|
+
})
|
|
12
41
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# {{name}}
|
|
2
2
|
|
|
3
3
|
Angular microfrontends on Native Federation, scaffolded by
|
|
4
|
-
[`@20baht/cli`](https://github.com/bank20baht/20baht) (`
|
|
4
|
+
[`@20baht/cli`](https://github.com/bank20baht/20baht) (`20baht create --federation`).
|
|
5
5
|
|
|
6
6
|
```
|
|
7
7
|
apps/shell the host (:4200): navigator from the federation manifest, a fallback page per remote
|
|
@@ -21,7 +21,7 @@ bun run check # lint, format, typecheck, madge, boundaries, knip, dup, buil
|
|
|
21
21
|
## Add a remote
|
|
22
22
|
|
|
23
23
|
```sh
|
|
24
|
-
|
|
24
|
+
20baht g remote orders # apps/orders-web on the next port, mounted at /orders
|
|
25
25
|
bun install
|
|
26
26
|
```
|
|
27
27
|
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"@angular/core": "^22.2.0",
|
|
20
20
|
"@angular/platform-browser": "^22.2.0",
|
|
21
21
|
"@angular/router": "^22.2.0",
|
|
22
|
-
"@20baht/engine": "0.
|
|
23
|
-
"@20baht/ui": "0.
|
|
24
|
-
"@20baht/angular": "0.
|
|
22
|
+
"@20baht/engine": "0.3.0",
|
|
23
|
+
"@20baht/ui": "0.3.0",
|
|
24
|
+
"@20baht/angular": "0.3.0",
|
|
25
25
|
"dompurify": "3.4.17",
|
|
26
26
|
"es-module-shims": "^2.8.0",
|
|
27
27
|
"flatpickr": "4.6.13",
|
package/templates/{angular → federation}/apps/shell/src/app/modules/home/home.component.ts.hbs
RENAMED
|
@@ -29,7 +29,7 @@ import { MANIFEST } from '../../core/federation/remote-loader'
|
|
|
29
29
|
} @empty {
|
|
30
30
|
<baht-empty-state
|
|
31
31
|
message="No remotes yet"
|
|
32
|
-
hint="Add one with
|
|
32
|
+
hint="Add one with 20baht g remote, then restart bun run dev."
|
|
33
33
|
></baht-empty-state>
|
|
34
34
|
}
|
|
35
35
|
</div>
|
package/templates/{angular → federation}/apps/shell/src/environments/environment.development.ts
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Manifest } from '../app/core/federation/manifest'
|
|
2
2
|
|
|
3
|
-
/** The remotes on their dev servers (`bun run dev`). `
|
|
3
|
+
/** The remotes on their dev servers (`bun run dev`). `20baht g remote` adds an entry. */
|
|
4
4
|
const remotes: Manifest = {
|
|
5
5
|
// bahtui:remotes
|
|
6
6
|
}
|
|
@@ -2,7 +2,7 @@ import { defineConfig } from 'eslint/config'
|
|
|
2
2
|
|
|
3
3
|
import { angular, bahtui, base } from '@20baht/strict/eslint'
|
|
4
4
|
|
|
5
|
-
/** The host, its remotes, and the kit they all compile in. `
|
|
5
|
+
/** The host, its remotes, and the kit they all compile in. `20baht g remote` adds a line. */
|
|
6
6
|
const WEB = [
|
|
7
7
|
'apps/shell/src/**',
|
|
8
8
|
'packages/kit/src/**',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# {{name}}
|
|
2
2
|
|
|
3
3
|
Bun-workspace monorepo scaffolded by [`@20baht/cli`](https://github.com/bank20baht/20baht)
|
|
4
|
-
(`
|
|
4
|
+
(`20baht new`). Toolchain only — add your own apps under `apps/*` and shared
|
|
5
5
|
libraries under `packages/*`.
|
|
6
6
|
|
|
7
7
|
## Quickstart
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"@angular/core": "^22.2.0",
|
|
18
18
|
"@angular/platform-browser": "^22.2.0",
|
|
19
19
|
"@angular/router": "^22.2.0",
|
|
20
|
-
"@20baht/engine": "0.
|
|
21
|
-
"@20baht/ui": "0.
|
|
22
|
-
"@20baht/angular": "0.
|
|
20
|
+
"@20baht/engine": "0.3.0",
|
|
21
|
+
"@20baht/ui": "0.3.0",
|
|
22
|
+
"@20baht/angular": "0.3.0",
|
|
23
23
|
"dompurify": "3.4.17",
|
|
24
24
|
"es-module-shims": "^2.8.0",
|
|
25
25
|
"flatpickr": "4.6.13",
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
An Angular app and an Elysia server on the `@20baht` libraries, scaffolded by
|
|
4
|
+
[`@20baht/cli`](https://github.com/bank20baht/20baht) (`20baht create`).
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
apps/web the Angular app (:4200): @20baht/ui app shell, one page calling one API route
|
|
8
|
+
apps/server Elysia API (:3001) under /api; exports `type App` as built declarations
|
|
9
|
+
packages/kit the typed ngx-eden client and error text, compiled into the app as @{{name}}/kit
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Quickstart
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
bun install
|
|
16
|
+
bun run dev # every apps/* dev script in parallel
|
|
17
|
+
bun run check # lint, format, typecheck, madge, boundaries, knip, dup, build, test
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## The form
|
|
21
|
+
|
|
22
|
+
`apps/server/src/forms/contact.form.ts` is one form as data. The server serves it
|
|
23
|
+
(`GET /api/contact/form`) and validates a submission against it (`POST /api/contact`,
|
|
24
|
+
`validateSubmission`); `apps/web/src/app/modules/contact` renders it with `<baht-form>`. That is all
|
|
25
|
+
a form needs: no workflow, no approval step. Add a field to the schema and both sides have it. For
|
|
26
|
+
the next form, copy the three pieces.
|
|
27
|
+
|
|
28
|
+
## Add a page
|
|
29
|
+
|
|
30
|
+
A component under `apps/web/src/app/modules/<page>/`, a route in `app.routes.ts`, an entry in
|
|
31
|
+
`core/navigation/navigation.ts`. What a page asks the server goes in a service under `services/`,
|
|
32
|
+
through the kit's `Api`.
|
|
33
|
+
|
|
34
|
+
## Things that are this way on purpose
|
|
35
|
+
|
|
36
|
+
- The web app sees the server only as types, from `apps/server/dist/types` (`build:types` runs
|
|
37
|
+
before every web build and typecheck). Keep the server's imports extensionless: Angular's
|
|
38
|
+
compiler rejects `./x.ts` specifiers in sources it follows.
|
|
39
|
+
- `apps/web` lists `marked`, `dompurify` and `flatpickr`: they are optional peers of @20baht/ui,
|
|
40
|
+
which imports them when a markdown or date field renders.
|
|
41
|
+
- In dev the app calls the server on `:3001`; in production it calls `/api` on its own origin, so
|
|
42
|
+
serve both behind one web server.
|
|
43
|
+
- This is one app. Microfrontends on Native Federation (a host that reads its remotes from a
|
|
44
|
+
manifest at runtime) are another scaffold: `20baht create <name> --federation`.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Architecture boundaries, checked with `bun run deps`. `generic` gives no-circular and
|
|
3
|
+
* production-not-tests. The rules below keep the web app's layers and the packages in order.
|
|
4
|
+
* Every rule is `error`: a violation is a decision, not a warning.
|
|
5
|
+
*/
|
|
6
|
+
import { makeConfig } from '@20baht/strict/depcruise'
|
|
7
|
+
|
|
8
|
+
const base = makeConfig({ generic: true })
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
...base,
|
|
12
|
+
forbidden: [
|
|
13
|
+
...base.forbidden,
|
|
14
|
+
{
|
|
15
|
+
name: 'web-lower-layers-know-no-page',
|
|
16
|
+
severity: 'error',
|
|
17
|
+
comment: 'core/, services/ and shared/ know no page; pages are reached through the router.',
|
|
18
|
+
from: { path: '^apps/web/src/app/(core|services|shared)/' },
|
|
19
|
+
to: { path: '^apps/web/src/app/modules/' },
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: 'packages-know-no-app',
|
|
23
|
+
severity: 'error',
|
|
24
|
+
comment: 'packages/ are below the apps; the server reaches them only as built types.',
|
|
25
|
+
from: { path: '^packages/' },
|
|
26
|
+
to: { path: '^apps/', pathNot: '^apps/server/dist/types/' },
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Editor configuration, see https://editorconfig.org
|
|
2
|
+
root = true
|
|
3
|
+
|
|
4
|
+
[*]
|
|
5
|
+
charset = utf-8
|
|
6
|
+
indent_style = space
|
|
7
|
+
indent_size = 2
|
|
8
|
+
insert_final_newline = true
|
|
9
|
+
trim_trailing_whitespace = true
|
|
10
|
+
|
|
11
|
+
[*.ts]
|
|
12
|
+
quote_type = single
|
|
13
|
+
ij_typescript_use_double_quotes = false
|
|
14
|
+
|
|
15
|
+
[*.md]
|
|
16
|
+
max_line_length = off
|
|
17
|
+
trim_trailing_whitespace = false
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
|
3
|
+
"version": 1,
|
|
4
|
+
"cli": {
|
|
5
|
+
"packageManager": "bun"
|
|
6
|
+
},
|
|
7
|
+
"newProjectRoot": "projects",
|
|
8
|
+
"projects": {
|
|
9
|
+
"web": {
|
|
10
|
+
"projectType": "application",
|
|
11
|
+
"schematics": {},
|
|
12
|
+
"root": "",
|
|
13
|
+
"sourceRoot": "src",
|
|
14
|
+
"prefix": "app",
|
|
15
|
+
"architect": {
|
|
16
|
+
"build": {
|
|
17
|
+
"builder": "@angular/build:application",
|
|
18
|
+
"options": {
|
|
19
|
+
"browser": "src/main.ts",
|
|
20
|
+
"tsConfig": "tsconfig.app.json",
|
|
21
|
+
"styles": ["node_modules/@20baht/ui/dist/tokens.css", "src/styles.css"]
|
|
22
|
+
},
|
|
23
|
+
"configurations": {
|
|
24
|
+
"production": {
|
|
25
|
+
"budgets": [
|
|
26
|
+
{
|
|
27
|
+
"type": "initial",
|
|
28
|
+
"maximumWarning": "500kB",
|
|
29
|
+
"maximumError": "1MB"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"type": "anyComponentStyle",
|
|
33
|
+
"maximumWarning": "4kB",
|
|
34
|
+
"maximumError": "8kB"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"outputHashing": "all"
|
|
38
|
+
},
|
|
39
|
+
"development": {
|
|
40
|
+
"optimization": false,
|
|
41
|
+
"extractLicenses": false,
|
|
42
|
+
"sourceMap": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"defaultConfiguration": "production"
|
|
46
|
+
},
|
|
47
|
+
"serve": {
|
|
48
|
+
"builder": "@angular/build:dev-server",
|
|
49
|
+
"configurations": {
|
|
50
|
+
"production": {
|
|
51
|
+
"buildTarget": "web:build:production"
|
|
52
|
+
},
|
|
53
|
+
"development": {
|
|
54
|
+
"buildTarget": "web:build:development"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"defaultConfiguration": "development",
|
|
58
|
+
"options": {
|
|
59
|
+
"port": 4200
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"test": {
|
|
63
|
+
"builder": "@angular/build:unit-test",
|
|
64
|
+
"options": {
|
|
65
|
+
"coverageReporters": ["text", "lcov"],
|
|
66
|
+
"coverageExclude": ["src/main.ts"]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|