@cododel/alto 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.
- package/dist/index.js +1 -1
- package/package.json +8 -12
package/dist/index.js
CHANGED
|
@@ -1083,4 +1083,4 @@ Perhaps URL is invalid: ${e.url}
|
|
|
1083
1083
|
ATTENTION:Note that the URL must point to Directus root (for example, do not include /admin)`)}catch(b){throw new Error(`Failed to ping Directus server at ${e.url}: ${b instanceof Error?b.message:String(b)}`)}let c=bOe(e.url).with(AOe()).with(vOe(e.token)),u=await c.request(xOe()),_=await c.request(DOe()),f=await c.request(wOe()),o=f.find((b)=>b.collection=="directus_settings"&&b.field=="public_favicon");if(o&&o.meta==null)o.meta={system:!0,many_collection:"directus_settings",many_field:"public_favicon",one_collection:"directus_files",one_field:null,one_allowed_collections:null,one_collection_field:null,one_deselect_action:"nullify",junction_field:null,sort_field:null};let l=new URL(e.url);l.pathname=(l.pathname?.replace(/\/$/,"")??"")+"/schema/snapshot",l.searchParams.set("export","json"),l.searchParams.set("access_token",e.token);let h=await fetch(l);if(!h.ok)throw new Error(`Failed to fetch schema (returned status ${h.status}): ${h.statusText}
|
|
1084
1084
|
${await h.text()}`);let d="",g=void 0;try{d=await h.text(),g=JSON.parse(d)}catch(b){throw new Error(`Server did not return JSON on schema export (${h.headers.get("content-type")??"<missing content type>"}): ${d}`)}s={...g,fields:_,collections:u,relations:f}}if(r)yy.writeFileSync(r,JSON.stringify(s,null,2));return{...s,fields:s.fields.filter((c)=>TOe.indexOf(c.collection)<0),collections:s.collections.filter((c)=>TOe.indexOf(c.collection)<0&&c.schema!==null),relations:s.relations}}var C$e=LS(spe(),1);var T$e=LS(jje(),1),S$e=LS(spe(),1),Hpe=LS(A$e(),1);import*as _1 from"node:fs";import*as Wl from"node:path";var __filename="/Users/cododel/Projects/alto-cli/src/commands/types/cli/types/template-loader.ts";var FCr=T$e.default(__filename,{});class Vpe extends S$e.Loader{async=!0;static async=!0;templateName;localDirs;projectDirs;macroDirs;templatesDirs;constructor(e,t){super();this.templateName=e,this.projectDirs=t??[],this.macroDirs=this.projectDirs.map((r)=>Wl.join(r,"macros")).filter((r)=>_1.existsSync(r)),this.templatesDirs=this.projectDirs.map((r)=>Wl.join(r,"templates")).filter((r)=>_1.existsSync(r)),this.localDirs=this.projectDirs.map((r)=>Wl.join(r,"templates",e)).filter((r)=>_1.existsSync(r))}getTemplateDirs(){return this.projectDirs.map((e)=>Wl.join(e,"templates",this.templateName)).filter((e)=>_1.existsSync(e)).map((e)=>Wl.resolve(e))}getTemplatesDirs(){return this.projectDirs.map((e)=>Wl.join(e,"templates")).filter((e)=>_1.existsSync(e)).map((e)=>Wl.resolve(e))}getMacroDirs(){return this.projectDirs.flatMap((e)=>[Wl.join(e,"macros")]).filter((e)=>_1.existsSync(e))}getFilterDirs(){return this.projectDirs.flatMap((e)=>[Wl.join(e,"extensions/filters")]).filter((e)=>_1.existsSync(e))}getTagsDirs(){return this.projectDirs.flatMap((e)=>[Wl.join(e,"extensions/tags")]).filter((e)=>_1.existsSync(e))}async getFiles(){let e=[];for(let t of this.getTemplateDirs()){let r=await Hpe.glob("**/*.njk",{cwd:t,absolute:!0});e.push(...r.map((i)=>Wl.relative(t,i)).map((i)=>({input:i,output:i.replace(/.njk$/i,"")})).filter((i)=>{return i.input!=i.output}).map((i)=>({input:Wl.join(t,i.input),output:i.output})))}return e.map(({input:t,output:r})=>({input:Wl.resolve(t),output:r}))}async getFilterFiles(){let e=[];for(let t of this.getFilterDirs())e.push(...(await Hpe.glob("**/*.{ts,mts,cts,js,mjs,cjs}",{cwd:t,ignore:["**/*.d.ts"]})).filter((r,i,s)=>{if(r.endsWith("js"))return s.indexOf(r.replace(/js$/i,"ts"))<0;return!0}).map((r)=>Wl.join(t,r)));return e}async getFilters(){let e=await this.getFilterFiles(),t={};for(let r of e){let i=FCr(r);if(typeof i==="object")Object.assign(t,i)}return t}async getSource(e,t){let r="default",i=e;if(e.indexOf(":")>0){if(!_1.existsSync(e)){let u=e.split(":",2);r=u[0],i=u.slice(1)[0]}}let s=[],n=null;if(r=="default")s=this.getTemplateDirs();else if(r=="template")s=this.getTemplatesDirs();else if(r=="macro")s=this.macroDirs;else if(r=="module")try{n=Gi.resolve(i)}catch(u){try{n=await import(i)}catch(_){n=null}}if(!n&&s.length)for(let u of s){let _=Wl.resolve(u),f=[Wl.resolve(u,i),Wl.resolve(u,`${i}.njk`)];for(let o of f)if(o.indexOf(_)===0&&_1.existsSync(o)){n=o;break}if(n)break}if(!n)try{}catch(u){}if(!n){t(new Error("Template not found"),null);return}let c={src:_1.readFileSync(n,"utf-8"),path:n,noCache:!0};this.emit("load",e,c),t(null,c)}}async function E$e(e,t){let r=new Vpe(e,t);if(!r.getTemplateDirs().length)throw new Error(`Could not find template "${e}"`);let i=await r.getFiles();async function s(n,c){let u=await r.getFilters(),_=new C$e.Environment(r,{autoescape:!1});return Object.entries(u).forEach(([f,o])=>{_.addFilter(f,(...l)=>{return o(c,...l)})}),new Promise((f,o)=>{_.render(n,c,(l,h)=>{if(l)return o(l);f(h??"")})})}return Object.assign(s,{files:i})}var __dirname="/Users/cododel/Projects/alto-cli/src/commands/types/cli/types",PCr=$m.object({url:$m.string().optional().default(process.env.DIRECTUS_URL??"http://localhost:8055"),token:$m.string().optional().default(process.env.DIRECTUS_TOKEN??""),template:$m.string().optional().default("default"),config:$m.string().optional().default("./.directus"),output:$m.string().optional().default("./.directus/generated").transform((e)=>{if(!F9.existsSync(e))F9.mkdirSync(e,{recursive:!0});return e}),useCache:$m.boolean().default(!1),watch:$m.boolean().default(!1),log:$m.string().optional().default("info")});class P$e extends F$e.default{}class N$e extends P$e{options;watcher;watching=!1;schema={version:0,collections:[],fields:[],directus:"",relations:[],vendor:""};engines={};registry;initialized=!1;constructor(e){super();this.options=e;this.watcher=new k$e.FSWatcher({persistent:!0,ignoreInitial:!0,awaitWriteFinish:!0,ignorePermissionErrors:!0})}async initialize(){if(this.initialized)return;await this.runTask("initialization",async()=>{if(this.schema.version)return;await this.runTask("schema",async()=>{this.schema=await COe({url:this.options.url,token:this.options.token},{cache:h4.join(this.options.config,"cache/schema.json"),useCache:this.options.useCache}),this.registry=yOe(this.schema),this.initialized=!0})})}watch(){return this.watching=!0,this.watcher.on("all",async(e,t)=>{await this.generate()}),this}async generate(e){return await this.initialize(),await this.runTask("generation",async()=>{let t=e??this.options.template,r=[h4.join(__dirname,"../default"),h4.join(__dirname,"../../default"),this.options.config].filter((n)=>F9.existsSync(n)),i=JSON.stringify({templateName:t,templateDirs:r}),s=this.engines[i]??await E$e(t,r);if(!(i in this.engines))this.engines[i]=s;if(this.watching);for(let n of s.files){let c=h4.join(this.options.output,n.output);if(F9.existsSync(c))await bV.rm(c)}for(let n of s.files)await this.runTask("file",async(c)=>{let u;await c("render",n),u=await s(n.input,{schema:this.schema,registry:this.registry});let _=await f_e(n.output);if(_.inferredParser)try{u=await RH(u,{parser:_.inferredParser})}catch(o){await this.emitAsync("file.format.error",n,this.makeError(o)),await this.emitAsync("generation.error",this.makeError(o))}let f=h4.dirname(h4.join(this.options.output,n.output));if(!F9.existsSync(f))F9.mkdirSync(f,{recursive:!0});await bV.writeFile(h4.join(this.options.output,n.output),u,{encoding:"utf-8"}),await this.emitAsync("file.output",n,u)})}),this}async runTask(e,t){try{await this.emitAsync(`${e}.begin`);let r=await t(async(i,...s)=>this.emitAsync(`${e}.${i}`,...s));return await this.emitAsync(`${e}.success`,r),r}catch(r){let i=this.makeError(r);throw await this.emitAsync(`${e}.error`,i),r}finally{await this.emitAsync(`${e}.end`)}}makeError(e){let t,r=[];if(typeof e!="string"&&e!=null){if(t=(e?.message??"").replace(/, file:/g,`
|
|
1085
1085
|
`).replace(/, line:/g,":").replace(/, col:/g,":"),e?.context){if(console.log(e),typeof e?.context=="string")r=e?.context?.split(`
|
|
1086
|
-
`)??[]}}else t=e??"Unknown error";if(typeof e=="string")e=new Error(t);return Object.assign(e,{message:t,context:r})}}async function I$e(e){let t=await PCr.parse(e);return new N$e(t)}import*as B$e from"node:path";var L$e={command:"generate",describe:"Generates an SDK for a given schema",builder:{url:{type:"string",required:!0,description:"The URL of the Directus instance to generate an SDK for",default:process.env.DIRECTUS_URL??"http://localhost:8055"},token:{type:"string",required:!0,description:"An static token with admin role assigned.",default:process.env.DIRECTUS_TOKEN??void 0},dir:{type:"string",required:!1,description:"Set a custom directory instead of '.directus'",default:"./.directus/"},outputDir:{type:"string",required:!1,description:"Set a custom directory instead of '.directus'",default:"./.directus/generated"},cache:{type:"boolean",required:!1,description:"Whether to enable caching to avoid subsequent requests to the server.",default:!1}},handler:async(e)=>{try{let t=await I$e({url:e.url,token:e.token,config:e.dir,output:e.outputDir??B$e.join(e.dir,"generated"),template:"default",useCache:e.cache});t.on("schema.begin",async()=>cm.start("Fetching schema")),t.on("schema.success",async()=>cm.success("Schema fetched")),t.on("schema.failure",async(r)=>{cm.fail("Error fetching schema"),cm.error(r)}),t.on("file.format.error",async(r,i)=>{cm.error("Error formatting file",i)}),t.on("generation.begin",async()=>cm.start("Starting generation")),t.on("generation.success",async()=>cm.success("Generation finished")),t.on("generation.failure",async(r)=>{cm.fail("Generation errror",r),cm.error(r)}),await t.initialize(),await t.generate()}catch(t){cm.error(t instanceof Error?t.message:String(t)),cm.error(t),process.exit(1)}}};var O$e={command:"types",describe:"Generate types for a Directus project",builder:(e)=>{return e.command(L$e).demandCommand(1,"You need to specify a command. Use --help for more information.").help()},handler:()=>{}};function M$e(){return NCr(ICr(process.argv)).scriptName("alto").usage("$0 <command> [options]").command(O$e).demandCommand(1,"You need at least one command").help().completion("completion","Generate bash completion script").recommendCommands().strict().argv}M$e().catch((e)=>{console.error("Error:",e.message),process.exit(1)});
|
|
1086
|
+
`)??[]}}else t=e??"Unknown error";if(typeof e=="string")e=new Error(t);return Object.assign(e,{message:t,context:r})}}async function I$e(e){let t=await PCr.parse(e);return new N$e(t)}import*as B$e from"node:path";var L$e={command:"generate",describe:"Generates an SDK for a given schema",builder:{url:{type:"string",required:!0,description:"The URL of the Directus instance to generate an SDK for",default:process.env.DIRECTUS_URL??"http://localhost:8055"},token:{type:"string",required:!0,description:"An static token with admin role assigned.",default:process.env.DIRECTUS_TOKEN??void 0},dir:{type:"string",required:!1,description:"Set a custom directory instead of '.directus'",default:"./.directus/"},outputDir:{type:"string",required:!1,description:"Set a custom directory instead of '.directus'",default:"./.directus/generated"},cache:{type:"boolean",required:!1,description:"Whether to enable caching to avoid subsequent requests to the server.",default:!1}},handler:async(e)=>{try{let t=await I$e({url:e.url,token:e.token,config:e.dir,output:e.outputDir??B$e.join(e.dir,"generated"),template:"default",useCache:e.cache});t.on("schema.begin",async()=>cm.start("Fetching schema")),t.on("schema.success",async()=>cm.success("Schema fetched")),t.on("schema.failure",async(r)=>{cm.fail("Error fetching schema"),cm.error(r)}),t.on("file.format.error",async(r,i)=>{cm.error("Error formatting file",i)}),t.on("generation.begin",async()=>cm.start("Starting generation")),t.on("generation.success",async()=>cm.success("Generation finished")),t.on("generation.failure",async(r)=>{cm.fail("Generation errror",r),cm.error(r)}),await t.initialize(),await t.generate()}catch(t){cm.error(t instanceof Error?t.message:String(t)),cm.error(t),process.exit(1)}}};var O$e={command:"types",describe:"Generate types for a Directus project",builder:(e)=>{return e.command(L$e).demandCommand(1,"You need to specify a command. Use --help for more information.").help()},handler:()=>{}};function M$e(){return NCr(ICr(process.argv)).scriptName("alto").usage("$0 <command> [options]").command(O$e).demandCommand(1,"You need at least one command").version().alias("v","version").help().completion("completion","Generate bash completion script").recommendCommands().strict().argv}M$e().catch((e)=>{console.error("Error:",e.message),process.exit(1)});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cododel/alto",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A powerful CLI tool for directus",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -33,17 +33,6 @@
|
|
|
33
33
|
"README.md",
|
|
34
34
|
"LICENSE"
|
|
35
35
|
],
|
|
36
|
-
"scripts": {
|
|
37
|
-
"alto": "bun ./src/index.ts",
|
|
38
|
-
"build": "bun build src/index.ts --outdir dist --target node --minify --external consola --external enquirer --external execa --external yargs",
|
|
39
|
-
"prepublishOnly": "bun run build",
|
|
40
|
-
"test:install": "./scripts/test-install.sh",
|
|
41
|
-
"test:uninstall": "./scripts/test-uninstall.sh",
|
|
42
|
-
"test:full-cycle": "./scripts/test-full-cycle.sh"
|
|
43
|
-
},
|
|
44
|
-
"workspaces": [
|
|
45
|
-
"src/commands/*"
|
|
46
|
-
],
|
|
47
36
|
"devDependencies": {
|
|
48
37
|
"@types/bun": "latest",
|
|
49
38
|
"@types/execa": "^2.0.2",
|
|
@@ -57,5 +46,12 @@
|
|
|
57
46
|
"enquirer": "^2.4.1",
|
|
58
47
|
"execa": "^9.6.0",
|
|
59
48
|
"yargs": "^18.0.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"alto": "bun ./src/index.ts",
|
|
52
|
+
"build": "bun build src/index.ts --outdir dist --target node --minify --external consola --external enquirer --external execa --external yargs",
|
|
53
|
+
"test:install": "./scripts/test-install.sh",
|
|
54
|
+
"test:uninstall": "./scripts/test-uninstall.sh",
|
|
55
|
+
"test:full-cycle": "./scripts/test-full-cycle.sh"
|
|
60
56
|
}
|
|
61
57
|
}
|