Skip to content

Commit bc4a13e

Browse files
authored
feat: support gcloudignore (#65)
* feat: support gcloudignore * fixlint * use posix path
1 parent c99cb30 commit bc4a13e

File tree

13 files changed

+3514
-33
lines changed

13 files changed

+3514
-33
lines changed

package-lock.json

Lines changed: 3348 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
"@actions/core": "^1.2.6",
3535
"@types/archiver": "^3.1.0",
3636
"archiver": "^5.0.0",
37+
"fast-glob": "^3.2.5",
3738
"fs": "0.0.1-security",
3839
"gaxios": "^3.1.0",
3940
"google-auth-library": "^6.0.6",
4041
"googleapis": "^58.0.0",
42+
"ignore": "^4.0.6",
4143
"yaml": "^1.10.0"
4244
},
4345
"devDependencies": {
@@ -54,6 +56,7 @@
5456
"eslint-config-prettier": "^6.11.0",
5557
"eslint-plugin-prettier": "^3.1.4",
5658
"mocha": "^8.1.1",
59+
"node-stream-zip": "^1.13.4",
5760
"prettier": "^2.0.5",
5861
"ts-node": "^8.10.2",
5962
"typescript": "^3.9.7"

src/util.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import * as fs from 'fs';
1818
import * as core from '@actions/core';
1919
import { Gaxios } from 'gaxios';
2020
import * as Archiver from 'archiver';
21+
import * as path from 'path';
22+
import ignore from 'ignore';
23+
import fg from 'fast-glob';
2124

2225
/**
2326
* Zip a directory.
@@ -56,12 +59,44 @@ export async function zipDir(
5659
});
5760
archive.pipe(output);
5861
// Add dir to root of archive
59-
archive.directory(dirPath, false);
62+
getFiles(dirPath).forEach((filepath) => {
63+
archive.glob(filepath, {
64+
cwd: dirPath,
65+
noglobstar: true,
66+
});
67+
});
6068
// Finish writing files
6169
archive.finalize();
6270
});
6371
}
6472

73+
/**
74+
* @param dir dir to collect files from
75+
* @returns list of files that are not ignored
76+
*/
77+
export function getFiles(dir: string): string[] {
78+
const files = fg.sync(['**'], { cwd: dir });
79+
// return list of files that are not ignored
80+
return ignore().add(getGcloudIgnores(dir)).filter(files);
81+
}
82+
83+
/**
84+
* @param dir dir which may contain .gcloudignore file
85+
* @returns list of ignores in .gcloudignore if present
86+
*/
87+
export function getGcloudIgnores(dir: string): string[] {
88+
const gcloudIgnorePath = path.posix.join(dir, '.gcloudignore');
89+
if (!fs.existsSync(gcloudIgnorePath)) {
90+
return [];
91+
}
92+
// read .gcloudignore, split on newline
93+
return fs
94+
.readFileSync(gcloudIgnorePath, { encoding: 'utf-8' })
95+
.toString()
96+
.split(/\r?\n/)
97+
.map((s) => s.trim());
98+
}
99+
65100
/**
66101
* Deletes a zip file from disk.
67102
*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data

tests/test-func-ignore-node/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Responds to any HTTP request.
3+
*
4+
* @param {!express:Request} req HTTP request context.
5+
* @param {!express:Response} res HTTP response context.
6+
*/
7+
exports.helloWorld = (req, res) => {
8+
let message = req.query.message || req.body.message || 'Hello World!!';
9+
res.status(200).send(message);
10+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baz
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "sample-http",
3+
"version": "0.0.1"
4+
}

tests/test-func-ignore/.gcloudignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

tests/test-func-ignore/ignore.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

0 commit comments

Comments
 (0)