-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (64 loc) · 2.18 KB
/
server.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require('express');
const fs = require('fs');
const server = express();
const OUTPUT_DEST_DIR = 'output/src/';
function makeRespFn(msg) {
return function(err) {
if(err) {
throw err;
}
console.log(msg);
};
}
// to receive actual
server.use(express.json());
server.post('/make-page', function(request, response) {
console.log(request.body);
// Get the unique list of components so we can write imports
const componentNameMap = {};
request.body.allComponents.map(function(component) {
componentNameMap[component.name] = true;
});
const componentImportList = Object.keys(componentNameMap);
console.log('Component Import List', componentImportList);
// create a files and start writing to it
const outputFile = fs.createWriteStream(OUTPUT_DEST_DIR + 'App.tsx');
// TODO eventually to be able to still add and manipulate things can
// use markers like "//--computer_generated--" to find the code to edit
outputFile.write('//--computer_generated-- import code\n');
const COMPONENTS_DIR = OUTPUT_DEST_DIR + 'components/';
const componentsFolderExists = fs.existsSync(COMPONENTS_DIR);
if(componentsFolderExists) {
console.log('Trying to remove component');
fs.rmSync(COMPONENTS_DIR, { recursive: true }, makeRespFn('Components Dir Del'));
}
fs.mkdirSync(COMPONENTS_DIR, makeRespFn('Components Dir Made'));
// write the import list
for(let idx = 0; idx < componentImportList.length; idx++) {
const file = componentImportList[idx];
// copy the component file over to the new build
fs.copyFile(
`src/components/${file}.tsx`,
`${COMPONENTS_DIR}${file}.tsx`,
makeRespFn(`Component copied: ${file}.tsx`)
);
outputFile.write(`import ${file} from './components/${file}';\n`);
}
outputFile.write('\n\n');
outputFile.write(`function App() {
return (
<div>`);
for(let idx = 0; idx < request.body.allComponents.length; idx++) {
const componentObj = request.body.allComponents[idx];
outputFile.write(`
<${componentObj.name} />`);
}
outputFile.write(`
</div>
);\n}\n`);
outputFile.write('export default App;');
outputFile.end();
response.json({hello: 'world'});
});
server.listen(8080);
console.log('Server started, listening on port 8080');