create-guren-app 0.1.1-alpha.2 → 0.1.1-alpha.4

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/cli.js ADDED
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import { cp, mkdir, readFile, writeFile } from "fs/promises";
5
+ import { basename, join, relative, resolve } from "path";
6
+ import { fileURLToPath } from "url";
7
+ import process from "process";
8
+ import { consola } from "consola";
9
+ import { defineCommand, runMain } from "citty";
10
+
11
+ // src/utils.ts
12
+ import { readdir, stat } from "fs/promises";
13
+ async function directoryExists(path) {
14
+ try {
15
+ const stats = await stat(path);
16
+ return stats.isDirectory();
17
+ } catch (error) {
18
+ if (error.code === "ENOENT") {
19
+ return false;
20
+ }
21
+ throw error;
22
+ }
23
+ }
24
+ async function isDirectoryEmpty(path) {
25
+ try {
26
+ const entries = await readdir(path);
27
+ return entries.length === 0;
28
+ } catch (error) {
29
+ if (error.code === "ENOENT") {
30
+ return true;
31
+ }
32
+ throw error;
33
+ }
34
+ }
35
+ function toKebabCase(value) {
36
+ return value.replace(/([a-z0-9])([A-Z])/gu, "$1-$2").replace(/[^a-zA-Z0-9]+/gu, "-").toLowerCase().replace(/^-+|-+$/gu, "");
37
+ }
38
+ function toTitleCase(value) {
39
+ const words = value.replace(/[-_]+/gu, " ").split(/\s+/u).filter(Boolean);
40
+ if (words.length === 0) {
41
+ return "Guren App";
42
+ }
43
+ return words.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
44
+ }
45
+ function toPackageName(value) {
46
+ const trimmed = value.trim();
47
+ if (trimmed.startsWith("@")) {
48
+ const withoutScope = trimmed.slice(1);
49
+ const [scope, name = "guren-app"] = withoutScope.split("/", 2);
50
+ const safeScope = toKebabCase(scope);
51
+ const safeName = toKebabCase(name);
52
+ return `@${safeScope || "guren"}/${safeName || "app"}`;
53
+ }
54
+ const safe = toKebabCase(trimmed);
55
+ return safe || "guren-app";
56
+ }
57
+
58
+ // src/cli.ts
59
+ async function ensureTargetDirectory(path, force) {
60
+ try {
61
+ await mkdir(path, { recursive: true });
62
+ } catch (error) {
63
+ if (error.code === "EEXIST") {
64
+ throw new Error(`Path "${path}" exists and is not a directory.`);
65
+ }
66
+ throw error;
67
+ }
68
+ if (!force) {
69
+ const empty = await isDirectoryEmpty(path);
70
+ if (!empty) {
71
+ throw new Error(`Directory "${path}" is not empty. Use --force to scaffold anyway.`);
72
+ }
73
+ }
74
+ }
75
+ async function copyTemplate(template, destination) {
76
+ await cp(template, destination, { recursive: true, force: true });
77
+ }
78
+ async function replaceTokens(destination, files, tokens) {
79
+ for (const file of files) {
80
+ const path = join(destination, file);
81
+ const content = await readFile(path, "utf8");
82
+ let updated = content;
83
+ for (const [token, replacement] of tokens) {
84
+ updated = updated.split(token).join(replacement);
85
+ }
86
+ if (updated !== content) {
87
+ await writeFile(path, updated, "utf8");
88
+ }
89
+ }
90
+ }
91
+ var command = defineCommand({
92
+ meta: {
93
+ name: "create-guren-app",
94
+ description: "Scaffold a new Guren application."
95
+ },
96
+ args: {
97
+ target: {
98
+ type: "positional",
99
+ description: "Directory to create the application in",
100
+ default: "."
101
+ },
102
+ force: {
103
+ type: "boolean",
104
+ alias: "f",
105
+ description: "Overwrite existing files in the target directory"
106
+ }
107
+ },
108
+ async run({ args }) {
109
+ const target = args.target;
110
+ const force = Boolean(args.force);
111
+ const templateName = "default";
112
+ const templateDir = fileURLToPath(new URL(`../templates/${templateName}`, import.meta.url));
113
+ const targetDir = resolve(process.cwd(), target);
114
+ if (await directoryExists(targetDir)) {
115
+ if (!force) {
116
+ const empty = await isDirectoryEmpty(targetDir);
117
+ if (!empty) {
118
+ throw new Error(`Directory "${targetDir}" is not empty. Use --force to scaffold anyway.`);
119
+ }
120
+ }
121
+ } else {
122
+ await ensureTargetDirectory(targetDir, true);
123
+ }
124
+ const appName = basename(targetDir);
125
+ const packageName = toPackageName(appName);
126
+ const appTitle = toTitleCase(appName);
127
+ await copyTemplate(templateDir, targetDir);
128
+ const tokenMap = /* @__PURE__ */ new Map([
129
+ ["guren-app-placeholder", packageName],
130
+ ["__APP_TITLE__", appTitle],
131
+ ["__APP_NAME__", appName]
132
+ ]);
133
+ const filesToTransform = [
134
+ "README.md",
135
+ "package.json",
136
+ "public/index.html",
137
+ "bin/serve.ts",
138
+ "app/Http/Controllers/HomeController.ts",
139
+ "resources/js/pages/Home.tsx"
140
+ ];
141
+ await replaceTokens(targetDir, filesToTransform, tokenMap);
142
+ const relativeTarget = relative(process.cwd(), targetDir) || ".";
143
+ consola.success(`Scaffolded a new Guren app in ${relativeTarget}`);
144
+ consola.info("Next steps:");
145
+ if (relativeTarget !== ".") {
146
+ consola.log(` cd ${relativeTarget}`);
147
+ }
148
+ consola.log(" bun install");
149
+ consola.log(" bun run dev");
150
+ }
151
+ });
152
+ runMain(command).catch((error) => {
153
+ if (error instanceof Error) {
154
+ consola.error(error.message);
155
+ } else {
156
+ consola.error(String(error));
157
+ }
158
+ process.exit(1);
159
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-guren-app",
3
- "version": "0.1.1-alpha.2",
3
+ "version": "0.1.1-alpha.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,10 +11,10 @@
11
11
  "db:seed": "bunx guren db:seed"
12
12
  },
13
13
  "dependencies": {
14
- "@guren/cli": "^0.1.1-alpha.1",
15
- "@guren/inertia-client": "^0.1.1-alpha.1",
16
- "@guren/orm": "^0.1.1-alpha.1",
17
- "@guren/server": "^0.1.1-alpha.1",
14
+ "@guren/cli": "^0.1.1-alpha.4",
15
+ "@guren/inertia-client": "^0.1.1-alpha.4",
16
+ "@guren/orm": "^0.1.1-alpha.4",
17
+ "@guren/server": "^0.1.1-alpha.4",
18
18
  "@inertiajs/react": "^2.2.15",
19
19
  "drizzle-orm": "0.44.7",
20
20
  "postgres": "^3.4.3",
@@ -28,4 +28,4 @@
28
28
  "tsup": "^8.5.0",
29
29
  "typescript": "^5.4.0"
30
30
  }
31
- }
31
+ }