From 36c8a8dc6027e751109d88536d4289a577b9a0bb Mon Sep 17 00:00:00 2001 From: Dmitrii Filippov Date: Tue, 25 Feb 2020 16:49:08 +0100 Subject: [PATCH] Preserve html file formatting in links-updater.ts Initial implementation of the links-updater.ts changes file formatting - it doesn't preserve line breaks inside tags. This change update links-updater.ts to avoid unnessecary formatting change. Change-Id: Ifccf588c63eefe69782e059fa63f3d1b70f570b9 --- tools/node_tools/package.json | 4 +- .../links-updater.ts | 66 +++++++++++-------- .../rollup.config.js | 9 ++- tools/node_tools/yarn.lock | 40 +++++++++++ 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/tools/node_tools/package.json b/tools/node_tools/package.json index c25d02e774..2af06b458d 100644 --- a/tools/node_tools/package.json +++ b/tools/node_tools/package.json @@ -3,12 +3,14 @@ "description": "Gerrit Build Tools", "browser": false, "dependencies": { - "@types/parse5": "^4.0.0", "@bazel/rollup": "^0.41.0", "@bazel/typescript": "^1.0.1", "@types/node": "^10.17.12", + "@types/parse5": "^4.0.0", + "@types/parse5-html-rewriting-stream": "^5.1.2", "crisper": "^2.1.1", "dom5": "^3.0.1", + "parse5-html-rewriting-stream": "^5.1.1", "polymer-bundler": "^4.0.10", "polymer-cli": "^1.9.11", "rollup": "^1.27.5", diff --git a/tools/node_tools/polygerrit_app_preprocessor/links-updater.ts b/tools/node_tools/polygerrit_app_preprocessor/links-updater.ts index eb7212be50..24e445d2d5 100644 --- a/tools/node_tools/polygerrit_app_preprocessor/links-updater.ts +++ b/tools/node_tools/polygerrit_app_preprocessor/links-updater.ts @@ -16,12 +16,11 @@ */ import * as fs from "fs"; -import * as parse5 from "parse5"; +import RewritingStream from "parse5-html-rewriting-stream"; import * as dom5 from "dom5"; import {HtmlFileUtils, RedirectsResolver} from "./utils"; import {Node} from 'dom5'; import {readMultilineParamFile} from "../utils/command-line"; -import {FileUtils} from "../utils/file-utils"; import { fail } from "../utils/common"; import {JSONRedirects} from "./redirects"; @@ -34,9 +33,7 @@ import {JSONRedirects} from "./redirects"; * Additionaly, update some test links (related to web-component-tester) */ -function main() { - console.log(process.cwd()); - +async function main() { if (process.argv.length < 4) { console.info("Usage:\n\tnode links_updater.js input_output_param_files redirectFile.json\n"); process.exit(1); @@ -50,7 +47,7 @@ function main() { for(let i = 0; i < input.length; i += 2) { const srcFile = input[i]; const targetFile = input[i + 1]; - updater.updateFile(srcFile, targetFile); + await updater.updateFile(srcFile, targetFile); } } @@ -66,29 +63,42 @@ class HtmlFileUpdater { isHtmlImport: (node: Node) => node.tagName === "link" && dom5.getAttribute(node, "rel") === "import" && dom5.hasAttribute(node, "href") }; + public constructor(private readonly redirectsResolver: RedirectsResolver) { } - public updateFile(srcFile: string, targetFile: string) { + public async updateFile(srcFile: string, targetFile: string) { const html = fs.readFileSync(srcFile, "utf-8"); - const ast = parse5.parseFragment(html, {locationInfo: true}) as Node; - - - const webComponentTesterImportNode = dom5.query(ast, HtmlFileUpdater.Predicates.isWebComponentTesterImport); - if(webComponentTesterImportNode) { - dom5.setAttribute(webComponentTesterImportNode, "src", "/components/wct-browser-legacy/browser.js"); - } - - // Update all HTML imports - const updateHtmlImportHref = (htmlImportNode: Node) => this.updateRefAttribute(htmlImportNode, srcFile, "href"); - dom5.queryAll(ast, HtmlFileUpdater.Predicates.isHtmlImport).forEach(updateHtmlImportHref); - - // Update all