UNPKG

@codecov/bundle-analyzer

Version:

Official Codecov Bundle Analyzer

121 lines (115 loc) 3.41 kB
#!/usr/bin/env node 'use strict'; const path = require('node:path'); const fs = require('node:fs'); const yargs = require('yargs'); const helpers = require('yargs/helpers'); const index = require('./index.cjs'); const bundlerPluginCore = require('@codecov/bundler-plugin-core'); require('node:fs/promises'); require('micromatch'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const path__default = /*#__PURE__*/_interopDefaultCompat(path); const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); const yargs__default = /*#__PURE__*/_interopDefaultCompat(yargs); const cliArgs = yargs__default(helpers.hideBin(process.argv)).usage("Usage: $0 <build-directories> [options]").command( "$0 <build-directories...>", "Analyze and upload bundle report", (yargs2) => { return yargs2.positional("buildDirectories", { describe: "The paths to the build directories (can be multiple)", type: "string", demandOption: true }); } ).options({ "dry-run": { alias: "d", type: "boolean", description: "Perform a dry run without uploading", default: false }, "upload-token": { alias: "t", type: "string", description: "Specify the upload token for authentication" }, "api-url": { alias: "u", type: "string", description: "Set the API URL", default: "https://api.codecov.io" }, "bundle-name": { alias: "n", type: "string", description: "Set the bundle identifier in Codecov" }, debug: { alias: "v", type: "boolean", description: "Enable debug mode for additional logging", default: false }, "ignore-patterns": { alias: "i", type: "array", description: "Specify file patterns to ignore during the analysis" }, "normalize-assets-pattern": { alias: "p", type: "string", description: "Pattern to normalize asset names, e.g., '[name]-[hash].js'" }, "config-file": { alias: "c", type: "string", description: "Path to a JSON configuration file" } }).strict().help("h").alias("h", "help").parseSync(); const getConfigFileArgs = (filePath) => { try { const configContent = fs__default.readFileSync(filePath, "utf-8"); return JSON.parse(configContent); } catch (error) { bundlerPluginCore.red(`Failed to load configuration file: ${error}`); process.exit(1); } }; const addConfigFileArgs = (baseArgs) => { let configFromFile = {}; if (baseArgs.configFile) { configFromFile = getConfigFileArgs(baseArgs.configFile); } return { ...configFromFile, ...baseArgs }; }; const runCli = async (baseArgs) => { const argv = addConfigFileArgs(baseArgs); if (argv.buildDirectories.length === 0) { bundlerPluginCore.red("Error: No build directories provided."); process.exit(1); } const resolvedDirectoryPaths = argv.buildDirectories.map( (dir) => path__default.resolve(process.cwd(), dir) ); const coreOptions = { ...argv }; const bundleAnalyzerOptions = { ...argv }; const reportAsJson = await index.createAndUploadReport( resolvedDirectoryPaths, coreOptions, bundleAnalyzerOptions ); if (coreOptions.dryRun) { console.log(reportAsJson); } }; runCli(cliArgs).catch((error) => { bundlerPluginCore.red(`An error occurred: ${error}`); process.exit(1); }); exports.runCli = runCli; //# sourceMappingURL=cli.cjs.map