resora 0.1.2 → 0.1.3

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.
Files changed (2) hide show
  1. package/bin/index.mjs +5 -167
  2. package/package.json +1 -1
package/bin/index.mjs CHANGED
@@ -1,124 +1,9 @@
1
1
  #!/usr/bin/env ts-node
2
- import path, { dirname, join } from "path";
3
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
4
- import { createRequire } from "module";
5
- import { fileURLToPath } from "url";
6
- import { Command, Kernel } from "@h3ravel/musket";
7
-
8
- //#region src/utility.ts
9
- const __dirname = /* @__PURE__ */ path.dirname(fileURLToPath(import.meta.url));
10
- let stubsDir = path.resolve(__dirname, "../node_modules/resora/stubs");
11
- if (!existsSync(stubsDir)) stubsDir = path.resolve(__dirname, "../stubs");
12
- /**
13
- * Define the configuration for the package
14
- *
15
- * @param userConfig The user configuration to override the default configuration
16
- * @returns The merged configuration object
17
- */
18
- const defineConfig = (userConfig = {}) => {
19
- return Object.assign({
20
- resourcesDir: "src/resources",
21
- stubsDir,
22
- stubs: {
23
- resource: "resource.stub",
24
- collection: "resource.collection.stub"
25
- }
26
- }, userConfig, { stubs: Object.assign({
27
- resource: "resource.stub",
28
- collection: "resource.collection.stub"
29
- }, userConfig.stubs || {}) });
30
- };
31
-
32
- //#endregion
33
- //#region src/cli/actions.ts
34
- var CliApp = class {
35
- command;
36
- config = {};
37
- constructor(config = {}) {
38
- this.config = defineConfig(config);
39
- const require = createRequire(import.meta.url);
40
- const possibleConfigPaths = [
41
- join(process.cwd(), "resora.config.ts"),
42
- join(process.cwd(), "resora.config.js"),
43
- join(process.cwd(), "resora.config.cjs")
44
- ];
45
- for (const configPath of possibleConfigPaths) if (existsSync(configPath)) try {
46
- const { default: userConfig } = require(configPath);
47
- Object.assign(this.config, defineConfig(userConfig));
48
- break;
49
- } catch (e) {
50
- console.error(`Error loading config file at ${configPath}:`, e);
51
- }
52
- }
53
- /**
54
- * Utility to ensure directory exists
55
- *
56
- * @param filePath
57
- */
58
- ensureDirectory(filePath) {
59
- const dir = dirname(filePath);
60
- if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
61
- }
62
- /**
63
- * Utility to generate file from stub
64
- *
65
- * @param stubPath
66
- * @param outputPath
67
- * @param replacements
68
- */
69
- generateFile(stubPath, outputPath, replacements, options) {
70
- if (existsSync(outputPath) && !options?.force) {
71
- this.command.error(`Error: ${outputPath} already exists.`);
72
- process.exit(1);
73
- } else if (existsSync(outputPath) && options?.force) rmSync(outputPath);
74
- let content = readFileSync(stubPath, "utf-8");
75
- for (const [key, value] of Object.entries(replacements)) content = content.replace(new RegExp(`{{${key}}}`, "g"), value);
76
- this.ensureDirectory(outputPath);
77
- writeFileSync(outputPath, content);
78
- return outputPath;
79
- }
80
- /**
81
- * Command to create a new resource or resource collection file
82
- *
83
- * @param name
84
- * @param options
85
- */
86
- makeResource(name, options) {
87
- let resourceName = name;
88
- if (options?.collection && !name.endsWith("Collection") && !name.endsWith("Resource")) resourceName += "Collection";
89
- else if (!options?.collection && !name.endsWith("Resource") && !name.endsWith("Collection")) resourceName += "Resource";
90
- const fileName = `${resourceName}.ts`;
91
- const outputPath = join(this.config.resourcesDir, fileName);
92
- const stubPath = join(this.config.stubsDir, options?.collection || name.endsWith("Collection") ? this.config.stubs.collection : this.config.stubs.resource);
93
- if (!existsSync(stubPath)) {
94
- this.command.error(`Error: Stub file ${stubPath} not found.`);
95
- process.exit(1);
96
- }
97
- const collectsName = resourceName.replace(/(Resource|Collection)$/, "") + "Resource";
98
- const collects = `/**
2
+ import e,{dirname as t,join as n}from"path";import{existsSync as r,mkdirSync as i,readFileSync as a,rmSync as o,writeFileSync as s}from"fs";import{createRequire as c}from"module";import{fileURLToPath as l}from"url";import{Command as u,Kernel as d}from"@h3ravel/musket";const f=e.dirname(l(import.meta.url));let p=e.resolve(f,`../node_modules/resora/stubs`);r(p)||(p=e.resolve(f,`../stubs`));const m=(e={})=>Object.assign({resourcesDir:`src/resources`,stubsDir:p,stubs:{resource:`resource.stub`,collection:`resource.collection.stub`}},e,{stubs:Object.assign({resource:`resource.stub`,collection:`resource.collection.stub`},e.stubs||{})});var h=class{command;config={};constructor(e={}){this.config=m(e);let t=c(import.meta.url),i=[n(process.cwd(),`resora.config.ts`),n(process.cwd(),`resora.config.js`),n(process.cwd(),`resora.config.cjs`)];for(let e of i)if(r(e))try{let{default:n}=t(e);Object.assign(this.config,m(n));break}catch(t){console.error(`Error loading config file at ${e}:`,t)}}ensureDirectory(e){let n=t(e);r(n)||i(n,{recursive:!0})}generateFile(e,t,n,i){r(t)&&!i?.force?(this.command.error(`Error: ${t} already exists.`),process.exit(1)):r(t)&&i?.force&&o(t);let c=a(e,`utf-8`);for(let[e,t]of Object.entries(n))c=c.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(t),s(t,c),t}makeResource(e,t){let i=e;t?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?i+=`Collection`:!t?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(i+=`Resource`);let a=`${i}.ts`,o=n(this.config.resourcesDir,a),s=n(this.config.stubsDir,t?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);r(s)||(this.command.error(`Error: Stub file ${s} not found.`),process.exit(1));let c=i.replace(/(Resource|Collection)$/,``)+`Resource`,l=`/**
99
3
  * The resource that this collection collects.
100
4
  */
101
- collects = ${collectsName}
102
- `;
103
- const collectsImport = `import ${collectsName} from './${collectsName}'\n`;
104
- const hasCollects = (!!options?.collection || name.endsWith("Collection")) && existsSync(join(this.config.resourcesDir, `${collectsName}.ts`));
105
- const path = this.generateFile(stubPath, outputPath, {
106
- ResourceName: resourceName,
107
- CollectionResourceName: resourceName.replace(/(Resource|Collection)$/, "") + "Resource",
108
- "collects = Resource": hasCollects ? collects : "",
109
- "import = Resource": hasCollects ? collectsImport : ""
110
- }, options);
111
- return {
112
- name: resourceName,
113
- path
114
- };
115
- }
116
- };
117
-
118
- //#endregion
119
- //#region src/cli/commands/MakeResource.ts
120
- var MakeResource = class extends Command {
121
- signature = `#create:
5
+ collects = ${c}
6
+ `,u=`import ${c} from './${c}'\n`,d=(!!t?.collection||e.endsWith(`Collection`))&&r(n(this.config.resourcesDir,`${c}.ts`)),f=this.generateFile(s,o,{ResourceName:i,CollectionResourceName:i.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?l:``,"import = Resource":d?u:``},t);return{name:i,path:f}}},g=class extends u{signature=`#create:
122
7
  {resource : Generates a new resource file.
123
8
  | {name : Name of the resource to create}
124
9
  | {--c|collection : Make a resource collection}
@@ -132,58 +17,11 @@ var MakeResource = class extends Command {
132
17
  | {prefix : prefix of the resources to create, "Admin" will create AdminResource, AdminCollection}
133
18
  | {--force : Create the resource or collection file even if it already exists.}
134
19
  }
135
- `;
136
- description = "Create a new resource or resource collection file";
137
- async handle() {
138
- this.app.command = this;
139
- let path = "";
140
- const action = this.dictionary.name || this.dictionary.baseCommand;
141
- if (["resource", "collection"].includes(action) && !this.argument("name")) return void this.error("Error: Name argument is required.");
142
- if (action === "all" && !this.argument("prefix")) return void this.error("Error: Prefix argument is required.");
143
- switch (action) {
144
- case "resource":
145
- ({path} = this.app.makeResource(this.argument("name"), this.options()));
146
- break;
147
- case "collection":
148
- ({path} = this.app.makeResource(this.argument("name") + "Collection", this.options()));
149
- break;
150
- case "all": {
151
- const o1 = this.app.makeResource(this.argument("prefix"), { force: this.option("force") });
152
- const o2 = this.app.makeResource(this.argument("prefix") + "Collection", {
153
- collection: true,
154
- force: this.option("force")
155
- });
156
- path = `${o1.path}, ${o2.path}`;
157
- break;
158
- }
159
- default: this.fail(`Unknown action: ${action}`);
160
- }
161
- this.success(`Created: ${path}`);
162
- }
163
- };
164
-
165
- //#endregion
166
- //#region src/cli/logo.ts
167
- var logo_default = String.raw`
20
+ `;description=`Create a new resource or resource collection file`;async handle(){this.app.command=this;let e=``,t=this.dictionary.name||this.dictionary.baseCommand;if([`resource`,`collection`].includes(t)&&!this.argument(`name`))return void this.error(`Error: Name argument is required.`);if(t===`all`&&!this.argument(`prefix`))return void this.error(`Error: Prefix argument is required.`);switch(t){case`resource`:({path:e}=this.app.makeResource(this.argument(`name`),this.options()));break;case`collection`:({path:e}=this.app.makeResource(this.argument(`name`)+`Collection`,this.options()));break;case`all`:{let t=this.app.makeResource(this.argument(`prefix`),{force:this.option(`force`)}),n=this.app.makeResource(this.argument(`prefix`)+`Collection`,{collection:!0,force:this.option(`force`)});e=`${t.path}, ${n.path}`;break}default:this.fail(`Unknown action: ${t}`)}this.success(`Created: ${e}`)}},_=String.raw`
168
21
  _____
169
22
  | __ \
170
23
  | |__) |___ ___ ___ _ __ __ _
171
24
  | _ // _ \/ __|/ _ \| '__/ _, |
172
25
  | | \ \ __/\__ \ (_) | | | (_| |
173
26
  |_| \_\___||___/\___/|_| \__,_|
174
- `;
175
-
176
- //#endregion
177
- //#region src/cli/index.ts
178
- const app = new CliApp();
179
- await Kernel.init(app, {
180
- logo: logo_default,
181
- name: "Resora CLI",
182
- baseCommands: [MakeResource],
183
- exceptionHandler(exception) {
184
- throw exception;
185
- }
186
- });
187
-
188
- //#endregion
189
- export { };
27
+ `;const v=new h;await d.init(v,{logo:_,name:`Resora CLI`,baseCommands:[g],exceptionHandler(e){throw e}});export{};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resora",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "A structured API response layer for Node.js and TypeScript with automatic JSON responses, collection support, and pagination handling.",
5
5
  "keywords": [
6
6
  "api",