@tagma/sdk 0.4.7 → 0.4.8

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/src/templates.ts DELETED
@@ -1,97 +0,0 @@
1
- // ═══ Template Discovery (F1) ═══
2
- //
3
- // Public helpers so editors / UIs can enumerate installed `@tagma/template-*`
4
- // packages in a workspace and read each template's declarative metadata
5
- // (name, description, params) without actually expanding the template.
6
- //
7
- // The legacy private `loadTemplate` in schema.ts uses Bun-specific APIs
8
- // (Bun.file, require.resolve). These helpers are Node-compatible because
9
- // the editor server runs on Node, not Bun.
10
-
11
- import { readFileSync, existsSync, readdirSync, statSync } from 'fs';
12
- import { join } from 'path';
13
- import yaml from 'js-yaml';
14
- import type { TemplateConfig } from './types';
15
-
16
- export interface TemplateManifest extends TemplateConfig {
17
- /** The package ref as it would appear in `task.use`, e.g. `@tagma/template-review`. */
18
- readonly ref: string;
19
- }
20
-
21
- /**
22
- * Scan the workspace's `node_modules/@tagma/*` for packages whose name starts
23
- * with `template-` and load each one's manifest. Packages without a valid
24
- * `template.yaml` (or that fail to parse) are silently skipped.
25
- *
26
- * Returns an empty array when `workDir` doesn't exist or has no such packages.
27
- */
28
- export function discoverTemplates(workDir: string): TemplateManifest[] {
29
- const out: TemplateManifest[] = [];
30
- const scopeDir = join(workDir, 'node_modules', '@tagma');
31
- if (!existsSync(scopeDir)) return out;
32
-
33
- let entries: string[] = [];
34
- try {
35
- entries = readdirSync(scopeDir);
36
- } catch {
37
- return out;
38
- }
39
-
40
- for (const entry of entries) {
41
- if (!entry.startsWith('template-')) continue;
42
- const pkgDir = join(scopeDir, entry);
43
- try {
44
- const st = statSync(pkgDir);
45
- if (!st.isDirectory()) continue;
46
- } catch {
47
- continue;
48
- }
49
-
50
- const ref = `@tagma/${entry}`;
51
- const manifest = loadTemplateManifestFromDir(pkgDir, ref);
52
- if (manifest) out.push(manifest);
53
- }
54
-
55
- // Sort alphabetically for deterministic UI rendering.
56
- out.sort((a, b) => a.ref.localeCompare(b.ref));
57
- return out;
58
- }
59
-
60
- /**
61
- * Load a single template's manifest by its ref (e.g. `@tagma/template-review`)
62
- * from the given workspace's `node_modules`. Returns `null` if the package
63
- * isn't installed or its manifest can't be parsed.
64
- */
65
- export function loadTemplateManifest(ref: string, workDir: string): TemplateManifest | null {
66
- // Only @tagma/template-* refs are supported (matches SDK validateTemplateRef).
67
- const stripped = ref.replace(/@v\d+$/, '');
68
- if (!stripped.startsWith('@tagma/template-')) return null;
69
- const pkgDir = join(workDir, 'node_modules', stripped);
70
- if (!existsSync(pkgDir)) return null;
71
- return loadTemplateManifestFromDir(pkgDir, stripped);
72
- }
73
-
74
- /**
75
- * Resolve a template manifest from an absolute package directory. Tries
76
- * `template.yaml` first (the documented convention), then a `template` export
77
- * from `package.json`'s `main`. Returns `null` on any failure so discovery
78
- * stays robust against malformed packages.
79
- */
80
- function loadTemplateManifestFromDir(pkgDir: string, ref: string): TemplateManifest | null {
81
- const yamlPath = join(pkgDir, 'template.yaml');
82
- if (existsSync(yamlPath)) {
83
- try {
84
- const content = readFileSync(yamlPath, 'utf-8');
85
- const doc = yaml.load(content) as { template?: TemplateConfig } | TemplateConfig;
86
- const tpl = (doc && typeof doc === 'object' && 'template' in doc
87
- ? (doc as { template?: TemplateConfig }).template
88
- : (doc as TemplateConfig)) as TemplateConfig | undefined;
89
- if (tpl && typeof tpl === 'object' && tpl.name && Array.isArray(tpl.tasks)) {
90
- return { ...tpl, ref };
91
- }
92
- } catch {
93
- return null;
94
- }
95
- }
96
- return null;
97
- }