Tim van der Lippe | 96e0562 | 2020-09-15 10:42:01 | [diff] [blame] | 1 | // Copyright 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 path = require('path'); |
| 7 | const {argv} = require('yargs'); |
Tim van der Lippe | 28af9fb | 2022-01-13 14:42:40 | [diff] [blame] | 8 | const {writeIfChanged} = require('./ninja/write-if-changed.js'); |
Tim van der Lippe | 96e0562 | 2020-09-15 10:42:01 | [diff] [blame] | 9 | |
Tim van der Lippe | 96e0562 | 2020-09-15 10:42:01 | [diff] [blame] | 10 | const {template} = argv; |
| 11 | |
| 12 | if (!template) { |
| 13 | throw new Error('Must specify --template location with the location of the HTML entrypoint template.'); |
| 14 | } |
| 15 | |
| 16 | const {outDirectory} = argv; |
| 17 | |
| 18 | if (!outDirectory) { |
| 19 | throw new Error('Must specify --out-directory location where the outputs must live.'); |
| 20 | } |
| 21 | |
Tim van der Lippe | 0f7a51b | 2021-07-12 15:31:57 | [diff] [blame] | 22 | const {entrypoints} = argv; |
| 23 | |
| 24 | if (!entrypoints) { |
| 25 | throw new Error('Must specify at least one entrypoint name.'); |
| 26 | } |
| 27 | |
| 28 | if (!Array.isArray(entrypoints)) { |
| 29 | throw new Error('Must specify multiple entrypoints as array'); |
| 30 | } |
| 31 | |
Tim van der Lippe | 96e0562 | 2020-09-15 10:42:01 | [diff] [blame] | 32 | const templateContent = fs.readFileSync(template, 'utf-8'); |
| 33 | |
Tim van der Lippe | 0f7a51b | 2021-07-12 15:31:57 | [diff] [blame] | 34 | for (const entrypoint of entrypoints) { |
Tim van der Lippe | 7b347ca | 2021-04-09 15:59:21 | [diff] [blame] | 35 | const rewrittenTemplateContent = templateContent.replace(new RegExp('%ENTRYPOINT_NAME%', 'g'), entrypoint); |
Tim van der Lippe | 28af9fb | 2022-01-13 14:42:40 | [diff] [blame] | 36 | writeIfChanged(path.join(outDirectory, `${entrypoint}.html`), rewrittenTemplateContent); |
Tim van der Lippe | 96e0562 | 2020-09-15 10:42:01 | [diff] [blame] | 37 | } |