@lifetechocms/react 0.0.0-stage → 0.1.4

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,55 @@
1
+
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ import {
6
+ discoverReactFiles,
7
+ getRelativeName
8
+ } from './GenerateEntries.js';
9
+
10
+ export function discoverLayouts(
11
+ projectRoot,
12
+ layoutsDirectory = 'src/Layouts'
13
+ ) {
14
+ const directory = path.resolve(
15
+ projectRoot,
16
+ layoutsDirectory
17
+ );
18
+
19
+ // Layouts are optional.
20
+ if (!fs.existsSync(directory)) {
21
+ return {};
22
+ }
23
+
24
+ if (!fs.statSync(directory).isDirectory()) {
25
+ throw new Error(
26
+ `LifeTechOCMS: Expected a directory: ${directory}`
27
+ );
28
+ }
29
+
30
+ const layouts = {};
31
+ const usedNames = new Set();
32
+
33
+ // Discover supported files recursively.
34
+ const layoutFiles = discoverReactFiles(directory);
35
+
36
+ for (const file of layoutFiles) {
37
+ // Example:
38
+ // Admin/Dashboard.tsx -> Admin/Dashboard
39
+ const name = getRelativeName(directory, file);
40
+
41
+ const collisionKey = name.toLowerCase();
42
+
43
+ if (usedNames.has(collisionKey)) {
44
+ throw new Error(
45
+ `LifeTechOCMS: Duplicate Layout name: ${name}`
46
+ );
47
+ }
48
+
49
+ usedNames.add(collisionKey);
50
+
51
+ layouts[name] = file;
52
+ }
53
+
54
+ return layouts;
55
+ }
@@ -0,0 +1,189 @@
1
+
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ const SUPPORTED_EXTENSIONS = new Set([
6
+ '.js',
7
+ '.jsx',
8
+ '.ts',
9
+ '.tsx'
10
+ ]);
11
+
12
+ const normalize = file => file.replace(/\\/g, '/');
13
+
14
+ // Recursively discover supported React files.
15
+ export function discoverReactFiles(directory) {
16
+ if (!fs.existsSync(directory)) {
17
+ return [];
18
+ }
19
+
20
+ const files = [];
21
+
22
+ function walk(currentDirectory) {
23
+ const entries = fs.readdirSync(currentDirectory, {
24
+ withFileTypes: true
25
+ });
26
+
27
+ for (const entry of entries) {
28
+ const fullPath = path.join(
29
+ currentDirectory,
30
+ entry.name
31
+ );
32
+
33
+ if (entry.isDirectory()) {
34
+ walk(fullPath);
35
+ } else if (
36
+ entry.isFile() &&
37
+ SUPPORTED_EXTENSIONS.has(
38
+ path.extname(entry.name).toLowerCase()
39
+ )
40
+ ) {
41
+ files.push(fullPath);
42
+ }
43
+ }
44
+ }
45
+
46
+ walk(directory);
47
+
48
+ return files.sort();
49
+ }
50
+
51
+ // Convert a file into its relative component name.
52
+ // Example: Blog/Details.tsx -> Blog/Details
53
+ export function getRelativeName(baseDirectory, file) {
54
+ const relative = normalize(
55
+ path.relative(baseDirectory, file)
56
+ );
57
+
58
+ return relative.replace(/\.(jsx?|tsx?)$/i, '');
59
+ }
60
+
61
+ // Create a unique Vite entry name.
62
+ export function getEntryName(type, relativeName) {
63
+ const normalized = relativeName
64
+ .replace(/\\/g, '/')
65
+ .replace(/\//g, '_');
66
+
67
+ return `${type}_${normalized}`;
68
+ }
69
+
70
+ export function generateEntries(
71
+ layouts,
72
+ pluginRoot,
73
+ projectRoot,
74
+ viewsDirectory = 'src/Views'
75
+ ) {
76
+ const entries = {};
77
+ const modules = {};
78
+
79
+ const layoutRuntime = normalize(
80
+ path.resolve(
81
+ pluginRoot,
82
+ 'runtime/LifeTechEntry.jsx'
83
+ )
84
+ );
85
+
86
+ const viewRuntime = normalize(
87
+ path.resolve(
88
+ pluginRoot,
89
+ 'runtime/LifeTechViewEntry.jsx'
90
+ )
91
+ );
92
+
93
+ // Layouts are supplied by discoverLayouts().
94
+ // Their keys must represent relative paths,
95
+ // e.g. Admin/Dashboard.
96
+ for (const [name, layoutPath] of Object.entries(layouts)) {
97
+ const entryName = getEntryName('Layout', name);
98
+ const resolvedId =
99
+ `\0virtual:lifetech-layout-${entryName}`;
100
+
101
+ entries[entryName] = resolvedId;
102
+
103
+ modules[resolvedId] = `
104
+ import Layout from ${JSON.stringify(
105
+ normalize(layoutPath)
106
+ )};
107
+
108
+ import { mountLifeTechLayout } from
109
+ ${JSON.stringify(layoutRuntime)};
110
+
111
+ mountLifeTechLayout(Layout);
112
+ `;
113
+ }
114
+
115
+ // Discover Views recursively.
116
+ const viewsPath = path.resolve(
117
+ projectRoot,
118
+ viewsDirectory
119
+ );
120
+
121
+ if (!fs.existsSync(viewsPath)) {
122
+ throw new Error(
123
+ 'LifeTechOCMS: Views directory not found: ' +
124
+ viewsPath
125
+ );
126
+ }
127
+ if (!fs.statSync(viewsPath).isDirectory()) {
128
+ throw new Error(
129
+ `LifeTechOCMS: Expected a Views directory: ${viewsPath}`
130
+ );
131
+ }
132
+
133
+ const viewFiles = discoverReactFiles(viewsPath);
134
+
135
+ // Prevent ambiguous names such as Home.jsx and Home.tsx
136
+ // within the same directory.
137
+ const usedNames = new Set();
138
+
139
+ for (const file of viewFiles) {
140
+ const relativeName = getRelativeName(
141
+ viewsPath,
142
+ file
143
+ );
144
+
145
+ const collisionKey = relativeName.toLowerCase();
146
+
147
+ if (usedNames.has(collisionKey)) {
148
+ throw new Error(
149
+ `LifeTechOCMS: Duplicate View name: ${relativeName}`
150
+ );
151
+ }
152
+
153
+ usedNames.add(collisionKey);
154
+
155
+ const entryName = getEntryName(
156
+ 'View',
157
+ relativeName
158
+ );
159
+
160
+ const resolvedId =
161
+ `\0virtual:lifetech-view-${entryName}`;
162
+
163
+ entries[entryName] = resolvedId;
164
+
165
+ modules[resolvedId] = `
166
+ import View from ${JSON.stringify(
167
+ normalize(file)
168
+ )};
169
+
170
+ import { mountLifeTechView } from
171
+ ${JSON.stringify(viewRuntime)};
172
+
173
+ document
174
+ .querySelectorAll(
175
+ ${JSON.stringify(
176
+ `[data-lt-view="${relativeName}"]`
177
+ )}
178
+ )
179
+ .forEach(element => {
180
+ mountLifeTechView(element, View);
181
+ });
182
+ `;
183
+ }
184
+
185
+ return {
186
+ entries,
187
+ modules
188
+ };
189
+ }
@@ -0,0 +1,213 @@
1
+
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ import {
6
+ discoverReactFiles,
7
+ getRelativeName,
8
+ getEntryName
9
+ } from './GenerateEntries.js';
10
+
11
+ export function generateLayoutHtml({
12
+ outputDirectory,
13
+ projectRoot,
14
+ viewsDirectory = 'src/Views',
15
+ layouts = {},
16
+ themeName
17
+ }) {
18
+ const manifestPath = path.join(
19
+ outputDirectory,
20
+ '.vite',
21
+ 'manifest.json'
22
+ );
23
+
24
+ if (!fs.existsSync(manifestPath)) {
25
+ throw new Error(
26
+ 'LifeTechOCMS: Vite manifest not found.'
27
+ );
28
+ }
29
+
30
+ const manifest = JSON.parse(
31
+ fs.readFileSync(manifestPath, 'utf8')
32
+ );
33
+
34
+ const layoutsOutput = path.join(
35
+ outputDirectory,
36
+ 'Layouts'
37
+ );
38
+
39
+ const viewsOutput = path.join(
40
+ outputDirectory,
41
+ 'Views'
42
+ );
43
+
44
+ fs.mkdirSync(layoutsOutput, { recursive: true });
45
+ fs.mkdirSync(viewsOutput, { recursive: true });
46
+
47
+ // Find the generated entry in the Vite manifest.
48
+ function findEntry(entryName) {
49
+ const expectedFile = `Assets/js/${entryName}.js`;
50
+
51
+ const match = Object.entries(manifest).find(
52
+ ([, entry]) =>
53
+ entry.isEntry &&
54
+ entry.file === expectedFile
55
+ );
56
+
57
+ if (!match) {
58
+ throw new Error(
59
+ `LifeTechOCMS: Missing Vite entry: ${entryName}`
60
+ );
61
+ }
62
+
63
+ const [key, entry] = match;
64
+
65
+ return { key, entry };
66
+ }
67
+
68
+ // Collect CSS from an entry and its imported chunks.
69
+ function collectCss(entryKey) {
70
+ const cssFiles = new Set();
71
+ const visited = new Set();
72
+
73
+ function visit(key) {
74
+ if (visited.has(key)) return;
75
+ visited.add(key);
76
+
77
+ const chunk = manifest[key];
78
+ if (!chunk) return;
79
+
80
+ for (const css of chunk.css ?? []) {
81
+ cssFiles.add(css);
82
+ }
83
+
84
+ for (const imported of chunk.imports ?? []) {
85
+ visit(imported);
86
+ }
87
+ }
88
+
89
+ visit(entryKey);
90
+
91
+ return [...cssFiles];
92
+ }
93
+
94
+ // Resolve a generated asset using LifeTechOCMS.
95
+ const assetUrl = file =>
96
+ `<?= ltThemePath() ?>/${themeName}/${file.replace(/\\/g, '/')}`;
97
+
98
+ function generateImports(entryKey, entry) {
99
+ const cssImports = collectCss(entryKey)
100
+ .map(file =>
101
+ `<link rel="stylesheet" href="${assetUrl(file)}">`
102
+ )
103
+ .join('\n');
104
+
105
+ const jsImport =
106
+ `<script type="module" src="${assetUrl(entry.file)}"></script>`;
107
+
108
+ return { cssImports, jsImport };
109
+ }
110
+
111
+ // Write an HTML file, preserving nested directories.
112
+ function writeHtml(root, relativeName, html) {
113
+ const destination = path.join(
114
+ root,
115
+ ...relativeName.split('/').slice(0, -1),
116
+ `${relativeName.split('/').at(-1)}.html`
117
+ );
118
+
119
+ fs.mkdirSync(
120
+ path.dirname(destination),
121
+ { recursive: true }
122
+ );
123
+
124
+ fs.writeFileSync(destination, html, 'utf8');
125
+
126
+ return destination;
127
+ }
128
+
129
+ // Generate Layout HTML resources.
130
+ for (const layoutName of Object.keys(layouts).sort()) {
131
+ const entryName = getEntryName(
132
+ 'Layout',
133
+ layoutName
134
+ );
135
+
136
+ const { key, entry } = findEntry(entryName);
137
+
138
+ const { cssImports, jsImport } =
139
+ generateImports(key, entry);
140
+
141
+ const html = [
142
+ '<!-- Generated by @lifetechocms/react -->',
143
+ '',
144
+ '<div id="lt-react-layout"></div>',
145
+ '',
146
+ '<div id="lt-dynamic-content" hidden>',
147
+ ' <?php dddm::loadDynamicContent(); ?>',
148
+ '</div>',
149
+ '',
150
+ cssImports,
151
+ jsImport,
152
+ ''
153
+ ].join('\n');
154
+
155
+ writeHtml(layoutsOutput, layoutName, html);
156
+
157
+ console.log(
158
+ `[LifeTechOCMS] Generated Layout: ${layoutName}.html`
159
+ );
160
+ }
161
+
162
+ // Generate View HTML resources.
163
+ const sourceViews = path.resolve(
164
+ projectRoot,
165
+ viewsDirectory
166
+ );
167
+
168
+ if (!fs.existsSync(sourceViews)) {
169
+ throw new Error(
170
+ `LifeTechOCMS: Views directory not found: ${sourceViews}`
171
+ );
172
+ }
173
+
174
+ if (!fs.statSync(sourceViews).isDirectory()) {
175
+ throw new Error(
176
+ `LifeTechOCMS: Expected a Views directory: ${sourceViews}`
177
+ );
178
+ }
179
+ const viewFiles = discoverReactFiles(sourceViews);
180
+
181
+ for (const file of viewFiles) {
182
+ const viewName = getRelativeName(
183
+ sourceViews,
184
+ file
185
+ );
186
+
187
+ const entryName = getEntryName(
188
+ 'View',
189
+ viewName
190
+ );
191
+
192
+ const { key, entry } = findEntry(entryName);
193
+
194
+ const { cssImports, jsImport } =
195
+ generateImports(key, entry);
196
+
197
+ const html = [
198
+ '<!-- Generated by @lifetechocms/react -->',
199
+ '',
200
+ `<div data-lt-view="${viewName}"></div>`,
201
+ '',
202
+ cssImports,
203
+ jsImport,
204
+ ''
205
+ ].join('\n');
206
+
207
+ writeHtml(viewsOutput, viewName, html);
208
+
209
+ console.log(
210
+ `[LifeTechOCMS] Generated View: ${viewName}.html`
211
+ );
212
+ }
213
+ }
@@ -0,0 +1,175 @@
1
+
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { pathToFileURL } from 'node:url';
5
+
6
+ // Find a resource recursively using its filename.
7
+ function findResource(directory, fileName) {
8
+ if (!fs.existsSync(directory)) {
9
+ return null;
10
+ }
11
+
12
+ const matches = [];
13
+
14
+ function scan(currentDirectory) {
15
+ for (const entry of fs.readdirSync(currentDirectory, {
16
+ withFileTypes: true
17
+ })) {
18
+ const fullPath = path.join(
19
+ currentDirectory,
20
+ entry.name
21
+ );
22
+
23
+ if (entry.isDirectory()) {
24
+ scan(fullPath);
25
+ } else if (
26
+ entry.isFile() &&
27
+ entry.name.toLowerCase() === fileName.toLowerCase()
28
+ ) {
29
+ matches.push(fullPath);
30
+ }
31
+ }
32
+ }
33
+
34
+ scan(directory);
35
+
36
+ if (matches.length > 1) {
37
+ throw new Error(
38
+ `LifeTechOCMS: Duplicate resource filename: ${fileName}`
39
+ );
40
+ }
41
+
42
+ return matches[0] ?? null;
43
+ }
44
+
45
+ export async function generateLayoutMapping({
46
+ projectRoot,
47
+ outputDirectory,
48
+ themeName,
49
+ mappingFile = 'src/LayoutMapping.js'
50
+ }) {
51
+ const sourcePath = path.resolve(
52
+ projectRoot,
53
+ mappingFile
54
+ );
55
+
56
+ if (!fs.existsSync(sourcePath)) {
57
+ console.log(
58
+ '[LifeTechOCMS] No Layout Mapping found.'
59
+ );
60
+ return;
61
+ }
62
+
63
+ const { default: mapping } = await import(
64
+ pathToFileURL(sourcePath).href
65
+ );
66
+
67
+ if (
68
+ !mapping ||
69
+ typeof mapping !== 'object' ||
70
+ Array.isArray(mapping)
71
+ ) {
72
+ throw new Error(
73
+ 'LifeTechOCMS: Invalid Layout Mapping.'
74
+ );
75
+ }
76
+
77
+ const layoutsDirectory = path.join(
78
+ outputDirectory,
79
+ 'Layouts'
80
+ );
81
+
82
+ const viewsDirectory = path.join(
83
+ outputDirectory,
84
+ 'Views'
85
+ );
86
+
87
+ const pendingLayouts = [];
88
+ const assignedViews = new Set();
89
+
90
+ // Accept resource filenames only.
91
+ function getHtmlFileName(name, type) {
92
+ if (
93
+ typeof name !== 'string' ||
94
+ !/^[A-Za-z0-9_-]+(?:\.html)?$/i.test(name)
95
+ ) {
96
+ throw new Error(
97
+ `LifeTechOCMS: Invalid ${type} filename: ${name}`
98
+ );
99
+ }
100
+
101
+ return name.toLowerCase().endsWith('.html')
102
+ ? name
103
+ : `${name}.html`;
104
+ }
105
+
106
+ for (const [layoutName, viewNames] of Object.entries(mapping)) {
107
+ const layoutFile = getHtmlFileName(
108
+ layoutName,
109
+ 'Layout'
110
+ );
111
+
112
+ if (!findResource(layoutsDirectory, layoutFile)) {
113
+ throw new Error(
114
+ `LifeTechOCMS: Layout not found: ${layoutFile}`
115
+ );
116
+ }
117
+
118
+ if (!Array.isArray(viewNames)) {
119
+ throw new Error(
120
+ `LifeTechOCMS: Expected an array of Views for ${layoutName}`
121
+ );
122
+ }
123
+
124
+ for (const viewName of viewNames) {
125
+ const viewFile = getHtmlFileName(
126
+ viewName,
127
+ 'View'
128
+ );
129
+
130
+ if (!findResource(viewsDirectory, viewFile)) {
131
+ throw new Error(
132
+ `LifeTechOCMS: View not found: ${viewFile}`
133
+ );
134
+ }
135
+
136
+ const assignmentKey = viewFile.toLowerCase();
137
+
138
+ if (assignedViews.has(assignmentKey)) {
139
+ throw new Error(
140
+ `LifeTechOCMS: View assigned more than once: ${viewFile}`
141
+ );
142
+ }
143
+
144
+ assignedViews.add(assignmentKey);
145
+
146
+ pendingLayouts.push({
147
+ layoutName: layoutFile,
148
+ contentName: viewFile,
149
+ layoutPackageName: themeName
150
+ });
151
+ }
152
+ }
153
+
154
+ const destination = path.join(
155
+ outputDirectory,
156
+ 'Storage',
157
+ 'Layouts',
158
+ 'PendingLayouts.json'
159
+ );
160
+
161
+ fs.mkdirSync(
162
+ path.dirname(destination),
163
+ { recursive: true }
164
+ );
165
+
166
+ fs.writeFileSync(
167
+ destination,
168
+ JSON.stringify(pendingLayouts, null, 2) + '\n',
169
+ 'utf8'
170
+ );
171
+
172
+ console.log(
173
+ `[LifeTechOCMS] Generated ${pendingLayouts.length} Layout mappings.`
174
+ );
175
+ }
@@ -0,0 +1,30 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ export function generateThemeMetadata({
5
+ outputDirectory,
6
+ themeName
7
+ }) {
8
+ const metadata = {
9
+ themeName
10
+ };
11
+
12
+ fs.mkdirSync(outputDirectory, {
13
+ recursive: true
14
+ });
15
+
16
+ const destination = path.join(
17
+ outputDirectory,
18
+ 'lifetech-theme.json'
19
+ );
20
+
21
+ fs.writeFileSync(
22
+ destination,
23
+ JSON.stringify(metadata, null, 2) + '\n',
24
+ 'utf8'
25
+ );
26
+
27
+ console.log(
28
+ `[LifeTechOCMS] Generated Theme metadata: ${themeName}`
29
+ );
30
+ }