resora 0.1.4 → 0.1.6

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/bin/index.mjs CHANGED
@@ -1,9 +1,11 @@
1
- #!/usr/bin/env ts-node
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=`/**
1
+ #!/usr/bin/env node
2
+ import{copyFileSync as e,existsSync as t,mkdirSync as n,readFileSync as r,rmSync as i,writeFileSync as a}from"fs";import o,{dirname as s,join as c}from"path";import{createRequire as l}from"module";import{fileURLToPath as u}from"url";import{Command as d,Kernel as f}from"@h3ravel/musket";const p=o.dirname(u(import.meta.url));let m=o.resolve(p,`../node_modules/resora/stubs`);t(m)||(m=o.resolve(p,`../stubs`));const h=(e={})=>Object.assign({resourcesDir:`src/resources`,stubsDir:m,stubs:{config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`}},e,{stubs:Object.assign({config:`resora.config.stub`,resource:`resource.stub`,collection:`resource.collection.stub`},e.stubs||{})});var g=class{command;config={};constructor(e={}){this.config=h(e);let n=l(import.meta.url),r=[c(process.cwd(),`resora.config.ts`),c(process.cwd(),`resora.config.js`),c(process.cwd(),`resora.config.cjs`)];for(let e of r)if(t(e))try{let{default:t}=n(e);Object.assign(this.config,h(t));break}catch(t){console.error(`Error loading config file at ${e}:`,t)}}init(){let n=c(process.cwd(),`resora.config.js`),i=c(this.config.stubsDir,this.config.stubs.config);return t(n)&&!this.command.option(`force`)&&(this.command.error(`Error: ${n} already exists.`),process.exit(1)),this.ensureDirectory(n),t(n)&&this.command.option(`force`)&&e(n,n.replace(/\.js$/,`.backup.${Date.now()}.js`)),a(n,r(i,`utf-8`)),{path:n}}ensureDirectory(e){let r=s(e);t(r)||n(r,{recursive:!0})}generateFile(e,n,o,s){t(n)&&!s?.force?(this.command.error(`Error: ${n} already exists.`),process.exit(1)):t(n)&&s?.force&&i(n);let c=r(e,`utf-8`);for(let[e,t]of Object.entries(o))c=c.replace(RegExp(`{{${e}}}`,`g`),t);return this.ensureDirectory(n),a(n,c),n}makeResource(e,n){let r=e;n?.collection&&!e.endsWith(`Collection`)&&!e.endsWith(`Resource`)?r+=`Collection`:!n?.collection&&!e.endsWith(`Resource`)&&!e.endsWith(`Collection`)&&(r+=`Resource`);let i=`${r}.ts`,a=c(this.config.resourcesDir,i),o=c(this.config.stubsDir,n?.collection||e.endsWith(`Collection`)?this.config.stubs.collection:this.config.stubs.resource);t(o)||(this.command.error(`Error: Stub file ${o} not found.`),process.exit(1));let s=r.replace(/(Resource|Collection)$/,``)+`Resource`,l=`/**
3
3
  * The resource that this collection collects.
4
4
  */
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:
5
+ collects = ${s}
6
+ `,u=`import ${s} from './${s}'\n`,d=(!!n?.collection||e.endsWith(`Collection`))&&t(c(this.config.resourcesDir,`${s}.ts`)),f=this.generateFile(o,a,{ResourceName:r,CollectionResourceName:r.replace(/(Resource|Collection)$/,``)+`Resource`,"collects = Resource":d?l:``,"import = Resource":d?u:``},n);return{name:r,path:f}}},_=class extends d{signature=`init
7
+ {--force : Force overwrite if config file already exists (existing file will be backed up) }
8
+ `;description=`Initialize Resora`;async handle(){this.app.command=this,this.app.init(),this.success(`Resora initialized`)}},v=class extends d{signature=`#create:
7
9
  {resource : Generates a new resource file.
8
10
  | {name : Name of the resource to create}
9
11
  | {--c|collection : Make a resource collection}
@@ -17,11 +19,11 @@ import e,{dirname as t,join as n}from"path";import{existsSync as r,mkdirSync as
17
19
  | {prefix : prefix of the resources to create, "Admin" will create AdminResource, AdminCollection}
18
20
  | {--force : Create the resource or collection file even if it already exists.}
19
21
  }
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`
22
+ `;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}`)}},y=String.raw`
21
23
  _____
22
24
  | __ \
23
25
  | |__) |___ ___ ___ _ __ __ _
24
26
  | _ // _ \/ __|/ _ \| '__/ _, |
25
27
  | | \ \ __/\__ \ (_) | | | (_| |
26
28
  |_| \_\___||___/\___/|_| \__,_|
27
- `;const v=new h;await d.init(v,{logo:_,name:`Resora CLI`,baseCommands:[g],exceptionHandler(e){throw e}});export{};
29
+ `;const b=new g;await f.init(b,{logo:y,name:`Resora CLI`,baseCommands:[v,_],exceptionHandler(e){throw e}});export{};
package/dist/index.cjs CHANGED
@@ -26,9 +26,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  }) : target, mod));
27
27
 
28
28
  //#endregion
29
+ let fs = require("fs");
29
30
  let path = require("path");
30
31
  path = __toESM(path);
31
- let fs = require("fs");
32
32
  let module$1 = require("module");
33
33
  let url = require("url");
34
34
  let _h3ravel_musket = require("@h3ravel/musket");
@@ -60,17 +60,19 @@ const defineConfig = (userConfig = {}) => {
60
60
  resourcesDir: "src/resources",
61
61
  stubsDir,
62
62
  stubs: {
63
+ config: "resora.config.stub",
63
64
  resource: "resource.stub",
64
65
  collection: "resource.collection.stub"
65
66
  }
66
67
  }, userConfig, { stubs: Object.assign({
68
+ config: "resora.config.stub",
67
69
  resource: "resource.stub",
68
70
  collection: "resource.collection.stub"
69
71
  }, userConfig.stubs || {}) });
70
72
  };
71
73
 
72
74
  //#endregion
73
- //#region src/cli/actions.ts
75
+ //#region src/cli/CliApp.ts
74
76
  var CliApp = class {
75
77
  command;
76
78
  config = {};
@@ -91,6 +93,23 @@ var CliApp = class {
91
93
  }
92
94
  }
93
95
  /**
96
+ * Initialize Resora by creating a default config file in the current directory
97
+ *
98
+ * @returns
99
+ */
100
+ init() {
101
+ const outputPath = (0, path.join)(process.cwd(), "resora.config.js");
102
+ const stubPath = (0, path.join)(this.config.stubsDir, this.config.stubs.config);
103
+ if ((0, fs.existsSync)(outputPath) && !this.command.option("force")) {
104
+ this.command.error(`Error: ${outputPath} already exists.`);
105
+ process.exit(1);
106
+ }
107
+ this.ensureDirectory(outputPath);
108
+ if ((0, fs.existsSync)(outputPath) && this.command.option("force")) (0, fs.copyFileSync)(outputPath, outputPath.replace(/\.js$/, `.backup.${Date.now()}.js`));
109
+ (0, fs.writeFileSync)(outputPath, (0, fs.readFileSync)(stubPath, "utf-8"));
110
+ return { path: outputPath };
111
+ }
112
+ /**
94
113
  * Utility to ensure directory exists
95
114
  *
96
115
  * @param filePath
package/dist/index.d.cts CHANGED
@@ -176,6 +176,10 @@ interface Config {
176
176
  * @description An object that defines the stub file names for different types of resources.
177
177
  */
178
178
  stubs: {
179
+ /**
180
+ * @description The stub file name for the configuration file. This stub will be used when generating the default configuration file.
181
+ */
182
+ config: string;
179
183
  /**
180
184
  * @description The stub file name for a resource. This stub will be used when generating a resource file.
181
185
  */
@@ -187,11 +191,19 @@ interface Config {
187
191
  };
188
192
  }
189
193
  //#endregion
190
- //#region src/cli/actions.d.ts
194
+ //#region src/cli/CliApp.d.ts
191
195
  declare class CliApp {
192
196
  command: Command;
193
197
  private config;
194
198
  constructor(config?: Partial<Config>);
199
+ /**
200
+ * Initialize Resora by creating a default config file in the current directory
201
+ *
202
+ * @returns
203
+ */
204
+ init(): {
205
+ path: string;
206
+ };
195
207
  /**
196
208
  * Utility to ensure directory exists
197
209
  *
package/dist/index.d.mts CHANGED
@@ -176,6 +176,10 @@ interface Config {
176
176
  * @description An object that defines the stub file names for different types of resources.
177
177
  */
178
178
  stubs: {
179
+ /**
180
+ * @description The stub file name for the configuration file. This stub will be used when generating the default configuration file.
181
+ */
182
+ config: string;
179
183
  /**
180
184
  * @description The stub file name for a resource. This stub will be used when generating a resource file.
181
185
  */
@@ -187,11 +191,19 @@ interface Config {
187
191
  };
188
192
  }
189
193
  //#endregion
190
- //#region src/cli/actions.d.ts
194
+ //#region src/cli/CliApp.d.ts
191
195
  declare class CliApp {
192
196
  command: Command;
193
197
  private config;
194
198
  constructor(config?: Partial<Config>);
199
+ /**
200
+ * Initialize Resora by creating a default config file in the current directory
201
+ *
202
+ * @returns
203
+ */
204
+ init(): {
205
+ path: string;
206
+ };
195
207
  /**
196
208
  * Utility to ensure directory exists
197
209
  *
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
1
2
  import path, { dirname, join } from "path";
2
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
3
3
  import { createRequire } from "module";
4
4
  import { fileURLToPath } from "url";
5
5
  import { Command } from "@h3ravel/musket";
@@ -31,17 +31,19 @@ const defineConfig = (userConfig = {}) => {
31
31
  resourcesDir: "src/resources",
32
32
  stubsDir,
33
33
  stubs: {
34
+ config: "resora.config.stub",
34
35
  resource: "resource.stub",
35
36
  collection: "resource.collection.stub"
36
37
  }
37
38
  }, userConfig, { stubs: Object.assign({
39
+ config: "resora.config.stub",
38
40
  resource: "resource.stub",
39
41
  collection: "resource.collection.stub"
40
42
  }, userConfig.stubs || {}) });
41
43
  };
42
44
 
43
45
  //#endregion
44
- //#region src/cli/actions.ts
46
+ //#region src/cli/CliApp.ts
45
47
  var CliApp = class {
46
48
  command;
47
49
  config = {};
@@ -62,6 +64,23 @@ var CliApp = class {
62
64
  }
63
65
  }
64
66
  /**
67
+ * Initialize Resora by creating a default config file in the current directory
68
+ *
69
+ * @returns
70
+ */
71
+ init() {
72
+ const outputPath = join(process.cwd(), "resora.config.js");
73
+ const stubPath = join(this.config.stubsDir, this.config.stubs.config);
74
+ if (existsSync(outputPath) && !this.command.option("force")) {
75
+ this.command.error(`Error: ${outputPath} already exists.`);
76
+ process.exit(1);
77
+ }
78
+ this.ensureDirectory(outputPath);
79
+ if (existsSync(outputPath) && this.command.option("force")) copyFileSync(outputPath, outputPath.replace(/\.js$/, `.backup.${Date.now()}.js`));
80
+ writeFileSync(outputPath, readFileSync(stubPath, "utf-8"));
81
+ return { path: outputPath };
82
+ }
83
+ /**
65
84
  * Utility to ensure directory exists
66
85
  *
67
86
  * @param filePath
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resora",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
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",
@@ -0,0 +1,5 @@
1
+ import { defineConfig } from 'resora'
2
+
3
+ export default defineConfig({
4
+ resourcesDir: 'src/resources',
5
+ })