-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
380 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @indirectus/cli | ||
|
||
## 0.0.2 | ||
|
||
### Patch Changes | ||
|
||
- refactor templates | ||
|
||
## 0.0.1 | ||
|
||
### Patch Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { Field } from "../../../types/registry"; | ||
|
||
import { contains } from "./contains"; | ||
import { json } from "./json"; | ||
import { quote, quoted } from "./quote"; | ||
|
||
import { match } from "ts-pattern"; | ||
import { lower_case, pascal_case, space_case } from "./string_cases"; | ||
import { drop_first } from "./drop_first"; | ||
import { split } from "./split"; | ||
import { regex_replace } from "./regex_replace"; | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function to_collection_text( | ||
context: TemplateContext, | ||
value: string, | ||
prefix: string = "", | ||
suffix: string = "", | ||
) { | ||
return `${prefix}${lower_case(context, space_case(context, value))}${suffix}`; | ||
} | ||
|
||
export function to_collection_string(context: TemplateContext, value: String) { | ||
return quoted(context, value); | ||
} | ||
|
||
export function to_collection_name( | ||
context: TemplateContext, | ||
value: string, | ||
partial = false, | ||
) { | ||
value = `${value}`; | ||
const system = context.registry.collections.find((c) => c.name.raw == value) | ||
?.is_system; | ||
let name = pascal_case(context, value); | ||
if (system) { | ||
if (partial) { | ||
name = regex_replace( | ||
context, | ||
pascal_case(context, drop_first(context, split(context, value, "_"))), | ||
"s$", | ||
"", | ||
); | ||
name = name == "Setting" ? "Settings" : name; | ||
} else { | ||
name = regex_replace(context, pascal_case(context, value), "s$", ""); | ||
name = name == "DirectusSetting" ? "DirectusSettings" : name; | ||
} | ||
} | ||
return name; | ||
} | ||
|
||
export function to_ts_type(context: TemplateContext, field: Field) { | ||
if (!field.type.is_data) { | ||
return "never"; | ||
} | ||
|
||
let types: string[] = []; | ||
let schema = field.type; | ||
let meta = field.type.raw.meta; | ||
let nullable = false; | ||
|
||
let db_type = match(field?.type?.database) | ||
.returnType<string | false>() | ||
.with("uuid", () => "UUID") | ||
.with("json", () => "UUID") | ||
.with("text", () => "string") | ||
.with("integer", () => "number") | ||
.with("decimal", () => "number") | ||
.with("numeric", () => "number") | ||
.with("bigint", () => "BigInt") | ||
.with("boolean", () => "boolean") | ||
.with("character varying", () => "string") | ||
.with("date", () => "Date") | ||
.with("time", () => "Date") | ||
.with("time with time zone", () => "Date") | ||
.with("time without time zone", () => "Date") | ||
.with("timestamp", () => "Date") | ||
.with("timestamp with time zone", () => "Date") | ||
.with("timestamp without time zone", () => "Date") | ||
.otherwise(() => false); | ||
|
||
if (db_type) { | ||
types.push(db_type); | ||
} | ||
|
||
let json_type: string | false = false; | ||
if (field.type.is_json) { | ||
if ("json_schema" in schema) { | ||
json_type = '"json_schema"'; | ||
} else { | ||
json_type = "any"; | ||
} | ||
} | ||
|
||
switch (meta.interface) { | ||
case "tags": | ||
types.unshift("string[]"); | ||
break; | ||
case "select-dropdown": | ||
let values = (meta?.options?.choices || []).map((v: any) => | ||
quote(context, v.value), | ||
); | ||
for (let value of values) { | ||
if (value == null) { | ||
nullable = true; | ||
} else { | ||
types.unshift(value); | ||
} | ||
} | ||
json_type = false; | ||
break; | ||
} | ||
|
||
if (schema.raw.schema?.is_nullable) { | ||
// types.push('null') | ||
nullable = true; | ||
} | ||
|
||
if (json_type != false) { | ||
types.unshift(json_type); | ||
} | ||
|
||
if (field.type.is_relationship) { | ||
if ( | ||
field.type.is_special("user-created") || | ||
field.type.is_special("user-updated") | ||
) { | ||
types.push("Collections.DirectusUser"); | ||
} else if (field.type.is_special("file")) { | ||
types.push("Collections.DirectusFile"); | ||
} else if (field.type.is_special("files")) { | ||
types.push("Collections.DirectusFile[]"); | ||
} else if (field.is_translations) { | ||
types.push( | ||
`${to_collection_name(context, field.translations_collection)}[]`, | ||
); | ||
} else { | ||
if (field.type.relationship?.type == "o2m") { | ||
types.push( | ||
to_collection_name(context, field.type.relationship.ref.collection), | ||
); | ||
} | ||
if (field.type.relationship?.type == "m2o") { | ||
types.push( | ||
to_collection_name(context, field.type.relationship.ref.collection), | ||
); | ||
} | ||
if (field.type.relationship?.type == "a2o") { | ||
field.type.relationship.refs.forEach((ref) => { | ||
types.push(to_collection_name(context, ref.collection)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
if (types.length <= 0) { | ||
let schemaStr = json(context, schema); | ||
let metaStr = json(context, meta); | ||
let unknown = `UnknownType<{ schema: ${schemaStr}, meta: ${metaStr} }>`; | ||
types.unshift(unknown); | ||
} | ||
|
||
let output = types.join(" | "); | ||
if (nullable) { | ||
output = `Optional<${output}>`; | ||
} | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export function indent(value: string, size: number) { | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function indent(context: TemplateContext, value: string, size: number) { | ||
const indent = new Array(size + 1).join(" "); | ||
return `${indent}${value.split("\n").join(`\n${indent}`)}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export function log(...values: any[]) { | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function log(context: TemplateContext, ...values: any[]) { | ||
console.log(JSON.stringify({ values })); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
export function object_set(obj: any, key: string, value: any) { | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function object_set( | ||
context: TemplateContext, | ||
obj: any, | ||
key: string, | ||
value: any, | ||
) { | ||
obj[key] = value; | ||
return obj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
export function quote(v: any) { | ||
v = `${v}`; | ||
if (typeof v == "string") { | ||
return JSON.stringify(v); | ||
} else if (Array.isArray(v)) { | ||
return v.map((e) => { | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function quote(context: TemplateContext, value: any) { | ||
value = `${value}`; | ||
if (typeof value == "string") { | ||
return JSON.stringify(value); | ||
} else if (Array.isArray(value)) { | ||
return value.map((e) => { | ||
if (typeof e == "string") { | ||
return JSON.stringify(e); | ||
} else { | ||
return e; | ||
} | ||
}); | ||
} | ||
return v; | ||
return value; | ||
} | ||
|
||
export const quoted = quote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export function split(value: string, splitter: string) { | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function split( | ||
context: TemplateContext, | ||
value: string, | ||
splitter: string, | ||
) { | ||
return `${value}`.split(splitter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { TemplateContext } from "../../../types/template"; | ||
|
||
export function to_ts_identifier(context: TemplateContext, identifier: string) { | ||
identifier = `${identifier}`; | ||
if (/^[_a-z]\w*$/i.test(identifier)) { | ||
return identifier; | ||
} else { | ||
return `["${identifier.replace('"', '\\"')}"]`; | ||
} | ||
} |
Oops, something went wrong.