@sealcode/sealgen 0.18.2 → 0.18.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.
@@ -0,0 +1,3 @@
1
+ export declare function crudFieldSnippets(params: Partial<{
2
+ [key in "collection" | "url"]: string;
3
+ }>, app_directory?: string): Promise<void>;
@@ -34,7 +34,7 @@ export declare abstract class StatefulPage<State extends Record<string, unknown>
34
34
  abstract actions: Actions;
35
35
  abstract getInitialState(ctx: Context): State | Promise<State>;
36
36
  abstract render(ctx: Context, state: State, inputs: Record<string, string>): Templatable | Promise<Templatable>;
37
- canAccess(): Promise<{
37
+ canAccess(_ctx: Context): Promise<{
38
38
  canAccess: boolean;
39
39
  message: string;
40
40
  }>;
@@ -0,0 +1,2 @@
1
+ import { FieldHandler, FieldHandlerResult } from "../shared-crud-form-fields.js";
2
+ export declare const settable_by_handler: FieldHandler<FieldHandlerResult, Partial<FieldHandlerResult>>;
@@ -30,7 +30,7 @@ export type FieldHandlerResult = {
30
30
  fallback_value_sealious: string;
31
31
  main_value_sealious: string;
32
32
  };
33
- export type FieldHandler<Defaults extends Partial<FieldHandlerResult>, Result extends Partial<FieldHandlerResult>> = (collection_name: string, field: ExtractedFieldInfo, importPath: (path: string) => string, defaults: Defaults, fields_var?: string) => Promise<Result>;
33
+ export type FieldHandler<Defaults extends Partial<FieldHandlerResult>, Result extends Partial<FieldHandlerResult>> = (collection_name: string, field: ExtractedFieldInfo, importPath: (path: string) => string, defaults: Defaults, all_other_handlers: Record<string, FieldHandler<FieldHandlerResult, Partial<FieldHandlerResult>> | undefined>, fields_var?: string) => Promise<Result>;
34
34
  export declare const field_handlers: Record<Exclude<string, "default">, FieldHandler<FieldHandlerResult, Partial<FieldHandlerResult>> | undefined> & {
35
35
  default: FieldHandler<Record<string, never>, FieldHandlerResult>;
36
36
  };
package/lib/cli.js CHANGED
@@ -1,16 +1,17 @@
1
1
  #!/usr/bin/env node
2
+ import yargs from "yargs/yargs";
2
3
  import { addCollection } from "./add-collection.js";
4
+ import { addCRUD } from "./add-crud.js";
3
5
  import { addJDDComponent } from "./add-jdd-component.js";
4
6
  import { addRoute } from "./add-route.js";
7
+ import { addVerboseCRUD } from "./add-verbose-crud.js";
5
8
  import { buildProject } from "./build.js";
6
9
  import { generateCollections } from "./generate-collections.js";
7
10
  import { generateComponents } from "./generate-components.js";
8
11
  import { generateRoutes } from "./generate-routes.js";
9
- import { makeEnv } from "./make-env.js";
10
- import yargs from "yargs/yargs";
11
12
  import { getFonts } from "./get-fonts.js";
12
- import { addVerboseCRUD } from "./add-verbose-crud.js";
13
- import { addCRUD } from "./add-crud.js";
13
+ import { makeEnv } from "./make-env.js";
14
+ import { crudFieldSnippets } from "./crud-field-snippets.js";
14
15
  const actions = {
15
16
  "add-collection": addCollection,
16
17
  "add-route": addRoute,
@@ -28,7 +29,8 @@ const actions = {
28
29
  },
29
30
  "make-env": makeEnv,
30
31
  "add-component": addJDDComponent,
31
- "get-fonts": getFonts
32
+ "get-fonts": getFonts,
33
+ "crud-field-snippets": crudFieldSnippets
32
34
  };
33
35
  void async function() {
34
36
  const action = process.argv.at(2);
@@ -0,0 +1,52 @@
1
+ import _locreq from "locreq";
2
+ import prompts from "prompts";
3
+ import { getFieldHandler } from "./templates/shared/shared-crud-form-fields.js";
4
+ import extract_fields_from_collection from "./utils/extract-fields-from-collection.js";
5
+ import { listCollections } from "./utils/list-collections.js";
6
+ import { formatWithPrettier } from "./utils/prettier.js";
7
+ async function crudFieldSnippets(params, app_directory = process.cwd()) {
8
+ const target_locreq = _locreq(app_directory);
9
+ prompts.override(params);
10
+ const { collection_name } = await prompts([
11
+ {
12
+ type: "autocomplete",
13
+ name: "collection_name",
14
+ message: "Which sealious collection do you like to show a CRUD field config for?",
15
+ choices: (await listCollections()).map((collection) => ({
16
+ title: collection,
17
+ value: collection
18
+ }))
19
+ }
20
+ ]);
21
+ const all_fields = await extract_fields_from_collection(collection_name);
22
+ const { field_name } = await prompts([
23
+ {
24
+ type: "autocomplete",
25
+ name: "field_name",
26
+ message: `Which field do you want to show CRUD config for??`,
27
+ choices: all_fields.map((field) => ({
28
+ title: field.name,
29
+ value: field.name
30
+ }))
31
+ }
32
+ ]);
33
+ const handler = await getFieldHandler(
34
+ collection_name,
35
+ all_fields.find((e) => e.name == field_name),
36
+ (p) => p
37
+ );
38
+ console.log(`
39
+
40
+ ### Field in shared.ts
41
+
42
+ ${handler.field}
43
+
44
+ ### Control in shared.ts
45
+
46
+ ${await formatWithPrettier(handler.controls)}
47
+ `);
48
+ }
49
+ export {
50
+ crudFieldSnippets
51
+ };
52
+ //# sourceMappingURL=crud-field-snippets.js.map
@@ -5,7 +5,7 @@ import { from_base64, to_base64 } from "../utils/base64.js";
5
5
  import { Mountable } from "./mountable.js";
6
6
  import { isPlainObject } from "is-what";
7
7
  class StatefulPage extends Mountable {
8
- async canAccess() {
8
+ async canAccess(_ctx) {
9
9
  return { canAccess: true, message: "" };
10
10
  }
11
11
  constructor() {
@@ -1,5 +1,5 @@
1
1
  import { toPascalCase } from "js-convert-case";
2
- const default_handler = async (collection_name, { name, is_readonly, is_required }, _, __, fields_var = `${toPascalCase(collection_name)}FormFields`) => {
2
+ const default_handler = async (collection_name, { name, is_readonly, is_required }, _, __, ___, fields_var = `${toPascalCase(collection_name)}FormFields`) => {
3
3
  const required_s = `${toPascalCase(
4
4
  collection_name
5
5
  )}.fields.${name}.required`;
@@ -36,6 +36,7 @@ async function getFieldHandler(collection_name, field, importPath, fields_var) {
36
36
  field,
37
37
  importPath,
38
38
  {},
39
+ field_handlers,
39
40
  fields_var
40
41
  );
41
42
  const result = {
@@ -45,6 +46,7 @@ async function getFieldHandler(collection_name, field, importPath, fields_var) {
45
46
  field,
46
47
  importPath,
47
48
  defaults,
49
+ field_handlers,
48
50
  fields_var
49
51
  ) || {}
50
52
  };
@@ -14,7 +14,7 @@ async function extract_fields_from_collection(collection_name) {
14
14
  const c = new the_app().collections["${collection_name}"];
15
15
  const fields = [];
16
16
  for (const field_name in c.fields){
17
- const field = c.fields[field_name];
17
+ let field = c.fields[field_name];
18
18
  let type = field.typeName;
19
19
  let target = null;
20
20
  let referencing_collection = null;
@@ -23,7 +23,7 @@ for (const field_name in c.fields){
23
23
  let is_readonly = false;
24
24
  let is_required = field.required;
25
25
  let subfields = [];
26
- if(["derived-value", "cached-value"].includes(type)){
26
+ if(["derived-value", "cached-value", "settable-by"].includes(type)){
27
27
  type = field.virtual_field.typeName;
28
28
  }
29
29
  if(["derived-value", "cached-value", "reverse-single-reference"].includes(field.typeName)){
@@ -36,7 +36,7 @@ for (const field_name in c.fields){
36
36
  intermediary_field_that_points_there = field.intermediary_field_that_points_there
37
37
  }
38
38
  if( type == "structured-array" ) {
39
- subfields = Object.keys(field.subfields).map(key=>({name: key}));
39
+ subfields = Object.keys(field.subfields || field.virtual_field.subfields).map(key=>({name: key}));
40
40
  }
41
41
  fields.push({ name: field_name, type, target, referencing_collection, referencing_field, intermediary_field_that_points_there, is_readonly, is_required, subfields })
42
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
package/src/cli.ts CHANGED
@@ -1,16 +1,17 @@
1
1
  #!/usr/bin/env node
2
+ import yargs from "yargs/yargs";
2
3
  import { addCollection } from "./add-collection.js";
4
+ import { addCRUD } from "./add-crud.js";
3
5
  import { addJDDComponent } from "./add-jdd-component.js";
4
6
  import { addRoute } from "./add-route.js";
7
+ import { addVerboseCRUD } from "./add-verbose-crud.js";
5
8
  import { buildProject } from "./build.js";
6
9
  import { generateCollections } from "./generate-collections.js";
7
10
  import { generateComponents } from "./generate-components.js";
8
11
  import { generateRoutes } from "./generate-routes.js";
9
- import { makeEnv } from "./make-env.js";
10
- import yargs from "yargs/yargs";
11
12
  import { getFonts } from "./get-fonts.js";
12
- import { addVerboseCRUD } from "./add-verbose-crud.js";
13
- import { addCRUD } from "./add-crud.js";
13
+ import { makeEnv } from "./make-env.js";
14
+ import { crudFieldSnippets } from "./crud-field-snippets.js";
14
15
 
15
16
  const actions: Record<
16
17
  string,
@@ -35,6 +36,7 @@ const actions: Record<
35
36
  "make-env": makeEnv,
36
37
  "add-component": addJDDComponent,
37
38
  "get-fonts": getFonts,
39
+ "crud-field-snippets": crudFieldSnippets,
38
40
  };
39
41
 
40
42
  void (async function () {
@@ -0,0 +1,57 @@
1
+ import _locreq from "locreq";
2
+ import prompts from "prompts";
3
+ import { getFieldHandler } from "./templates/shared/shared-crud-form-fields.js";
4
+ import extract_fields_from_collection from "./utils/extract-fields-from-collection.js";
5
+ import { listCollections } from "./utils/list-collections.js";
6
+ import { formatWithPrettier } from "./utils/prettier.js";
7
+
8
+ export async function crudFieldSnippets(
9
+ params: Partial<{ [key in "collection" | "url"]: string }>,
10
+ app_directory: string = process.cwd()
11
+ ) {
12
+ const target_locreq = _locreq(app_directory);
13
+ prompts.override(params);
14
+ const { collection_name } = (await prompts([
15
+ {
16
+ type: "autocomplete",
17
+ name: "collection_name",
18
+ message:
19
+ "Which sealious collection do you like to show a CRUD field config for?",
20
+ choices: (
21
+ await listCollections()
22
+ ).map((collection) => ({
23
+ title: collection,
24
+ value: collection,
25
+ })),
26
+ },
27
+ ])) as { collection_name: string };
28
+ const all_fields = await extract_fields_from_collection(collection_name);
29
+ const { field_name } = (await prompts([
30
+ {
31
+ type: "autocomplete",
32
+ name: "field_name",
33
+ message: `Which field do you want to show CRUD config for??`,
34
+ choices: all_fields.map((field) => ({
35
+ title: field.name,
36
+ value: field.name,
37
+ })),
38
+ },
39
+ ])) as { field_name: string };
40
+
41
+ const handler = await getFieldHandler(
42
+ collection_name,
43
+ all_fields.find((e) => e.name == field_name)!,
44
+ (p) => p
45
+ );
46
+
47
+ console.log(`
48
+
49
+ ### Field in shared.ts
50
+
51
+ ${handler.field}
52
+
53
+ ### Control in shared.ts
54
+
55
+ ${await formatWithPrettier(handler.controls)}
56
+ `);
57
+ }
@@ -1,6 +1,3 @@
1
- import { promises as fs } from "fs";
2
1
  import _locreq from "locreq";
3
- import { relative } from "path";
4
- import { walkDir } from "./utils/walk.js";
5
2
 
6
3
  const target_locreq = _locreq(process.cwd());
@@ -67,7 +67,9 @@ export abstract class StatefulPage<
67
67
  inputs: Record<string, string>
68
68
  ): Templatable | Promise<Templatable>;
69
69
 
70
- async canAccess(): Promise<{ canAccess: boolean; message: string }> {
70
+ async canAccess(
71
+ _ctx: Context
72
+ ): Promise<{ canAccess: boolean; message: string }> {
71
73
  return <const>{ canAccess: true, message: "" };
72
74
  }
73
75
 
@@ -12,6 +12,7 @@ export const default_handler: FieldHandler<
12
12
  { name, is_readonly, is_required },
13
13
  _,
14
14
  __,
15
+ ___,
15
16
  fields_var = `${toPascalCase(collection_name)}FormFields`
16
17
  ) => {
17
18
  const required_s = `${toPascalCase(
@@ -61,6 +61,11 @@ export type FieldHandler<
61
61
  field: ExtractedFieldInfo,
62
62
  importPath: (path: string) => string,
63
63
  defaults: Defaults,
64
+ all_other_handlers: Record<
65
+ string,
66
+ | FieldHandler<FieldHandlerResult, Partial<FieldHandlerResult>>
67
+ | undefined
68
+ >,
64
69
  fields_var?: string
65
70
  ) => Promise<Result>;
66
71
 
@@ -95,8 +100,10 @@ export async function getFieldHandler(
95
100
  field,
96
101
  importPath,
97
102
  {},
103
+ field_handlers,
98
104
  fields_var
99
105
  );
106
+
100
107
  const result = {
101
108
  ...defaults,
102
109
  ...((await field_handlers[field.type]?.(
@@ -104,6 +111,7 @@ export async function getFieldHandler(
104
111
  field,
105
112
  importPath,
106
113
  defaults,
114
+ field_handlers,
107
115
  fields_var
108
116
  )) || {}),
109
117
  };
@@ -30,7 +30,7 @@ export default async function extract_fields_from_collection(
30
30
  const c = new the_app().collections["${collection_name}"];
31
31
  const fields = [];
32
32
  for (const field_name in c.fields){
33
- const field = c.fields[field_name];
33
+ let field = c.fields[field_name];
34
34
  let type = field.typeName;
35
35
  let target = null;
36
36
  let referencing_collection = null;
@@ -39,7 +39,7 @@ for (const field_name in c.fields){
39
39
  let is_readonly = false;
40
40
  let is_required = field.required;
41
41
  let subfields = [];
42
- if(["derived-value", "cached-value"].includes(type)){
42
+ if(["derived-value", "cached-value", "settable-by"].includes(type)){
43
43
  type = field.virtual_field.typeName;
44
44
  }
45
45
  if(["derived-value", "cached-value", "reverse-single-reference"].includes(field.typeName)){
@@ -52,7 +52,7 @@ for (const field_name in c.fields){
52
52
  intermediary_field_that_points_there = field.intermediary_field_that_points_there
53
53
  }
54
54
  if( type == "structured-array" ) {
55
- subfields = Object.keys(field.subfields).map(key=>({name: key}));
55
+ subfields = Object.keys(field.subfields || field.virtual_field.subfields).map(key=>({name: key}));
56
56
  }
57
57
  fields.push({ name: field_name, type, target, referencing_collection, referencing_field, intermediary_field_that_points_there, is_readonly, is_required, subfields })
58
58
  }