Skip to content

Commit

Permalink
fix typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLfulus committed Sep 23, 2023
1 parent 7f9c8b0 commit 32a5fcb
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 20 deletions.
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.2.1

### Patch Changes

- fix type issue with empty types

## 0.1.0

### Minor Changes
Expand Down
20 changes: 18 additions & 2 deletions packages/cli/default/extensions/filters/directus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Field } from "../../../types/registry";
import { Collection, Field } from "../../../types/registry";

import { contains } from "./contains";
import { json } from "./json";
import { quote, quoted } from "./quote";

Expand Down Expand Up @@ -168,3 +167,20 @@ export function to_ts_type(context: TemplateContext, field: Field) {

return output;
}

export function only_system_fields(context: TemplateContext, fields: Field[]) {
return fields.filter((field) => field.is_system);
}

export function only_custom_fields(context: TemplateContext, fields: Field[]) {
return fields.filter((field) => !field.is_system);
}

export function only_with_custom_fields(
context: TemplateContext,
collections: Collection[],
) {
return collections.filter(
(field) => field.fields.filter((field) => !field.is_system).length > 0,
);
}
37 changes: 23 additions & 14 deletions packages/cli/default/templates/default/client.ts.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{%- import "macro:typescript/types" as ts -%}
{% block header %}
/**
* This file is automatically generated by the `@indirectus/sdk` package.
* This file is automatically generated by the `@indirectus/cli` package.
* Follow the package's instruction to update this file with the latest schema.
*/
{% endblock %}
Expand Down Expand Up @@ -32,20 +31,27 @@ export type BigInt = number;
export namespace Collections {
{% for collection in registry.collections -%}
{% if collection.is_system %}

{%- set field_count = collection.fields | only_custom_fields | length -%}
{%- if field_count > 0 -%}
{% filter comment -%}
The {{ collection.name | to_collection_text }} custom fields.
The extra fields added to the {{ collection.name | to_collection_text }} collection.
{% endfilter %}
export interface {{ collection.name | to_collection_name }} extends Directus.{{ collection.name | to_collection_name }}<Schema> {
{%- for field in collection.fields %}
{% if not collection.is_system %}
{%- set type = field | to_ts_type -%}
{%- if type != 'never' %}
{{ field.name | to_ts_identifier }}: {{ type }};
{%- endif -%}
{% endif %}
{%- endfor %}
export interface Extra{{ collection.name | to_collection_name }} {
{%- for field in collection.fields | only_custom_fields %}
{%- set type = field | to_ts_type -%}
{%- if type != 'never' %}
{{ field.name | to_ts_identifier }}: {{ type }};
{%- endif -%}
{%- endfor %}
};
{% endif %}

{% filter comment -%}
The resolved {{ collection.name | to_collection_text }} collection type.
{% endfilter %}
export type {{ collection.name | to_collection_name }} = Directus.{{ collection.name | to_collection_name }}<Schema>;
{% endif %}
{% endfor %}
}

Expand Down Expand Up @@ -78,13 +84,15 @@ export namespace Collections {
*/
export interface Schema {

{% for collection in registry.collections -%}
{% for collection in registry.collections | only_with_custom_fields -%}
{% if collection.is_system %}
{% set suffix = "" if collection.is_singleton else "[]" %}

{%- set field_count = collection.fields | only_custom_fields | length -%}
/**
* The extended {{ collection.name | to_collection_text }} collection.
*/
{{ collection.name | to_ts_identifier }}: Collections.{{ collection.name | to_collection_name }}{{ suffix }};
{{ collection.name | to_ts_identifier }}: Collections.Extra{{ collection.name | to_collection_name }}{{ suffix }};
{% endif %}
{% endfor %}

Expand Down Expand Up @@ -150,3 +158,4 @@ export function read{{ collection.name | to_collection_name }}<

{% endif %}
{% endfor %}

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indirectus/cli",
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"description": "Directus Tools CLI.",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class Generator extends TypedEventEmitter<GeneratorEvents> {
let context: string[] = [];

if (typeof err != "string" && err != null) {
message = err?.message
message = (err?.message || "")
.replace(/, file:/g, "\n")
.replace(/, line:/g, ":")
.replace(/, col:/g, ":");
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/types/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Type {
}

public get is_system() {
return "system" in this.raw.meta && this.raw.meta.system;
return "system" in this.raw.meta && !!this.raw.meta.system;
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/indirectus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# indirectus

## 0.2.1

### Patch Changes

- Updated dependencies
- @indirectus/cli@0.2.1

## 0.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/indirectus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "indirectus",
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"description": "Directus Tools CLI.",
"keywords": [
Expand Down

0 comments on commit 32a5fcb

Please sign in to comment.