blob: 24e445d2d5c0d046e8b049b6a430911420094133 [file] [log] [blame]
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +01001/**
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
18import * as fs from "fs";
Dmitrii Filippov36c8a8d2020-02-25 16:49:08 +010019import RewritingStream from "parse5-html-rewriting-stream";
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010020import * as dom5 from "dom5";
21import {HtmlFileUtils, RedirectsResolver} from "./utils";
22import {Node} from 'dom5';
23import {readMultilineParamFile} from "../utils/command-line";
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010024import { fail } from "../utils/common";
25import {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 Filippov36c8a8d2020-02-25 16:49:08 +010036async function main() {
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010037 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 Filippov36c8a8d2020-02-25 16:49:08 +010050 await updater.updateFile(srcFile, targetFile);
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010051 }
52}
53
54/** Update all links in HTML file based on redirects.
55 * Additionally, update references to web-component-tester */
56class 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 Filippov36c8a8d2020-02-25 16:49:08 +010066
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010067 public constructor(private readonly redirectsResolver: RedirectsResolver) {
68 }
69
Dmitrii Filippov36c8a8d2020-02-25 16:49:08 +010070 public async updateFile(srcFile: string, targetFile: string) {
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +010071 const html = fs.readFileSync(srcFile, "utf-8");
Dmitrii Filippov36c8a8d2020-02-25 16:49:08 +010072 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 Filippovfbdc89d2020-01-20 19:38:06 +0100102 }
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 Filippov36c8a8d2020-02-25 16:49:08 +0100122 if (!ref) {
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +0100123 fail(`Internal error - ${node} in ${parentHtml} doesn't have attribute ${attributeName}`);
124 }
125 const newRef = this.getResolvedPath(parentHtml, ref);
Dmitrii Filippov36c8a8d2020-02-25 16:49:08 +0100126 if (newRef === ref) {
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +0100127 return;
128 }
Dmitrii Filippov36c8a8d2020-02-25 16:49:08 +0100129 dom5.setAttribute(node, attributeName, newRef);
Dmitrii Filippovfbdc89d2020-01-20 19:38:06 +0100130 }
131}
132
133main();