Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/actionspack. Lockfile-first GitHub Actions workflow packer
actions github workflow
Something went wrong. Try again.
4.0 kB · 143 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144#!/usr/bin/env nodeimport process from 'node:process'import { styleText } from 'node:util'import { cac } from 'cac'import pkg from '../package.json' with { type: 'json' }import { diff, pack, scan, tree, update, verify, why } from './commands.ts'import type { ActionspackOptions, WorkflowEntry } from './types.ts'
const cli = cac(pkg.name)
interface ConfigFlags { entry?: string | string[] external?: string | string[]}
interface UpdateFlags extends ConfigFlags { lockfileOnly?: boolean}
async function main(): Promise<void> { cli .command('', 'Pack workflows') .alias('pack') .option( '--entry <source:output>', 'Use an explicit source/output workflow mapping', ) .option( '--external <selector>', 'Pin a workflow or action without bundling it', ) .action(async (flags: ConfigFlags) => { const config = normalizeConfigFlags(flags) await pack({ ...config, stderr: process.stderr, stdout: process.stdout, }) })
cli .command('scan', 'Update the lockfile graph without generating workflows') .option( '--entry <source:output>', 'Use an explicit source/output workflow mapping', ) .option( '--external <selector>', 'Pin a workflow or action without bundling it', ) .action(async (flags: ConfigFlags) => { const config = normalizeConfigFlags(flags) await scan({ ...config, stderr: process.stderr, stdout: process.stdout, }) })
cli .command('update [packageName]', 'Refresh locked dependency versions') .option( '--entry <source:output>', 'Use an explicit source/output workflow mapping', ) .option( '--external <selector>', 'Pin a workflow or action without bundling it', ) .option('--lockfile-only', 'Write only .github/workflow.lock.yml') .action(async (packageName: string | undefined, flags: UpdateFlags) => { const { lockfileOnly, ...rest } = flags const config = normalizeConfigFlags(rest) await update({ ...config, packageName, lockfileOnly, stderr: process.stderr, stdout: process.stdout, }) })
cli .command('tree', 'Show the dependency tree from the lockfile') .action(async () => { await tree({ stdout: process.stdout }) })
cli .command('why <packageName>', 'Explain why a dependency exists') .action(async (packageName: string) => { await why(packageName, { stdout: process.stdout }) })
cli .command('diff', 'Show lockfile changes compared with HEAD') .option('--json', 'Print JSON output') .action(async (flags: { json?: boolean }) => { await diff({ json: flags.json, stdout: process.stdout }) })
cli .command('verify', 'Verify lockfile and packed workflows') .action(async () => { await verify() })
cli.version(pkg.version).help() cli.parse(process.argv, { run: false }) await cli.runMatchedCommand()}
function normalizeConfigFlags( flags: ConfigFlags,): Pick<ActionspackOptions, 'entries' | 'external'> { return { ...(flags.entry ? { entries: normalizeEntries(flags.entry) } : {}), ...(flags.external ? { external: normalizeStringList(flags.external) } : {}), }}
function normalizeEntries(value: string | string[]): WorkflowEntry[] { return normalizeStringList(value).map((item) => { const [source, ...outputParts] = item.split(':') const output = outputParts.join(':') if (!source || !output) { throw new Error(`Invalid --entry value: ${item}`) } return { source, output } })}
function normalizeStringList(value: string | string[]): string[] { return Array.isArray(value) ? value : [value]}
main().catch((error: unknown) => { const message = error instanceof Error ? error.message : String(error) process.stderr.write(`${styleText('red', `actionspack: ${message}`)}\n`) process.exitCode = 1})