| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 "use strict"; |
| 6 |
| 7 var childProcess = require("child_process"); |
| 8 const fs = require('fs'); |
| 9 var http = require("http"); |
| 10 const path = require('path'); |
| 11 var parseURL = require("url").parse; |
| 12 |
| 13 const utils = require('../utils'); |
| 14 |
| 15 const FRONTEND_PATH = path.resolve(__dirname, '..', '..', 'front_end'); |
| 16 const OUT_DIR_PATH = path.resolve(__dirname, 'out'); |
| 17 const OUT_FILE_PATH = path.resolve(OUT_DIR_PATH, 'dependencies.dot'); |
| 18 |
| 19 const SERVER_PORT = parseInt(process.env.PORT, 10) || 8001; |
| 20 |
| 21 function main() { |
| 22 generateDot(); |
| 23 try { |
| 24 childProcess.execSync(`dot -O -Tsvg ${OUT_FILE_PATH}`); |
| 25 } catch (err) { |
| 26 console.log(`Could not generate dot svg because: ${err}`); |
| 27 console.log('Make sure you have graphviz installed (e.g. sudo apt install gr
aphviz)'); |
| 28 return; |
| 29 } |
| 30 console.log('Generated dot file & svg'); |
| 31 startServer(); |
| 32 console.log(`Go to: http://localhost:${SERVER_PORT}/jquery_svg.html`); |
| 33 } |
| 34 |
| 35 function generateDot() { |
| 36 if (!utils.isDir(OUT_DIR_PATH)) |
| 37 fs.mkdirSync(OUT_DIR_PATH); |
| 38 const modules = new Set(); |
| 39 const moduleToDependencyList = ['digraph dependencies {']; |
| 40 moduleToDependencyList.push('fixedsize = true;'); |
| 41 fs.readdirSync(FRONTEND_PATH).forEach(function(file) { |
| 42 const moduleJSONPath = path.join(FRONTEND_PATH, file, 'module.json'); |
| 43 if (fs.statSync(path.join(FRONTEND_PATH, file)).isDirectory() && |
| 44 utils.isFile(moduleJSONPath)) { |
| 45 const module = file; |
| 46 if (module === 'audits2_worker') |
| 47 return; |
| 48 modules.add(module); |
| 49 const moduleJSON = require(moduleJSONPath); |
| 50 let moduleSize = 0; |
| 51 |
| 52 let resources = (moduleJSON.scripts || []).concat((moduleJSON.resources ||
[])); |
| 53 for (let script of resources) { |
| 54 if (fs.existsSync(path.join(FRONTEND_PATH, module, script))) { |
| 55 moduleSize += fs.statSync(path.join(FRONTEND_PATH, module, script)).si
ze; |
| 56 } |
| 57 } |
| 58 moduleSize /= 200000; |
| 59 moduleSize = Math.max(0.5, moduleSize); |
| 60 let fontSize = Math.max(moduleSize * 14, 14); |
| 61 |
| 62 moduleToDependencyList.push(`${module} [width=${moduleSize}, height=${modu
leSize} fontsize=${fontSize}];`); |
| 63 |
| 64 if (moduleJSON.dependencies) { |
| 65 for (let d of moduleJSON.dependencies) { |
| 66 moduleToDependencyList.push(` ${module} -> ${d}`); |
| 67 } |
| 68 } |
| 69 } |
| 70 }); |
| 71 moduleToDependencyList.push('}'); |
| 72 const content = moduleToDependencyList.join('\n'); |
| 73 fs.writeFileSync(OUT_FILE_PATH, content); |
| 74 } |
| 75 |
| 76 function startServer() { |
| 77 http.createServer(requestHandler).listen(SERVER_PORT); |
| 78 |
| 79 function requestHandler(request, response) { |
| 80 var filePath = parseURL(request.url).pathname; |
| 81 var absoluteFilePath = path.join(__dirname, filePath); |
| 82 if (!path.resolve(absoluteFilePath).startsWith(__dirname)) { |
| 83 console.log(`File requested is outside of folder: ${__dirname}`); |
| 84 sendResponse(403, `403 - Access denied. File requested is outside of folde
r: ${__dirname}`); |
| 85 return; |
| 86 } |
| 87 |
| 88 fs.exists(absoluteFilePath, fsExistsCallback); |
| 89 |
| 90 function fsExistsCallback(fileExists) { |
| 91 if (!fileExists) { |
| 92 console.log(`Cannot find file ${absoluteFilePath}`); |
| 93 sendResponse(404, "404 - File not found"); |
| 94 return; |
| 95 } |
| 96 fs.readFile(absoluteFilePath, "binary", readFileCallback); |
| 97 } |
| 98 |
| 99 function readFileCallback(err, file) { |
| 100 if (err) { |
| 101 console.log(`Unable to read local file ${absoluteFilePath}:`, err); |
| 102 sendResponse(500, "500 - Internal Server Error"); |
| 103 return; |
| 104 } |
| 105 sendResponse(200, file); |
| 106 } |
| 107 |
| 108 function sendResponse(statusCode, data) { |
| 109 response.writeHead(statusCode); |
| 110 response.write(data, "binary"); |
| 111 response.end(); |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 if (require.main === module) |
| 117 main(); |
| OLD | NEW |