create-fsaa 0.1.7 → 0.2.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/LICENSE +21 -21
- package/README.md +159 -160
- package/bin/fsaa.mjs +15 -15
- package/lib/cli.mjs +97 -92
- package/lib/download.mjs +66 -66
- package/lib/generate.mjs +316 -161
- package/lib/init.mjs +171 -171
- package/lib/net.mjs +12 -12
- package/lib/snippets.mjs +283 -221
- package/package.json +32 -32
package/lib/download.mjs
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
/* fsaa/lib/download.mjs — degit-style fetch of a template repo tarball (zero deps).
|
|
2
|
-
Downloads the codeload tar.gz, extracts with the system `tar` (ships with
|
|
3
|
-
Linux, macOS, Windows 10+), then moves the tarball's root folder to the target dir. */
|
|
4
|
-
import { mkdtempSync, mkdirSync, readdirSync, writeFileSync, rmSync, renameSync, cpSync, existsSync, readFileSync, statSync } from "node:fs";
|
|
5
|
-
import { tmpdir } from "node:os";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
import { spawnSync } from "node:child_process";
|
|
8
|
-
import { fetchFile } from "./net.mjs";
|
|
9
|
-
|
|
10
|
-
export const TEMPLATES = {
|
|
11
|
-
next: { repo: "Aramex86/fsaa-next", label: "Next.js App Router" },
|
|
12
|
-
vite: { repo: "Aramex86/fsaa-vite", label: "plain React (Vite + react-router)" },
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export function templateLabel(template) {
|
|
16
|
-
return TEMPLATES[template]?.label ?? template;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Download template@main and unpack its root folder contents into targetDir. */
|
|
20
|
-
export async function fetchTemplate(template, targetDir) {
|
|
21
|
-
const t = TEMPLATES[template];
|
|
22
|
-
if (!t) throw new Error(`unknown template "${template}"`);
|
|
23
|
-
const tmp = mkdtempSync(join(tmpdir(), "fsaa-template-"));
|
|
24
|
-
try {
|
|
25
|
-
const tgz = join(tmp, "template.tar.gz");
|
|
26
|
-
writeFileSync(tgz, await fetchFile(TARBALL_URL(t.repo)));
|
|
27
|
-
const x = join(tmp, "x");
|
|
28
|
-
mkdirSync(x, { recursive: true });
|
|
29
|
-
const r = spawnSync("tar", ["-xzf", tgz, "-C", x], { stdio: "pipe" });
|
|
30
|
-
if (r.status !== 0) {
|
|
31
|
-
const msg = (r.stderr || "tar not found on PATH").toString().trim().slice(0, 160);
|
|
32
|
-
throw new Error(`tar extract failed: ${msg}`);
|
|
33
|
-
}
|
|
34
|
-
const entries = readdirSync(x).filter((e) => e !== "pax_global_header");
|
|
35
|
-
if (entries.length !== 1) throw new Error("unexpected tarball layout");
|
|
36
|
-
const root = join(x, entries[0]);
|
|
37
|
-
if (!statSync(root).isDirectory()) throw new Error("tarball root is not a directory");
|
|
38
|
-
moveDir(root, targetDir);
|
|
39
|
-
return t.repo;
|
|
40
|
-
} finally {
|
|
41
|
-
rmSync(tmp, { recursive: true, force: true });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const TARBALL_URL = (repo) => `https://codeload.github.com/${repo}/tar.gz/refs/heads/main`;
|
|
46
|
-
|
|
47
|
-
function moveDir(src, dest) {
|
|
48
|
-
// dest must not exist as a directory: rename would nest (same for `mv`, `move`, and cp fallback).
|
|
49
|
-
// Callers guarantee a non-existent dest (init/generate check first).
|
|
50
|
-
if (existsSync(dest)) {
|
|
51
|
-
throw new Error(
|
|
52
|
-
`destination already exists: ${dest}\n` +
|
|
53
|
-
` (safeguard: refuse to overwrite; run fsaa in an empty or non-existent directory)`,
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
// fs-only move: rename first, cpSync fallback for anything Windows throws at
|
|
57
|
-
// a freshly-extracted dir on rename (EXDEV cross-device, EPERM/EBUSY from an
|
|
58
|
-
// antivirus/indexing scan holding a handle). Copy-then-cleanup self-heals all.
|
|
59
|
-
try {
|
|
60
|
-
renameSync(src, dest);
|
|
61
|
-
return;
|
|
62
|
-
} catch (err) {
|
|
63
|
-
if (!["EXDEV", "EPERM", "EACCES", "EBUSY", "ENOTEMPTY", "EISDIR"].includes(err?.code)) throw err;
|
|
64
|
-
}
|
|
65
|
-
cpSync(src, dest, { recursive: true });
|
|
66
|
-
rmSync(src, { recursive: true, force: true });
|
|
1
|
+
/* fsaa/lib/download.mjs — degit-style fetch of a template repo tarball (zero deps).
|
|
2
|
+
Downloads the codeload tar.gz, extracts with the system `tar` (ships with
|
|
3
|
+
Linux, macOS, Windows 10+), then moves the tarball's root folder to the target dir. */
|
|
4
|
+
import { mkdtempSync, mkdirSync, readdirSync, writeFileSync, rmSync, renameSync, cpSync, existsSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { fetchFile } from "./net.mjs";
|
|
9
|
+
|
|
10
|
+
export const TEMPLATES = {
|
|
11
|
+
next: { repo: "Aramex86/fsaa-next", label: "Next.js App Router" },
|
|
12
|
+
vite: { repo: "Aramex86/fsaa-vite", label: "plain React (Vite + react-router)" },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function templateLabel(template) {
|
|
16
|
+
return TEMPLATES[template]?.label ?? template;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Download template@main and unpack its root folder contents into targetDir. */
|
|
20
|
+
export async function fetchTemplate(template, targetDir) {
|
|
21
|
+
const t = TEMPLATES[template];
|
|
22
|
+
if (!t) throw new Error(`unknown template "${template}"`);
|
|
23
|
+
const tmp = mkdtempSync(join(tmpdir(), "fsaa-template-"));
|
|
24
|
+
try {
|
|
25
|
+
const tgz = join(tmp, "template.tar.gz");
|
|
26
|
+
writeFileSync(tgz, await fetchFile(TARBALL_URL(t.repo)));
|
|
27
|
+
const x = join(tmp, "x");
|
|
28
|
+
mkdirSync(x, { recursive: true });
|
|
29
|
+
const r = spawnSync("tar", ["-xzf", tgz, "-C", x], { stdio: "pipe" });
|
|
30
|
+
if (r.status !== 0) {
|
|
31
|
+
const msg = (r.stderr || "tar not found on PATH").toString().trim().slice(0, 160);
|
|
32
|
+
throw new Error(`tar extract failed: ${msg}`);
|
|
33
|
+
}
|
|
34
|
+
const entries = readdirSync(x).filter((e) => e !== "pax_global_header");
|
|
35
|
+
if (entries.length !== 1) throw new Error("unexpected tarball layout");
|
|
36
|
+
const root = join(x, entries[0]);
|
|
37
|
+
if (!statSync(root).isDirectory()) throw new Error("tarball root is not a directory");
|
|
38
|
+
moveDir(root, targetDir);
|
|
39
|
+
return t.repo;
|
|
40
|
+
} finally {
|
|
41
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const TARBALL_URL = (repo) => `https://codeload.github.com/${repo}/tar.gz/refs/heads/main`;
|
|
46
|
+
|
|
47
|
+
function moveDir(src, dest) {
|
|
48
|
+
// dest must not exist as a directory: rename would nest (same for `mv`, `move`, and cp fallback).
|
|
49
|
+
// Callers guarantee a non-existent dest (init/generate check first).
|
|
50
|
+
if (existsSync(dest)) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`destination already exists: ${dest}\n` +
|
|
53
|
+
` (safeguard: refuse to overwrite; run fsaa in an empty or non-existent directory)`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
// fs-only move: rename first, cpSync fallback for anything Windows throws at
|
|
57
|
+
// a freshly-extracted dir on rename (EXDEV cross-device, EPERM/EBUSY from an
|
|
58
|
+
// antivirus/indexing scan holding a handle). Copy-then-cleanup self-heals all.
|
|
59
|
+
try {
|
|
60
|
+
renameSync(src, dest);
|
|
61
|
+
return;
|
|
62
|
+
} catch (err) {
|
|
63
|
+
if (!["EXDEV", "EPERM", "EACCES", "EBUSY", "ENOTEMPTY", "EISDIR"].includes(err?.code)) throw err;
|
|
64
|
+
}
|
|
65
|
+
cpSync(src, dest, { recursive: true });
|
|
66
|
+
rmSync(src, { recursive: true, force: true });
|
|
67
67
|
}
|
package/lib/generate.mjs
CHANGED
|
@@ -1,162 +1,317 @@
|
|
|
1
|
-
/* fsaa/lib/generate.mjs — fsaa g
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
1
|
+
/* fsaa/lib/generate.mjs — fsaa g: canonical generators inside an existing
|
|
2
|
+
FSAA app, per agents/add-module.md and the fsaa canon:
|
|
3
|
+
|
|
4
|
+
fsaa g <slice> legacy pair: full entity + full feature + thin route
|
|
5
|
+
fsaa g entity <noun> entity only (index/types/api/slice/__tests__)
|
|
6
|
+
fsaa g feature <name> feature only (index/ui/config/__tests__)
|
|
7
|
+
fsaa g atom <Name> shared/ui/atoms/<Name>.tsx (stateless primitive)
|
|
8
|
+
fsaa g molecule <Name> shared/ui/molecules/<Name>/ (+ graduation hint)
|
|
9
|
+
fsaa g page <name> [route] thin page (+ router registration on vite)
|
|
10
|
+
|
|
11
|
+
Names: entity/feature/page are kebab-case; atom/molecule are PascalCase.
|
|
12
|
+
All snippets live in snippets.mjs — one source of truth. */
|
|
13
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
14
|
+
import { join, resolve } from "node:path";
|
|
15
|
+
import { entityFiles, featureFiles, atomFile, moleculeFiles, pascal } from "./snippets.mjs";
|
|
16
|
+
|
|
17
|
+
const KINDS = ["entity", "feature", "atom", "molecule", "page"];
|
|
18
|
+
|
|
19
|
+
const USAGE = `fsaa g — canonical generators (run inside an FSAA app):
|
|
20
|
+
fsaa g <slice-name> # pair: entity + feature + thin route (e.g. fsaa g checkout)
|
|
21
|
+
fsaa g entity <noun> # entity only: src/entities/<noun>/{index,types,api,slice,__tests__}
|
|
22
|
+
fsaa g feature <name> # feature only: src/features/<name>/{index,ui,config,__tests__}
|
|
23
|
+
fsaa g atom <Name> # src/shared/ui/atoms/<Name>.tsx (stateless primitive)
|
|
24
|
+
fsaa g molecule <Name> # src/shared/ui/molecules/<Name>/ (+ graduation hint)
|
|
25
|
+
fsaa g page <name> [route] # thin page; route defaults to the name`;
|
|
26
|
+
|
|
27
|
+
export async function generateCmd({ args }) {
|
|
28
|
+
const [first] = args.filter((a) => !a.startsWith("--"));
|
|
29
|
+
if (!first) {
|
|
30
|
+
console.error(USAGE);
|
|
31
|
+
return 1;
|
|
32
|
+
}
|
|
33
|
+
const positional = args.filter((a) => !a.startsWith("--"));
|
|
34
|
+
if ((KINDS).includes(first)) {
|
|
35
|
+
const [, ...rest] = positional;
|
|
36
|
+
if (!rest.length || !rest[0]) {
|
|
37
|
+
console.error(`fsaa g: missing name — ${USAGE}`);
|
|
38
|
+
return 1;
|
|
39
|
+
}
|
|
40
|
+
const cwd = resolve(process.cwd());
|
|
41
|
+
const detected = detectTemplate(cwd);
|
|
42
|
+
if (!detected) {
|
|
43
|
+
console.error('fsaa g: run this inside an FSAA app (scaffolded by "fsaa init")');
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
const { template } = detected;
|
|
47
|
+
const name = rest[0];
|
|
48
|
+
const route = rest[1];
|
|
49
|
+
if (first === "entity") return genEntity(cwd, name);
|
|
50
|
+
if (first === "feature") return genFeature(cwd, name, template);
|
|
51
|
+
if (first === "atom") return genAtom(cwd, name);
|
|
52
|
+
if (first === "molecule") return genMolecule(cwd, name);
|
|
53
|
+
if (first === "page") return genPage(cwd, name, route, template);
|
|
54
|
+
}
|
|
55
|
+
// legacy: fsaa g <slice> = pair (entity + feature + route)
|
|
56
|
+
return genPair(positional[0]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* ---------- shared validation ---------- */
|
|
60
|
+
|
|
61
|
+
const KEBAB = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
|
|
62
|
+
const PASCAL_NAME = /^[A-Z][A-Za-z0-9]*$/;
|
|
63
|
+
|
|
64
|
+
function fail(msg) {
|
|
65
|
+
console.error(msg);
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
const verify = () =>
|
|
69
|
+
`\nVerify (agents/add-module.md):\n pnpm lint · pnpm typecheck · pnpm test\n node scripts/check-agents-md.mjs`;
|
|
70
|
+
|
|
71
|
+
/* ---------- generators ---------- */
|
|
72
|
+
|
|
73
|
+
function genEntity(cwd, raw) {
|
|
74
|
+
const name = raw.toLowerCase();
|
|
75
|
+
if (!KEBAB.test(name))
|
|
76
|
+
return fail(`fsaa g: invalid entity name "${raw}" — lowercase words/digits, single dashes (e.g. invoices-overview)`);
|
|
77
|
+
const dir = join(cwd, "src", "entities", name);
|
|
78
|
+
if (existsSync(dir))
|
|
79
|
+
return fail(`fsaa g: entity already exists: src/entities/${name}`);
|
|
80
|
+
writeTree(dir, entityFiles(name));
|
|
81
|
+
console.log(` ✔ src/entities/${name}/ (index.ts, types.ts, api.ts, slice.ts, __tests__/)${verify()}`);
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function genFeature(cwd, raw, template) {
|
|
86
|
+
const name = raw.toLowerCase();
|
|
87
|
+
if (!KEBAB.test(name))
|
|
88
|
+
return fail(`fsaa g: invalid feature name "${raw}" — lowercase words/digits, single dashes (e.g. invoices-overview)`);
|
|
89
|
+
const dir = join(cwd, "src", "features", name);
|
|
90
|
+
if (existsSync(dir))
|
|
91
|
+
return fail(`fsaa g: feature already exists: src/features/${name}`);
|
|
92
|
+
writeTree(dir, featureFiles(name, template));
|
|
93
|
+
console.log(` ✔ src/features/${name}/ (index.ts, ui/, config/, __tests__/)${verify()}`);
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function genAtom(cwd, raw) {
|
|
98
|
+
const Name = raw;
|
|
99
|
+
if (!PASCAL_NAME.test(Name))
|
|
100
|
+
return fail(`fsaa g: invalid atom name "${raw}" — PascalCase, no dashes (e.g. BadgeButton)`);
|
|
101
|
+
const path = join(cwd, "src", "shared", "ui", "atoms", `${Name}.tsx`);
|
|
102
|
+
if (existsSync(path))
|
|
103
|
+
return fail(`fsaa g: atom already exists: src/shared/ui/atoms/${Name}.tsx`);
|
|
104
|
+
write(path, atomFile(Name));
|
|
105
|
+
console.log(` ✔ src/shared/ui/atoms/${Name}.tsx (stateless primitive)${verify()}`);
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function genMolecule(cwd, raw) {
|
|
110
|
+
const Name = raw;
|
|
111
|
+
if (!PASCAL_NAME.test(Name))
|
|
112
|
+
return fail(`fsaa g: invalid molecule name "${raw}" — PascalCase, no dashes (e.g. ConfirmDelete)`);
|
|
113
|
+
const dir = join(cwd, "src", "shared", "ui", "molecules", Name);
|
|
114
|
+
if (existsSync(dir))
|
|
115
|
+
return fail(`fsaa g: molecule already exists: src/shared/ui/molecules/${Name}/`);
|
|
116
|
+
writeTree(dir, moleculeFiles(Name));
|
|
117
|
+
console.log(` ✔ src/shared/ui/molecules/${Name}/ (index.ts, ${Name}.tsx, __tests__/)`);
|
|
118
|
+
console.log(` ↪ graduation hint: shared earns its place on the SECOND confirmed duplication —
|
|
119
|
+
the first duplicate stays feature-local (see the graduation section of the docs).${verify()}`);
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function genPage(cwd, raw, route, template) {
|
|
124
|
+
const name = raw.toLowerCase();
|
|
125
|
+
if (!KEBAB.test(name))
|
|
126
|
+
return fail(`fsaa g: invalid page name "${raw}" — lowercase words/digits, single dashes (e.g. checkout-report)`);
|
|
127
|
+
const use = route || name;
|
|
128
|
+
const Title = pascal(use);
|
|
129
|
+
const pagePath =
|
|
130
|
+
template === "vite"
|
|
131
|
+
? join(cwd, "src", "pages", `${Title}Page.tsx`)
|
|
132
|
+
: join(cwd, "app", name, "page.tsx");
|
|
133
|
+
if (existsSync(pagePath))
|
|
134
|
+
return fail(`fsaa g: page already exists: ${pagePath}`);
|
|
135
|
+
const routePath = template === "vite" ? join(cwd, "src", "pages", "router.tsx") : null;
|
|
136
|
+
|
|
137
|
+
// the feature side must exist — pages compose features, never live alone
|
|
138
|
+
const featureIndex = join(cwd, "src", "features", use, "index.ts");
|
|
139
|
+
if (!existsSync(featureIndex))
|
|
140
|
+
return fail(
|
|
141
|
+
`fsaa g: no feature to compose — generate one first: fsaa g feature ${use} (pages compose features, per ui-location)`,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (template === "vite") {
|
|
145
|
+
write(pagePath, vitePage(use, pascal(use)));
|
|
146
|
+
let router = null;
|
|
147
|
+
try {
|
|
148
|
+
router = readFileSync(routePath, "utf8");
|
|
149
|
+
} catch {
|
|
150
|
+
router = null;
|
|
151
|
+
}
|
|
152
|
+
if (router && !router.includes(`./${Title}Page`)) {
|
|
153
|
+
if (router.includes(`<Route path="/${name}"`)) {
|
|
154
|
+
console.log(` (route /${name} already registered — router.tsx untouched)`);
|
|
155
|
+
} else {
|
|
156
|
+
const withRoute = addRoute(router, `<Route path="/${name}" element={<${Title}Page />} />`);
|
|
157
|
+
if (withRoute) {
|
|
158
|
+
writeFileSync(routePath, addImport(withRoute, `import { ${Title}Page } from './${Title}Page';`));
|
|
159
|
+
} else {
|
|
160
|
+
console.error(` (no </Routes> found in router.tsx — add the import+route by hand, see agents/add-module.md)`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
write(pagePath, nextPage(use, pascal(use)));
|
|
166
|
+
}
|
|
167
|
+
console.log(
|
|
168
|
+
` ✔ thin page: ${pagePath.replace(cwd, ".").replace(/^[./]+/, "./")} (composes @features/${use})${verify()}`,
|
|
169
|
+
);
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/* legacy pair: fsaa g <slice> — entity + feature + thin route */
|
|
174
|
+
function genPair(raw) {
|
|
175
|
+
const name = raw.toLowerCase();
|
|
176
|
+
// no leading digit, no trailing dash, no double dash — each segment is a
|
|
177
|
+
// valid identifier fragment so pascal()/camel() can never crash
|
|
178
|
+
if (!KEBAB.test(name)) {
|
|
179
|
+
console.error(`fsaa g: invalid slice name "${raw}" — lowercase words/digits joined by single dashes (e.g. invoices-overview)`);
|
|
180
|
+
return 1;
|
|
181
|
+
}
|
|
182
|
+
const cwd = resolve(process.cwd());
|
|
183
|
+
const detected = detectTemplate(cwd);
|
|
184
|
+
if (!detected) {
|
|
185
|
+
console.error('fsaa g: run this inside an FSAA app (scaffolded by "fsaa init")');
|
|
186
|
+
return 1;
|
|
187
|
+
}
|
|
188
|
+
const { template } = detected;
|
|
189
|
+
|
|
190
|
+
const pagePath = template === "vite"
|
|
191
|
+
? join(cwd, "src", "pages", `${pascal(name)}Page.tsx`)
|
|
192
|
+
: join(cwd, "app", name, "page.tsx");
|
|
193
|
+
if (existsSync(pagePath)) {
|
|
194
|
+
console.error(`fsaa g: page already exists: ${pagePath} — pick another slice name or remove the old page first`);
|
|
195
|
+
return 1;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const featureDir = join(cwd, "src", "features", name);
|
|
199
|
+
const entityDir = join(cwd, "src", "entities", name);
|
|
200
|
+
if (existsSync(featureDir) || existsSync(entityDir)) {
|
|
201
|
+
console.error(`fsaa g: slice already exists: src/features/${name} (and/or src/entities/${name})`);
|
|
202
|
+
return 1;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
writeTree(join(entityDir), entityFiles(name));
|
|
206
|
+
writeTree(join(featureDir), featureFiles(name, template));
|
|
207
|
+
|
|
208
|
+
if (template === "vite") {
|
|
209
|
+
const routerPath = join(cwd, "src", "pages", "router.tsx");
|
|
210
|
+
let router;
|
|
211
|
+
try {
|
|
212
|
+
router = readFileSync(routerPath, "utf8");
|
|
213
|
+
} catch {
|
|
214
|
+
router = null;
|
|
215
|
+
}
|
|
216
|
+
write(pagePath, vitePage(name, pascal(name)));
|
|
217
|
+
if (router && !router.includes(`./${pascal(name)}Page`)) {
|
|
218
|
+
if (router.includes(`<Route path="/${name}"`)) {
|
|
219
|
+
console.log(` (route /${name} already registered — router.tsx untouched)`);
|
|
220
|
+
} else {
|
|
221
|
+
const withRoute = addRoute(router, `<Route path="/${name}" element={<${pascal(name)}Page />} />`);
|
|
222
|
+
if (withRoute) {
|
|
223
|
+
writeFileSync(routerPath, addImport(withRoute, `import { ${pascal(name)}Page } from './${pascal(name)}Page';`));
|
|
224
|
+
} else {
|
|
225
|
+
console.error(` (no </Routes> found in router.tsx — add the import+route by hand, see agents/add-module.md)`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
write(join(cwd, "app", name, "page.tsx"), nextPage(name, pascal(name)));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
console.log(`
|
|
234
|
+
✔ src/entities/${name}/ (index.ts, types.ts, api.ts, slice.ts, __tests__/)
|
|
235
|
+
✔ src/features/${name}/ (index.ts, ui/, config/, __tests__/)
|
|
236
|
+
✔ thin route registered${template === "next" ? " (app/" + name + "/page.tsx)" : " (src/pages/" + pascal(name) + "Page.tsx + router.tsx)"}
|
|
237
|
+
${verify()}`);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function detectTemplate(cwd) {
|
|
242
|
+
const marker = join(cwd, ".fsaa.json");
|
|
243
|
+
if (existsSync(marker)) {
|
|
244
|
+
try {
|
|
245
|
+
const m = JSON.parse(readFileSync(marker, "utf8"));
|
|
246
|
+
if (m.template === "vite" || m.template === "next") return { template: m.template };
|
|
247
|
+
} catch {
|
|
248
|
+
/* broken marker — fall through to file detection */
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const cfg of ["next.config.mjs", "next.config.js", "next.config.ts"]) {
|
|
252
|
+
if (existsSync(join(cwd, cfg))) return { template: "next" };
|
|
253
|
+
}
|
|
254
|
+
if (existsSync(join(cwd, "vite.config.mjs")) || existsSync(join(cwd, "vite.config.ts"))) return { template: "vite" };
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function addImport(src, importLine) {
|
|
259
|
+
const lines = src.split("\n");
|
|
260
|
+
// insert after the last COMPLETE single-line import; multiline imports
|
|
261
|
+
// ("import {", no "from") would break if we spliced inside them
|
|
262
|
+
const lastImport = lines.reduce(
|
|
263
|
+
(acc, l, i) => (l.trim().startsWith("import ") && l.includes("from") ? i : acc),
|
|
264
|
+
-1,
|
|
265
|
+
);
|
|
266
|
+
if (lastImport === -1) return importLine + "\n" + src;
|
|
267
|
+
lines.splice(lastImport + 1, 0, importLine);
|
|
268
|
+
return lines.join("\n");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function addRoute(src, routeLine) {
|
|
272
|
+
const lines = src.split("\n");
|
|
273
|
+
const closeIdx = lines.findIndex((l) => l.includes("</Routes>"));
|
|
274
|
+
if (closeIdx === -1) return null;
|
|
275
|
+
// indent matches the sibling <Route> lines, not the closing tag
|
|
276
|
+
const sibling = lines.find((l) => l.trim().startsWith("<Route "));
|
|
277
|
+
const routeIndent = sibling ? (sibling.match(/^\s*/) || [" "])[0] : " ";
|
|
278
|
+
lines.splice(closeIdx, 0, routeIndent + routeLine.trim());
|
|
279
|
+
return lines.join("\n");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function writeTree(dir, files) {
|
|
283
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
284
|
+
const path = join(dir, rel);
|
|
285
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
286
|
+
writeFileSync(path, content);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function write(path, content) {
|
|
291
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
292
|
+
writeFileSync(path, content);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export { vitePage, nextPage };
|
|
296
|
+
|
|
297
|
+
function vitePage(name, Title) {
|
|
298
|
+
return `import { ${pascal(name)}Overview } from "@features/${name}";
|
|
299
|
+
|
|
300
|
+
/* Thin page composition (FSAA pages layer). */
|
|
301
|
+
|
|
302
|
+
export function ${Title}Page() {
|
|
303
|
+
return <${pascal(name)}Overview />;
|
|
304
|
+
}
|
|
305
|
+
`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function nextPage(name, Title) {
|
|
309
|
+
return `import { ${pascal(name)}Overview } from '@features/${name}';
|
|
310
|
+
|
|
311
|
+
/* app/${name}/page.tsx — thin FSAA route. */
|
|
312
|
+
|
|
313
|
+
export default function ${Title}Page() {
|
|
314
|
+
return <${pascal(name)}Overview />;
|
|
315
|
+
}
|
|
316
|
+
`;
|
|
162
317
|
}
|