15
15
*/
16
16
17
17
import fs from 'fs' ;
18
- import * as Archiver from 'archiver' ;
19
18
import * as path from 'path' ;
19
+
20
+ import * as Archiver from 'archiver' ;
21
+ import { parseGcloudIgnore , toPlatformPath } from '@google-github-actions/actions-utils' ;
20
22
import ignore from 'ignore' ;
21
23
22
24
/**
@@ -40,16 +42,28 @@ export type ZipOptions = {
40
42
* @param opts Options with which to invoke the zip.
41
43
* @returns filepath of the created zip file.
42
44
*/
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 > {
44
50
// Check dirpath
45
51
if ( ! fs . existsSync ( dirPath ) ) {
46
52
throw new Error ( `Unable to find ${ dirPath } ` ) ;
47
53
}
48
54
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
+ } ;
52
65
66
+ return new Promise ( ( resolve , reject ) => {
53
67
// Initialize archive
54
68
const archive = Archiver . create ( 'zip' , { zlib : { level : 7 } } ) ;
55
69
archive . on ( 'entry' , ( entry ) => {
@@ -65,42 +79,14 @@ export function zipDir(dirPath: string, outputPath: string, opts?: ZipOptions):
65
79
// Pipe all archive data to be written
66
80
archive . pipe ( output ) ;
67
81
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
-
79
82
// Add files in dir to archive iff file not ignored
80
- archive . directory ( dirPath , false , gIgnoreF ) ;
83
+ archive . directory ( dirPath , false , ignoreFn ) ;
81
84
82
85
// Finish writing files
83
86
archive . finalize ( ) ;
84
87
} ) ;
85
88
}
86
89
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
-
104
90
/**
105
91
* RealEntryData is an extended form of entry data.
106
92
*/
0 commit comments