1515 */
1616
1717import fs from 'fs' ;
18- import * as Archiver from 'archiver' ;
1918import * as path from 'path' ;
19+
20+ import * as Archiver from 'archiver' ;
21+ import { parseGcloudIgnore , toPlatformPath } from '@google-github-actions/actions-utils' ;
2022import 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 */
0 commit comments