|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {writeFileSync} from 'node:fs'; |
| 7 | +import {gzipSync} from 'node:zlib'; |
| 8 | +import path from 'node:path'; |
| 9 | +import {Flags} from '@oclif/core'; |
| 10 | +import {BaseCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 11 | +import {createBundle, getDefaultMessage, DEFAULT_SSR_PARAMETERS} from '@salesforce/b2c-tooling-sdk/operations/mrt'; |
| 12 | +import {t, withDocs} from '../../../i18n/index.js'; |
| 13 | + |
| 14 | +export interface SaveBundleResult { |
| 15 | + filePath: string; |
| 16 | + projectSlug: string; |
| 17 | + message: string; |
| 18 | + ssrOnlyCount: number; |
| 19 | + ssrSharedCount: number; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Save a bundle to a local directory without uploading it to Managed Runtime. |
| 24 | + */ |
| 25 | +export default class MrtBundleSave extends BaseCommand<typeof MrtBundleSave> { |
| 26 | + static description = withDocs( |
| 27 | + t('commands.mrt.bundle.save.description', 'Save a Managed Runtime bundle to a local directory'), |
| 28 | + '/cli/mrt.html#b2c-mrt-bundle-save', |
| 29 | + ); |
| 30 | + |
| 31 | + static enableJsonFlag = true; |
| 32 | + |
| 33 | + static examples = [ |
| 34 | + '<%= config.bin %> <%= command.id %> --project my-storefront --save-dir ./artifacts', |
| 35 | + '<%= config.bin %> <%= command.id %> --project my-storefront --save-dir ./artifacts --gzip', |
| 36 | + '<%= config.bin %> <%= command.id %> --project my-storefront --save-dir ./artifacts --build-dir ./dist', |
| 37 | + '<%= config.bin %> <%= command.id %> --project my-storefront --save-dir ./artifacts --json', |
| 38 | + ]; |
| 39 | + |
| 40 | + static flags = { |
| 41 | + ...BaseCommand.baseFlags, |
| 42 | + project: Flags.string({ |
| 43 | + char: 'p', |
| 44 | + description: 'MRT project slug (or set MRT_PROJECT env var)', |
| 45 | + env: 'MRT_PROJECT', |
| 46 | + default: async () => process.env.SFCC_MRT_PROJECT || undefined, |
| 47 | + }), |
| 48 | + 'save-dir': Flags.string({ |
| 49 | + char: 's', |
| 50 | + description: 'Directory to save the bundle to', |
| 51 | + required: true, |
| 52 | + }), |
| 53 | + 'build-dir': Flags.string({ |
| 54 | + char: 'b', |
| 55 | + description: 'Path to the build directory', |
| 56 | + default: 'build', |
| 57 | + }), |
| 58 | + 'ssr-only': Flags.string({ |
| 59 | + description: 'Glob patterns for server-only files (comma-separated or JSON array)', |
| 60 | + }), |
| 61 | + 'ssr-shared': Flags.string({ |
| 62 | + description: 'Glob patterns for shared files (comma-separated or JSON array)', |
| 63 | + }), |
| 64 | + 'node-version': Flags.string({ |
| 65 | + char: 'n', |
| 66 | + description: `Node.js version for SSR runtime (default: ${DEFAULT_SSR_PARAMETERS.SSRFunctionNodeVersion})`, |
| 67 | + }), |
| 68 | + gzip: Flags.boolean({ |
| 69 | + char: 'g', |
| 70 | + description: 'Gzip the bundle (saves as bundle.tgz instead of bundle.tar)', |
| 71 | + default: false, |
| 72 | + }), |
| 73 | + }; |
| 74 | + |
| 75 | + async run(): Promise<SaveBundleResult> { |
| 76 | + const project = this.flags.project; |
| 77 | + |
| 78 | + if (!project) { |
| 79 | + this.error('MRT project is required. Provide --project flag or set MRT_PROJECT.'); |
| 80 | + } |
| 81 | + |
| 82 | + const saveDir = this.flags['save-dir']; |
| 83 | + const buildDir = this.flags['build-dir']; |
| 84 | + const ssrOnly = this.flags['ssr-only'] ? parseGlobPatterns(this.flags['ssr-only']) : undefined; |
| 85 | + const ssrShared = this.flags['ssr-shared'] ? parseGlobPatterns(this.flags['ssr-shared']) : undefined; |
| 86 | + const ssrParameters: Record<string, unknown> = {}; |
| 87 | + |
| 88 | + if (this.flags['node-version']) { |
| 89 | + ssrParameters.SSRFunctionNodeVersion = this.flags['node-version']; |
| 90 | + } |
| 91 | + |
| 92 | + const message = getDefaultMessage(); |
| 93 | + |
| 94 | + this.log(t('commands.mrt.bundle.save.creating', 'Creating bundle for {{project}}...', {project})); |
| 95 | + |
| 96 | + const bundle = await createBundle({ |
| 97 | + projectSlug: project, |
| 98 | + buildDirectory: buildDir, |
| 99 | + message, |
| 100 | + ssrOnly, |
| 101 | + ssrShared, |
| 102 | + ssrParameters, |
| 103 | + }); |
| 104 | + |
| 105 | + const gzip = this.flags.gzip; |
| 106 | + const fileName = gzip ? 'bundle.tgz' : 'bundle.tar'; |
| 107 | + const filePath = path.resolve(saveDir, fileName); |
| 108 | + |
| 109 | + let data = Buffer.from(bundle.data, 'base64'); |
| 110 | + if (gzip) { |
| 111 | + data = gzipSync(data); |
| 112 | + } |
| 113 | + |
| 114 | + writeFileSync(filePath, data); |
| 115 | + |
| 116 | + if (!this.jsonEnabled()) { |
| 117 | + this.log(t('commands.mrt.bundle.save.success', 'Bundle saved to {{filePath}}', {filePath})); |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + filePath, |
| 122 | + projectSlug: project, |
| 123 | + message: bundle.message, |
| 124 | + ssrOnlyCount: bundle.ssr_only.length, |
| 125 | + ssrSharedCount: bundle.ssr_shared.length, |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function parseGlobPatterns(value: string): string[] { |
| 131 | + const trimmed = value.trim(); |
| 132 | + if (trimmed.startsWith('[')) { |
| 133 | + const parsed: unknown = JSON.parse(trimmed); |
| 134 | + if (!Array.isArray(parsed) || !parsed.every((item) => typeof item === 'string')) { |
| 135 | + throw new Error(`Invalid glob pattern array: expected an array of strings`); |
| 136 | + } |
| 137 | + return parsed.map((s: string) => s.trim()).filter(Boolean); |
| 138 | + } |
| 139 | + return trimmed |
| 140 | + .split(',') |
| 141 | + .map((s) => s.trim()) |
| 142 | + .filter(Boolean); |
| 143 | +} |
0 commit comments