Skip to content

Commit

Permalink
fix: switch to ignore parsing logic in actions-utils (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed May 10, 2022
1 parent 1fc3ce8 commit 14963a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
54 changes: 20 additions & 34 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

import fs from 'fs';
import * as Archiver from 'archiver';
import * as path from 'path';

import * as Archiver from 'archiver';
import { parseGcloudIgnore, toPlatformPath } from '@google-github-actions/actions-utils';
import ignore from 'ignore';

/**
Expand All @@ -40,16 +42,28 @@ export type ZipOptions = {
* @param opts Options with which to invoke the zip.
* @returns filepath of the created zip file.
*/
export function zipDir(dirPath: string, outputPath: string, opts?: ZipOptions): Promise<string> {
export async function zipDir(
dirPath: string,
outputPath: string,
opts?: ZipOptions,
): Promise<string> {
// Check dirpath
if (!fs.existsSync(dirPath)) {
throw new Error(`Unable to find ${dirPath}`);
}

return new Promise((resolve, reject) => {
// Create output file stream
const output = fs.createWriteStream(outputPath);
// Create output file stream
const output = fs.createWriteStream(outputPath);

// Process gcloudignore
const ignoreFile = toPlatformPath(path.join(dirPath, '.gcloudignore'));
const ignores = await parseGcloudIgnore(ignoreFile);
const ignorer = ignore().add(ignores);
const ignoreFn = (entry: Archiver.EntryData): false | Archiver.EntryData => {
return ignorer.ignores(entry.name) ? false : entry;
};

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

// gcloudignore
// TODO(sethvargo): switch to actions-utils#parseGcloudIgnore
let gIgnoreF = undefined;
const ignores = getGcloudIgnores(dirPath);
if (ignores.length > 0) {
const gIgnore = ignore().add(ignores);
gIgnoreF = function (file: Archiver.EntryData): false | Archiver.EntryData {
return !gIgnore.ignores(file.name) ? file : false;
};
}

// Add files in dir to archive iff file not ignored
archive.directory(dirPath, false, gIgnoreF);
archive.directory(dirPath, false, ignoreFn);

// Finish writing files
archive.finalize();
});
}

/**
* @param dir dir which may contain .gcloudignore file
* @returns list of ignores in .gcloudignore if present
*/
export function getGcloudIgnores(dir: string): string[] {
const gcloudIgnorePath = path.posix.join(dir, '.gcloudignore');
if (!fs.existsSync(gcloudIgnorePath)) {
return [];
}
// read .gcloudignore, split on newline
return fs
.readFileSync(gcloudIgnorePath, { encoding: 'utf-8' })
.toString()
.split(/\r?\n/)
.map((s) => s.trim());
}

/**
* RealEntryData is an extended form of entry data.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Util', () => {

/**
*
* @param zipFile path to zipfile
* @param zipFilePath path to zipfile
* @returns list of files within zipfile
*/
async function getFilesInZip(zipFilePath: string): Promise<string[]> {
Expand Down

0 comments on commit 14963a4

Please sign in to comment.