create-fsaa 0.1.6 → 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/lib/init.mjs CHANGED
@@ -1,172 +1,172 @@
1
- /* fsaa/lib/init.mjs — fsaa init: scaffold an app from an official FSAA starter. */
2
- import { existsSync, readFileSync, writeFileSync, rmSync, statSync, readdirSync } from "node:fs";
3
- import { join, resolve } from "node:path";
4
- import { spawnSync } from "node:child_process";
5
- import { fetchTemplate, TEMPLATES, templateLabel } from "./download.mjs";
6
- import { prompt } from "./cli.mjs";
7
-
8
- export async function initCmd({ flags, interactive }) {
9
- console.log("> FSAA init — Feature-Sliced Atomic Architecture\n");
10
-
11
- let name = flags.name;
12
- let template = flags.template;
13
-
14
- if (!name && interactive) name = await prompt("App name [my-app]: ", "my-app");
15
- if (!template && interactive) {
16
- const answer = await prompt("Template — 1) next (Next.js App Router) 2) vite (plain React) [next]: ", "next");
17
- template = answer.startsWith("2") ? "vite" : "next";
18
- }
19
- if (!interactive && (!name || !template)) {
20
- console.log(" (no TTY — defaults applied: name=my-app, template=next; pin them with --name/--template)");
21
- }
22
- if (name === true || template === true) {
23
- const e = new Error("--name and --template need values: fsaa init --name my-app --template vite");
24
- e.userError = true;
25
- throw e;
26
- }
27
- name = name || "my-app";
28
- template = template || "next";
29
- const manager = "pnpm"; // contract pin (single-lockfile rule lives on pnpm)
30
-
31
- if (typeof flags.dir !== "undefined" && typeof flags.dir !== "string") {
32
- throw new Error("--dir needs a value: fsaa init --dir ./somewhere");
33
- }
34
- validate(name, template);
35
-
36
- const dest = resolve(flags.dir || ".", name);
37
- if (existsSync(dest)) {
38
- if (!flags.force) {
39
- console.error(`fsaa: directory already exists: ${dest} (add --force to use an EMPTY directory; non-existing is preferred)`);
40
- return 1;
41
- }
42
- if (!statSync(dest).isDirectory()) {
43
- console.error(`fsaa: --force target is not a directory: ${dest}`);
44
- return 1;
45
- }
46
- const left = readdirSync(dest).filter((e) => e !== ".git" && e !== ".gitignore");
47
- if (left.length > 0) {
48
- console.error(`fsaa: refusing --force: directory is not empty: ${dest} (${left.length} items) — move away or delete them yourself`);
49
- return 1;
50
- }
51
- rmSync(dest, { recursive: true });
52
- }
53
-
54
- console.log(` template: ${templateLabel(template)}`);
55
- console.log(` manager: pnpm (contract)`);
56
- console.log(` target: ${dest}\n`);
57
-
58
- console.log("> downloading template (codeload tarball)…");
59
- await fetchTemplate(template, dest);
60
- stamp(dest, { template });
61
-
62
- console.log("> renaming project…");
63
- const pkgPath = join(dest, "package.json");
64
- const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
65
- pkg.name = name;
66
- writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
67
-
68
- // Windows: pnpm ships as pnpm.cmd, and Node ≥18.20 REFUSES to spawn .cmd/.bat
69
- // without the shell option (EINVAL). So: .cmd name + shell on win32.
70
- const win = process.platform === "win32";
71
- const pnpmBin = win ? "pnpm.cmd" : "pnpm";
72
- console.log("> installing dependencies (this may take a minute)…");
73
- let r = spawnSync(pnpmBin, ["install"], { cwd: dest, stdio: "inherit", shell: win });
74
- if (r.error) {
75
- // pnpm missing from PATH — recover INSTEAD of nagging: run the pinned
76
- // version through npx (npx exists wherever this CLI already runs).
77
- const pinned = (pkg.packageManager || "pnpm@10.34.9").split("@")[1] || "10.34.9";
78
- const npxBin = win ? "npx.cmd" : "npx";
79
- console.log(` pnpm not on PATH — running the pinned pnpm@${pinned} through npx…`);
80
- r = spawnSync(npxBin, ["-y", `pnpm@${pinned}`, "install"], { cwd: dest, stdio: "inherit", shell: win });
81
- if (r.error) {
82
- console.error(`\nfsaa: could not install dependencies automatically (npx unavailable).\n recover by hand: cd ${dest} && npm exec -y pnpm@${pinned} install`);
83
- const e = new Error("install failed");
84
- e.userError = true;
85
- throw e;
86
- }
87
- }
88
- if (r.status !== 0) {
89
- console.error(`fsaa: dependency install failed — the template files are in place.
90
- recover by hand: cd ${dest} && pnpm install
91
- (re-running fsaa init will refuse the non-empty dir; the files are all there)`);
92
- return 1;
93
- }
94
- // single-lockfile contract: the template ships pnpm-lock.yaml — an npm install
95
- // adds package-lock.json next to it, which makes drift-check red. Keep exactly one.
96
- const otherLock = join(dest, "package-lock.json");
97
- if (existsSync(otherLock)) rmSync(otherLock);
98
-
99
- console.log("\n> running the contract drift-check as proof…");
100
- const dc = spawnSync("node", ["scripts/check-agents-md.mjs"], { cwd: dest, stdio: "inherit" });
101
- if (dc.status !== 0) {
102
- console.error("fsaa: drift-check is red after init — report this: https://github.com/Aramex86/fsaa/issues");
103
- return 1;
104
- }
105
-
106
- gitInit(dest, "feat/init", nameSafe(dest));
107
-
108
- console.log(`\n${"=".repeat(52)}`);
109
- console.log(` ✔ ${name} is ready (${templateLabel(template)})`);
110
- console.log(` cd ${dest}`);
111
- console.log(` ${manager} run dev`);
112
- console.log(` agent contract: AGENTS.md · skills: agents/`);
113
- console.log(` generate slices: ${manager === "pnpm" ? "pnpm exec" : "npx"} fsaa g <slice-name> (or: npx create-fsaa g <slice-name>)`);
114
- console.log("=".repeat(52));
115
- return 0;
116
- }
117
-
118
- function validate(name, template) {
119
- if (!TEMPLATES[template]) {
120
- throw new Error(`unknown template "${template}" — choose: ${Object.keys(TEMPLATES).join(" | ")}`);
121
- }
122
- // npm/pnpm require lowercase package names; a strict lowercase regex also
123
- // keeps the project folder name predictable (no /i flag deliberately).
124
- if (!/^[a-z0-9][a-z0-9._-]*$/.test(name)) {
125
- throw new Error(`invalid app name "${name}" — lowercase letters, digits, dots, dashes, underscores only`);
126
- }
127
- // Windows: trailing dots/space vanish in paths; reserved device names break mkdir.
128
- if (/[.\s-]$/.test(name) || /^(con|prn|aux|nul|com\d|lpt\d)$/i.test(name)) {
129
- throw new Error(`invalid app name "${name}" — no trailing dot/space/dash, and no Windows reserved names (con, prn, aux, nul, com1..9, lpt1..9)`);
130
- }
131
- }
132
-
133
- function stamp(dest, { template }) {
134
- writeFileSync(join(dest, ".fsaa.json"), JSON.stringify({ fsaa: "0.1.0", template, generatedAt: new Date().toISOString() }, null, 2) + "\n");
135
- }
136
-
137
- function gitInit(dest, branch, appName) {
138
- const state = (args) => spawnSync("git", args, { cwd: dest, stdio: "pipe" });
139
- if (existsSync(dest + "/.git")) return;
140
- // SECURITY-CRITICAL: run add/commit ONLY inside a repo that we actually
141
- // created. git init -b wants git >= 2.28; on older git fall back to plain
142
- // init + symbolic-ref; if git is absent or init fails — skip git entirely
143
- // (committing into a PARENT repo would stage unrelated files).
144
- if (state(["rev-parse", "--is-inside-work-tree"]).status === 0) {
145
- console.error(" (inside an enclosing git repo — skipped git init; create the project repo yourself)");
146
- return;
147
- }
148
- let created = state(["init", "-q", "-b", branch]);
149
- if (created.error || created.status !== 0) {
150
- created = state(["init", "-q"]);
151
- if (created.error || created.status !== 0) {
152
- console.error(created.error ? " (git not found — files are ready; run git init + commit yourself)"
153
- : " (git init failed — files are ready; run git init + commit yourself)");
154
- return;
155
- }
156
- state(["symbolic-ref", "HEAD", "refs/heads/" + branch]);
157
- }
158
- const gd = (state(["rev-parse", "--git-dir"]).stdout || "").toString().trim();
159
- if (state(["rev-parse", "--is-inside-work-tree"]).status !== 0 || gd.includes("../")) {
160
- console.error(" (git repo resolves outside the project — skipped commit)");
161
- return;
162
- }
163
- state(["add", "-A"]);
164
- const c = state(["commit", "-q", "-m", `feat/init — scaffold ${appName} from FSAA starter`]);
165
- if (c.status !== 0) {
166
- console.error(" (git repo initialized but the commit needs identity — set git config user.name/user.email and commit)");
167
- }
168
- }
169
-
170
- function nameSafe(dest) {
171
- return dest.split(/[/\\]/).filter(Boolean).pop() || "app";
1
+ /* fsaa/lib/init.mjs — fsaa init: scaffold an app from an official FSAA starter. */
2
+ import { existsSync, readFileSync, writeFileSync, rmSync, statSync, readdirSync } from "node:fs";
3
+ import { join, resolve } from "node:path";
4
+ import { spawnSync } from "node:child_process";
5
+ import { fetchTemplate, TEMPLATES, templateLabel } from "./download.mjs";
6
+ import { prompt } from "./cli.mjs";
7
+
8
+ export async function initCmd({ flags, interactive }) {
9
+ console.log("> FSAA init — Feature-Sliced Atomic Architecture\n");
10
+
11
+ let name = flags.name;
12
+ let template = flags.template;
13
+
14
+ if (!name && interactive) name = await prompt("App name [my-app]: ", "my-app");
15
+ if (!template && interactive) {
16
+ const answer = await prompt("Template — 1) next (Next.js App Router) 2) vite (plain React) [next]: ", "next");
17
+ template = answer.startsWith("2") ? "vite" : "next";
18
+ }
19
+ if (!interactive && (!name || !template)) {
20
+ console.log(" (no TTY — defaults applied: name=my-app, template=next; pin them with --name/--template)");
21
+ }
22
+ if (name === true || template === true) {
23
+ const e = new Error("--name and --template need values: fsaa init --name my-app --template vite");
24
+ e.userError = true;
25
+ throw e;
26
+ }
27
+ name = name || "my-app";
28
+ template = template || "next";
29
+ const manager = "pnpm"; // contract pin (single-lockfile rule lives on pnpm)
30
+
31
+ if (typeof flags.dir !== "undefined" && typeof flags.dir !== "string") {
32
+ throw new Error("--dir needs a value: fsaa init --dir ./somewhere");
33
+ }
34
+ validate(name, template);
35
+
36
+ const dest = resolve(flags.dir || ".", name);
37
+ if (existsSync(dest)) {
38
+ if (!flags.force) {
39
+ console.error(`fsaa: directory already exists: ${dest} (add --force to use an EMPTY directory; non-existing is preferred)`);
40
+ return 1;
41
+ }
42
+ if (!statSync(dest).isDirectory()) {
43
+ console.error(`fsaa: --force target is not a directory: ${dest}`);
44
+ return 1;
45
+ }
46
+ const left = readdirSync(dest).filter((e) => e !== ".git" && e !== ".gitignore");
47
+ if (left.length > 0) {
48
+ console.error(`fsaa: refusing --force: directory is not empty: ${dest} (${left.length} items) — move away or delete them yourself`);
49
+ return 1;
50
+ }
51
+ rmSync(dest, { recursive: true });
52
+ }
53
+
54
+ console.log(` template: ${templateLabel(template)}`);
55
+ console.log(` manager: pnpm (contract)`);
56
+ console.log(` target: ${dest}\n`);
57
+
58
+ console.log("> downloading template (codeload tarball)…");
59
+ await fetchTemplate(template, dest);
60
+ stamp(dest, { template });
61
+
62
+ console.log("> renaming project…");
63
+ const pkgPath = join(dest, "package.json");
64
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
65
+ pkg.name = name;
66
+ writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
67
+
68
+ // Windows: pnpm ships as pnpm.cmd, and Node ≥18.20 REFUSES to spawn .cmd/.bat
69
+ // without the shell option (EINVAL). So: .cmd name + shell on win32.
70
+ const win = process.platform === "win32";
71
+ const pnpmBin = win ? "pnpm.cmd" : "pnpm";
72
+ console.log("> installing dependencies (this may take a minute)…");
73
+ let r = spawnSync(pnpmBin, ["install"], { cwd: dest, stdio: "inherit", shell: win });
74
+ if (r.error) {
75
+ // pnpm missing from PATH — recover INSTEAD of nagging: run the pinned
76
+ // version through npx (npx exists wherever this CLI already runs).
77
+ const pinned = (pkg.packageManager || "pnpm@10.34.9").split("@")[1] || "10.34.9";
78
+ const npxBin = win ? "npx.cmd" : "npx";
79
+ console.log(` pnpm not on PATH — running the pinned pnpm@${pinned} through npx…`);
80
+ r = spawnSync(npxBin, ["-y", `pnpm@${pinned}`, "install"], { cwd: dest, stdio: "inherit", shell: win });
81
+ if (r.error) {
82
+ console.error(`\nfsaa: could not install dependencies automatically (npx unavailable).\n recover by hand: cd ${dest} && npm exec -y pnpm@${pinned} install`);
83
+ const e = new Error("install failed");
84
+ e.userError = true;
85
+ throw e;
86
+ }
87
+ }
88
+ if (r.status !== 0) {
89
+ console.error(`fsaa: dependency install failed — the template files are in place.
90
+ recover by hand: cd ${dest} && pnpm install
91
+ (re-running fsaa init will refuse the non-empty dir; the files are all there)`);
92
+ return 1;
93
+ }
94
+ // single-lockfile contract: the template ships pnpm-lock.yaml — an npm install
95
+ // adds package-lock.json next to it, which makes drift-check red. Keep exactly one.
96
+ const otherLock = join(dest, "package-lock.json");
97
+ if (existsSync(otherLock)) rmSync(otherLock);
98
+
99
+ console.log("\n> running the contract drift-check as proof…");
100
+ const dc = spawnSync("node", ["scripts/check-agents-md.mjs"], { cwd: dest, stdio: "inherit" });
101
+ if (dc.status !== 0) {
102
+ console.error("fsaa: drift-check is red after init — report this: https://github.com/Aramex86/fsaa/issues");
103
+ return 1;
104
+ }
105
+
106
+ gitInit(dest, "feat/init", nameSafe(dest));
107
+
108
+ console.log(`\n${"=".repeat(52)}`);
109
+ console.log(` ✔ ${name} is ready (${templateLabel(template)})`);
110
+ console.log(` cd ${dest}`);
111
+ console.log(` ${manager} run dev`);
112
+ console.log(` agent contract: AGENTS.md · skills: agents/`);
113
+ console.log(` generate slices: ${manager === "pnpm" ? "pnpm exec" : "npx"} fsaa g <slice-name> (or: npx create-fsaa g <slice-name>)`);
114
+ console.log("=".repeat(52));
115
+ return 0;
116
+ }
117
+
118
+ function validate(name, template) {
119
+ if (!TEMPLATES[template]) {
120
+ throw new Error(`unknown template "${template}" — choose: ${Object.keys(TEMPLATES).join(" | ")}`);
121
+ }
122
+ // npm/pnpm require lowercase package names; a strict lowercase regex also
123
+ // keeps the project folder name predictable (no /i flag deliberately).
124
+ if (!/^[a-z0-9][a-z0-9._-]*$/.test(name)) {
125
+ throw new Error(`invalid app name "${name}" — lowercase letters, digits, dots, dashes, underscores only`);
126
+ }
127
+ // Windows: trailing dots/space vanish in paths; reserved device names break mkdir.
128
+ if (/[.\s-]$/.test(name) || /^(con|prn|aux|nul|com\d|lpt\d)$/i.test(name)) {
129
+ throw new Error(`invalid app name "${name}" — no trailing dot/space/dash, and no Windows reserved names (con, prn, aux, nul, com1..9, lpt1..9)`);
130
+ }
131
+ }
132
+
133
+ function stamp(dest, { template }) {
134
+ writeFileSync(join(dest, ".fsaa.json"), JSON.stringify({ fsaa: "0.1.0", template, generatedAt: new Date().toISOString() }, null, 2) + "\n");
135
+ }
136
+
137
+ function gitInit(dest, branch, appName) {
138
+ const state = (args) => spawnSync("git", args, { cwd: dest, stdio: "pipe" });
139
+ if (existsSync(dest + "/.git")) return;
140
+ // SECURITY-CRITICAL: run add/commit ONLY inside a repo that we actually
141
+ // created. git init -b wants git >= 2.28; on older git fall back to plain
142
+ // init + symbolic-ref; if git is absent or init fails — skip git entirely
143
+ // (committing into a PARENT repo would stage unrelated files).
144
+ if (state(["rev-parse", "--is-inside-work-tree"]).status === 0) {
145
+ console.error(" (inside an enclosing git repo — skipped git init; create the project repo yourself)");
146
+ return;
147
+ }
148
+ let created = state(["init", "-q", "-b", branch]);
149
+ if (created.error || created.status !== 0) {
150
+ created = state(["init", "-q"]);
151
+ if (created.error || created.status !== 0) {
152
+ console.error(created.error ? " (git not found — files are ready; run git init + commit yourself)"
153
+ : " (git init failed — files are ready; run git init + commit yourself)");
154
+ return;
155
+ }
156
+ state(["symbolic-ref", "HEAD", "refs/heads/" + branch]);
157
+ }
158
+ const gd = (state(["rev-parse", "--git-dir"]).stdout || "").toString().trim();
159
+ if (state(["rev-parse", "--is-inside-work-tree"]).status !== 0 || gd.includes("../")) {
160
+ console.error(" (git repo resolves outside the project — skipped commit)");
161
+ return;
162
+ }
163
+ state(["add", "-A"]);
164
+ const c = state(["commit", "-q", "-m", `feat/init — scaffold ${appName} from FSAA starter`]);
165
+ if (c.status !== 0) {
166
+ console.error(" (git repo initialized but the commit needs identity — set git config user.name/user.email and commit)");
167
+ }
168
+ }
169
+
170
+ function nameSafe(dest) {
171
+ return dest.split(/[/\\]/).filter(Boolean).pop() || "app";
172
172
  }
package/lib/net.mjs CHANGED
@@ -1,13 +1,13 @@
1
- /* fsaa/lib/net.mjs — fetch binary over HTTPS (zero deps, no shell). */
2
- export async function fetchFile(url) {
3
- const resp = await fetch(url, {
4
- headers: { "User-Agent": "fsaa-cli/0.1 (+https://github.com/Aramex86/fsaa)" },
5
- redirect: "follow",
6
- signal: AbortSignal.timeout(120_000), // no unbounded hangs on a stalled connection
7
- });
8
- if (!resp.ok) {
9
- throw new Error(`download failed: HTTP ${resp.status} for ${url}`);
10
- }
11
- const buf = await resp.arrayBuffer();
12
- return Buffer.from(buf);
1
+ /* fsaa/lib/net.mjs — fetch binary over HTTPS (zero deps, no shell). */
2
+ export async function fetchFile(url) {
3
+ const resp = await fetch(url, {
4
+ headers: { "User-Agent": "fsaa-cli/0.1 (+https://github.com/Aramex86/fsaa)" },
5
+ redirect: "follow",
6
+ signal: AbortSignal.timeout(120_000), // no unbounded hangs on a stalled connection
7
+ });
8
+ if (!resp.ok) {
9
+ throw new Error(`download failed: HTTP ${resp.status} for ${url}`);
10
+ }
11
+ const buf = await resp.arrayBuffer();
12
+ return Buffer.from(buf);
13
13
  }