Skip to content

Commit

Permalink
feat: fire event with zip file size and file count
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Aug 14, 2024
1 parent 79416cd commit 7ac8c82
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export {
PackageOptions,
RetrieveOptions,
DeployVersionData,
DeployData,
DeployZipData,
RetrieveVersionData,
MetadataApiRetrieveOptions,
} from './types';
43 changes: 31 additions & 12 deletions src/client/metadataApiDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ export class MetadataApiDeploy extends MetadataTransfer<
})
);

const [zipBuffer] = await Promise.all([this.getZipBuffer(), this.maybeSaveTempDirectory('metadata')]);
const [{ zipBuffer, zipFileCount }] = await Promise.all([
this.getZipBuffer(),
this.maybeSaveTempDirectory('metadata'),
]);
// SDR modifies what the mdapi expects by adding a rest param
const { rest, ...optionsWithoutRest } = this.options.apiOptions ?? {};

Expand All @@ -217,8 +220,16 @@ export class MetadataApiDeploy extends MetadataTransfer<
const manifestMsg = manifestVersion ? ` in v${manifestVersion} shape` : '';
const debugMsg = format(`Deploying metadata source%s using ${webService} v${apiVersion}`, manifestMsg);
this.logger.debug(debugMsg);

// Event and Debug output for the zip file used for deploy
const zipSize = zipBuffer.byteLength;
await LifecycleInstance.emit('deployData', { webService, manifestVersion, apiVersion, zipSize });
let zipMessage = `Deployment zip file size = ${zipSize} Bytes`;
if (zipFileCount) {
zipMessage += ` containing ${zipFileCount} files`;
}
this.logger.debug(zipMessage);
await LifecycleInstance.emit('apiVersionDeploy', { webService, manifestVersion, apiVersion });
await LifecycleInstance.emit('deployZipData', { zipSize, zipFileCount });

return this.isRestDeploy
? connection.metadata.deployRest(zipBuffer, optionsWithoutRest)
Expand Down Expand Up @@ -293,14 +304,17 @@ export class MetadataApiDeploy extends MetadataTransfer<
return deployResult;
}

private async getZipBuffer(): Promise<Buffer> {
private async getZipBuffer(): Promise<{ zipBuffer: Buffer; zipFileCount?: number }> {
const mdapiPath = this.options.mdapiPath;

// Zip a directory of metadata format source
if (mdapiPath) {
if (!fs.existsSync(mdapiPath) || !fs.lstatSync(mdapiPath).isDirectory()) {
throw messages.createError('error_directory_not_found_or_not_directory', [mdapiPath]);
}

const zip = JSZip();
let zipFileCount = 0;

const zipDirRecursive = (dir: string): void => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
Expand All @@ -314,33 +328,38 @@ export class MetadataApiDeploy extends MetadataTransfer<
// Ensure only posix paths are added to zip files
const relPosixPath = relPath.replace(/\\/g, '/');
zip.file(relPosixPath, fs.createReadStream(fullPath));
zipFileCount++;
}
}
};
this.logger.debug('Zipping directory for metadata deploy:', mdapiPath);
zipDirRecursive(mdapiPath);

return zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: { level: 9 },
});
return {
zipBuffer: await zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: { level: 9 },
}),
zipFileCount,
};
}
// read the zip into a buffer
// Read a zip of metadata format source into a buffer
if (this.options.zipPath) {
if (!fs.existsSync(this.options.zipPath)) {
throw new SfError(messages.getMessage('error_path_not_found', [this.options.zipPath]));
}
// does encoding matter for zip files? I don't know
return fs.promises.readFile(this.options.zipPath);
return { zipBuffer: await fs.promises.readFile(this.options.zipPath) };
}
// Convert a ComponentSet of metadata in source format and zip
if (this.options.components && this.components) {
const converter = new MetadataConverter(this.registry);
const { zipBuffer } = await converter.convert(this.components, 'metadata', { type: 'zip' });
const { zipBuffer, zipFileCount } = await converter.convert(this.components, 'metadata', { type: 'zip' });
if (!zipBuffer) {
throw new SfError(messages.getMessage('zipBufferError'));
}
return zipBuffer;
return { zipBuffer, zipFileCount };
}
throw new Error('Options should include components, zipPath, or mdapiPath');
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,11 @@ export type DeployVersionData = {
};

