UNPKG

@dintero/sri-to-dist

Version:

HTML tool for adding subresource integrity hashes

75 lines (74 loc) 3.11 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); const commander_1 = require("commander"); const package_json_1 = require("../package.json"); const lib_1 = require("./lib"); const main = async () => { try { // Set up command-line parser const program = new commander_1.Command(); program .version(package_json_1.version) .description("HTML processing tool to add subresource integrity hashes") .requiredOption("-i, --input <file>", "Input HTML file") .option("-o, --output <file>", "Optional output HTML file") .option("-b, --base-url <url>", "Optional base URL") .option("-n, --no-remote", "Optional flag, no remote sri files allowed") .option("-v, --verify", "Optional flag, verify hashes in input"); program.parse(process.argv); const options = program.opts(); // Extract values const inputPath = options.input; const outputPath = options.output; const baseUrl = options.baseUrl; const noRemote = options.noRemote || false; const verify = options.verify || false; // Read HTML file const htmlContent = fs.readFileSync(inputPath, "utf-8"); const updatedHtml = await (0, lib_1.toHtmlWithSri)(htmlContent, path.dirname(inputPath), baseUrl, noRemote, verify); (0, lib_1.handleUpdatedHtml)(process.stdout, outputPath, updatedHtml); } catch (error) { console.error(`Error: ${error}`); process.exit(1); } }; main().catch((error) => { console.error(`Error: ${error}`); process.exit(1); });