Skip to content

Commit 14963a4

Browse files
authored
fix: switch to ignore parsing logic in actions-utils (#322)
1 parent 1fc3ce8 commit 14963a4

File tree

2 files changed

+21
-35
lines changed

2 files changed

+21
-35
lines changed

src/util.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616

1717
import fs from 'fs';
18-
import * as Archiver from 'archiver';
1918
import * as path from 'path';
19+
20+
import * as Archiver from 'archiver';
21+
import { parseGcloudIgnore, toPlatformPath } from '@google-github-actions/actions-utils';
2022
import ignore from 'ignore';
2123

2224
/**
@@ -40,16 +42,28 @@ export type ZipOptions = {
4042
* @param opts Options with which to invoke the zip.
4143
* @returns filepath of the created zip file.
4244
*/
43-
export function zipDir(dirPath: string, outputPath: string, opts?: ZipOptions): Promise<string> {
45+
export async function zipDir(
46+
dirPath: string,
47+
outputPath: string,
48+
opts?: ZipOptions,
49+
): Promise<string> {
4450
// Check dirpath
4551
if (!fs.existsSync(dirPath)) {
4652
throw new Error(`Unable to find ${dirPath}`);
4753
}
4854

49-
return new Promise((resolve, reject) => {
50-
// Create output file stream
51-
const output = fs.createWriteStream(outputPath);
55+
// Create output file stream
56+
const output = fs.createWriteStream(outputPath);
57+
58+
// Process gcloudignore
59+
const ignoreFile = toPlatformPath(path.join(dirPath, '.gcloudignore'));
60+
const ignores = await parseGcloudIgnore(ignoreFile);
61+
const ignorer = ignore().add(ignores);
62+
const ignoreFn = (entry: Archiver.EntryData): false | Archiver.EntryData => {
63+
return ignorer.ignores(entry.name) ? false : entry;
64+
};
5265

66+
return new Promise((resolve, reject) => {
5367
// Initialize archive
5468
const archive = Archiver.create('zip', { zlib: { level: 7 } });
5569
archive.on('entry', (entry) => {
@@ -65,42 +79,14 @@ export function zipDir(dirPath: string, outputPath: string, opts?: ZipOptions):
6579
// Pipe all archive data to be written
6680
archive.pipe(output);
6781

68-
// gcloudignore
69-
// TODO(sethvargo): switch to actions-utils#parseGcloudIgnore
70-
let gIgnoreF = undefined;
71-
const ignores = getGcloudIgnores(dirPath);
72-
if (ignores.length > 0) {
73-
const gIgnore = ignore().add(ignores);
74-
gIgnoreF = function (file: Archiver.EntryData): false | Archiver.EntryData {
75-
return !gIgnore.ignores(file.name) ? file : false;
76-
};
77-
}
78-
7982
// Add files in dir to archive iff file not ignored
80-
archive.directory(dirPath, false, gIgnoreF);
83+
archive.directory(dirPath, false, ignoreFn);
8184

8285
// Finish writing files
8386
archive.finalize();
8487
});
8588
}
8689

87-
/**
88-
* @param dir dir which may contain .gcloudignore file
89-
* @returns list of ignores in .gcloudignore if present
90-
*/
91-
export function getGcloudIgnores(dir: string): string[] {
92-
const gcloudIgnorePath = path.posix.join(dir, '.gcloudignore');
93-
if (!fs.existsSync(gcloudIgnorePath)) {
94-
return [];
95-
}
96-
// read .gcloudignore, split on newline
97-
return fs
98-
.readFileSync(gcloudIgnorePath, { encoding: 'utf-8' })
99-
.toString()
100-
.split(/\r?\n/)
101-
.map((s) => s.trim());
102-
}
103-
10490
/**
10591
* RealEntryData is an extended form of entry data.
10692
*/

tests/util.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Util', () => {
6363

6464
/**
6565
*
66-
* @param zipFile path to zipfile
66+
* @param zipFilePath path to zipfile
6767
* @returns list of files within zipfile
6868
*/
6969
async function getFilesInZip(zipFilePath: string): Promise<string[]> {

0 commit comments

Comments
 (0)