/**
* Data about a deployment about to be sent to the Metadata API
* Data about a deployment zip file being sent to the Metadata API.
*/
export type DeployData = DeployVersionData & {
export type DeployZipData = {
zipSize: number;
zipFileCount: number;
};

export type RetrieveVersionData = {
Expand Down
3 changes: 1 addition & 2 deletions src/convert/metadataConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ const getResult =
async (writer: StandardWriter | ZipWriter): Promise<ConvertResult> => {
// union type discrimination
if ('addToZip' in writer) {
// console.log('zip file count =', writer.fileCount);
const buffer = writer.buffer;
if (!packagePath) {
return { packagePath, zipBuffer: buffer };
return { packagePath, zipBuffer: buffer, zipFileCount: writer.fileCount };
} else if (buffer) {
await promises.writeFile(packagePath, buffer);
return { packagePath };
Expand Down
3 changes: 3 additions & 0 deletions src/convert/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ export class StandardWriter extends ComponentWriter {
}

export class ZipWriter extends ComponentWriter {
/**
* Count of files (not directories) added to the zip file.
*/
public fileCount: number = 0;
private zip = JSZip();
private zipBuffer?: Buffer;
Expand Down
4 changes: 4 additions & 0 deletions src/convert/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export type ConvertResult = {
* Buffer of converted package. `Undefined` if `outputDirectory` is omitted from zip output config.
*/
zipBuffer?: Buffer;
/**
* When a zip buffer is created, this is the number of files in the zip.
*/
zipFileCount?: number;
/**
* Converted source components. Not set if archiving the package.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export {
PackageOptions,
RetrieveOptions,
DeployVersionData,
DeployData,
DeployZipData,
RetrieveVersionData,
} from './client';
export {
Expand Down

2 comments on commit 7ac8c82

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7ac8c82 Previous: e1edd17 Ratio
eda-componentSetCreate-linux 228 ms 230 ms 0.99
eda-sourceToMdapi-linux 2331 ms 2367 ms 0.98
eda-sourceToZip-linux 1876 ms 1883 ms 1.00
eda-mdapiToSource-linux 2831 ms 2871 ms 0.99
lotsOfClasses-componentSetCreate-linux 422 ms 455 ms 0.93
lotsOfClasses-sourceToMdapi-linux 3611 ms 3678 ms 0.98
lotsOfClasses-sourceToZip-linux 3084 ms 3132 ms 0.98
lotsOfClasses-mdapiToSource-linux 3538 ms 3546 ms 1.00
lotsOfClassesOneDir-componentSetCreate-linux 741 ms 747 ms 0.99
lotsOfClassesOneDir-sourceToMdapi-linux 6448 ms 6476 ms 1.00
lotsOfClassesOneDir-sourceToZip-linux 5701 ms 5570 ms 1.02
lotsOfClassesOneDir-mdapiToSource-linux 6425 ms 6425 ms 1

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7ac8c82 Previous: e1edd17 Ratio
eda-componentSetCreate-win32 613 ms 609 ms 1.01
eda-sourceToMdapi-win32 4355 ms 4238 ms 1.03
eda-sourceToZip-win32 2883 ms 3008 ms 0.96
eda-mdapiToSource-win32 5516 ms 5575 ms 0.99
lotsOfClasses-componentSetCreate-win32 1165 ms 1215 ms 0.96
lotsOfClasses-sourceToMdapi-win32 7622 ms 7589 ms 1.00
lotsOfClasses-sourceToZip-win32 4975 ms 4980 ms 1.00
lotsOfClasses-mdapiToSource-win32 7729 ms 7795 ms 0.99
lotsOfClassesOneDir-componentSetCreate-win32 2050 ms 2128 ms 0.96
lotsOfClassesOneDir-sourceToMdapi-win32 13536 ms 13646 ms 0.99
lotsOfClassesOneDir-sourceToZip-win32 8805 ms 8889 ms 0.99
lotsOfClassesOneDir-mdapiToSource-win32 13885 ms 14088 ms 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.