1616import { existsSync } from 'node:fs' ;
1717import { SfCommand } from '@salesforce/sf-plugins-core' ;
1818import { Messages , SfError } from '@salesforce/core' ;
19- import { DatacodeBinaryExecutor , type DatacodeZipExecutionResult } from '../utils/datacodeBinaryExecutor.js' ;
20- import { checkEnvironment } from '../utils/environmentChecker.js' ;
21- import { type SharedResultProps } from './types.js' ;
19+ import { zipWithSfError , type ZipResult as ZipBuilderResult } from '../utils/zipBuilder.js' ;
2220
2321export type BaseZipFlags = {
2422 'package-dir' : string ;
2523 network ?: string ;
2624} ;
2725
28- export type ZipResult = SharedResultProps & {
29- archivePath ?: string ;
30- executionResult ?: DatacodeZipExecutionResult ;
26+ export type ZipResult = {
27+ success : boolean ;
28+ codeType : 'script' | 'function' ;
29+ packageDir : string ;
30+ archivePath : string ;
31+ fileCount : number ;
32+ archiveSizeBytes : number ;
33+ message : string ;
3134} ;
3235
36+ function formatBytes ( bytes : number ) : string {
37+ const units = [ 'B' , 'KB' , 'MB' , 'GB' ] ;
38+ let value = bytes ;
39+ let unit = 0 ;
40+ while ( value >= 1024 && unit < units . length - 1 ) {
41+ value /= 1024 ;
42+ unit += 1 ;
43+ }
44+ return `${ value . toFixed ( unit === 0 ? 0 : 2 ) } ${ units [ unit ] } ` ;
45+ }
46+
3347// eslint-disable-next-line sf-plugin/command-summary, sf-plugin/command-example
3448export abstract class ZipBase extends SfCommand < ZipResult > {
3549 public static enableJsonFlag = false ;
@@ -39,7 +53,7 @@ export abstract class ZipBase extends SfCommand<ZipResult> {
3953 const codeType = this . getCodeType ( ) ;
4054 const messages = this . getMessages ( ) ;
4155 const packageDir = flags [ 'package-dir' ] ;
42- const network = flags . network ;
56+ const network = flags . network ?? 'default' ;
4357
4458 if ( ! existsSync ( packageDir ) ) {
4559 throw new SfError (
@@ -50,45 +64,25 @@ export abstract class ZipBase extends SfCommand<ZipResult> {
5064 }
5165
5266 try {
53- const { pythonInfo, packageInfo, binaryInfo } = await checkEnvironment (
54- this . spinner ,
55- this . log . bind ( this ) ,
56- messages
57- ) ;
58-
5967 this . spinner . start ( messages . getMessage ( 'info.executingZip' ) ) ;
60- const executionResult = await DatacodeBinaryExecutor . executeBinaryZip ( packageDir , network ) ;
61-
68+ const result : ZipBuilderResult = await zipWithSfError ( packageDir , network , this . log . bind ( this ) ) ;
6269 this . spinner . stop ( ) ;
6370
64- if ( executionResult . archivePath ) {
65- this . log ( messages . getMessage ( 'info.archiveCreated' , [ executionResult . archivePath ] ) ) ;
66- }
67-
68- if ( executionResult . fileCount !== undefined ) {
69- this . log ( messages . getMessage ( 'info.filesIncluded' , [ executionResult . fileCount . toString ( ) ] ) ) ;
70- }
71-
72- if ( executionResult . archiveSize ) {
73- this . log ( messages . getMessage ( 'info.archiveSize' , [ executionResult . archiveSize ] ) ) ;
74- }
71+ this . log ( messages . getMessage ( 'info.archiveCreated' , [ result . archivePath ] ) ) ;
72+ this . log ( messages . getMessage ( 'info.filesIncluded' , [ result . fileCount . toString ( ) ] ) ) ;
73+ this . log ( messages . getMessage ( 'info.archiveSize' , [ formatBytes ( result . archiveSizeBytes ) ] ) ) ;
7574
7675 return {
7776 success : true ,
78- pythonVersion : pythonInfo ,
79- packageInfo,
80- binaryInfo,
8177 codeType,
8278 packageDir,
83- archivePath : executionResult . archivePath ,
84- executionResult,
79+ archivePath : result . archivePath ,
80+ fileCount : result . fileCount ,
81+ archiveSizeBytes : result . archiveSizeBytes ,
8582 message : messages . getMessage ( 'info.zipCompleted' ) ,
8683 } ;
8784 } catch ( error ) {
8885 this . spinner . stop ( ) ;
89-
90- // The error will be properly handled by the Salesforce CLI framework
91- // as an SfError with actions, so we just throw it
9286 throw error ;
9387 }
9488 }
0 commit comments