@intijs/cli 0.0.0-stage โ†’ 0.1.1

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.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -2
  3. package/bin/inti-dev.ts +40 -0
  4. package/bin/inti.js +54 -0
  5. package/dist/commands/dev.d.ts +2 -0
  6. package/dist/commands/dev.d.ts.map +1 -0
  7. package/dist/commands/dev.js +12 -0
  8. package/dist/commands/generate.d.ts +25 -0
  9. package/dist/commands/generate.d.ts.map +1 -0
  10. package/dist/commands/generate.js +149 -0
  11. package/dist/commands/sync.d.ts +2 -0
  12. package/dist/commands/sync.d.ts.map +1 -0
  13. package/dist/commands/sync.js +114 -0
  14. package/dist/generator/ai-generator.d.ts +14 -0
  15. package/dist/generator/ai-generator.d.ts.map +1 -0
  16. package/dist/generator/ai-generator.js +55 -0
  17. package/dist/generator/component-generator.d.ts +42 -0
  18. package/dist/generator/component-generator.d.ts.map +1 -0
  19. package/dist/generator/component-generator.js +228 -0
  20. package/dist/generator/index.d.ts +5 -0
  21. package/dist/generator/index.d.ts.map +1 -0
  22. package/dist/generator/index.js +3 -0
  23. package/dist/generator/types-generator.d.ts +16 -0
  24. package/dist/generator/types-generator.d.ts.map +1 -0
  25. package/dist/generator/types-generator.js +155 -0
  26. package/dist/index.d.ts +8 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +7 -0
  29. package/package.json +49 -4
  30. package/src/commands/dev.ts +14 -0
  31. package/src/commands/generate.ts +194 -0
  32. package/src/commands/sync.ts +134 -0
  33. package/src/generator/ai-generator.ts +62 -0
  34. package/src/generator/component-generator.ts +314 -0
  35. package/src/generator/index.ts +4 -0
  36. package/src/generator/types-generator.ts +167 -0
  37. package/src/index.ts +10 -0
