UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __asyncValues = (this && this.__asyncValues) || function (o) {
16 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17 var m = o[Symbol.asyncIterator], i;
18 return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
19 function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
20 function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
21};
22var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
23var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
24 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25 var g = generator.apply(thisArg, _arguments || []), i, q = [];
26 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
27 function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
28 function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
29 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
30 function fulfill(value) { resume("next", value); }
31 function reject(value) { resume("throw", value); }
32 function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
33};
34Object.defineProperty(exports, "__esModule", { value: true });
35const dom5 = require("dom5/lib/index-next");
36const parse5 = require("parse5");
37const streams_1 = require("./streams");
38const baseMatcher = dom5.predicates.hasTagName('base');
39/**
40 * Find a `<base>` tag in the specified file and if found, update its `href`
41 * with the given new value.
42 */
43class BaseTagUpdater extends streams_1.AsyncTransformStream {
44 constructor(filePath, newHref) {
45 super({ objectMode: true });
46 this.filePath = filePath;
47 this.newHref = newHref;
48 }
49 _transformIter(files) {
50 return __asyncGenerator(this, arguments, function* _transformIter_1() {
51 var e_1, _a;
52 try {
53 for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield __await(files_1.next()), !files_1_1.done;) {
54 const file = files_1_1.value;
55 if (file.path !== this.filePath) {
56 yield yield __await(file);
57 continue;
58 }
59 const contents = yield __await(streams_1.getFileContents(file));
60 const parsed = parse5.parse(contents, { locationInfo: true });
61 const base = dom5.query(parsed, baseMatcher);
62 if (!base || dom5.getAttribute(base, 'href') === this.newHref) {
63 yield yield __await(file);
64 continue;
65 }
66 dom5.setAttribute(base, 'href', this.newHref);
67 dom5.removeFakeRootElements(parsed);
68 const updatedFile = file.clone();
69 updatedFile.contents = Buffer.from(parse5.serialize(parsed), 'utf-8');
70 yield yield __await(updatedFile);
71 }
72 }
73 catch (e_1_1) { e_1 = { error: e_1_1 }; }
74 finally {
75 try {
76 if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield __await(_a.call(files_1));
77 }
78 finally { if (e_1) throw e_1.error; }
79 }
80 });
81 }
82}
83exports.BaseTagUpdater = BaseTagUpdater;
84//# sourceMappingURL=base-tag-updater.js.map
\No newline at end of file