-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup-dev-server.js
More file actions
50 lines (43 loc) · 1.69 KB
/
setup-dev-server.js
File metadata and controls
50 lines (43 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// setup-dev-server.js
// Auto-generates wrapper .jsx files for each .tsx in input/
const fs = require('fs');
const path = require('path');
const inputDir = path.join(__dirname, 'input');
const componentsDir = path.join(__dirname, 'RenderingServer', 'src', 'components');
function toComponentName(tsxFile) {
// Remove extension, convert to PascalCase
return tsxFile
.replace(/\.tsx$/, '')
.split('-')
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
.join('');
}
function jsxWrapperContent(tsxFile) {
return `import React from 'react';\nimport ReactDOM from 'react-dom/client';\nimport Component from '../../../input/${tsxFile}';\nimport '../index.css';\n\nReactDOM.createRoot(document.getElementById('root')).render(\n <React.StrictMode>\n <Component />\n </React.StrictMode>\n);\n`;
}
function main() {
if (!fs.existsSync(inputDir)) {
console.error('Input directory does not exist:', inputDir);
process.exit(1);
}
if (!fs.existsSync(componentsDir)) {
fs.mkdirSync(componentsDir, { recursive: true });
}
const tsxFiles = fs.readdirSync(inputDir).filter(f => f.endsWith('.tsx'));
tsxFiles.forEach(tsxFile => {
const componentName = toComponentName(tsxFile);
const jsxFile = path.join(componentsDir, `${componentName}.jsx`);
// Only write if missing or content is outdated
const newContent = jsxWrapperContent(tsxFile);
let shouldWrite = true;
if (fs.existsSync(jsxFile)) {
const existing = fs.readFileSync(jsxFile, 'utf8');
if (existing === newContent) shouldWrite = false;
}
if (shouldWrite) {
fs.writeFileSync(jsxFile, newContent, 'utf8');
console.log('Generated wrapper:', jsxFile);
}
});
}
main();