@@ -0,0 +1,194 @@
1
+ /**
2
+ * @module commands/generate
3
+ * CLI command handler for `inti generate`.
4
+ * Introspects the CMS schema via the configured adapter and generates
5
+ * ready-to-use Astro component files in the project.
6
+ */
7
+
8
+ import { writeFileSync, existsSync, mkdirSync } from 'fs';
9
+ import { resolve } from 'path';
10
+ import { pathToFileURL } from 'url';
11
+
12
+ import { generateAstroComponents } from '../generator/index.js';
13
+ import type { ComponentGeneratorOptions } from '../generator/index.js';
14
+
15
+ // ============================================
16
+ // Command Options
17
+ // ============================================
18
+
19
+ export interface GenerateCommandOptions {
20
+ /** Directory to write generated components into */
21
+ outputDir?: string;
22
+ /** Whether to overwrite existing component files */
23
+ overwrite?: boolean;
24
+ /** Style preset: 'minimal' (no classes) or 'styled' (Tailwind/UnoCSS) */
25
+ style?: string;
26
+ }
27
+
28
+ // ============================================
29
+ // Command Implementation
30
+ // ============================================
31
+
32
+ /**
33
+ * Execute the `inti generate` command.
34
+ *
35
+ * Loads the project config, resolves the CMS adapter, introspects the schema,
36
+ * generates `.astro` components for each collection, and writes them to disk.
37
+ *
38
+ * @param configPath - Path to the inti config file (default: `./inti.config.ts`)
39
+ * @param options - Optional overrides for output directory, overwrite, and style
40
+ */
41
+ export async function generateCommand(
42
+ configPath: string = './inti.config.ts',
43
+ options?: GenerateCommandOptions,
44
+ ): Promise<void> {
45
+ console.log('๐Ÿงฉ Starting Inti Component Generation...');
46
+
47
+ try {
48
+ // Step 1: Load config
49
+ const fullConfigPath = resolve(process.cwd(), configPath);
50
+ if (!existsSync(fullConfigPath)) {
51
+ console.error(`โŒ Config file not found at ${fullConfigPath}`);
52
+ process.exit(1);
53
+ }
54
+
55
+ const config = await loadConfig(fullConfigPath);
56
+ console.log(`๐Ÿ“ก Using adapter: ${config.adapter}`);
57
+
58
+ // Step 2: Resolve and initialise adapter
59
+ const AdapterClass = await resolveAdapterClass(config.adapter);
60
+ const adapter = new AdapterClass(config.adapterConfig);
61
+
62
+ // Step 3: Introspect CMS
63
+ console.log('๐Ÿ” Introspecting CMS schema...');
64
+ const schema = await adapter.introspect();
65
+ console.log(` Found ${schema.collections.length} collections.`);
66
+
67
+ if (schema.collections.length === 0) {
68
+ console.log('โš ๏ธ No collections found โ€” nothing to generate.');
69
+ return;
70
+ }
71
+
72
+ // Step 4: Resolve options
73
+ const outputDir = resolve(
74
+ process.cwd(),
75
+ options?.outputDir || config.componentsDir || './src/components/blocks',
76
+ );
77
+ const overwrite = options?.overwrite ?? false;
78
+ const style = (options?.style === 'minimal' ? 'minimal' : 'styled') as ComponentGeneratorOptions['style'];
79
+
80
+ // Ensure output directory exists
81
+ if (!existsSync(outputDir)) {
82
+ mkdirSync(outputDir, { recursive: true });
83
+ }
84
+
85
+ // Step 5: Generate components
86
+ console.log('\n๐Ÿงฑ Generating Astro components...');
87
+
88
+ const generatorOptions: ComponentGeneratorOptions = {
89
+ outputDir,
90
+ overwrite,
91
+ style,
92
+ includeProps: true,
93
+ includeJsDoc: true,
94
+ };
95
+
96
+ const components = generateAstroComponents(schema, generatorOptions);
97
+
98
+ // Step 6: Write to disk
99
+ let created = 0;
100
+ let skipped = 0;
101
+
102
+ for (const component of components) {
103
+ const filePath = resolve(outputDir, component.filename);
104
+ const exists = existsSync(filePath);
105
+
106
+ if (exists && !overwrite) {
107
+ component.isNew = false;
108
+ skipped++;
109
+ console.log(` โญ๏ธ Skipped (exists): ${component.filename}`);
110
+ continue;
111
+ }
112
+
113
+ writeFileSync(filePath, component.content, 'utf-8');
114
+ component.isNew = !exists;
115
+ created++;
116
+ console.log(` โœ… ${exists ? 'Overwrote' : 'Created'}: ${filePath.replace(process.cwd(), '.')}`);
117
+ }
118
+
119
+ // Step 7: Summary
120
+ console.log('\nโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€');
121
+ console.log(`๐ŸŽ‰ Component generation complete!`);
122
+ console.log(` ๐Ÿ“‚ Output: ${outputDir.replace(process.cwd(), '.')}`);
123
+ console.log(` โœ… Created: ${created}`);
124
+ if (skipped > 0) {
125
+ console.log(` โญ๏ธ Skipped: ${skipped} (use --overwrite to replace)`);
126
+ }
127
+ console.log('โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\n');
128
+
129
+ } catch (error) {
130
+ console.error('โŒ Generation failed:');
131
+ console.error(error instanceof Error ? error.message : error);
132
+ process.exit(1);
133
+ }
134
+ }
135
+
136
+ // ============================================
137
+ // Internal Helpers (mirrored from sync.ts)
138
+ // ============================================
139
+
140
+ /**
141
+ * Load config from file
142
+ */
143
+ async function loadConfig(configPath: string): Promise<any> {
144
+ try {
145
+ const fileUrl = pathToFileURL(configPath).href;
146
+ const module = await import(fileUrl);
147
+ const config = module.default || module;
148
+
149
+ if (!config.adapter) {
150
+ throw new Error('Config must specify "adapter"');
151
+ }
152
+
153
+ return config;
154
+ } catch (error) {
155
+ throw new Error(`Failed to load config: ${error instanceof Error ? error.message : error}`);
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Resolve adapter class dynamically
161
+ */
162
+ async function resolveAdapterClass(adapterName: string): Promise<any> {
163
+ const tryPackages = [
164
+ `@intijs/adapter-${adapterName}`,
165
+ `inti-adapter-${adapterName}`,
166
+ ];
167
+
168
+ for (const pkg of tryPackages) {
169
+ try {
170
+ const module = await import(pkg);
171
+ const AdapterClass = module.default || Object.values(module).find((v: any) => typeof v === 'function');
172
+ if (AdapterClass) {
173
+ return AdapterClass;
174
+ }
175
+ } catch {
176
+ continue;
177
+ }
178
+ }
179
+
180
+ // Fallback to internal core adapters if name matches
181
+ if (adapterName === 'contentful' || adapterName === 'strapi' || adapterName === 'sanity') {
182
+ try {
183
+ const pkg = `@intijs/core/adapters`;
184
+ const module = await import(pkg);
185
+ const className = `${adapterName.charAt(0).toUpperCase()}${adapterName.slice(1)}Adapter`;
186
+ if (module[className]) return module[className];
187
+ } catch {}
188
+ }
189
+
190
+ throw new Error(
191
+ `Adapter "${adapterName}" not found.\n` +
192
+ `Try installing: bun add @intijs/adapter-${adapterName}`,
193
+ );
194
+ }
@@ -0,0 +1,134 @@
1
+ import { writeFileSync, existsSync, mkdirSync } from 'fs';
2
+ import { resolve } from 'path';
3
+ import { pathToFileURL } from 'url';
4
+
5
+ // Import generators directly
6
+ import { generateTypeDefinitions, generateAIManifest, generateMockData } from '../generator/index.js';
7
+ import { registry } from '@intijs/core/registry';
8
+
9
+ export async function syncCommand(configPath: string = './inti.config.ts') {
10
+ console.log('๐Ÿš€ Starting Inti Sync...');
11
+
12
+ try {
13
+ // Step 1: Load config
14
+ const fullConfigPath = resolve(process.cwd(), configPath);
15
+ if (!existsSync(fullConfigPath)) {
16
+ console.error(`โŒ Config file not found at ${fullConfigPath}`);
17
+ process.exit(1);
18
+ }
19
+
20
+ const config = await loadConfig(fullConfigPath);
21
+ console.log(`๐Ÿ“ก Using adapter: ${config.adapter}`);
22
+
23
+ // Step 2: Resolve and initialize adapter
24
+ const AdapterClass = await resolveAdapterClass(config.adapter);
25
+ const adapter = new AdapterClass(config.adapterConfig);
26
+
27
+ // Step 3: Introspect CMS
28
+ console.log('๐Ÿ” Introspecting CMS schema...');
29
+ const schema = await adapter.introspect();
30
+ console.log(` Found ${schema.collections.length} collections.`);
31
+
32
+ // Step 4: Generate code
33
+ const finalOutputDir = resolve(process.cwd(), config.outputDir || './src/lib/inti');
34
+
35
+ // Ensure output directory exists
36
+ if (!existsSync(finalOutputDir)) {
37
+ mkdirSync(finalOutputDir, { recursive: true });
38
+ }
39
+
40
+ // Generate types file
41
+ console.log('\n๐Ÿ“ Generating TypeScript types...');
42
+ const typesContent = generateTypeDefinitions(schema);
43
+ const typesPath = resolve(finalOutputDir, 'inti.types.ts');
44
+ writeFileSync(typesPath, typesContent);
45
+ console.log(` โœ“ Generated: ${typesPath.replace(process.cwd(), '.')}`);
46
+
47
+ // Development versions also generated inti-sdk.ts, with functions that
48
+ // requested `${endpoint}/api/<collection>`: a route that no supported
49
+ // CMS has. Content is read with the adapter. A user's file is not
50
+ // deleted without asking; we warn instead.
51
+ const legacySdk = resolve(finalOutputDir, 'inti-sdk.ts');
52
+ if (existsSync(legacySdk)) {
53
+ console.warn(
54
+ ` โš  ${legacySdk.replace(process.cwd(), '.')} is from an earlier development version and its functions do not ` +
55
+ 'work with any supported CMS. Delete it and read content with the adapter.'
56
+ );
57
+ }
58
+
59
+ // Step 5: AI-Native Generation
60
+ console.log('๐Ÿค– Generating AI Discovery Manifest...');
61
+ const manifest = registry.getManifest ? registry.getManifest() : {};
62
+ const manifestPath = generateAIManifest(manifest, finalOutputDir);
63
+ console.log(` โœ“ Generated: ${manifestPath.replace(process.cwd(), '.')}`);
64
+
65
+ console.log('๐Ÿงช Generating Mock Data examples...');
66
+ const mocksPath = generateMockData(manifest, finalOutputDir);
67
+ console.log(` โœ“ Generated: ${mocksPath.replace(process.cwd(), '.')}`);
68
+
69
+ // Success
70
+ console.log('\nโœ… Synchronization complete!');
71
+
72
+ } catch (error) {
73
+ console.error('โŒ Sync failed:');
74
+ console.error(error instanceof Error ? error.message : error);
75
+ process.exit(1);
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Load config from file
81
+ */
82
+ async function loadConfig(configPath: string): Promise<any> {
83
+ try {
84
+ const fileUrl = pathToFileURL(configPath).href;
85
+ const module = await import(fileUrl);
86
+ const config = module.default || module;
87
+
88
+ if (!config.adapter) {
89
+ throw new Error('Config must specify "adapter"');
90
+ }
91
+
92
+ return config;
93
+ } catch (error) {
94
+ throw new Error(`Failed to load config: ${error instanceof Error ? error.message : error}`);
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Resolve adapter class dynamically
100
+ */
101
+ async function resolveAdapterClass(adapterName: string): Promise<any> {
102
+ const tryPackages = [
103
+ `@intijs/adapter-${adapterName}`,
104
+ `inti-adapter-${adapterName}`
105
+ ];
106
+
107
+ for (const pkg of tryPackages) {
108
+ try {
109
+ const module = await import(pkg);
110
+ // Try default export first, then look for any exported function
111
+ const AdapterClass = module.default || Object.values(module).find((v: any) => typeof v === 'function');
112
+ if (AdapterClass) {
113
+ return AdapterClass;
114
+ }
115
+ } catch {
116
+ continue;
117
+ }
118
+ }
119
+
120
+ // Fallback to internal core adapters if name matches
121
+ if (adapterName === 'contentful' || adapterName === 'strapi' || adapterName === 'sanity' || adapterName === 'kitsuden') {
122
+ try {
123
+ const pkg = `@intijs/core/adapters`;
124
+ const module = await import(pkg);
125
+ const className = `${adapterName.charAt(0).toUpperCase()}${adapterName.slice(1)}Adapter`;
126
+ if (module[className]) return module[className];
127
+ } catch {}
128
+ }
129
+
130
+ throw new Error(
131
+ `Adapter "${adapterName}" not found.\n` +
132
+ `Try installing: bun add @intijs/adapter-${adapterName}`
133
+ );
134
+ }
@@ -0,0 +1,62 @@
1
+ import { writeFileSync, existsSync, mkdirSync } from 'fs';
2
+ import { resolve } from 'path';
3
+
4
+ /**
5
+ * AI Manifest Generator
6
+ * Generates a JSON file describing all available Inti blocks.
7
+ * This file acts as a "source of truth" for AI agents to understand
8
+ * what components can be used to build pages.
9
+ */
10
+ export function generateAIManifest(manifest: Record<string, any>, outputDir: string): string {
11
+ const manifestPath = resolve(outputDir, 'inti-manifest.json');
12
+
13
+ // Enrich manifest with AI instructions
14
+ const aiManifest = {
15
+ _instructions: {
16
+ goal: "Use this manifest to construct valid IntiPage and IntiBlock structures.",
17
+ blockRules: "Each block in 'blocks' object contains metadata about its component. 'type' matches the 'type' field in an IntiBlock.",
18
+ mapping: "When building a page, map CMS fields to the properties defined in these blocks.",
19
+ styles: "Components are styled with UnoCSS (Tailwind-compatible). Use utility classes for customization."
20
+ },
21
+ blocks: manifest,
22
+ projectRules: "Blocks are declared in code and composed by editors. Never invent block types that are not listed here."
23
+ };
24
+
25
+ writeFileSync(manifestPath, JSON.stringify(aiManifest, null, 2));
26
+ return manifestPath;
27
+ }
28
+
29
+ /**
30
+ * Mock Data Generator
31
+ * Generates example JSON structures for the CMS based on the manifest.
32
+ * This helps AI agents create realistic test data.
33
+ */
34
+ export function generateMockData(manifest: Record<string, any>, outputDir: string): string {
35
+ const mocksPath = resolve(outputDir, 'inti-mocks.ts');
36
+ const mocks: Record<string, any> = {};
37
+
38
+ for (const [type, reg] of Object.entries(manifest)) {
39
+ mocks[type] = (reg as any).mockData || {
40
+ title: `Example ${type}`,
41
+ description: `This is a high-quality placeholder for the ${type} block, designed to help AI agents understand the expected structure.`,
42
+ content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
43
+ image: {
44
+ url: "https://images.unsplash.com/photo-1618005182384-a83a8bd57fbe",
45
+ alt: `Placeholder for ${type}`
46
+ }
47
+ };
48
+ }
49
+
50
+ const content = `/**
51
+ * Inti Mock Data
52
+ * Generated automatically. Use these examples for testing and AI prompting.
53
+ */
54
+
55
+ export const IntiMocks = ${JSON.stringify(mocks, null, 2)};
56
+
57
+ export default IntiMocks;
58
+ `;
59
+
60
+ writeFileSync(mocksPath, content);
61
+ return mocksPath;
62
+ }