resora 0.1.1 → 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.
- package/bin/index.mjs +18 -134
- package/package.json +1 -1
package/bin/index.mjs
CHANGED
|
@@ -1,143 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env ts-node
|
|
2
|
-
import
|
|
3
|
-
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
4
|
-
import { createRequire } from "module";
|
|
5
|
-
import { fileURLToPath } from "url";
|
|
6
|
-
import { Kernel } from "@h3ravel/musket";
|
|
7
|
-
import path$1 from "node:path";
|
|
8
|
-
|
|
9
|
-
//#region src/utility.ts
|
|
10
|
-
const __dirname = /* @__PURE__ */ path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
-
let stubsDir = path.resolve(__dirname, "../node_modules/resora/stubs");
|
|
12
|
-
if (!existsSync(stubsDir)) stubsDir = path.resolve(__dirname, "../stubs");
|
|
13
|
-
/**
|
|
14
|
-
* Define the configuration for the package
|
|
15
|
-
*
|
|
16
|
-
* @param userConfig The user configuration to override the default configuration
|
|
17
|
-
* @returns The merged configuration object
|
|
18
|
-
*/
|
|
19
|
-
const defineConfig = (userConfig = {}) => {
|
|
20
|
-
return Object.assign({
|
|
21
|
-
resourcesDir: "src/resources",
|
|
22
|
-
stubsDir,
|
|
23
|
-
stubs: {
|
|
24
|
-
resource: "resource.stub",
|
|
25
|
-
collection: "resource.collection.stub"
|
|
26
|
-
}
|
|
27
|
-
}, userConfig, { stubs: Object.assign({
|
|
28
|
-
resource: "resource.stub",
|
|
29
|
-
collection: "resource.collection.stub"
|
|
30
|
-
}, userConfig.stubs || {}) });
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/cli/actions.ts
|
|
35
|
-
var CliApp = class {
|
|
36
|
-
command;
|
|
37
|
-
config = {};
|
|
38
|
-
constructor(config = {}) {
|
|
39
|
-
this.config = defineConfig(config);
|
|
40
|
-
const require = createRequire(import.meta.url);
|
|
41
|
-
const possibleConfigPaths = [
|
|
42
|
-
join(process.cwd(), "resora.config.ts"),
|
|
43
|
-
join(process.cwd(), "resora.config.js"),
|
|
44
|
-
join(process.cwd(), "resora.config.cjs")
|
|
45
|
-
];
|
|
46
|
-
for (const configPath of possibleConfigPaths) if (existsSync(configPath)) try {
|
|
47
|
-
const { default: userConfig } = require(configPath);
|
|
48
|
-
Object.assign(this.config, defineConfig(userConfig));
|
|
49
|
-
break;
|
|
50
|
-
} catch (e) {
|
|
51
|
-
console.error(`Error loading config file at ${configPath}:`, e);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Utility to ensure directory exists
|
|
56
|
-
*
|
|
57
|
-
* @param filePath
|
|
58
|
-
*/
|
|
59
|
-
ensureDirectory(filePath) {
|
|
60
|
-
const dir = dirname(filePath);
|
|
61
|
-
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Utility to generate file from stub
|
|
65
|
-
*
|
|
66
|
-
* @param stubPath
|
|
67
|
-
* @param outputPath
|
|
68
|
-
* @param replacements
|
|
69
|
-
*/
|
|
70
|
-
generateFile(stubPath, outputPath, replacements, options) {
|
|
71
|
-
if (existsSync(outputPath) && !options?.force) {
|
|
72
|
-
this.command.error(`Error: ${outputPath} already exists.`);
|
|
73
|
-
process.exit(1);
|
|
74
|
-
} else if (existsSync(outputPath) && options?.force) rmSync(outputPath);
|
|
75
|
-
let content = readFileSync(stubPath, "utf-8");
|
|
76
|
-
for (const [key, value] of Object.entries(replacements)) content = content.replace(new RegExp(`{{${key}}}`, "g"), value);
|
|
77
|
-
this.ensureDirectory(outputPath);
|
|
78
|
-
writeFileSync(outputPath, content);
|
|
79
|
-
return outputPath;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Command to create a new resource or resource collection file
|
|
83
|
-
*
|
|
84
|
-
* @param name
|
|
85
|
-
* @param options
|
|
86
|
-
*/
|
|
87
|
-
makeResource(name, options) {
|
|
88
|
-
let resourceName = name;
|
|
89
|
-
if (options?.collection && !name.endsWith("Collection") && !name.endsWith("Resource")) resourceName += "Collection";
|
|
90
|
-
else if (!options?.collection && !name.endsWith("Resource") && !name.endsWith("Collection")) resourceName += "Resource";
|
|
91
|
-
const fileName = `${resourceName}.ts`;
|
|
92
|
-
const outputPath = join(this.config.resourcesDir, fileName);
|
|
93
|
-
const stubPath = join(this.config.stubsDir, options?.collection || name.endsWith("Collection") ? this.config.stubs.collection : this.config.stubs.resource);
|
|
94
|
-
if (!existsSync(stubPath)) {
|
|
95
|
-
this.command.error(`Error: Stub file ${stubPath} not found.`);
|
|
96
|
-
process.exit(1);
|
|
97
|
-
}
|
|
98
|
-
const collectsName = resourceName.replace(/(Resource|Collection)$/, "") + "Resource";
|
|
99
|
-
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=`/**
|
|
100
3
|
* The resource that this collection collects.
|
|
101
4
|
*/
|
|
102
|
-
collects = ${
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/cli/logo.ts
|
|
121
|
-
var logo_default = String.raw`
|
|
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:
|
|
7
|
+
{resource : Generates a new resource file.
|
|
8
|
+
| {name : Name of the resource to create}
|
|
9
|
+
| {--c|collection : Make a resource collection}
|
|
10
|
+
| {--force : Create the resource or collection file even if it already exists.}
|
|
11
|
+
}
|
|
12
|
+
{collection : Create a new resource collection file.
|
|
13
|
+
| {name : Name of the resource collection to create}
|
|
14
|
+
| {--force : Create the resource or collection file even if it already exists.}
|
|
15
|
+
}
|
|
16
|
+
{all : Create both resource and collection files.
|
|
17
|
+
| {prefix : prefix of the resources to create, "Admin" will create AdminResource, AdminCollection}
|
|
18
|
+
| {--force : Create the resource or collection file even if it already exists.}
|
|
19
|
+
}
|
|
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`
|
|
122
21
|
_____
|
|
123
22
|
| __ \
|
|
124
23
|
| |__) |___ ___ ___ _ __ __ _
|
|
125
24
|
| _ // _ \/ __|/ _ \| '__/ _, |
|
|
126
25
|
| | \ \ __/\__ \ (_) | | | (_| |
|
|
127
26
|
|_| \_\___||___/\___/|_| \__,_|
|
|
128
|
-
`;
|
|
129
|
-
|
|
130
|
-
//#endregion
|
|
131
|
-
//#region src/cli/index.ts
|
|
132
|
-
const app = new CliApp();
|
|
133
|
-
await Kernel.init(app, {
|
|
134
|
-
logo: logo_default,
|
|
135
|
-
name: "Resora CLI",
|
|
136
|
-
discoveryPaths: [path$1.join(process.cwd(), "src/cli/commands/*.ts")],
|
|
137
|
-
exceptionHandler(exception) {
|
|
138
|
-
throw exception;
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
//#endregion
|
|
143
|
-
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