core-nails 1.0.2 → 1.0.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.
@@ -13,10 +13,11 @@ export async function generateModel(modelName) {
13
13
  // Pluralize collection name
14
14
  const collectionName = pluralize(modelName.charAt(0).toLowerCase() + modelName.slice(1));
15
15
  // Boilerplate content for model
16
- const modelContent = `import CreateService from "../concerns/CreateService";
16
+ const modelContent = `import FirebaseAdmin from "@lib/FirebaseAdmin";
17
+ import Nails from "core-nails";
17
18
  import { ${modelName}Schema } from "@schema";
18
19
 
19
- const ${modelName} = CreateService({
20
+ const ${modelName} = Nails.InitializeModel({
20
21
  collection: "${collectionName}",
21
22
  schema: ${modelName}Schema,
22
23
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-nails",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "scripts": {
5
5
  "build": "tsc"
6
6
  },
@@ -19,10 +19,11 @@ export async function generateModel(modelName: string) {
19
19
  );
20
20
 
21
21
  // Boilerplate content for model
22
- const modelContent = `import CreateService from "../concerns/CreateService";
22
+ const modelContent = `import FirebaseAdmin from "@lib/FirebaseAdmin";
23
+ import Nails from "core-nails";
23
24
  import { ${modelName}Schema } from "@schema";
24
25
 
25
- const ${modelName} = CreateService({
26
+ const ${modelName} = Nails.InitializeModel({
26
27
  collection: "${collectionName}",
27
28
  schema: ${modelName}Schema,
28
29
  });
@@ -1 +0,0 @@
1
- export declare function runGenerate(args: string[]): Promise<void>;
@@ -1,23 +0,0 @@
1
- export async function runGenerate(args) {
2
- const [type, name] = args;
3
- if (!type || !name) {
4
- console.error("Usage: nails generate <type> <name>");
5
- process.exit(1);
6
- }
7
- switch (type) {
8
- case "model": {
9
- const { generateModel } = await import("./model.js");
10
- await generateModel(name);
11
- break;
12
- }
13
- case "scaffold": {
14
- const { generateScaffold } = await import("./scaffold.js");
15
- await generateScaffold(name);
16
- break;
17
- }
18
- default: {
19
- console.error(`Unknown generator: ${type}`);
20
- process.exit(1);
21
- }
22
- }
23
- }
@@ -1 +0,0 @@
1
- export declare function generateModel(modelName: string): Promise<void>;
@@ -1,61 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import pluralize from "pluralize";
4
- export async function generateModel(modelName) {
5
- if (!modelName) {
6
- console.error("Usage: yarn generate model <ModelName>");
7
- console.error("Example: yarn generate model UserActivity");
8
- process.exit(1);
9
- }
10
- // Convert model name to match filename conventions
11
- const dirName = path.join("app", "(model)", modelName);
12
- const filePath = path.join(dirName, "index.ts");
13
- // Pluralize collection name
14
- const collectionName = pluralize(modelName.charAt(0).toLowerCase() + modelName.slice(1));
15
- // Boilerplate content for model
16
- const modelContent = `import CreateService from "../concerns/CreateService";
17
- import { ${modelName}Schema } from "@schema";
18
-
19
- const ${modelName} = CreateService({
20
- collection: "${collectionName}",
21
- schema: ${modelName}Schema,
22
- });
23
-
24
- export default ${modelName};
25
- `;
26
- // Create model directory and write index.ts
27
- fs.mkdirSync(dirName, { recursive: true });
28
- fs.writeFileSync(filePath, modelContent);
29
- console.log(`create ${path.relative(process.cwd(), filePath)}`);
30
- // --- Append schema stub to db/schema.ts ---
31
- const dbDir = path.join("db");
32
- fs.mkdirSync(dbDir, { recursive: true });
33
- const schemaFilePath = path.join(dbDir, "schema.ts");
34
- const schemaBaseContent = `import FirebaseAdmin from "@lib/FirebaseAdmin";
35
- import { z } from "zod";
36
-
37
- // Helper Function
38
- const FirebaseTimestamp = z.union([
39
- z.instanceof(FirebaseAdmin.firestore.Timestamp),
40
- z.date(),
41
- z.custom((val) => val === FirebaseAdmin.firestore.FieldValue.serverTimestamp(), { message: "Expected serverTimestamp()" }),
42
- ]);
43
- `;
44
- // Ensure schema file exists
45
- if (!fs.existsSync(schemaFilePath)) {
46
- fs.writeFileSync(schemaFilePath, schemaBaseContent);
47
- console.log(`create ${path.relative(process.cwd(), schemaFilePath)}`);
48
- }
49
- else {
50
- console.log(`append ${path.relative(process.cwd(), schemaFilePath)}`);
51
- }
52
- const schemaStub = `
53
- // ${modelName} Schema
54
- export const ${modelName}Schema = z.object({
55
- createdAt: FirebaseTimestamp.optional(),
56
- updatedAt: FirebaseTimestamp.optional(),
57
- });
58
- export type ${modelName}Type = z.infer<typeof ${modelName}Schema>;
59
- `;
60
- fs.appendFileSync(schemaFilePath, schemaStub);
61
- }
@@ -1 +0,0 @@
1
- export declare function generateScaffold(scaffoldName: string): Promise<void>;
@@ -1,105 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import pluralize from "pluralize";
4
- import { generateModel } from "./model.js";
5
- export async function generateScaffold(scaffoldName) {
6
- if (!scaffoldName) {
7
- console.error("Usage: yarn generate scaffold <ScaffoldName>");
8
- console.error("Example: yarn generate scaffold UserActivity");
9
- process.exit(1);
10
- }
11
- function toSnakeCase(name) {
12
- const snake = name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
13
- return pluralize(snake);
14
- }
15
- const camelName = scaffoldName.charAt(0).toUpperCase() + scaffoldName.slice(1);
16
- const snakePluralName = toSnakeCase(scaffoldName);
17
- // --- 1️⃣ Generate Model (if missing) ---
18
- const modelDir = path.join("app", "(model)", camelName);
19
- if (!fs.existsSync(modelDir)) {
20
- console.log(`Model not found for ${camelName}, generating...`);
21
- await generateModel(scaffoldName);
22
- }
23
- // --- 2️⃣ Paths ---
24
- const controllerDir = path.join("app", "(controller)", camelName);
25
- const apiDir = path.join("app", "api", snakePluralName);
26
- const apiIdDir = path.join(apiDir, "[id]");
27
- // --- 3️⃣ Action files ---
28
- const actions = ["index", "show", "create", "update", "destroy"];
29
- fs.mkdirSync(controllerDir, { recursive: true });
30
- const actionsDir = path.join(controllerDir, "actions");
31
- fs.mkdirSync(actionsDir, { recursive: true });
32
- actions.forEach((action) => {
33
- const filePath = path.join(actionsDir, `${action}_action.ts`);
34
- const content = `export default async function ${action}_action(req: Request${["index", "create"].includes(action) ? "" : ", id: string"}) {
35
- return new Response(JSON.stringify({ message: "${action} ${camelName}" }));
36
- }
37
- `;
38
- fs.writeFileSync(filePath, content);
39
- console.log(`create ${path.relative(process.cwd(), filePath)}`);
40
- });
41
- // --- 4️⃣ controller index.ts ---
42
- const indexContent = actions
43
- .map((action) => `import ${action}_action from "./actions/${action}_action";`)
44
- .join("\n") +
45
- `
46
-
47
- export default {
48
- ${actions.map((a) => ` ${a}_action,`).join("\n")}
49
- };
50
- `;
51
- const controllerIndexPath = path.join(controllerDir, "index.ts");
52
- fs.writeFileSync(controllerIndexPath, indexContent);
53
- console.log(`create ${path.relative(process.cwd(), controllerIndexPath)}`);
54
- // --- 5️⃣ API route.ts ---
55
- fs.mkdirSync(apiDir, { recursive: true });
56
- const apiRoutePath = path.join(apiDir, "route.ts");
57
- const apiRouteContent = `/**
58
- * Standard RESTful Routes
59
- *
60
- * | HTTP Verb | Controller#Action | Purpose | Path |
61
- * |-----------|-------------------|------------------|--------------------------------------------|
62
- * | GET | index | List all | /${snakePluralName}
63
- * | GET | show | Get one | /${snakePluralName}/:id
64
- * | POST | create | Create | /${snakePluralName}
65
- * | PATCH | update | Update (partial) | /${snakePluralName}/:id
66
- * | PUT | update | Update (full) | /${snakePluralName}/:id
67
- * | DELETE | destroy | Delete | /${snakePluralName}/:id
68
- */
69
-
70
- import ${camelName}Controller from "@controller/${camelName}";
71
-
72
- export async function GET(req: Request) {
73
- return ${camelName}Controller.index_action(req);
74
- }
75
-
76
- export async function POST(req: Request) {
77
- return ${camelName}Controller.create_action(req);
78
- }
79
- `;
80
- fs.writeFileSync(apiRoutePath, apiRouteContent);
81
- console.log(`create ${path.relative(process.cwd(), apiRoutePath)}`);
82
- // --- 6️⃣ API [id]/route.ts ---
83
- fs.mkdirSync(apiIdDir, { recursive: true });
84
- const apiIdRoutePath = path.join(apiIdDir, "route.ts");
85
- const apiIdRouteContent = `import ${camelName}Controller from "@controller/${camelName}";
86
-
87
- export async function GET(req: Request, { params }: { params: { id: string } }) {
88
- return ${camelName}Controller.show_action(req, params.id);
89
- }
90
-
91
- export async function PATCH(req: Request, { params }: { params: { id: string } }) {
92
- return ${camelName}Controller.update_action(req, params.id);
93
- }
94
-
95
- export async function PUT(req: Request, { params }: { params: { id: string } }) {
96
- return ${camelName}Controller.update_action(req, params.id);
97
- }
98
-
99
- export async function DELETE(req: Request, { params }: { params: { id: string } }) {
100
- return ${camelName}Controller.destroy_action(req, params.id);
101
- }
102
- `;
103
- fs.writeFileSync(apiIdRoutePath, apiIdRouteContent);
104
- console.log(`create ${path.relative(process.cwd(), apiIdRoutePath)}`);
105
- }
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- declare const command: any, args: any;
3
- declare function run(): Promise<void>;
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- const [, , command, ...args] = process.argv;
4
- async function run() {
5
- switch (command) {
6
- case "generate": {
7
- const { runGenerate } = await import("./generate/index.js");
8
- await runGenerate(args);
9
- break;
10
- }
11
- default: {
12
- console.log(`Unknown command: ${command}`);
13
- console.log("Available commands: generate");
14
- process.exit(1);
15
- }
16
- }
17
- }
18
- run();