@appling/codegen-ash 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/out/tests.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import type { MapContext, Step } from '@appling/codegen';
2
2
  import type { GraphNode } from '@appling/model';
3
+ /** A test case: its name, and its body when the model says enough to write it. */
4
+ export interface TestCase {
5
+ name: string;
6
+ /** The body lines, unindented; undefined leaves the test skipped as a registry task. */
7
+ body?: string[];
8
+ }
3
9
  /**
4
- * Skipped ExUnit tests for a rule in the test module of its resource, one per case. Bodies are registry
5
- * tasks; each test points at its entry.
10
+ * ExUnit tests for a rule in the test module of its resource, one per case. A case with a body runs; one
11
+ * without is skipped with a pointer to its registry entry, which gets a task to write the missing bodies.
6
12
  */
7
- export declare function testSteps(node: GraphNode, resource: GraphNode, title: string, cases: string[], context: MapContext): Step[];
13
+ export declare function testSteps(node: GraphNode, resource: GraphNode, title: string, cases: (string | TestCase)[], context: MapContext): Step[];
package/out/tests.js CHANGED
@@ -1,19 +1,23 @@
1
1
  import { lastOf } from './elixir.js';
2
2
  import { app, snake, task, typeModule } from './naming.js';
3
3
  /**
4
- * Skipped ExUnit tests for a rule in the test module of its resource, one per case. Bodies are registry
5
- * tasks; each test points at its entry.
4
+ * ExUnit tests for a rule in the test module of its resource, one per case. A case with a body runs; one
5
+ * without is skipped with a pointer to its registry entry, which gets a task to write the missing bodies.
6
6
  */
7
7
  export function testSteps(node, resource, title, cases, context) {
8
8
  const module = `${typeModule(resource, context)}Test`;
9
9
  const file = ['test', app(context), ...resource.module.split('.').map(snake), `${snake(lastOf(resource.qn))}_test.exs`].join('/');
10
- const tests = cases.map(c => ` @tag skip: ${JSON.stringify(`registry task: ${node.qn}`)}\n test ${JSON.stringify(`${title} ${c}`)}\n`).join('\n');
10
+ const all = cases.map(c => typeof c === 'string' ? { name: c } : c);
11
+ const tests = all.map(c => c.body
12
+ ? ` test ${JSON.stringify(`${title} ${c.name}`)} do\n${c.body.map(l => ` ${l}\n`).join('')} end\n`
13
+ : ` @tag skip: ${JSON.stringify(`registry task: ${node.qn}`)}\n test ${JSON.stringify(`${title} ${c.name}`)}\n`).join('\n');
14
+ const missing = all.filter(c => !c.body);
11
15
  return [
12
16
  {
13
17
  kind: 'template', file, write: existing => existing
14
18
  ? existing.replace(/end\n?$/, `\n${tests}end\n`)
15
19
  : `defmodule ${module} do\n use ExUnit.Case, async: true\n\n${tests}end\n`
16
20
  },
17
- task(`write the tests "${title}" in ${file}`)
21
+ ...(missing.length ? [task(`write the tests "${title}" in ${file}${missing.length < all.length ? ` (${missing.map(c => `"${title} ${c.name}"`).join(', ')})` : ''}`)] : [])
18
22
  ];
19
23
  }
