|
| 1 | +import * as path from "path"; |
| 2 | +import { |
| 3 | + arrayOption, |
| 4 | + Command, |
| 5 | + CommandOptionsSchema, |
| 6 | + stringOption, |
| 7 | +} from "../common/define-command"; |
| 8 | +import { inject } from "../common/di"; |
| 9 | +import { IMetadataTrimService } from "../definitions/metadata-trim-service"; |
| 10 | + |
| 11 | +const PLATFORMS = ["ios", "macos", "android"]; |
| 12 | + |
| 13 | +const metadataTrimOptions = { |
| 14 | + out: stringOption(), |
| 15 | + keep: arrayOption(), |
| 16 | + analyzer: stringOption(), |
| 17 | + generator: stringOption(), |
| 18 | + framework: stringOption(), |
| 19 | + androidProject: stringOption(), |
| 20 | + sdk: stringOption(), |
| 21 | + arch: stringOption(), |
| 22 | + minVersion: stringOption(), |
| 23 | +} satisfies CommandOptionsSchema; |
| 24 | + |
| 25 | +function formatBytes(bytes: number): string { |
| 26 | + return `${(bytes / 1024 / 1024).toFixed(2)} MB`; |
| 27 | +} |
| 28 | + |
| 29 | +function resolved(value: string | undefined): string | undefined { |
| 30 | + return value ? path.resolve(value) : undefined; |
| 31 | +} |
| 32 | + |
| 33 | +export class MetadataTrimCommand extends Command({ |
| 34 | + name: "metadata|trim", |
| 35 | + description: "Reduces native API metadata to what an app's JavaScript uses.", |
| 36 | + options: metadataTrimOptions, |
| 37 | + params: "any", |
| 38 | +}) { |
| 39 | + private $logger = inject<ILogger>("logger"); |
| 40 | + private $metadataTrimService = inject<IMetadataTrimService>( |
| 41 | + "metadataTrimService", |
| 42 | + ); |
| 43 | + |
| 44 | + public canExecute(): boolean { |
| 45 | + const [platform, ...bundles] = this.args; |
| 46 | + if (!platform || !PLATFORMS.includes(platform.toLowerCase())) { |
| 47 | + this.context.fail(`Specify a platform: ${PLATFORMS.join(", ")}.`); |
| 48 | + } |
| 49 | + if (!bundles.length) { |
| 50 | + this.context.fail( |
| 51 | + "Specify at least one emitted JavaScript bundle or directory.", |
| 52 | + ); |
| 53 | + } |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + public async run(): Promise<void> { |
| 58 | + const [platform, ...bundles] = this.args; |
| 59 | + const options = this.options; |
| 60 | + const result = await this.$metadataTrimService.trim({ |
| 61 | + platform: platform.toLowerCase(), |
| 62 | + bundles: bundles.map((bundle) => path.resolve(bundle)), |
| 63 | + outDir: path.resolve(options.out ?? "metadata-trim"), |
| 64 | + keep: options.keep ?? [], |
| 65 | + analyzerPath: resolved(options.analyzer), |
| 66 | + generatorPath: resolved(options.generator), |
| 67 | + framework: resolved(options.framework), |
| 68 | + androidProject: resolved(options.androidProject), |
| 69 | + sdk: options.sdk, |
| 70 | + arch: options.arch, |
| 71 | + minVersion: options.minVersion, |
| 72 | + }); |
| 73 | + |
| 74 | + if (result.metadataBytes !== undefined) { |
| 75 | + this.$logger.info( |
| 76 | + `Generated ${formatBytes(result.metadataBytes)} of metadata: ${result.metadataPath}`, |
| 77 | + ); |
| 78 | + } |
| 79 | + if (result.installedTo) { |
| 80 | + const saved = result.replacedBytes |
| 81 | + ? ` (${formatBytes(result.replacedBytes)} smaller)` |
| 82 | + : ""; |
| 83 | + this.$logger.info(`Installed to ${result.installedTo}${saved}`); |
| 84 | + } |
| 85 | + if (result.nextStep) { |
| 86 | + this.$logger.info(result.nextStep); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments