UNPKG

@mischback/buster

Version:

buster is a tool to support cache busting for static assets of a website.

182 lines 6.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConfig = exports.checkConfig = exports.BusterConfigError = exports.cmdLineOptions = exports.MODE_RENAME = exports.MODE_COPY = void 0; const fs_1 = require("fs"); const path_1 = require("path"); const stdio_1 = require("stdio"); const { R_OK, W_OK } = fs_1.constants; const errors_1 = require("./errors"); const logging_1 = require("./logging"); exports.MODE_COPY = "copy"; exports.MODE_RENAME = "rename"; const defaultExtensions = ["css", "js"]; const defaultHashLength = 10; const defaultOutFile = "asset-manifest.json"; exports.cmdLineOptions = { commonPathLength: { args: 1, default: false, description: "Manually override the common path length", required: false, }, debug: { args: 0, default: false, description: "Flag to activate debug mode", key: "d", required: false, }, extension: { args: "*", default: false, description: "A file extension to include in processing. May be specified multiple times. (default: [css, js])", key: "e", multiple: true, required: false, }, hashLength: { args: 1, default: false, description: "The length of the hash string to append", required: false, }, input: { args: 1, default: false, description: "The input, either a directory or a file", key: "i", required: false, }, mode: { args: 1, default: false, description: "Operation mode, either files are copied or simply renamed", key: "m", required: false, }, outFile: { args: 1, default: false, description: `Path and filename of the output file (default: ${defaultOutFile})`, key: "o", required: false, }, quiet: { args: 0, default: false, description: "Flag to activate quiet mode", key: "q", required: false, }, }; class BusterConfigError extends errors_1.BusterError { constructor(message) { super(message); } } exports.BusterConfigError = BusterConfigError; function checkConfig(config) { return new Promise((resolve, reject) => { const normalizedInput = (0, path_1.normalize)((0, path_1.resolve)(config.input)); try { (0, fs_1.accessSync)(normalizedInput, R_OK | W_OK); } catch (err) { return reject(new BusterConfigError("The specified input can not be read/written to")); } const inputIsDirectory = (0, fs_1.existsSync)(normalizedInput) && (0, fs_1.lstatSync)(normalizedInput).isDirectory(); let normalizedCommonPathLength = config.commonPathLength; if (normalizedCommonPathLength === -1 && inputIsDirectory === true) { normalizedCommonPathLength = normalizedInput.length + path_1.sep.length; } let normalizedOutFile = config.outFile; if (normalizedOutFile === (0, path_1.basename)(normalizedOutFile)) { if (inputIsDirectory === true) { normalizedOutFile = (0, path_1.normalize)((0, path_1.resolve)(normalizedInput, normalizedOutFile)); } else { normalizedOutFile = (0, path_1.normalize)((0, path_1.resolve)((0, path_1.dirname)(normalizedInput), normalizedOutFile)); } } else { normalizedOutFile = (0, path_1.normalize)((0, path_1.resolve)(normalizedOutFile)); } try { (0, fs_1.accessSync)((0, path_1.dirname)(normalizedOutFile), R_OK | W_OK); } catch (err) { return reject(new BusterConfigError("The specified output file can not be read/written to")); } return resolve(Object.assign(config, { commonPathLength: normalizedCommonPathLength }, { input: normalizedInput }, { outFile: normalizedOutFile })); }); } exports.checkConfig = checkConfig; function getConfig(argv) { return new Promise((resolve, reject) => { const cmdLineParams = (0, stdio_1.getopt)(exports.cmdLineOptions, argv); if (cmdLineParams === null) return reject(new BusterConfigError("Could not parse command line parameters")); let tmpInput; if (cmdLineParams.input === false) { return reject(new BusterConfigError("Missing parameter input")); } else { tmpInput = cmdLineParams.input; } let tmpCommonPathLength; if (cmdLineParams.commonPathLength === false) { tmpCommonPathLength = -1; } else { tmpCommonPathLength = cmdLineParams.commonPathLength; } let tmpExtensions; if (cmdLineParams.extension === false) { tmpExtensions = defaultExtensions; logging_1.logger.debug(`No extensions specified, using ${tmpExtensions}`); } else { if (typeof cmdLineParams.extension === "string") { tmpExtensions = [cmdLineParams.extension]; } else { tmpExtensions = cmdLineParams.extension; } } let tmpHashLength; if (cmdLineParams.hashLength === false) { tmpHashLength = defaultHashLength; logging_1.logger.debug(`No hash length specified, using "${tmpHashLength}"`); } else { tmpHashLength = parseInt(cmdLineParams.hashLength); } let tmpMode; if (cmdLineParams.mode === false) { tmpMode = exports.MODE_COPY; logging_1.logger.info(`No mode specified, using "${tmpMode}"`); } else { tmpMode = cmdLineParams.mode; } let tmpOutFile; if (cmdLineParams.outFile === false) { tmpOutFile = defaultOutFile; logging_1.logger.info(`No output file specified, using "${tmpOutFile}"`); } else { tmpOutFile = cmdLineParams.outFile; } return resolve({ commonPathLength: tmpCommonPathLength, extensions: tmpExtensions, hashLength: tmpHashLength, input: tmpInput, mode: tmpMode, outFile: tmpOutFile, }); }); } exports.getConfig = getConfig; //# sourceMappingURL=configure.js.map