@anaemia/cli 0.1.8 → 0.3.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/dist/commands/build.js +23 -0
- package/dist/commands/create.js +203 -0
- package/dist/commands/dev.js +102 -0
- package/dist/commands/routes.js +30 -0
- package/dist/commands/start.js +15 -0
- package/dist/commands/typecheck.js +10 -0
- package/dist/index.js +12 -354
- package/dist/utils/config.js +18 -0
- package/dist/utils/logger.js +22 -0
- package/package.json +4 -5
- package/src/commands/build.ts +28 -0
- package/src/commands/create.ts +229 -0
- package/src/commands/dev.ts +125 -0
- package/src/commands/routes.ts +34 -0
- package/src/commands/start.ts +17 -0
- package/src/commands/typecheck.ts +10 -0
- package/src/index.ts +12 -412
- package/src/utils/config.ts +24 -0
- /package/src/{logger.ts → utils/logger.ts} +0 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type { CAC } from "cac";
|
|
2
|
+
import logger from "../utils/logger.js";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { generateSharedComponent, scaffoldFeature, scaffoldHook, scaffoldPage } from "../scaffold.js";
|
|
7
|
+
import { execSync } from "node:child_process";
|
|
8
|
+
import prompts from "prompts";
|
|
9
|
+
import { transform } from "sucrase";
|
|
10
|
+
|
|
11
|
+
export function register(cli: CAC) {
|
|
12
|
+
cli
|
|
13
|
+
.command("create [target]", "initialize an application or generate domain features (e.g., feature:name)")
|
|
14
|
+
.alias("init")
|
|
15
|
+
.action(async (target) => {
|
|
16
|
+
const appRoot = process.cwd();
|
|
17
|
+
|
|
18
|
+
if (target && target.includes(":")) {
|
|
19
|
+
const [type, name] = target.split(":");
|
|
20
|
+
|
|
21
|
+
if (!name) {
|
|
22
|
+
logger.error(`missing name modifier. Use layout template like: ${pc.cyan(`create ${type}:your-name`)}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!fs.existsSync(path.join(appRoot, "package.json"))) {
|
|
27
|
+
logger.error("no package.json detected. code generation commands must run inside an Anaemia project root.");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const normalizedName = name.trim();
|
|
32
|
+
|
|
33
|
+
if (type === "feature") {
|
|
34
|
+
scaffoldFeature(normalizedName, appRoot);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (type === "component") {
|
|
39
|
+
generateSharedComponent(appRoot, normalizedName, { logger, pc });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (type === "page") {
|
|
44
|
+
scaffoldPage(normalizedName, appRoot);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (type === "hook") {
|
|
49
|
+
scaffoldHook(normalizedName, appRoot);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
logger.error(`unknown layout generator type "${type}". Supported variants: "feature:", "component:", "page:", "hook:"`);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
logger.compiler("launching Anaemia project initialization wizard...");
|
|
58
|
+
|
|
59
|
+
const response = await prompts([
|
|
60
|
+
{
|
|
61
|
+
type: target ? null : "text",
|
|
62
|
+
name: "projectName",
|
|
63
|
+
message: "Project name:",
|
|
64
|
+
initial: "anaemia-app",
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
type: "select",
|
|
68
|
+
name: "variant",
|
|
69
|
+
message: "Select a variant:",
|
|
70
|
+
choices: [
|
|
71
|
+
{ title: pc.blue("TypeScript (Recommended)"), value: "ts" },
|
|
72
|
+
{ title: pc.yellow("JavaScript"), value: "js" },
|
|
73
|
+
],
|
|
74
|
+
initial: 0,
|
|
75
|
+
},
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
if (!response.variant && response.variant !== 0) {
|
|
79
|
+
logger.warn("project creation aborted.");
|
|
80
|
+
process.exit(0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const targetDir = target || response.projectName;
|
|
84
|
+
const targetPath = path.resolve(appRoot, targetDir);
|
|
85
|
+
|
|
86
|
+
if (fs.existsSync(targetPath)) {
|
|
87
|
+
const files = fs.readdirSync(targetPath);
|
|
88
|
+
if (files.length > 0) {
|
|
89
|
+
const { overwrite } = await prompts({
|
|
90
|
+
type: "confirm",
|
|
91
|
+
name: "overwrite",
|
|
92
|
+
message: `target directory "${targetDir}" is not empty. remove existing files and continue?`,
|
|
93
|
+
initial: false,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (!overwrite) {
|
|
97
|
+
logger.error("aborted to protect existing project directory.");
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
logger.warn(`purging existing files inside ${targetDir}...`);
|
|
102
|
+
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
103
|
+
fs.mkdirSync(targetPath, { recursive: true });
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
fs.mkdirSync(targetPath, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let templatePath = path.resolve(__dirname, "../templates/template-base");
|
|
110
|
+
if (!fs.existsSync(templatePath)) {
|
|
111
|
+
templatePath = path.resolve(__dirname, "../templates/base-app");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (fs.existsSync(templatePath)) {
|
|
115
|
+
logger.info("unpacking localized scaffolding architecture layout structures...");
|
|
116
|
+
fs.cpSync(templatePath, targetPath, {
|
|
117
|
+
recursive: true,
|
|
118
|
+
filter: (src) => !["node_modules", "dist", ".anaemia", ".rspack"].includes(path.basename(src)),
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
logger.warn("local templates missing. fetching remote registry packages over the network...");
|
|
122
|
+
const userAgent = process.env.npm_config_user_agent || "";
|
|
123
|
+
let packageManager = "npm";
|
|
124
|
+
if (userAgent.includes("pnpm")) packageManager = "pnpm";
|
|
125
|
+
else if (userAgent.includes("yarn")) packageManager = "yarn";
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
if (packageManager === "pnpm") {
|
|
129
|
+
execSync(`pnpm dlx degit colourlabs/anaemia/templates/base-app "${targetPath}"`, { stdio: "ignore" });
|
|
130
|
+
} else {
|
|
131
|
+
execSync(`npx degit colourlabs/anaemia/templates/base-app "${targetPath}"`, { stdio: "ignore" });
|
|
132
|
+
}
|
|
133
|
+
} catch (err) {
|
|
134
|
+
logger.error("could not source template workspace assets locally or from network registry nodes. " + err);
|
|
135
|
+
process.exit(1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const removeGitKeepFiles = (dir: string) => {
|
|
140
|
+
const files = fs.readdirSync(dir);
|
|
141
|
+
for (const file of files) {
|
|
142
|
+
const fullPath = path.join(dir, file);
|
|
143
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
144
|
+
removeGitKeepFiles(fullPath);
|
|
145
|
+
} else if (file === ".gitkeep") {
|
|
146
|
+
fs.unlinkSync(fullPath);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
removeGitKeepFiles(targetPath);
|
|
151
|
+
|
|
152
|
+
if (response.variant === "js") {
|
|
153
|
+
logger.info("converting workspace assets to vanilla JavaScript...");
|
|
154
|
+
|
|
155
|
+
const convertTypeScriptToJs = (dir: string) => {
|
|
156
|
+
const files = fs.readdirSync(dir);
|
|
157
|
+
|
|
158
|
+
for (const file of files) {
|
|
159
|
+
const fullPath = path.join(dir, file);
|
|
160
|
+
const stat = fs.statSync(fullPath);
|
|
161
|
+
|
|
162
|
+
if (stat.isDirectory()) {
|
|
163
|
+
convertTypeScriptToJs(fullPath);
|
|
164
|
+
} else if (file.endsWith(".ts") || file.endsWith(".tsx")) {
|
|
165
|
+
const isTsx = file.endsWith(".tsx");
|
|
166
|
+
const code = fs.readFileSync(fullPath, "utf8");
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
const compiled = transform(code, {
|
|
170
|
+
transforms: isTsx ? ["typescript", "jsx"] : ["typescript"],
|
|
171
|
+
jsxRuntime: "preserve",
|
|
172
|
+
production: true,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const newExt = isTsx ? ".jsx" : ".js";
|
|
176
|
+
const newPath = fullPath.replace(/\.tsx?$/, newExt);
|
|
177
|
+
|
|
178
|
+
fs.writeFileSync(newPath, compiled.code, "utf8");
|
|
179
|
+
fs.unlinkSync(fullPath);
|
|
180
|
+
} catch {
|
|
181
|
+
logger.warn(`failed to strip types from ${file}, skipping...`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
convertTypeScriptToJs(targetPath);
|
|
188
|
+
|
|
189
|
+
const tsconfigPath = path.join(targetPath, "tsconfig.json");
|
|
190
|
+
if (fs.existsSync(tsconfigPath)) {
|
|
191
|
+
fs.unlinkSync(tsconfigPath);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const pkgJsonPath = path.join(targetPath, "package.json");
|
|
196
|
+
if (fs.existsSync(pkgJsonPath)) {
|
|
197
|
+
try {
|
|
198
|
+
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
199
|
+
pkg.name = path.basename(targetPath);
|
|
200
|
+
|
|
201
|
+
if (response.variant === "js") {
|
|
202
|
+
if (pkg.devDependencies) {
|
|
203
|
+
delete pkg.devDependencies["typescript"];
|
|
204
|
+
delete pkg.devDependencies["@types/node"];
|
|
205
|
+
delete pkg.devDependencies["@typescript-eslint/eslint-plugin"];
|
|
206
|
+
delete pkg.devDependencies["@typescript-eslint/parser"];
|
|
207
|
+
}
|
|
208
|
+
if (pkg.scripts && pkg.scripts.typecheck) {
|
|
209
|
+
delete pkg.scripts.typecheck;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkg, null, 2), "utf8");
|
|
214
|
+
} catch (err) {
|
|
215
|
+
logger.error("failed rewriting package.json manifest structures:", err);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
logger.success(`\n🎉 project successfully scaffolded into ${pc.magenta(targetDir)}!`);
|
|
220
|
+
console.log(pc.dim("\nfollow these steps to begin execution:\n"));
|
|
221
|
+
|
|
222
|
+
if (targetDir !== ".") {
|
|
223
|
+
console.log(` cd ${pc.cyan(targetDir)}`);
|
|
224
|
+
}
|
|
225
|
+
console.log(` ${pc.cyan("pnpm install")} ${pc.dim("# or npm i / yarn install")}`);
|
|
226
|
+
console.log(` ${pc.cyan("pnpm dev")} ${pc.dim("# launches hot reload server")}\n`);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { CAC } from "cac";
|
|
2
|
+
import { rspack } from "@rspack/core";
|
|
3
|
+
import { RspackDevServer } from "@rspack/dev-server";
|
|
4
|
+
import { getRspackConfig } from "@anaemia/bundler";
|
|
5
|
+
import spawn from "cross-spawn";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import http from "node:http";
|
|
8
|
+
import { WebSocketServer } from "ws";
|
|
9
|
+
import { WebSocket as NodeWS } from "ws";
|
|
10
|
+
import { loadUserConfig } from "../utils/config.js";
|
|
11
|
+
import logger from "../utils/logger.js";
|
|
12
|
+
import { ChildProcess } from "node:child_process";
|
|
13
|
+
|
|
14
|
+
export function register(cli: CAC) {
|
|
15
|
+
cli.command("dev", "launch local development environment").action(async () => {
|
|
16
|
+
process.env.NODE_ENV = "development";
|
|
17
|
+
const appRoot = process.cwd();
|
|
18
|
+
|
|
19
|
+
const userConfig = await loadUserConfig(appRoot);
|
|
20
|
+
const targetPort = userConfig.port || 3000;
|
|
21
|
+
|
|
22
|
+
const [clientConfig, serverConfig] = await getRspackConfig(appRoot, userConfig);
|
|
23
|
+
|
|
24
|
+
const clientCompiler = rspack(clientConfig);
|
|
25
|
+
const devServer = new RspackDevServer(clientConfig.devServer || {}, clientCompiler);
|
|
26
|
+
|
|
27
|
+
const bridgeServer = http.createServer();
|
|
28
|
+
const wss = new WebSocketServer({ server: bridgeServer });
|
|
29
|
+
|
|
30
|
+
wss.on("connection", (clientWs) => {
|
|
31
|
+
const rspackSocket = new NodeWS(`ws://localhost:${targetPort + 1}/ws`);
|
|
32
|
+
|
|
33
|
+
rspackSocket.on("message", (data) => {
|
|
34
|
+
const flattened = Array.isArray(data) ? Buffer.concat(data) : data;
|
|
35
|
+
if (Buffer.isBuffer(flattened)) clientWs.send(flattened.toString("utf-8"));
|
|
36
|
+
else clientWs.send(String(flattened));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
rspackSocket.on("error", (err) => {
|
|
40
|
+
console.warn("[anaemia hmr] rspack socket error:", err.message);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
clientWs.on("message", (data) => {
|
|
44
|
+
if (rspackSocket.readyState === NodeWS.OPEN) rspackSocket.send(data);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
clientWs.on("close", () => {
|
|
48
|
+
if (rspackSocket.readyState === NodeWS.OPEN) rspackSocket.close();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
let serverProcess: ChildProcess | null = null;
|
|
53
|
+
|
|
54
|
+
const startServer = () => {
|
|
55
|
+
if (serverProcess) {
|
|
56
|
+
serverProcess.kill("SIGTERM");
|
|
57
|
+
serverProcess = null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setTimeout(() => {
|
|
61
|
+
serverProcess = spawn(
|
|
62
|
+
"node",
|
|
63
|
+
["--enable-source-maps", path.resolve(appRoot, "./dist/server/index.js")],
|
|
64
|
+
{
|
|
65
|
+
stdio: "inherit",
|
|
66
|
+
env: {
|
|
67
|
+
...process.env,
|
|
68
|
+
NODE_ENV: "development",
|
|
69
|
+
PORT: String(targetPort),
|
|
70
|
+
RSPACK_DEV_PORT: String(targetPort + 1),
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}, 200);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const cleanup = async () => {
|
|
78
|
+
if (serverProcess) {
|
|
79
|
+
serverProcess.kill("SIGTERM");
|
|
80
|
+
serverProcess = null;
|
|
81
|
+
}
|
|
82
|
+
bridgeServer.close();
|
|
83
|
+
serverCompiler?.close(() => {});
|
|
84
|
+
if (devServer) {
|
|
85
|
+
await devServer.stop();
|
|
86
|
+
}
|
|
87
|
+
process.exit(0);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
process.on("SIGINT", cleanup);
|
|
91
|
+
process.on("SIGTERM", cleanup);
|
|
92
|
+
|
|
93
|
+
logger.compiler("warming up and analyzing assets...");
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
await devServer.start();
|
|
97
|
+
logger.success(`hot reload asset infrastructure running on port ${targetPort + 1}`);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
logger.error("failed to start client asset dev server:", err);
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bridgeServer.listen(targetPort + 2, () => {
|
|
104
|
+
logger.info(`HMR bridge running on port ${targetPort + 2}`);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const serverCompiler = rspack(serverConfig);
|
|
108
|
+
|
|
109
|
+
serverCompiler.watch({}, (err, stats) => {
|
|
110
|
+
if (err) {
|
|
111
|
+
logger.error("server compilation critical failure:", err);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (stats?.hasErrors()) {
|
|
116
|
+
console.error(stats.toString({ colors: true, all: false, errors: true, warnings: true }));
|
|
117
|
+
logger.error("server compilation encountered build script errors.");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
logger.info(`server bundles updated. booting runtime on http://localhost:${targetPort}`);
|
|
122
|
+
startServer();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CAC } from "cac";
|
|
2
|
+
import logger from "../utils/logger.js";
|
|
3
|
+
import { scanRoutes } from "@anaemia/bundler";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
|
|
6
|
+
export function register(cli: CAC) {
|
|
7
|
+
cli.command("routes", "print the scanned route manifest").action(async () => {
|
|
8
|
+
const appRoot = process.cwd();
|
|
9
|
+
const routes = await scanRoutes(appRoot);
|
|
10
|
+
|
|
11
|
+
logger.info("scanned route architecture:\n");
|
|
12
|
+
|
|
13
|
+
routes.forEach((r, i) => {
|
|
14
|
+
const isLast = i === routes.length - 1;
|
|
15
|
+
const branch = isLast ? "└─" : "├─";
|
|
16
|
+
const indent = isLast ? " " : "│ ";
|
|
17
|
+
|
|
18
|
+
const typeLabel = r.type === "catch-all" ? pc.red(`[${r.type}]`) : r.type === "layout" ? pc.yellow(`[${r.type}]`) : pc.green(`[${r.type}]`);
|
|
19
|
+
console.log(`${branch} ${pc.cyan(r.urlPattern)} ${typeLabel}`);
|
|
20
|
+
|
|
21
|
+
const lines: string[] = [];
|
|
22
|
+
if (r.params.length) lines.push(`params: ${pc.magenta(r.params.join(", "))}`);
|
|
23
|
+
if (r.chunkName) lines.push(`chunk: ${pc.dim(r.chunkName)}`);
|
|
24
|
+
if (r.layouts.length) lines.push(`layouts: ${pc.dim(String(r.layouts.length))}`);
|
|
25
|
+
|
|
26
|
+
lines.forEach((line, li) => {
|
|
27
|
+
const isLastLine = li === lines.length - 1;
|
|
28
|
+
console.log(`${indent}${isLastLine ? "└─" : "├─"} ${line}`);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!isLast) console.log("│");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CAC } from "cac";
|
|
2
|
+
import { loadUserConfig } from "../utils/config.js";
|
|
3
|
+
import spawn from "cross-spawn";
|
|
4
|
+
import path from "path";
|
|
5
|
+
|
|
6
|
+
export function register(cli: CAC) {
|
|
7
|
+
cli.command("start", "serve the production build").action(async () => {
|
|
8
|
+
const appRoot = process.cwd();
|
|
9
|
+
const userConfig = await loadUserConfig(appRoot);
|
|
10
|
+
const targetPort = userConfig.port || 3000;
|
|
11
|
+
|
|
12
|
+
spawn("node", [path.resolve(appRoot, "./dist/server/index.js")], {
|
|
13
|
+
stdio: "inherit",
|
|
14
|
+
env: { ...process.env, NODE_ENV: "production", PORT: String(targetPort) },
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CAC } from "cac";
|
|
2
|
+
import spawn from "cross-spawn";
|
|
3
|
+
|
|
4
|
+
export function register(cli: CAC) {
|
|
5
|
+
cli.command("typecheck", "run TypeScript type checking without emitting").action(async () => {
|
|
6
|
+
const appRoot = process.cwd();
|
|
7
|
+
const result = spawn.sync("tsc", ["--noEmit"], { stdio: "inherit", cwd: appRoot });
|
|
8
|
+
if (result.status !== 0) process.exit(result.status ?? 1);
|
|
9
|
+
});
|
|
10
|
+
};
|