import type { UploadSummary } from './core/reporter'; import { ensureRuntimeEnvLoaded } from './config/runtimeEnv'; (async () => { const [{ Command }, { uploadDirectory }] = await Promise.all([ import('commander'), import('./middleware/OssService'), ]); const { ScriptReporter } = await import('./core/reporter'); const program = new Command(); let actionResult: UploadSummary | undefined; let outputResultArray = false; program .name('oss-upload') .description('Upload a local directory to OBS.') .argument('', 'Local directory to upload') .option('-b, --bucket ', 'Bucket name', '') .option('-e, --env ', 'Environment tag for bucket/CDN selection', 'dev') .option('--mode ', 'Load .env. in addition to .env') .option('--suffix ', 'Leading key segment, defaults to static', 'static') .option('--include ', 'Glob patterns to include') .option('--exclude ', 'Glob patterns to exclude') .option('-f, --force', 'Force upload files even when remote keys already exist') .option('--hash', 'Append an md5 hash to uploaded file names') .action(async (localDir: string, options: any) => { ensureRuntimeEnvLoaded(options.mode); const { loadObsCredentialsFromUrlIfNeeded } = await import('./config/loadObsCredentialsFromUrl'); await loadObsCredentialsFromUrlIfNeeded(); options.env = options.env || 'dev'; if (!options.bucket) { options.bucket = ['prod', 'production'].includes(options.env.toLowerCase()) ? 'hr-pro' : 'hr-uat'; } try { const summary = await uploadDirectory({ env: options.env, bucket: options.bucket, localDir, suffix: options.suffix, include: options.include, exclude: options.exclude, force: options.force, hash: options.hash, reporter: options.hash ? new ScriptReporter() : undefined, }); actionResult = summary; if (options.hash) outputResultArray = true; if (summary.failed.length > 0) { process.exitCode = 1; } } catch (err: any) { // eslint-disable-next-line no-console console.error('Upload failed:', err?.message || err); process.exitCode = 1; } }); await program.parseAsync(process.argv); if (outputResultArray && actionResult) { // eslint-disable-next-line no-console console.log(JSON.stringify(actionResult.success)); } })();