Skip to content

Commit 88aa99f

Browse files
authored
feat: output archive entries in debug mode (#300)
1 parent bd2e56a commit 88aa99f

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
removeFile,
3232
} from '@google-github-actions/actions-utils';
3333

34-
import { zipDir } from './util';
34+
import { zipDir, ZipOptions } from './util';
3535

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

186186
export type OnFunction = () => void;
187187
export type OnZipFunction = (sourceDir: string, zipPath: string) => void;
@@ -458,7 +458,7 @@ export class CloudFunctionsClient {
458458
const randomName = randomBytes(12).toString('hex');
459459
const zipPath = path.join(tmpdir(), `cfsrc-${randomName}.zip`);
460460
try {
461-
await zipDir(sourceDir, zipPath);
461+
await zipDir(sourceDir, zipPath, opts);
462462
if (opts?.onZip) opts.onZip(sourceDir, zipPath);
463463
} catch (err) {
464464
throw new Error(`Zip file ${zipPath} creation failed: ${err}`);

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import { posix } from 'path';
1818

19+
import { EntryData } from 'archiver';
1920
import {
21+
debug as logDebug,
2022
getBooleanInput,
2123
getInput,
2224
info as logInfo,
@@ -42,6 +44,7 @@ import {
4244
SecretVolume,
4345
} from './client';
4446
import { SecretName } from './secret';
47+
import { formatEntry } from './util';
4548

4649
async function run(): Promise<void> {
4750
try {
@@ -261,6 +264,9 @@ async function run(): Promise<void> {
261264
onZip: (sourceDir: string, zipPath: string) => {
262265
logInfo(`Created zip file from '${sourceDir}' at '${zipPath}'`);
263266
},
267+
onZipEntry: (entry: EntryData) => {
268+
logDebug(formatEntry(entry));
269+
},
264270
onNew: () => {
265271
logInfo('Creating new Cloud Function deployment');
266272
},

src/util.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@ import * as Archiver from 'archiver';
1919
import * as path from 'path';
2020
import ignore from 'ignore';
2121

22+
/**
23+
* OnZipEntryFunction is a function that is called for each entry in the
24+
* archive.
25+
*/
26+
export type OnZipEntryFunction = (entry: Archiver.EntryData) => void;
27+
28+
/**
29+
* ZipOptions is used as input to the zip function.
30+
*/
31+
export type ZipOptions = {
32+
onZipEntry?: OnZipEntryFunction;
33+
};
34+
2235
/**
2336
* Zip a directory.
2437
*
2538
* @param dirPath Directory to zip.
39+
* @param outputPath Path to output file.
40+
* @param opts Options with which to invoke the zip.
2641
* @returns filepath of the created zip file.
2742
*/
28-
export function zipDir(dirPath: string, outputPath: string): Promise<string> {
43+
export function zipDir(
44+
dirPath: string,
45+
outputPath: string,
46+
opts?: ZipOptions,
47+
): Promise<string> {
2948
// Check dirpath
3049
if (!fs.existsSync(dirPath)) {
3150
throw new Error(`Unable to find ${dirPath}`);
@@ -37,15 +56,15 @@ export function zipDir(dirPath: string, outputPath: string): Promise<string> {
3756

3857
// Initialize archive
3958
const archive = Archiver.create('zip', { zlib: { level: 7 } });
40-
archive.on('warning', (err: Archiver.ArchiverError) => {
41-
reject(err);
42-
});
43-
archive.on('error', (err: Archiver.ArchiverError) => {
44-
reject(err);
45-
});
46-
output.on('finish', () => {
47-
resolve(outputPath);
59+
archive.on('entry', (entry) => {
60+
// For some reason, TypeScript complains if this guard is outside the
61+
// closure. It would be more performant just not create this listener, but
62+
// alas...
63+
if (opts?.onZipEntry) opts.onZipEntry(entry);
4864
});
65+
archive.on('warning', (err) => reject(err));
66+
archive.on('error', (err) => reject(err));
67+
output.on('finish', () => resolve(outputPath));
4968

5069
// Pipe all archive data to be written
5170
archive.pipe(output);
@@ -85,3 +104,23 @@ export function getGcloudIgnores(dir: string): string[] {
85104
.split(/\r?\n/)
86105
.map((s) => s.trim());
87106
}
107+
108+
/**
109+
* RealEntryData is an extended form of entry data.
110+
*/
111+
type RealEntryData = Archiver.EntryData & {
112+
sourcePath?: string;
113+
type?: string;
114+
};
115+
116+
/**
117+
* formatEntry formats the given entry data into a single-line string.
118+
* @returns string
119+
*/
120+
export function formatEntry(entry: RealEntryData): string {
121+
const name = entry.name;
122+
const mode = entry.mode || '000';
123+
const sourcePath = entry.sourcePath || 'unknown';
124+
const type = (entry.type || 'unknown').toUpperCase()[0];
125+
return `[${type}] (${mode}) ${name} => ${sourcePath}`;
126+
}

0 commit comments

Comments
 (0)