create-template-html-css 2.0.4 → 2.2.0

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 (99) hide show
  1. package/CHANGELOG.md +436 -0
  2. package/CODE-SPLITTING-GUIDE.md +274 -0
  3. package/COMPONENTS-GALLERY.html +143 -8
  4. package/HTML-VS-REACT.md +289 -0
  5. package/QUICKSTART-REACT.md +293 -0
  6. package/REACT-SUPPORT-SUMMARY.md +235 -0
  7. package/README.md +261 -12
  8. package/bin/cli.js +100 -759
  9. package/bin/commands/create.js +288 -0
  10. package/bin/commands/gallery.js +42 -0
  11. package/bin/commands/insert.js +123 -0
  12. package/bin/commands/list.js +73 -0
  13. package/package.json +10 -3
  14. package/src/component-choices.js +7 -0
  15. package/src/components-registry.js +112 -0
  16. package/src/format-utils.js +49 -0
  17. package/src/generator.js +83 -594
  18. package/src/generators/color-schemes.js +78 -0
  19. package/src/generators/color-utils.js +108 -0
  20. package/src/generators/component-filters.js +151 -0
  21. package/src/generators/html-generators.js +180 -0
  22. package/src/generators/validation.js +43 -0
  23. package/src/index.js +2 -1
  24. package/src/inserter.js +55 -233
  25. package/src/inserters/backup-utils.js +20 -0
  26. package/src/inserters/component-loader.js +68 -0
  27. package/src/inserters/html-utils.js +31 -0
  28. package/src/inserters/indentation-utils.js +90 -0
  29. package/src/inserters/validation-utils.js +49 -0
  30. package/src/react-component-choices.js +97 -0
  31. package/src/react-component-templates.js +182 -0
  32. package/src/react-file-operations.js +172 -0
  33. package/src/react-generator.js +219 -0
  34. package/src/react-templates.js +418 -0
  35. package/src/templates/basic-components-templates.js +157 -0
  36. package/src/templates/form-components-templates.js +194 -0
  37. package/src/templates/interactive-components-templates.js +139 -0
  38. package/src/utils/file-utils.js +97 -0
  39. package/src/utils/path-utils.js +32 -0
  40. package/src/utils/string-utils.js +51 -0
  41. package/src/utils/template-loader.js +91 -0
  42. package/templates/_shared/PATTERNS.md +246 -0
  43. package/templates/_shared/README.md +74 -0
  44. package/templates/_shared/base.css +18 -0
  45. package/templates/blackjack/index.html +1 -1
  46. package/templates/breakout/index.html +1 -1
  47. package/templates/connect-four/index.html +1 -1
  48. package/templates/dice-game/index.html +1 -1
  49. package/templates/flappy-bird/index.html +1 -1
  50. package/templates/pong/index.html +1 -1
  51. package/templates/skeleton/index.html +4 -4
  52. package/templates/slot-machine/index.html +1 -1
  53. package/templates/tetris/index.html +1 -1
  54. package/templates-react/README.md +126 -0
  55. package/templates-react/alert/Alert.css +158 -0
  56. package/templates-react/alert/Alert.example.jsx +106 -0
  57. package/templates-react/alert/Alert.jsx +61 -0
  58. package/templates-react/badge/Badge.css +196 -0
  59. package/templates-react/badge/Badge.example.jsx +182 -0
  60. package/templates-react/badge/Badge.jsx +44 -0
  61. package/templates-react/button/Button.css +88 -0
  62. package/templates-react/button/Button.example.jsx +40 -0
  63. package/templates-react/button/Button.jsx +29 -0
  64. package/templates-react/card/Card.css +86 -0
  65. package/templates-react/card/Card.example.jsx +49 -0
  66. package/templates-react/card/Card.jsx +35 -0
  67. package/templates-react/checkbox/Checkbox.css +217 -0
  68. package/templates-react/checkbox/Checkbox.example.jsx +141 -0
  69. package/templates-react/checkbox/Checkbox.jsx +82 -0
  70. package/templates-react/counter/Counter.css +99 -0
  71. package/templates-react/counter/Counter.example.jsx +45 -0
  72. package/templates-react/counter/Counter.jsx +70 -0
  73. package/templates-react/dropdown/Dropdown.css +237 -0
  74. package/templates-react/dropdown/Dropdown.example.jsx +98 -0
  75. package/templates-react/dropdown/Dropdown.jsx +154 -0
  76. package/templates-react/form/Form.css +128 -0
  77. package/templates-react/form/Form.example.jsx +64 -0
  78. package/templates-react/form/Form.jsx +125 -0
  79. package/templates-react/input/Input.css +113 -0
  80. package/templates-react/input/Input.example.jsx +82 -0
  81. package/templates-react/input/Input.jsx +87 -0
  82. package/templates-react/modal/Modal.css +152 -0
  83. package/templates-react/modal/Modal.example.jsx +90 -0
  84. package/templates-react/modal/Modal.jsx +46 -0
  85. package/templates-react/navbar/Navbar.css +139 -0
  86. package/templates-react/navbar/Navbar.example.jsx +37 -0
  87. package/templates-react/navbar/Navbar.jsx +62 -0
  88. package/templates-react/progress/Progress.css +247 -0
  89. package/templates-react/progress/Progress.example.jsx +244 -0
  90. package/templates-react/progress/Progress.jsx +79 -0
  91. package/templates-react/switch/Switch.css +244 -0
  92. package/templates-react/switch/Switch.example.jsx +221 -0
  93. package/templates-react/switch/Switch.jsx +98 -0
  94. package/templates-react/todo-list/TodoList.css +236 -0
  95. package/templates-react/todo-list/TodoList.example.jsx +15 -0
  96. package/templates-react/todo-list/TodoList.jsx +84 -0
  97. package/templates-react/tooltip/Tooltip.css +165 -0
  98. package/templates-react/tooltip/Tooltip.example.jsx +166 -0
  99. package/templates-react/tooltip/Tooltip.jsx +176 -0
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Form Component Templates
3
+ * Form-related components with input fields and state management
4
+ *
5
+ * @module templates/form-components-templates
6
+ */
7
+
8
+ export const FORM_TEMPLATES = {
9
+ form: ` return (
10
+ <div className="App" style={{ padding: '40px' }}>
11
+ <{ComponentName}
12
+ title="Contact Form"
13
+ fields={[
14
+ { name: 'name', label: 'Name', type: 'text', required: true },
15
+ { name: 'email', label: 'Email', type: 'email', required: true },
16
+ ]}
17
+ onSubmit={(data) => console.log('Form data:', data)}
18
+ />
19
+ </div>
20
+ );`,
21
+
22
+ input: ` const [value, setValue] = useState('');
23
+
24
+ return (
25
+ <div className="App" style={{ padding: '40px', maxWidth: '400px', margin: '0 auto' }}>
26
+ <h1 style={{ marginBottom: '30px' }}>Input Component Examples</h1>
27
+
28
+ <{ComponentName}
29
+ label="Name"
30
+ placeholder="Enter your name"
31
+ required
32
+ />
33
+
34
+ <{ComponentName}
35
+ type="email"
36
+ label="Email"
37
+ placeholder="your@email.com"
38
+ value={value}
39
+ onChange={(e) => setValue(e.target.value)}
40
+ required
41
+ />
42
+
43
+ <{ComponentName}
44
+ label="Search"
45
+ placeholder="Search..."
46
+ icon="🔍"
47
+ />
48
+ </div>
49
+ );`,
50
+
51
+ checkbox: ` const [isChecked, setIsChecked] = useState(false);
52
+
53
+ return (
54
+ <div className="App" style={{ padding: '40px', maxWidth: '600px', margin: '0 auto' }}>
55
+ <h1 style={{ marginBottom: '30px' }}>Checkbox Component Examples</h1>
56
+
57
+ <div style={{ marginBottom: '30px' }}>
58
+ <h2>Basic Checkbox</h2>
59
+ <{ComponentName}
60
+ label="I agree to the terms and conditions"
61
+ checked={isChecked}
62
+ onChange={setIsChecked}
63
+ />
64
+ <p>Status: {isChecked ? 'Checked' : 'Unchecked'}</p>
65
+ </div>
66
+
67
+ <div style={{ marginBottom: '30px' }}>
68
+ <h2>Different Sizes</h2>
69
+ <{ComponentName} label="Small" size="small" />
70
+ <{ComponentName} label="Medium (default)" size="medium" />
71
+ <{ComponentName} label="Large" size="large" />
72
+ </div>
73
+
74
+ <div style={{ marginBottom: '30px' }}>
75
+ <h2>Different Colors</h2>
76
+ <{ComponentName} label="Primary" checked color="primary" />
77
+ <{ComponentName} label="Success" checked color="success" />
78
+ <{ComponentName} label="Error" checked color="error" />
79
+ </div>
80
+
81
+ <div style={{ marginBottom: '30px' }}>
82
+ <h2>Disabled State</h2>
83
+ <{ComponentName} label="Disabled unchecked" disabled />
84
+ <{ComponentName} label="Disabled checked" checked disabled />
85
+ </div>
86
+ </div>
87
+ );`,
88
+
89
+ dropdown: ` const [selected, setSelected] = useState('');
90
+
91
+ const options = [
92
+ { value: 'apple', label: 'Apple' },
93
+ { value: 'banana', label: 'Banana' },
94
+ { value: 'cherry', label: 'Cherry' },
95
+ { value: 'date', label: 'Date' },
96
+ { value: 'elderberry', label: 'Elderberry' }
97
+ ];
98
+
99
+ return (
100
+ <div className="App" style={{ padding: '40px', maxWidth: '600px', margin: '0 auto' }}>
101
+ <h1 style={{ marginBottom: '30px' }}>Dropdown Component Examples</h1>
102
+
103
+ <div style={{ marginBottom: '30px' }}>
104
+ <{ComponentName}
105
+ label="Choose a fruit"
106
+ options={options}
107
+ value={selected}
108
+ onChange={setSelected}
109
+ placeholder="Select a fruit"
110
+ />
111
+ <p>Selected: {selected || 'None'}</p>
112
+ </div>
113
+
114
+ <div style={{ marginBottom: '30px' }}>
115
+ <{ComponentName}
116
+ label="Searchable Dropdown"
117
+ options={options}
118
+ placeholder="Search and select..."
119
+ searchable
120
+ />
121
+ </div>
122
+
123
+ <div style={{ marginBottom: '30px' }}>
124
+ <{ComponentName}
125
+ label="Disabled"
126
+ options={options}
127
+ placeholder="Cannot select"
128
+ disabled
129
+ />
130
+ </div>
131
+ </div>
132
+ );`,
133
+
134
+ switch: ` const [enabled, setEnabled] = useState(false);
135
+ const [darkMode, setDarkMode] = useState(false);
136
+
137
+ return (
138
+ <div className="App" style={{ padding: '40px', maxWidth: '800px', margin: '0 auto' }}>
139
+ <h1 style={{ marginBottom: '30px' }}>Switch Component Examples</h1>
140
+
141
+ <div style={{ marginBottom: '30px' }}>
142
+ <h2>Basic Switch</h2>
143
+ <{ComponentName}
144
+ checked={enabled}
145
+ onChange={setEnabled}
146
+ label="Enable notifications"
147
+ />
148
+ <p style={{ marginTop: '0.5rem' }}>Status: {enabled ? 'ON' : 'OFF'}</p>
149
+ </div>
150
+
151
+ <div style={{ marginBottom: '30px' }}>
152
+ <h2>Sizes</h2>
153
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
154
+ <{ComponentName} size="small" label="Small" />
155
+ <{ComponentName} size="medium" label="Medium (default)" />
156
+ <{ComponentName} size="large" label="Large" />
157
+ </div>
158
+ </div>
159
+
160
+ <div style={{ marginBottom: '30px' }}>
161
+ <h2>Colors</h2>
162
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
163
+ <{ComponentName} checked color="primary" label="Primary" />
164
+ <{ComponentName} checked color="success" label="Success" />
165
+ <{ComponentName} checked color="error" label="Error" />
166
+ </div>
167
+ </div>
168
+
169
+ <div style={{ marginBottom: '30px' }}>
170
+ <h2>With Icons</h2>
171
+ <{ComponentName}
172
+ checked={darkMode}
173
+ onChange={setDarkMode}
174
+ label="Dark mode"
175
+ color="secondary"
176
+ icons={{
177
+ checked: '🌙',
178
+ unchecked: '☀️'
179
+ }}
180
+ />
181
+ </div>
182
+
183
+ <div style={{ marginBottom: '30px' }}>
184
+ <h2>States</h2>
185
+ <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
186
+ <{ComponentName} label="Normal" />
187
+ <{ComponentName} checked label="Checked" />
188
+ <{ComponentName} disabled label="Disabled" />
189
+ <{ComponentName} loading label="Loading" />
190
+ </div>
191
+ </div>
192
+ </div>
193
+ );`,
194
+ };
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Interactive Component Templates
3
+ * Complex interactive components with navigation, modals, and animations
4
+ *
5
+ * @module templates/interactive-components-templates
6
+ */
7
+
8
+ export const INTERACTIVE_TEMPLATES = {
9
+ modal: ` const [isOpen, setIsOpen] = useState(false);
10
+
11
+ return (
12
+ <div className="App" style={{ padding: '40px' }}>
13
+ <button onClick={() => setIsOpen(true)}>Open Modal</button>
14
+ <{ComponentName}
15
+ isOpen={isOpen}
16
+ onClose={() => setIsOpen(false)}
17
+ title="Example Modal"
18
+ >
19
+ <p>This is the modal content</p>
20
+ </{ComponentName}>
21
+ </div>
22
+ );`,
23
+
24
+ navbar: ` const links = [
25
+ { label: 'Home', href: '#home' },
26
+ { label: 'Features', href: '#features' },
27
+ { label: 'Pricing', href: '#pricing' },
28
+ { label: 'Contact', href: '#contact' }
29
+ ];
30
+
31
+ return (
32
+ <div>
33
+ <{ComponentName}
34
+ logo="MyApp"
35
+ links={links}
36
+ onLinkClick={(link) => console.log('Clicked:', link.label)}
37
+ />
38
+ <div style={{ padding: '40px' }}>
39
+ <h1>Scroll down to see sticky navbar</h1>
40
+ <div style={{ height: '2000px' }}>
41
+ <p>Content goes here...</p>
42
+ </div>
43
+ </div>
44
+ </div>
45
+ );`,
46
+
47
+ tooltip: ` return (
48
+ <div className="App" style={{ padding: '80px', maxWidth: '800px', margin: '0 auto', textAlign: 'center' }}>
49
+ <h1 style={{ marginBottom: '50px' }}>Tooltip Component Examples</h1>
50
+
51
+ <div style={{ marginBottom: '50px', display: 'flex', gap: '40px', justifyContent: 'center', flexWrap: 'wrap' }}>
52
+ <{ComponentName} content="This is a top tooltip" position="top">
53
+ <button style={buttonStyle}>Top</button>
54
+ </{ComponentName}>
55
+
56
+ <{ComponentName} content="This is a bottom tooltip" position="bottom">
57
+ <button style={buttonStyle}>Bottom</button>
58
+ </{ComponentName}>
59
+
60
+ <{ComponentName} content="This is a left tooltip" position="left">
61
+ <button style={buttonStyle}>Left</button>
62
+ </{ComponentName}>
63
+
64
+ <{ComponentName} content="This is a right tooltip" position="right">
65
+ <button style={buttonStyle}>Right</button>
66
+ </{ComponentName}>
67
+ </div>
68
+
69
+ <div style={{ marginBottom: '50px' }}>
70
+ <{ComponentName} content="Click me to toggle!" trigger="click">
71
+ <button style={buttonStyle}>Click Trigger</button>
72
+ </{ComponentName}>
73
+ </div>
74
+
75
+ <div>
76
+ <{ComponentName}
77
+ content="This is a longer tooltip that demonstrates how it handles more text content."
78
+ maxWidth="300px"
79
+ >
80
+ <button style={buttonStyle}>Long Content</button>
81
+ </{ComponentName}>
82
+ </div>
83
+ </div>
84
+ );
85
+
86
+ const buttonStyle = {
87
+ padding: '0.5rem 1rem',
88
+ backgroundColor: '#3b82f6',
89
+ color: 'white',
90
+ border: 'none',
91
+ borderRadius: '0.375rem',
92
+ cursor: 'pointer'
93
+ };`,
94
+
95
+ progress: ` const [progress, setProgress] = useState(0);
96
+
97
+ useEffect(() => {
98
+ const timer = setInterval(() => {
99
+ setProgress((prev) => (prev >= 100 ? 0 : prev + 1));
100
+ }, 100);
101
+ return () => clearInterval(timer);
102
+ }, []);
103
+
104
+ return (
105
+ <div className="App" style={{ padding: '40px', maxWidth: '800px', margin: '0 auto' }}>
106
+ <h1 style={{ marginBottom: '30px' }}>Progress Component Examples</h1>
107
+
108
+ <div style={{ marginBottom: '30px' }}>
109
+ <h2>Basic Progress</h2>
110
+ <{ComponentName} value={25} variant="primary" />
111
+ <{ComponentName} value={50} variant="success" />
112
+ <{ComponentName} value={75} variant="warning" />
113
+ </div>
114
+
115
+ <div style={{ marginBottom: '30px' }}>
116
+ <h2>With Percentage</h2>
117
+ <{ComponentName} value={60} variant="primary" showPercentage size="large" />
118
+ </div>
119
+
120
+ <div style={{ marginBottom: '30px' }}>
121
+ <h2>Animated Progress</h2>
122
+ <div style={{ marginBottom: '0.5rem' }}>Uploading: {progress}%</div>
123
+ <{ComponentName} value={progress} variant="info" striped animated />
124
+ </div>
125
+
126
+ <div style={{ marginBottom: '30px' }}>
127
+ <h2>Indeterminate (Loading)</h2>
128
+ <{ComponentName} indeterminate variant="primary" />
129
+ </div>
130
+ </div>
131
+ );`,
132
+
133
+ 'todo-list': ` return (
134
+ <div className="App" style={{ padding: '40px' }}>
135
+ <h1>Todo List</h1>
136
+ <{ComponentName} />
137
+ </div>
138
+ );`,
139
+ };
@@ -0,0 +1,97 @@
1
+ /**
2
+ * File system utilities
3
+ * Handles common file and directory operations with formatting
4
+ */
5
+
6
+ import fs from "fs/promises";
7
+ import path from "path";
8
+ import { formatHtml, formatCss, formatJs } from "../format-utils.js";
9
+
10
+ /**
11
+ * Creates a directory with all parent directories
12
+ * @param {string} dirPath - Directory path to create
13
+ * @returns {Promise<void>}
14
+ */
15
+ export async function ensureDir(dirPath) {
16
+ await fs.mkdir(dirPath, { recursive: true });
17
+ }
18
+
19
+ /**
20
+ * Creates multiple directories
21
+ * @param {string[]} dirPaths - Array of directory paths to create
22
+ * @returns {Promise<void>}
23
+ */
24
+ export async function ensureDirs(...dirPaths) {
25
+ await Promise.all(dirPaths.map((dir) => ensureDir(dir)));
26
+ }
27
+
28
+ /**
29
+ * Writes formatted HTML content to a file
30
+ * @param {string} filePath - Path to the HTML file
31
+ * @param {string} content - HTML content to write
32
+ * @returns {Promise<void>}
33
+ */
34
+ export async function writeHtmlFile(filePath, content) {
35
+ const formatted = await formatHtml(content);
36
+ await fs.writeFile(filePath, formatted);
37
+ }
38
+
39
+ /**
40
+ * Writes formatted CSS content to a file
41
+ * @param {string} filePath - Path to the CSS file
42
+ * @param {string} content - CSS content to write
43
+ * @returns {Promise<void>}
44
+ */
45
+ export async function writeCssFile(filePath, content) {
46
+ const formatted = await formatCss(content);
47
+ await fs.writeFile(filePath, formatted);
48
+ }
49
+
50
+ /**
51
+ * Writes formatted JavaScript content to a file
52
+ * @param {string} filePath - Path to the JS file
53
+ * @param {string} content - JavaScript content to write
54
+ * @returns {Promise<void>}
55
+ */
56
+ export async function writeJsFile(filePath, content) {
57
+ const formatted = await formatJs(content);
58
+ await fs.writeFile(filePath, formatted);
59
+ }
60
+
61
+ /**
62
+ * Creates directory structure for component files
63
+ * @param {string} basePath - Base output directory path
64
+ * @returns {Promise<Object>} Object with outputDir, cssDir, jsDir paths
65
+ */
66
+ export async function createComponentDirs(basePath) {
67
+ const outputDir = basePath;
68
+ const cssDir = path.join(basePath, "css");
69
+ const jsDir = path.join(basePath, "js");
70
+
71
+ await ensureDirs(outputDir, cssDir, jsDir);
72
+
73
+ return { outputDir, cssDir, jsDir };
74
+ }
75
+
76
+ /**
77
+ * Writes component files (HTML, CSS, JS) to their respective directories
78
+ * @param {Object} options - File writing options
79
+ * @param {string} options.outputDir - Output directory path
80
+ * @param {string} options.cssDir - CSS directory path
81
+ * @param {string} options.jsDir - JS directory path
82
+ * @param {string} options.htmlContent - HTML content
83
+ * @param {string} options.cssContent - CSS content
84
+ * @param {string} [options.jsContent] - Optional JS content
85
+ * @returns {Promise<void>}
86
+ */
87
+ export async function writeComponentFiles(options) {
88
+ const { outputDir, cssDir, jsDir, htmlContent, cssContent, jsContent } =
89
+ options;
90
+
91
+ await writeHtmlFile(path.join(outputDir, "index.html"), htmlContent);
92
+ await writeCssFile(path.join(cssDir, "style.css"), cssContent);
93
+
94
+ if (jsContent) {
95
+ await writeJsFile(path.join(jsDir, "script.js"), jsContent);
96
+ }
97
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Path utilities for ES Modules
3
+ * Provides __dirname functionality in ES Modules environment
4
+ */
5
+
6
+ import { fileURLToPath } from "url";
7
+ import { dirname } from "path";
8
+
9
+ /**
10
+ * Gets the directory name of a module (equivalent to __dirname in CommonJS)
11
+ * @param {string} importMetaUrl - import.meta.url from the calling module
12
+ * @returns {string} Directory path of the module
13
+ * @example
14
+ * import { getDirname } from './utils/path-utils.js';
15
+ * const __dirname = getDirname(import.meta.url);
16
+ */
17
+ export function getDirname(importMetaUrl) {
18
+ const __filename = fileURLToPath(importMetaUrl);
19
+ return dirname(__filename);
20
+ }
21
+
22
+ /**
23
+ * Gets the file path of a module (equivalent to __filename in CommonJS)
24
+ * @param {string} importMetaUrl - import.meta.url from the calling module
25
+ * @returns {string} File path of the module
26
+ * @example
27
+ * import { getFilename } from './utils/path-utils.js';
28
+ * const __filename = getFilename(import.meta.url);
29
+ */
30
+ export function getFilename(importMetaUrl) {
31
+ return fileURLToPath(importMetaUrl);
32
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * String manipulation utilities
3
+ * Helper functions for common string operations
4
+ */
5
+
6
+ /**
7
+ * Convert a string to a valid HTML/CSS ID
8
+ * Converts to lowercase and replaces spaces with hyphens
9
+ * @param {string} text - Text to convert
10
+ * @returns {string} Valid ID string (e.g., "My Item" -> "my-item")
11
+ */
12
+ export function textToId(text) {
13
+ return text.toLowerCase().replace(/\s+/g, "-");
14
+ }
15
+
16
+ /**
17
+ * Sanitize a string for use as a filename
18
+ * Removes invalid characters and trims whitespace
19
+ * @param {string} filename - Filename to sanitize
20
+ * @returns {string} Sanitized filename
21
+ */
22
+ export function sanitizeForFilename(filename) {
23
+ return filename.replace(/[<>:"|?*]/g, "").trim();
24
+ }
25
+
26
+ /**
27
+ * Parse comma-separated list into array
28
+ * Trims whitespace and filters empty items
29
+ * @param {string} input - Comma-separated string
30
+ * @returns {string[]} Array of trimmed non-empty strings
31
+ */
32
+ export function parseCommaSeparated(input) {
33
+ return input
34
+ .split(",")
35
+ .map((item) => item.trim())
36
+ .filter((item) => item.length > 0);
37
+ }
38
+
39
+ /**
40
+ * Parse key:value pairs from comma-separated string
41
+ * @param {string} input - Comma-separated "key:value" pairs
42
+ * @returns {Array<{key: string, value: string}>} Array of parsed pairs
43
+ */
44
+ export function parseKeyValuePairs(input) {
45
+ return parseCommaSeparated(input)
46
+ .map((pair) => {
47
+ const [key, value] = pair.split(":").map((s) => s.trim());
48
+ return key && value ? { key, value } : null;
49
+ })
50
+ .filter(Boolean);
51
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Template loader utilities
3
+ * Centralized functions for loading template files from the templates directory
4
+ */
5
+
6
+ import fs from "fs/promises";
7
+ import path from "path";
8
+ import { getDirname } from "./path-utils.js";
9
+
10
+ const __dirname = getDirname(import.meta.url);
11
+
12
+ /**
13
+ * Get the path to a template directory
14
+ * @param {string} component - Component name
15
+ * @returns {string} Absolute path to template directory
16
+ */
17
+ export function getTemplatePath(component) {
18
+ return path.join(__dirname, "..", "..", "templates", component);
19
+ }
20
+
21
+ /**
22
+ * Read a template file from the templates directory
23
+ * @param {string} component - Component name
24
+ * @param {string} filename - File to read (e.g., "index.html", "style.css", "script.js")
25
+ * @returns {Promise<string>} File content
26
+ */
27
+ export async function readTemplateFile(component, filename) {
28
+ const templatePath = getTemplatePath(component);
29
+ return await fs.readFile(path.join(templatePath, filename), "utf-8");
30
+ }
31
+
32
+ /**
33
+ * Read template HTML file
34
+ * @param {string} component - Component name
35
+ * @returns {Promise<string>} HTML content
36
+ */
37
+ export async function readTemplateHtml(component) {
38
+ return await readTemplateFile(component, "index.html");
39
+ }
40
+
41
+ /**
42
+ * Read template CSS file
43
+ * Tries css/style.css first, falls back to style.css
44
+ * @param {string} component - Component name
45
+ * @returns {Promise<string>} CSS content
46
+ */
47
+ export async function readTemplateCss(component) {
48
+ const templatePath = getTemplatePath(component);
49
+
50
+ try {
51
+ return await fs.readFile(
52
+ path.join(templatePath, "css", "style.css"),
53
+ "utf-8"
54
+ );
55
+ } catch {
56
+ return await fs.readFile(path.join(templatePath, "style.css"), "utf-8");
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Read template JavaScript file
62
+ * Tries js/script.js first, falls back to script.js, returns null if not found
63
+ * @param {string} component - Component name
64
+ * @returns {Promise<string|null>} JavaScript content or null if not found
65
+ */
66
+ export async function readTemplateJs(component) {
67
+ const templatePath = getTemplatePath(component);
68
+
69
+ try {
70
+ return await fs.readFile(
71
+ path.join(templatePath, "js", "script.js"),
72
+ "utf-8"
73
+ );
74
+ } catch {
75
+ try {
76
+ return await fs.readFile(path.join(templatePath, "script.js"), "utf-8");
77
+ } catch {
78
+ return null; // No JavaScript file for this component
79
+ }
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Check if a template JavaScript file exists
85
+ * @param {string} component - Component name
86
+ * @returns {Promise<boolean>} True if JS file exists
87
+ */
88
+ export async function hasTemplateJs(component) {
89
+ const js = await readTemplateJs(component);
90
+ return js !== null;
91
+ }