@@ -0,0 +1,19 @@
1
+ import type { MapContext, Step } from '@appling/codegen';
2
+ import type { GraphNode } from '@appling/model';
3
+ /** The environment variable of an external's key: `<EXTERNAL>_<KEY>` in SCREAMING_SNAKE (`CLERK_SECRET_KEY`). */
4
+ export declare function envName(key: GraphNode): string;
5
+ /**
6
+ * A setting or secret is read from the environment in `config/runtime.exs`, under the external's
7
+ * module (`Application.get_env(:shop, Shop.Sales.Clerk)[:secret_key]`), and listed in `.env.example`.
8
+ * A missing setting is `nil` (`false` for a `Bool`); a missing secret stops the app at startup (`System.fetch_env!`).
9
+ */
10
+ export declare function externalKeySteps(key: GraphNode, context: MapContext): Step[];
11
+ /**
12
+ * Placeholder values for every secret, for the generator's own commands that load the settings
13
+ * (which stop without them). Empty when the model has no secrets.
14
+ */
15
+ export declare function secretPlaceholders(context: MapContext): {
16
+ env?: Record<string, string>;
17
+ };
18
+ /** The vendor side of an interface: the webhook endpoints to register, the tokens to issue for `auth ... from`. */
19
+ export declare function vendorTasks(api: GraphNode, context: MapContext): Step[];
package/out/vendors.js ADDED
@@ -0,0 +1,79 @@
1
+ import { lastOf } from './elixir.js';
2
+ import { app, children, leftToCode, moduleName, snake } from './naming.js';
3
+ /** The environment variable of an external's key: `<EXTERNAL>_<KEY>` in SCREAMING_SNAKE (`CLERK_SECRET_KEY`). */
4
+ export function envName(key) {
5
+ return `${snake(externalOf(key))}_${snake(lastOf(key.qn))}`.toUpperCase();
6
+ }
7
+ function externalOf(key) {
8
+ return lastOf(key.qn.slice(0, key.qn.lastIndexOf('.')));
9
+ }
10
+ /**
11
+ * A setting or secret is read from the environment in `config/runtime.exs`, under the external's
12
+ * module (`Application.get_env(:shop, Shop.Sales.Clerk)[:secret_key]`), and listed in `.env.example`.
13
+ * A missing setting is `nil` (`false` for a `Bool`); a missing secret stops the app at startup (`System.fetch_env!`).
14
+ */
15
+ export function externalKeySteps(key, context) {
16
+ const name = envName(key);
17
+ const line = `config :${app(context)}, ${moduleName(key.module, context)}.${externalOf(key)}, ${lastOf(key.qn)}: ${elixirRead(name, key.data.type, key.data.kind === 'secret')}`;
18
+ return [
19
+ {
20
+ kind: 'template', phase: 2, file: 'config/runtime.exs', write: existing => {
21
+ const text = existing ?? 'import Config\n';
22
+ return text.includes(line) ? text : `${text.trimEnd()}\n\n${line}\n`;
23
+ }
24
+ },
25
+ {
26
+ kind: 'template', phase: 2, file: '.env.example', write: existing => {
27
+ if (existing?.split('\n').includes(`${name}=`))
28
+ return existing;
29
+ const head = existing ?? '# The settings and secrets of the vendors this app needs. Copy to .env and fill in; never commit values.\n';
30
+ const about = `# ${key.data.kind} of ${externalOf(key)}${key.data.does ? `: ${String(key.data.does).replace(/\s+/g, ' ').trim()}` : ''}`;
31
+ return `${head.trimEnd()}\n\n${about}\n${name}=\n`;
32
+ }
33
+ }
34
+ ];
35
+ }
36
+ function elixirRead(name, type, required) {
37
+ if (required) {
38
+ const fetch = `System.fetch_env!(${JSON.stringify(name)})`;
39
+ switch (type.primitive) {
40
+ case 'Int': return `${fetch} |> String.to_integer()`;
41
+ case 'Bool': return `${fetch} in ~w(1 true yes)`;
42
+ default: return fetch;
43
+ }
44
+ }
45
+ const get = `System.get_env(${JSON.stringify(name)})`;
46
+ switch (type.primitive) {
47
+ case 'Int': return `${get} |> then(&(&1 && String.to_integer(&1)))`;
48
+ case 'Bool': return `${get} in ~w(1 true yes)`;
49
+ default: return get;
50
+ }
51
+ }
52
+ /**
53
+ * Placeholder values for every secret, for the generator's own commands that load the settings
54
+ * (which stop without them). Empty when the model has no secrets.
55
+ */
56
+ export function secretPlaceholders(context) {
57
+ const secrets = context.graph.nodes.filter(n => n.kind === 'ExternalKey' && n.data.kind === 'secret');
58
+ if (!secrets.length)
59
+ return {};
60
+ return { env: Object.fromEntries(secrets.map(key => [envName(key), key.data.type.primitive === 'Int' ? '0' : 'placeholder'])) };
61
+ }
62
+ /** The vendor side of an interface: the webhook endpoints to register, the tokens to issue for `auth ... from`. */
63
+ export function vendorTasks(api, context) {
64
+ const tasks = [];
65
+ const name = lastOf(api.qn);
66
+ if (api.data.protocol === 'webhook') {
67
+ const vendor = api.data.source ? lastOf(api.data.source) : 'the vendor';
68
+ for (const endpoint of children(api, context, 'Endpoint')) {
69
+ const target = lastOf(endpoint.data.target);
70
+ tasks.push(leftToCode(`register ${endpoint.data.verb} ${endpoint.data.path} as a webhook at ${vendor} for the events ${target} handles`));
71
+ tasks.push(leftToCode(`receive ${endpoint.data.verb} ${endpoint.data.path} from ${vendor} for ${name}: verify the signature, then call ${target}`));
72
+ }
73
+ }
74
+ if (api.data.authBy) {
75
+ const roles = context.graph.nodes.filter(n => n.kind === 'Role').map(r => lastOf(r.qn));
76
+ tasks.push(leftToCode(`set up ${lastOf(api.data.authBy)} to issue ${api.data.auth} tokens for ${name}${roles.length ? ` with a role claim naming ${roles.join(', ')}` : ''}`));
77
+ }
78
+ return tasks;
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appling/codegen-ash",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Appling code generation extension for Ash",
5
5
  "homepage": "https://www.appling.tools/docs/",
6
6
  "repository": {
@@ -19,13 +19,13 @@
19
19
  "out"
20
20
  ],
21
21
  "dependencies": {
22
- "@appling/model": "0.1.2"
22
+ "@appling/model": "0.1.3"
23
23
  },
24
24
  "peerDependencies": {
25
- "@appling/codegen": "0.1.2"
25
+ "@appling/codegen": "0.1.3"
26
26
  },
27
27
  "devDependencies": {
28
- "@appling/codegen": "0.1.2",
28
+ "@appling/codegen": "0.1.3",
29
29
  "@types/node": "^24.19.2",
30
30
  "typescript": "7.0.2"
31
31
  },