Skip to content

Commit

Permalink
refactor templates
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLfulus committed Sep 22, 2023
1 parent 78f4573 commit cded4e9
Show file tree
Hide file tree
Showing 34 changed files with 380 additions and 195 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
"name": "@indirectus/workspace",
"version": "0.0.0",
"private": true,
"scripts": {
"ci:build": "pnpm -r build",
"ci:version": "pnpm changeset version",
"ci:publish": "pnpm -r publish --access public",
"ci": "pnpm ci:build && pnpm ci:version && pnpm ci:publish"
},
"devDependencies": {
"@types/node": "^20.6.3",
"typescript": "^5.2.2",
"vitest": "^0.34.5"
},
"dependencies": {
"@changesets/cli": "^2.26.2"
}
},
"files": []
}
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
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
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/cli/sdk/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export default defineCommand({
console.error(err);
});

generator.on("file.format.error", async (file, err) => {
console.error("Error formatting file", err);
});

generator.on("generation.begin", async () =>
console.start("Starting generation"),
);
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/comment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function comment(str: Array<any> | string) {
import type { TemplateContext } from "../../../types/template";

export function comment(context: TemplateContext, str: Array<any> | string) {
if (Array.isArray(str)) {
str = str.join("\n").replace(/(^\n*)|(\n*$)/gi, "");
}
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/contains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function contains(arr: Array<any>, value: any) {
import type { TemplateContext } from "../../../types/template";

export function contains(context: TemplateContext, arr: any, value: any) {
if (!Array.isArray(arr)) {
return false;
}
Expand Down
170 changes: 170 additions & 0 deletions packages/cli/default/extensions/filters/directus.ts
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;
}
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/drop_first.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function drop_first(arr: Array<any>) {
import type { TemplateContext } from "../../../types/template";

export function drop_first(context: TemplateContext, arr: Array<any>) {
if (Array.isArray(arr)) {
arr.shift();
}
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/default/extensions/filters/entries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export function entries(obj: any, keyName?: string, valueName?: string) {
import type { TemplateContext } from "../../../types/template";

export function entries(
context: TemplateContext,
obj: any,
keyName?: string,
valueName?: string,
) {
return Object.entries(obj).map(([key, value]) => ({
[keyName || "key"]: key,
[valueName || "value"]: value,
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/default/extensions/filters/falsey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export function falsey(condition: boolean, falsey: any, truthy: any) {
import type { TemplateContext } from "../../../types/template";

export function falsey(
context: TemplateContext,
condition: boolean,
falsey: any,
truthy: any,
) {
if (!condition) {
return falsey;
} else {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/indent.ts
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}`)}`;
}
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function json(v: any) {
import type { TemplateContext } from "../../../types/template";

export function json(context: TemplateContext, v: any) {
if (typeof v == "undefined") {
return "undefined";
} else if (v === null) {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/log.ts
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 }));
}
9 changes: 8 additions & 1 deletion packages/cli/default/extensions/filters/object_set.ts
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;
}
4 changes: 3 additions & 1 deletion packages/cli/default/extensions/filters/push.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function push(arr: Array<any>, value: any) {
import type { TemplateContext } from "../../../types/template";

export function push(context: TemplateContext, arr: Array<any>, value: any) {
if (Array.isArray(value)) {
arr.push(...value);
} else {
Expand Down
16 changes: 9 additions & 7 deletions packages/cli/default/extensions/filters/quote.ts
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;
3 changes: 3 additions & 0 deletions packages/cli/default/extensions/filters/regex_replace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { TemplateContext } from "../../../types/template";

export function regex_replace(
context: TemplateContext,
value: string,
regex: string,
replacement: string,
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/default/extensions/filters/splice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { TemplateContext } from "../../../types/template";

export function splice(
context: TemplateContext,
arr: Array<any>,
start: number,
count: number | undefined = undefined,
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/default/extensions/filters/split.ts
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);
}
3 changes: 2 additions & 1 deletion packages/cli/default/extensions/filters/string_cases.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as cc from "@wolfpkgs/core/strings";
import type { TemplateContext } from "../../../types/template";

function stringifyFirst(fn: (...args: any[]) => any) {
return (first: any, ...args: any[]) => {
return (context: TemplateContext, first: any, ...args: any[]) => {
return fn(`${first}`, ...args).toString();
};
}
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/default/extensions/filters/truthy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export function truthy(condition: boolean, truthy: any, falsey: any) {
import type { TemplateContext } from "../../../types/template";

export function truthy(
context: TemplateContext,
condition: boolean,
truthy: any,
falsey: any,
) {
if (condition) {
return truthy;
} else {
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/default/extensions/filters/typescript.ts
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('"', '\\"')}"]`;
}
}
Loading

0 comments on commit cded4e9

Please sign in to comment.