UNPKG

sammich.js

Version:

Too many JavaScript files? Put 'em in a sammich.

131 lines (108 loc) 2.78 kB
#!/usr/bin/env node /* * Copyright (C) 2025 Ashei Juniperus - Available under the MPL 2.0 * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ const { writeFile } = require("node:fs/promises"); const Sammich = require("../src"); /** * The short version of the `sammich.js` copyright. */ const COPYRIGHT_SHORT = "Copyright (C) 2025 Ashei Juniperus - Available under the MPL 2.0"; /** * The long version of the `sammich.js` copyright. */ const COPYRIGHT_LONG = [ "This Source Code Form is subject to the terms of the Mozilla Public", "License, v. 2.0. If a copy of the MPL was not distributed with this", "file, You can obtain one at https://mozilla.org/MPL/2.0/." ]; /** * The `sammich.js` usage info. */ const USAGE = "usage: sammich.js [input_files...]"; /** * The `sammich.js` motto. */ const MOTTO = "Too many JavaScript files? Put 'em in a sammich."; const { parseArgs } = require("node:util"); const options = { 'verbose': { type: 'boolean', short: 'c', }, 'output': { type: 'string', short: 'o', } }; function parseSammichArgs() { return parseArgs( { allowNegative: true, allowPositionals: true, args: process.argv.slice(2), options, strict: true } ); } /** * Log the `sammich.js` intro. */ function logIntro() { console.log(COPYRIGHT_SHORT); logMotto(); console.log(); } /** * Log the `sammich.js` motto. */ function logMotto() { console.log("\"" + MOTTO + "\""); } /** * Log each `value` in an `Array`. */ function logArray(array) { for (const value of array) { console.log(value); } } /** * Log the `sammich.js` usage information. */ function logUsage() { console.log(USAGE); } function assertInputFilePathsAreValid(inputFilePaths) { if (inputFilePaths.length < 1) { logIntro(); logUsage(); process.exit(1); } } async function bundleInputFilesFromPaths(inputFilePaths, outputFilePath) { assertInputFilePathsAreValid(inputFilePaths); const sammichSourceCode = await Sammich.bundleFiles(inputFilePaths); if (outputFilePath) { await writeFile(outputFilePath, sammichSourceCode + "\n", "utf-8"); } else { console.log(sammichSourceCode); } } /** * The main entry point. */ async function main() { const args = parseSammichArgs(); const inputFilePaths = args.positionals; const outputFilePath = args.values.output; await bundleInputFilesFromPaths(inputFilePaths, outputFilePath); } // Start the program. main();