Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2020 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import * as fs from "fs"; |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 19 | import RewritingStream from "parse5-html-rewriting-stream"; |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 20 | import * as dom5 from "dom5"; |
| 21 | import {HtmlFileUtils, RedirectsResolver} from "./utils"; |
| 22 | import {Node} from 'dom5'; |
| 23 | import {readMultilineParamFile} from "../utils/command-line"; |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 24 | import { fail } from "../utils/common"; |
| 25 | import {JSONRedirects} from "./redirects"; |
| 26 | |
| 27 | /** Update links in HTML file |
| 28 | * input_output_param_files - is a list of paths; each path is placed on a separate line |
| 29 | * The first line is the path to a first input file (relative to process working directory) |
| 30 | * The second line is the path to the output file (relative to process working directory) |
| 31 | * The next 2 lines describe the second file and so on. |
| 32 | * redirectFile.json describes how to update links (see {@link JSONRedirects} for exact format) |
| 33 | * Additionaly, update some test links (related to web-component-tester) |
| 34 | */ |
| 35 | |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 36 | async function main() { |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 37 | if (process.argv.length < 4) { |
| 38 | console.info("Usage:\n\tnode links_updater.js input_output_param_files redirectFile.json\n"); |
| 39 | process.exit(1); |
| 40 | } |
| 41 | |
| 42 | const jsonRedirects: JSONRedirects = JSON.parse(fs.readFileSync(process.argv[3], {encoding: "utf-8"})); |
| 43 | const redirectsResolver = new RedirectsResolver(jsonRedirects.redirects); |
| 44 | |
| 45 | const input = readMultilineParamFile(process.argv[2]); |
| 46 | const updater = new HtmlFileUpdater(redirectsResolver); |
| 47 | for(let i = 0; i < input.length; i += 2) { |
| 48 | const srcFile = input[i]; |
| 49 | const targetFile = input[i + 1]; |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 50 | await updater.updateFile(srcFile, targetFile); |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | /** Update all links in HTML file based on redirects. |
| 55 | * Additionally, update references to web-component-tester */ |
| 56 | class HtmlFileUpdater { |
| 57 | private static readonly Predicates = { |
| 58 | isScriptWithSrcTag: (node: Node) => node.tagName === "script" && dom5.hasAttribute(node, "src"), |
| 59 | |
| 60 | isWebComponentTesterImport: (node: Node) => HtmlFileUpdater.Predicates.isScriptWithSrcTag(node) && |
| 61 | dom5.getAttribute(node, "src")!.endsWith("/bower_components/web-component-tester/browser.js"), |
| 62 | |
| 63 | isHtmlImport: (node: Node) => node.tagName === "link" && dom5.getAttribute(node, "rel") === "import" && |
| 64 | dom5.hasAttribute(node, "href") |
| 65 | }; |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 66 | |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 67 | public constructor(private readonly redirectsResolver: RedirectsResolver) { |
| 68 | } |
| 69 | |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 70 | public async updateFile(srcFile: string, targetFile: string) { |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 71 | const html = fs.readFileSync(srcFile, "utf-8"); |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 72 | const readStream = fs.createReadStream(srcFile, {encoding: "utf-8"}); |
| 73 | const rewriterOutput = srcFile === targetFile ? targetFile + ".tmp" : targetFile; |
| 74 | const writeStream = fs.createWriteStream(rewriterOutput, {encoding: "utf-8"}); |
| 75 | const rewriter = new RewritingStream(); |
| 76 | (rewriter as any).tokenizer.preprocessor.bufferWaterline = Infinity; |
| 77 | rewriter.on("startTag", (tag: any) => { |
| 78 | if (HtmlFileUpdater.Predicates.isWebComponentTesterImport(tag)) { |
| 79 | dom5.setAttribute(tag, "src", "/components/wct-browser-legacy/browser.js"); |
| 80 | } else if (HtmlFileUpdater.Predicates.isHtmlImport(tag)) { |
| 81 | this.updateRefAttribute(tag, srcFile, "href"); |
| 82 | } else if (HtmlFileUpdater.Predicates.isScriptWithSrcTag(tag)) { |
| 83 | this.updateRefAttribute(tag, srcFile, "src"); |
| 84 | } else { |
| 85 | const location = tag.sourceCodeLocation; |
| 86 | const raw = html.substring(location.startOffset, location.endOffset); |
| 87 | rewriter.emitRaw(raw); |
| 88 | return; |
| 89 | } |
| 90 | rewriter.emitStartTag(tag); |
| 91 | }); |
| 92 | return new Promise<void>((resolve, reject) => { |
| 93 | writeStream.on("close", () => { |
| 94 | writeStream.close(); |
| 95 | if (rewriterOutput !== targetFile) { |
| 96 | fs.renameSync(rewriterOutput, targetFile); |
| 97 | } |
| 98 | resolve(); |
| 99 | }); |
| 100 | readStream.pipe(rewriter).pipe(writeStream); |
| 101 | }); |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private getResolvedPath(parentHtml: string, href: string) { |
| 105 | const originalPath = '/' + HtmlFileUtils.getPathRelativeToRoot(parentHtml, href); |
| 106 | |
| 107 | const resolvedInfo = this.redirectsResolver.resolve(originalPath, true); |
| 108 | if (!resolvedInfo.insideNodeModules && resolvedInfo.target === originalPath) { |
| 109 | return href; |
| 110 | } |
| 111 | if (resolvedInfo.insideNodeModules) { |
| 112 | return '/node_modules/' + resolvedInfo.target; |
| 113 | } |
| 114 | if (href.startsWith('/')) { |
| 115 | return resolvedInfo.target; |
| 116 | } |
| 117 | return HtmlFileUtils.getPathRelativeToRoot(parentHtml, resolvedInfo.target); |
| 118 | } |
| 119 | |
| 120 | private updateRefAttribute(node: Node, parentHtml: string, attributeName: string) { |
| 121 | const ref = dom5.getAttribute(node, attributeName); |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 122 | if (!ref) { |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 123 | fail(`Internal error - ${node} in ${parentHtml} doesn't have attribute ${attributeName}`); |
| 124 | } |
| 125 | const newRef = this.getResolvedPath(parentHtml, ref); |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 126 | if (newRef === ref) { |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 127 | return; |
| 128 | } |
Dmitrii Filippov | 36c8a8d | 2020-02-25 16:49:08 +0100 | [diff] [blame] | 129 | dom5.setAttribute(node, attributeName, newRef); |
Dmitrii Filippov | fbdc89d | 2020-01-20 19:38:06 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | main(); |