UNPKG

@argos-ci/cli

Version:

Command-line (CLI) for visual testing with Argos.

146 lines (140 loc) 4.97 kB
// src/index.ts import { readFile } from "fs/promises"; import { fileURLToPath } from "url"; import { resolve } from "path"; import { program } from "commander"; // src/commands/upload.ts import { Option as Option2 } from "commander"; import { upload } from "@argos-ci/core"; import ora from "ora"; // src/options.ts import { Option } from "commander"; var parallelNonce = new Option( "--parallel-nonce <string>", "A unique ID for this parallel build" ).env("ARGOS_PARALLEL_NONCE"); // src/commands/upload.ts function uploadCommand(program2) { program2.command("upload").argument("<directory>", "Directory to upload").description("Upload screenshots to Argos").option( "-f, --files <patterns...>", "One or more globs matching image file paths to upload", "**/*.{png,jpg,jpeg}" ).option( "-i, --ignore <patterns...>", 'One or more globs matching image file paths to ignore (ex: "**/*.png **/diff.jpg")' ).addOption( new Option2("--token <token>", "Repository token").env("ARGOS_TOKEN") ).addOption( new Option2( "--build-name <string>", "Name of the build, in case you want to run multiple Argos builds in a single CI job" ).env("ARGOS_BUILD_NAME") ).addOption( new Option2( "--mode <string>", "Mode of comparison applied. CI for visual regression testing, monitoring for visual monitoring." ).default("ci").choices(["ci", "monitoring"]).env("ARGOS_MODE") ).addOption( new Option2( "--parallel", "Enable parallel mode. Run multiple Argos builds and combine them at the end" ).env("ARGOS_PARALLEL") ).addOption( new Option2( "--parallel-total <number>", "The number of parallel nodes being ran" ).env("ARGOS_PARALLEL_TOTAL") ).addOption(parallelNonce).addOption( new Option2( "--parallel-index <number>", "The index of the parallel node being ran (must be at least 1)" ).env("ARGOS_PARALLEL_INDEX") ).addOption( new Option2( "--reference-branch <string>", "Branch used as baseline for screenshot comparison" ).env("ARGOS_REFERENCE_BRANCH") ).addOption( new Option2( "--reference-commit <string>", "Commit used as baseline for screenshot comparison" ).env("ARGOS_REFERENCE_COMMIT") ).addOption( new Option2( "--threshold <number>", "Sensitivity threshold between 0 and 1. The higher the threshold, the less sensitive the diff will be. Default to 0.5" ).env("ARGOS_THRESHOLD") ).addOption( new Option2( "--skipped", "Mark this build as skipped. No screenshots are uploaded, and the commit status is marked as success." ).env("ARGOS_SKIPPED") ).action(async (directory, options) => { const spinner = ora("Uploading screenshots").start(); try { const result = await upload({ token: options.token, root: directory, buildName: options.buildName, files: options.files, ignore: options.ignore, prNumber: options.pullRequest ? Number(options.pullRequest) : void 0, parallel: options.parallel ? { nonce: options.parallelNonce, total: options.parallelTotal, index: options.parallelIndex } : void 0, referenceBranch: options.referenceBranch, referenceCommit: options.referenceCommit, mode: options.mode, threshold: options.threshold, skipped: options.skipped }); spinner.succeed(`Build created: ${result.build.url}`); } catch (error) { if (error instanceof Error) { spinner.fail(`Build failed: ${error.message}`); console.error(error.stack); } process.exit(1); } }); } // src/commands/finalize.ts import ora2 from "ora"; import { finalize } from "@argos-ci/core"; function finalizeCommand(program2) { program2.command("finalize").description("Finalize pending parallel builds").addOption(parallelNonce).action(async (options) => { const spinner = ora2("Finalizing builds").start(); try { const result = await finalize({ parallel: { nonce: options.parallelNonce } }); spinner.succeed( result.builds.length === 0 ? "No builds to finalize" : `Builds finalized: ${result.builds.map((b) => b.url).join(", ")}` ); } catch (error) { if (error instanceof Error) { spinner.fail(`Failed to finalize: ${error.message}`); console.error(error.stack); } process.exit(1); } }); } // src/index.ts var __dirname = fileURLToPath(new URL(".", import.meta.url)); var rawPkg = await readFile(resolve(__dirname, "..", "package.json"), "utf8"); var pkg = JSON.parse(rawPkg); program.name(pkg.name).description( "Interact with and upload screenshots to Argos via command line." ).version(pkg.version); uploadCommand(program); finalizeCommand(program); if (!process.argv.slice(2).length) { program.outputHelp(); } else { program.parse(process.argv); }