-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
run.js
executable file
·78 lines (68 loc) · 2.07 KB
/
run.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
#!/usr/bin/env node
'use strict';
const {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
rmdirSync,
} = require('fs');
const {join} = require('path');
const http = require('http');
const DEPENDENCIES = [
['scheduler/umd/scheduler.development.js', 'scheduler.js'],
['react/umd/react.development.js', 'react.js'],
['react-dom/umd/react-dom.development.js', 'react-dom.js'],
];
const BUILD_DIRECTORY = '../../../build/node_modules/';
const DEPENDENCIES_DIRECTORY = 'dependencies';
function initDependencies() {
if (existsSync(DEPENDENCIES_DIRECTORY)) {
rmdirSync(DEPENDENCIES_DIRECTORY, {recursive: true});
}
mkdirSync(DEPENDENCIES_DIRECTORY);
DEPENDENCIES.forEach(([from, to]) => {
const fromPath = join(__dirname, BUILD_DIRECTORY, from);
const toPath = join(__dirname, DEPENDENCIES_DIRECTORY, to);
console.log(`Copying ${fromPath} => ${toPath}`);
copyFileSync(fromPath, toPath);
});
}
function initServer() {
const host = 'localhost';
const port = 8000;
const requestListener = function (request, response) {
let contents;
switch (request.url) {
case '/react.js':
case '/react-dom.js':
case '/scheduler.js':
response.setHeader('Content-Type', 'text/javascript');
response.writeHead(200);
contents = readFileSync(
join(__dirname, DEPENDENCIES_DIRECTORY, request.url)
);
response.end(contents);
break;
case '/app.js':
response.setHeader('Content-Type', 'text/javascript');
response.writeHead(200);
contents = readFileSync(join(__dirname, 'app.js'));
response.end(contents);
break;
case '/index.html':
default:
response.setHeader('Content-Type', 'text/html');
response.writeHead(200);
contents = readFileSync(join(__dirname, 'index.html'));
response.end(contents);
break;
}
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
}
initDependencies();
initServer();