Skip to content

Commit

Permalink
feat: output archive entries in debug mode (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Mar 14, 2022
1 parent bd2e56a commit 88aa99f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
removeFile,
} from '@google-github-actions/actions-utils';

import { zipDir } from './util';
import { zipDir, ZipOptions } from './util';

// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -181,7 +181,7 @@ export type DeployOptions = {
onZip?: OnZipFunction;
onNew?: OnFunction;
onExisting?: OnFunction;
};
} & ZipOptions;

export type OnFunction = () => void;
export type OnZipFunction = (sourceDir: string, zipPath: string) => void;
Expand Down Expand Up @@ -458,7 +458,7 @@ export class CloudFunctionsClient {
const randomName = randomBytes(12).toString('hex');
const zipPath = path.join(tmpdir(), `cfsrc-${randomName}.zip`);
try {
await zipDir(sourceDir, zipPath);
await zipDir(sourceDir, zipPath, opts);
if (opts?.onZip) opts.onZip(sourceDir, zipPath);
} catch (err) {
throw new Error(`Zip file ${zipPath} creation failed: ${err}`);
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import { posix } from 'path';

import { EntryData } from 'archiver';
import {
debug as logDebug,
getBooleanInput,
getInput,
info as logInfo,
Expand All @@ -42,6 +44,7 @@ import {
SecretVolume,
} from './client';
import { SecretName } from './secret';
import { formatEntry } from './util';

async function run(): Promise<void> {
try {
Expand Down Expand Up @@ -261,6 +264,9 @@ async function run(): Promise<void> {
onZip: (sourceDir: string, zipPath: string) => {
logInfo(`Created zip file from '${sourceDir}' at '${zipPath}'`);
},
onZipEntry: (entry: EntryData) => {
logDebug(formatEntry(entry));
},
onNew: () => {
logInfo('Creating new Cloud Function deployment');
},
Expand Down
57 changes: 48 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@ import * as Archiver from 'archiver';
import * as path from 'path';
import ignore from 'ignore';

/**
* OnZipEntryFunction is a function that is called for each entry in the
* archive.
*/
export type OnZipEntryFunction = (entry: Archiver.EntryData) => void;

/**
* ZipOptions is used as input to the zip function.
*/
export type ZipOptions = {
onZipEntry?: OnZipEntryFunction;
};

/**
* Zip a directory.
*
* @param dirPath Directory to zip.
* @param outputPath Path to output file.
* @param opts Options with which to invoke the zip.
* @returns filepath of the created zip file.
*/
export function zipDir(dirPath: string, outputPath: string): Promise<string> {
export function zipDir(
dirPath: string,
outputPath: string,
opts?: ZipOptions,
): Promise<string> {
// Check dirpath
if (!fs.existsSync(dirPath)) {
throw new Error(`Unable to find ${dirPath}`);
Expand All @@ -37,15 +56,15 @@ export function zipDir(dirPath: string, outputPath: string): Promise<string> {

// Initialize archive
const archive = Archiver.create('zip', { zlib: { level: 7 } });
archive.on('warning', (err: Archiver.ArchiverError) => {
reject(err);
});
archive.on('error', (err: Archiver.ArchiverError) => {
reject(err);
});
output.on('finish', () => {
resolve(outputPath);
archive.on('entry', (entry) => {
// For some reason, TypeScript complains if this guard is outside the
// closure. It would be more performant just not create this listener, but
// alas...
if (opts?.onZipEntry) opts.onZipEntry(entry);
});
archive.on('warning', (err) => reject(err));
archive.on('error', (err) => reject(err));
output.on('finish', () => resolve(outputPath));

// Pipe all archive data to be written
archive.pipe(output);
Expand Down Expand Up @@ -85,3 +104,23 @@ export function getGcloudIgnores(dir: string): string[] {
.split(/\r?\n/)
.map((s) => s.trim());
}

/**
* RealEntryData is an extended form of entry data.
*/
type RealEntryData = Archiver.EntryData & {
sourcePath?: string;
type?: string;
};

/**
* formatEntry formats the given entry data into a single-line string.
* @returns string
*/
export function formatEntry(entry: RealEntryData): string {
const name = entry.name;
const mode = entry.mode || '000';
const sourcePath = entry.sourcePath || 'unknown';
const type = (entry.type || 'unknown').toUpperCase()[0];
return `[${type}] (${mode}) ${name} => ${sourcePath}`;
}

0 comments on commit 88aa99f

Please sign in to comment.