import { program, Option } from "commander"; import { findAndReadConfigFile } from "../../utils/fsUtils.js"; import { DEFAULT_DIFF_BASE_COUNT, release } from "./release.js"; import { DEFAULT_OVERSIZED_PATCH_POLICY, OVERSIZED_PATCH_POLICIES, resolveBinaryBundlePathOption, type OversizedPatchPolicy, } from "../../functions/makeBinaryPatchBundle.js"; import { OUTPUT_BUNDLE_DIR, CONFIG_FILE_NAME, ROOT_OUTPUT_DIR, ENTRY_FILE } from "../../constant.js"; type Options = { binaryVersion: string; appVersion: string; framework: 'expo' | undefined; platform: 'ios' | 'android'; identifier?: string; config: string; outputPath: string; entryFile: string; // Commander derives this from the "-j, --js-bundle-name" flag. jsBundleName?: string; mandatory: boolean; enable: boolean; rollout?: number; skipBundle: boolean; skipCleanup: boolean; outputBundleDir: string; outputMetroDir?: string; hashCalc?: boolean; binaryBundlePath?: string; onOversizedPatch: OversizedPatchPolicy; diffBaseCount: number; minimumBackgroundDuration?: number; diffRetentionCount?: number; } program.command('release') .description('Deploys a new CodePush update for a target binary app.\nAfter creating the CodePush bundle, it uploads the file and updates the ReleaseHistory information.\n`bundleUploader`, `getReleaseHistory`, and `setReleaseHistory` functions should be implemented in the config file.') .requiredOption('-b, --binary-version ', '(Required) The target binary version') .requiredOption('-v, --app-version ', '(Required) The app version to be released. It must be greater than the binary version.') .addOption(new Option('-f, --framework ', 'framework type (expo)').choices(['expo'])) .addOption(new Option('-p, --platform ', 'platform').choices(['ios', 'android']).default('ios')) .option('-i, --identifier ', 'reserved characters to distinguish the release.') .option('-c, --config ', 'set config file name (JS/TS)', CONFIG_FILE_NAME) .option('-o, --output-path ', 'path to output root directory', ROOT_OUTPUT_DIR) .option('-e, --entry-file ', 'path to JS/TS entry file', ENTRY_FILE) .option('-j, --js-bundle-name ', 'JS bundle file name (default-ios: "main.jsbundle" / default-android: "index.android.bundle")') .option('-m, --mandatory ', 'make the release to be mandatory', parseBoolean, false) .option('--enable ', 'make the release to be enabled', parseBoolean, true) .option('--rollout ', 'rollout percentage (0-100)', parseFloat) .option('--skip-bundle ', 'skip bundle process', parseBoolean, false) .option('--hash-calc ', 'calculates the bundle file hash used for packageHash in the release history (Requires setting --skip-bundle to true)', parseBoolean) .option('--skip-cleanup ', 'skip cleanup process', parseBoolean, false) .option('--output-metro-dir ', 'name of directory to copy the Metro JS bundle and sourcemap before Hermes compilation') .option('--output-bundle-dir ', 'name of directory containing the bundle file created by the "bundle" command', OUTPUT_BUNDLE_DIR) .option('--binary-bundle-path ', 'path to the JS bundle of the target binary. Releases an additional binary patch bundle against it, and aligns the Hermes compilation with it.') .addOption(new Option('--on-oversized-patch ', 'what to do when the binary patch bundle is not smaller than the full bundle: "skip" releases the full bundle only, "fail" stops the release before any upload') .choices(OVERSIZED_PATCH_POLICIES) .default(DEFAULT_OVERSIZED_PATCH_POLICY)) .option('--diff-base-count ', 'how many recent releases to build asset diff archives against (0 disables). Requires `bundleDownloader` in the config file.', parseDecimalInt, DEFAULT_DIFF_BASE_COUNT) .option('--diff-retention-count ', 'how many of the newest releases keep their asset diff archives in the release history (1 or greater). Older entries lose `diffPackages`. Every entry keeps them when this is not set.', parseDecimalInt) .option('--minimum-background-duration ', 'seconds the app must have been in the background before this update is applied on resume. Overrides the minimumBackgroundDuration sync option.', parseDecimalInt) .action(async (options: Options) => { const config = findAndReadConfigFile(process.cwd(), options.config); if (typeof options.rollout === 'number' && (options.rollout < 0 || options.rollout > 100)) { console.error('Rollout percentage number must be between 0 and 100 (inclusive).'); process.exit(1); } if (!Number.isInteger(options.diffBaseCount) || options.diffBaseCount < 0) { console.error('--diff-base-count must be a whole number of releases, 0 or greater.'); process.exit(1); } if (options.diffRetentionCount !== undefined && (!Number.isInteger(options.diffRetentionCount) || options.diffRetentionCount < 1)) { console.error('--diff-retention-count must be a whole number of releases, 1 or greater.'); process.exit(1); } if (options.minimumBackgroundDuration !== undefined && (!Number.isInteger(options.minimumBackgroundDuration) || options.minimumBackgroundDuration < 0)) { console.error('--minimum-background-duration must be a whole number of seconds, 0 or greater.'); process.exit(1); } if (options.hashCalc && !options.skipBundle) { console.error('--hash-calc option can be used only when --skip-bundle is set to true.'); process.exit(1); } // Resolved before bundling so a wrong path fails now rather than after a build. const baseBundlePath = resolveBinaryBundlePathOption(options.binaryBundlePath); await release( config.bundleUploader, config.getReleaseHistory, config.setReleaseHistory, options.binaryVersion, options.appVersion, options.framework, options.platform, options.identifier, options.outputPath, options.entryFile, options.jsBundleName, options.mandatory, options.enable, options.rollout, options.skipBundle, options.skipCleanup, `${options.outputPath}/${options.outputBundleDir}`, options.outputMetroDir, options.hashCalc, baseBundlePath, options.onOversizedPatch, config.bundleDownloader, options.diffBaseCount, options.minimumBackgroundDuration, options.diffRetentionCount, ) console.log('🚀 Release completed.') }); // Not `parseInt` itself: commander hands a coercion function the current value as its // second argument, which `parseInt` reads as the radix. function parseDecimalInt(value: string): number { return parseInt(value, 10); } function parseBoolean(value: string): boolean | undefined { if (value === 'true') return true; if (value === 'false') return false; else return undefined; }