|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const { |
| 6 | + copyFileSync, |
| 7 | + existsSync, |
| 8 | + mkdirSync, |
| 9 | + readFileSync, |
| 10 | + rmdirSync, |
| 11 | +} = require('fs'); |
| 12 | +const {join} = require('path'); |
| 13 | +const http = require('http'); |
| 14 | + |
| 15 | +const DEPENDENCIES = [ |
| 16 | + ['scheduler/umd/scheduler.development.js', 'scheduler.js'], |
| 17 | + ['react/umd/react.development.js', 'react.js'], |
| 18 | + ['react-dom/umd/react-dom.development.js', 'react-dom.js'], |
| 19 | +]; |
| 20 | + |
| 21 | +const BUILD_DIRECTORY = '../../../build/node_modules/'; |
| 22 | +const DEPENDENCIES_DIRECTORY = 'dependencies'; |
| 23 | + |
| 24 | +function initDependencies() { |
| 25 | + if (existsSync(DEPENDENCIES_DIRECTORY)) { |
| 26 | + rmdirSync(DEPENDENCIES_DIRECTORY, {recursive: true}); |
| 27 | + } |
| 28 | + mkdirSync(DEPENDENCIES_DIRECTORY); |
| 29 | + |
| 30 | + DEPENDENCIES.forEach(([from, to]) => { |
| 31 | + const fromPath = join(__dirname, BUILD_DIRECTORY, from); |
| 32 | + const toPath = join(__dirname, DEPENDENCIES_DIRECTORY, to); |
| 33 | + console.log(`Copying ${fromPath} => ${toPath}`); |
| 34 | + copyFileSync(fromPath, toPath); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function initServer() { |
| 39 | + const host = 'localhost'; |
| 40 | + const port = 8000; |
| 41 | + |
| 42 | + const requestListener = function(request, response) { |
| 43 | + let contents; |
| 44 | + switch (request.url) { |
| 45 | + case '/react.js': |
| 46 | + case '/react-dom.js': |
| 47 | + case '/scheduler.js': |
| 48 | + response.setHeader('Content-Type', 'text/javascript'); |
| 49 | + response.writeHead(200); |
| 50 | + contents = readFileSync( |
| 51 | + join(__dirname, DEPENDENCIES_DIRECTORY, request.url) |
| 52 | + ); |
| 53 | + response.end(contents); |
| 54 | + break; |
| 55 | + case '/app.js': |
| 56 | + response.setHeader('Content-Type', 'text/javascript'); |
| 57 | + response.writeHead(200); |
| 58 | + contents = readFileSync(join(__dirname, 'app.js')); |
| 59 | + response.end(contents); |
| 60 | + break; |
| 61 | + case '/index.html': |
| 62 | + default: |
| 63 | + response.setHeader('Content-Type', 'text/html'); |
| 64 | + response.writeHead(200); |
| 65 | + contents = readFileSync(join(__dirname, 'index.html')); |
| 66 | + response.end(contents); |
| 67 | + break; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const server = http.createServer(requestListener); |
| 72 | + server.listen(port, host, () => { |
| 73 | + console.log(`Server is running on http://${host}:${port}`); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +initDependencies(); |
| 78 | +initServer(); |
0 commit comments