#!/usr/bin/env node const yargs = require('yargs') .usage('$0 --root -o [options] ') .wrap(72) .options('h', { alias: 'help', describe: 'Show this help', type: 'boolean', default: false, }) .options('algorithm', { describe: 'The hash algorithm to use', type: 'string', default: 'sha256', demand: false, }) .options('root', { describe: 'Path to your web root (will be deduced from your input files if not specified)', type: 'string', demand: false, }) .options('output', { alias: ['o', 'outroot'], describe: 'Path to the output folder (will be generated if non-existing). Defaults to overwrite the source files in-place', type: 'string', demand: false, }); const commandLineOptions = yargs.argv; if (commandLineOptions.h) { yargs.showHelp(); process.exit(0); } const AssetGraph = require('../lib/AssetGraph'); const urlTools = require('urltools'); const output = commandLineOptions.output && urlTools.fsDirToFileUrl(commandLineOptions.output); let rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true); let inputUrls; if (commandLineOptions._.length > 0) { inputUrls = commandLineOptions._.map(function (urlOrFsPath) { return urlTools.urlOrFsPathToUrl(String(urlOrFsPath), false); }); if (!rootUrl) { rootUrl = urlTools.findCommonUrlPrefix( inputUrls.filter((inputUrl) => /^file:/.test(inputUrl)) ); if (rootUrl) { console.warn('Guessing --root from input files: ' + rootUrl); } } } else if (rootUrl && /^file:/.test(rootUrl)) { inputUrls = [rootUrl + '*.html']; console.warn('No input files specified, defaulting to ' + inputUrls[0]); } else { throw new Error( "No input files and no --root specified (or it isn't file:), cannot proceed" ); } new AssetGraph({ root: rootUrl }) .loadAssets(inputUrls) .populate({ from: { type: 'Html' }, followRelations: { type: ['HtmlScript', 'HtmlStyle'], to: { protocol: { $not: 'file:' } }, }, }) .reviewSubResourceIntegrity( { type: 'Html', isInline: false, isFragment: false, isLoaded: true }, { update: true, single: true, algorithm: commandLineOptions.algorithm } ) .writeAssetsToDisc( { url: /^file:/, type: 'Html', isLoaded: true, isDirty: true }, output || rootUrl ) .writeStatsToStderr() .run();