Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 1 | // Copyright (c) 2020 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 | const fs = require('fs'); |
| 6 | const http = require('http'); |
| 7 | const path = require('path'); |
| 8 | const parseURL = require('url').parse; |
| 9 | const {argv} = require('yargs'); |
| 10 | |
| 11 | const serverPort = parseInt(process.env.PORT, 10) || 8090; |
| 12 | |
| 13 | const target = argv.target || 'Default'; |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * When you run npm run components-server we run the script as is from scripts/, |
| 17 | * but when this server is run as part of a test suite it's run from |
| 18 | * out/Default/gen/scripts, so we have to do a bit of path mangling to figure |
| 19 | * out where we are. |
| 20 | */ |
| 21 | const isRunningInGen = __dirname.includes(path.join(path.sep, 'gen', path.sep, 'scripts')); |
Jack Franklin | b36ad7e | 2020-12-07 10:20:52 +0000 | [diff] [blame] | 22 | |
| 23 | const pathToBuiltOutTargetDirectory = isRunningInGen ? path.resolve(path.join(__dirname), '..', '..', '..') : |
| 24 | path.resolve(path.join(__dirname, '..', '..', 'out', target)); |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 25 | const devtoolsRootFolder = path.resolve(path.join(pathToBuiltOutTargetDirectory, 'gen')); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 26 | |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 27 | if (!fs.existsSync(devtoolsRootFolder)) { |
| 28 | console.error(`ERROR: Generated front_end folder (${devtoolsRootFolder}) does not exist.`); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 29 | console.log( |
| 30 | 'The components server works from the built Ninja output; you may need to run Ninja to update your built DevTools.'); |
| 31 | console.log('If you build to a target other than default, you need to pass --target=X as an argument'); |
| 32 | process.exit(1); |
| 33 | } |
| 34 | |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 35 | const server = http.createServer(requestHandler); |
| 36 | server.listen(serverPort); |
| 37 | server.once('listening', () => { |
| 38 | if (process.send) { |
| 39 | process.send(serverPort); |
| 40 | } |
| 41 | console.log(`Started components server at http://localhost:${serverPort}\n`); |
| 42 | }); |
| 43 | |
| 44 | server.once('error', error => { |
| 45 | if (process.send) { |
| 46 | process.send('ERROR'); |
| 47 | } |
| 48 | throw error; |
| 49 | }); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 50 | |
| 51 | function createComponentIndexFile(componentPath, componentExamples) { |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 52 | const componentName = componentPath.replace('/front_end/component_docs/', '').replace(/_/g, ' '); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 53 | // clang-format off |
| 54 | return `<!DOCTYPE html> |
| 55 | <html> |
| 56 | <head> |
| 57 | <meta charset="UTF-8" /> |
| 58 | <meta name="viewport" content="width=device-width" /> |
| 59 | <title>DevTools component: ${componentName}</title> |
| 60 | <style> |
| 61 | h1 { text-transform: capitalize; } |
| 62 | |
| 63 | .example { |
| 64 | padding: 5px; |
| 65 | margin: 10px; |
| 66 | } |
| 67 | iframe { display: block; width: 100%; } |
| 68 | </style> |
| 69 | </head> |
| 70 | <body> |
| 71 | <h1>${componentName}</h1> |
| 72 | ${componentExamples.map(example => { |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 73 | const fullPath = path.join(componentPath, example); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 74 | return `<div class="example"> |
| 75 | <h3><a href="${fullPath}">${example}</a></h3> |
| 76 | <iframe src="${fullPath}"></iframe> |
| 77 | </div>`; |
| 78 | }).join('\n')} |
| 79 | </body> |
| 80 | </html>`; |
| 81 | // clang-format on |
| 82 | } |
| 83 | |
| 84 | function createServerIndexFile(componentNames) { |
| 85 | // clang-format off |
| 86 | return `<!DOCTYPE html> |
| 87 | <html> |
| 88 | <head> |
| 89 | <meta charset="UTF-8" /> |
| 90 | <meta name="viewport" content="width=device-width" /> |
| 91 | <title>DevTools components</title> |
| 92 | <style> |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 93 | a:link, a:visited { |
| 94 | color: blue; |
| 95 | text-transform: capitalize; |
| 96 | text-decoration: none; |
| 97 | } |
| 98 | a:hover { |
| 99 | text-decoration: underline; |
| 100 | } |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 101 | </style> |
| 102 | </head> |
| 103 | <body> |
| 104 | <h1>DevTools components</h1> |
| 105 | <ul> |
| 106 | ${componentNames.map(name => { |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 107 | const niceName = name.replace(/_/g, ' '); |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 108 | return `<li><a href='/front_end/component_docs/${name}'>${niceName}</a></li>`; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 109 | }).join('\n')} |
| 110 | </ul> |
| 111 | </body> |
| 112 | </html>`; |
| 113 | // clang-format on |
| 114 | } |
| 115 | |
| 116 | async function getExamplesForPath(filePath) { |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 117 | const componentDirectory = path.join(devtoolsRootFolder, filePath); |
Jack Franklin | c150122 | 2020-10-02 09:42:08 +0100 | [diff] [blame] | 118 | const allFiles = await fs.promises.readdir(componentDirectory); |
| 119 | const htmlExampleFiles = allFiles.filter(file => { |
| 120 | return path.extname(file) === '.html'; |
| 121 | }); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 122 | |
Jack Franklin | c150122 | 2020-10-02 09:42:08 +0100 | [diff] [blame] | 123 | return createComponentIndexFile(filePath, htmlExampleFiles); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | function respondWithHtml(response, html) { |
| 127 | response.setHeader('Content-Type', 'text/html; charset=utf-8'); |
| 128 | response.writeHead(200); |
| 129 | response.write(html, 'utf8'); |
| 130 | response.end(); |
| 131 | } |
| 132 | |
| 133 | function send404(response, message) { |
| 134 | response.writeHead(404); |
| 135 | response.write(message, 'utf8'); |
| 136 | response.end(); |
| 137 | } |
| 138 | |
Jack Franklin | 279564e | 2020-07-06 15:25:18 +0100 | [diff] [blame] | 139 | async function checkFileExists(filePath) { |
| 140 | try { |
| 141 | const errorsAccessingFile = await fs.promises.access(filePath, fs.constants.R_OK); |
| 142 | return !errorsAccessingFile; |
| 143 | } catch (e) { |
| 144 | return false; |
| 145 | } |
| 146 | } |
| 147 | |
Jack Franklin | 9d4ecf7 | 2020-09-16 14:04:30 +0100 | [diff] [blame] | 148 | /** |
| 149 | * In Devtools-Frontend we load images without a leading slash, e.g. |
| 150 | * url(Images/checker.png). This works within devtools, but breaks this component |
Jack Franklin | 4738bc7 | 2020-09-17 09:51:24 +0100 | [diff] [blame] | 151 | * server as the path ends up as /component_docs/my_component/Images/checker.png. |
Jack Franklin | 9d4ecf7 | 2020-09-16 14:04:30 +0100 | [diff] [blame] | 152 | * So we check if the path ends in Images/*.* and if so, remove anything before |
| 153 | * it. Then it will be resolved correctly. |
| 154 | */ |
| 155 | function normalizeImagePathIfRequired(filePath) { |
| 156 | const imagePathRegex = /\/Images\/(\S+)\.(\w{3})/; |
| 157 | const match = imagePathRegex.exec(filePath); |
| 158 | if (!match) { |
| 159 | return filePath; |
| 160 | } |
| 161 | |
| 162 | const [, imageName, imageExt] = match; |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 163 | const normalizedPath = path.join('front_end', 'Images', `${imageName}.${imageExt}`); |
Jack Franklin | 9d4ecf7 | 2020-09-16 14:04:30 +0100 | [diff] [blame] | 164 | return normalizedPath; |
| 165 | } |
| 166 | |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 167 | async function requestHandler(request, response) { |
| 168 | const filePath = parseURL(request.url).pathname; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 169 | if (filePath === '/favicon.ico') { |
| 170 | send404(response, '404, no favicon'); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if (filePath === '/' || filePath === '/index.html') { |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 175 | const components = await fs.promises.readdir(path.join(devtoolsRootFolder, 'front_end', 'component_docs')); |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 176 | const html = createServerIndexFile(components.filter(filePath => { |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 177 | const stats = fs.lstatSync(path.join(devtoolsRootFolder, 'front_end', 'component_docs', filePath)); |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 178 | // Filter out some build config files (tsconfig, d.ts, etc), and just list the directories. |
| 179 | return stats.isDirectory(); |
| 180 | })); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 181 | respondWithHtml(response, html); |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 182 | } else if (filePath.startsWith('/front_end/component_docs') && path.extname(filePath) === '') { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 183 | // This means it's a component path like /breadcrumbs. |
| 184 | const componentHtml = await getExamplesForPath(filePath); |
| 185 | respondWithHtml(response, componentHtml); |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 186 | return; |
| 187 | } else if (/component_docs\/(.+)\/(.+)\.html/.test(filePath)) { |
| 188 | /** This conditional checks if we are viewing an individual example's HTML |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 189 | * file. e.g. localhost:8090/front_end/component_docs/data_grid/basic.html For each |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 190 | * example we inject themeColors.css into the page so all CSS variables |
| 191 | * that components use are available. |
| 192 | */ |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 193 | const fileContents = await fs.promises.readFile(path.join(devtoolsRootFolder, filePath), {encoding: 'utf8'}); |
| 194 | const themeColoursLink = '<link rel="stylesheet" href="/front_end/ui/themeColors.css" type="text/css" />'; |
| 195 | const toggleDarkModeScript = '<script type="module" src="/front_end/component_docs/component_docs.js"></script>'; |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 196 | const newFileContents = fileContents.replace('<style>', themeColoursLink + '\n<style>') |
| 197 | .replace('<script', toggleDarkModeScript + '\n<script'); |
| 198 | respondWithHtml(response, newFileContents); |
| 199 | |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 200 | } else { |
Jack Franklin | 9d4ecf7 | 2020-09-16 14:04:30 +0100 | [diff] [blame] | 201 | // This means it's an asset like a JS file or an image. |
| 202 | const normalizedPath = normalizeImagePathIfRequired(filePath); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 203 | |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 204 | let fullPath = path.join(devtoolsRootFolder, normalizedPath); |
Jack Franklin | d034512 | 2020-12-21 09:12:04 +0000 | [diff] [blame] | 205 | if (fullPath.endsWith(path.join('locales', 'en-US.json'))) { |
| 206 | // Rewrite this path so we can load up the locale in the component-docs |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 207 | fullPath = path.join(devtoolsRootFolder, 'front_end', 'i18n', 'locales', 'en-US.json'); |
Jack Franklin | d034512 | 2020-12-21 09:12:04 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Jack Franklin | 6bdb362 | 2020-12-22 15:18:46 +0000 | [diff] [blame^] | 210 | if (!fullPath.startsWith(devtoolsRootFolder) && !fileIsInTestFolder) { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 211 | console.error(`Path ${fullPath} is outside the DevTools Frontend root dir.`); |
| 212 | process.exit(1); |
| 213 | } |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 214 | |
Jack Franklin | 279564e | 2020-07-06 15:25:18 +0100 | [diff] [blame] | 215 | const fileExists = await checkFileExists(fullPath); |
| 216 | |
| 217 | if (!fileExists) { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 218 | send404(response, '404, File not found'); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | let encoding = 'utf8'; |
| 223 | if (fullPath.endsWith('.wasm') || fullPath.endsWith('.png') || fullPath.endsWith('.jpg')) { |
| 224 | encoding = 'binary'; |
| 225 | } |
| 226 | |
| 227 | const fileContents = await fs.promises.readFile(fullPath, encoding); |
| 228 | |
| 229 | encoding = 'utf8'; |
| 230 | if (fullPath.endsWith('.js')) { |
| 231 | response.setHeader('Content-Type', 'text/javascript; charset=utf-8'); |
| 232 | } else if (fullPath.endsWith('.css')) { |
| 233 | response.setHeader('Content-Type', 'text/css; charset=utf-8'); |
| 234 | } else if (fullPath.endsWith('.wasm')) { |
| 235 | response.setHeader('Content-Type', 'application/wasm'); |
| 236 | encoding = 'binary'; |
| 237 | } else if (fullPath.endsWith('.svg')) { |
| 238 | response.setHeader('Content-Type', 'image/svg+xml; charset=utf-8'); |
| 239 | } else if (fullPath.endsWith('.png')) { |
| 240 | response.setHeader('Content-Type', 'image/png'); |
| 241 | encoding = 'binary'; |
| 242 | } else if (fullPath.endsWith('.jpg')) { |
| 243 | response.setHeader('Content-Type', 'image/jpg'); |
| 244 | encoding = 'binary'; |
| 245 | } |
| 246 | |
| 247 | response.writeHead(200); |
| 248 | response.write(fileContents, encoding); |
| 249 | response.end(); |
| 250 | } |
| 251 | } |