Skip to content

Commit

Permalink
feat: support gcloudignore (#65)
Browse files Browse the repository at this point in the history
* feat: support gcloudignore

* fixlint

* use posix path
  • Loading branch information
bharathkkb committed Jun 9, 2021
1 parent c99cb30 commit bc4a13e
Show file tree
Hide file tree
Showing 13 changed files with 3,514 additions and 33 deletions.
3,380 changes: 3,348 additions & 32 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"@actions/core": "^1.2.6",
"@types/archiver": "^3.1.0",
"archiver": "^5.0.0",
"fast-glob": "^3.2.5",
"fs": "0.0.1-security",
"gaxios": "^3.1.0",
"google-auth-library": "^6.0.6",
"googleapis": "^58.0.0",
"ignore": "^4.0.6",
"yaml": "^1.10.0"
},
"devDependencies": {
Expand All @@ -54,6 +56,7 @@
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"mocha": "^8.1.1",
"node-stream-zip": "^1.13.4",
"prettier": "^2.0.5",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
Expand Down
37 changes: 36 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import * as fs from 'fs';
import * as core from '@actions/core';
import { Gaxios } from 'gaxios';
import * as Archiver from 'archiver';
import * as path from 'path';
import ignore from 'ignore';
import fg from 'fast-glob';

/**
* Zip a directory.
Expand Down Expand Up @@ -56,12 +59,44 @@ export async function zipDir(
});
archive.pipe(output);
// Add dir to root of archive
archive.directory(dirPath, false);
getFiles(dirPath).forEach((filepath) => {
archive.glob(filepath, {
cwd: dirPath,
noglobstar: true,
});
});
// Finish writing files
archive.finalize();
});
}

/**
* @param dir dir to collect files from
* @returns list of files that are not ignored
*/
export function getFiles(dir: string): string[] {
const files = fg.sync(['**'], { cwd: dir });
// return list of files that are not ignored
return ignore().add(getGcloudIgnores(dir)).filter(files);
}

/**
* @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());
}

/**
* Deletes a zip file from disk.
*
Expand Down
1 change: 1 addition & 0 deletions tests/test-func-ignore-node/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1 change: 1 addition & 0 deletions tests/test-func-ignore-node/foo/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data
10 changes: 10 additions & 0 deletions tests/test-func-ignore-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let message = req.query.message || req.body.message || 'Hello World!!';
res.status(200).send(message);
};
1 change: 1 addition & 0 deletions tests/test-func-ignore-node/notIgnored.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz
4 changes: 4 additions & 0 deletions tests/test-func-ignore-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "sample-http",
"version": "0.0.1"
}
1 change: 1 addition & 0 deletions tests/test-func-ignore/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
1 change: 1 addition & 0 deletions tests/test-func-ignore/ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
10 changes: 10 additions & 0 deletions tests/test-func-ignore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let message = req.query.message || req.body.message || 'Hello World!!';
res.status(200).send(message);
};
4 changes: 4 additions & 0 deletions tests/test-func-ignore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "sample-http",
"version": "0.0.1"
}
94 changes: 94 additions & 0 deletions tests/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { expect } from 'chai';
import os from 'os';
import * as fs from 'fs';
import 'mocha';
import * as path from 'path';
import { zipDir } from '../src/util';
import StreamZip from 'node-stream-zip';

const testDirNoIgnore = 'tests/test-node-func';
const testDirSimpleIgnore = 'tests/test-func-ignore';
const testDirNodeIgnore = 'tests/test-func-ignore-node';
const name = `zip-${Math.round(Math.random() * 100000)}`;
describe('Zip', function () {
it('creates a zipfile with correct files without gcloudignore', async function () {
const zf = await zipDir(
testDirNoIgnore,
path.posix.join(os.tmpdir(), name),
);
const uzf = new StreamZip.async({ file: zf });
const filesInsideZip = await uzf.entries();
const expectedFiles = getNonIgnoredFiles(testDirNoIgnore, testDirNoIgnore);
expect(await uzf.entriesCount).equal(expectedFiles.length);
for (const entry of Object.values(filesInsideZip)) {
expect(expectedFiles).to.be.include(entry.name);
}
});

it('creates a zipfile with correct files with simple gcloudignore', async function () {
const zf = await zipDir(
testDirSimpleIgnore,
path.posix.join(os.tmpdir(), name),
);
const uzf = new StreamZip.async({ file: zf });
const filesInsideZip = await uzf.entries();
const expectedFiles = getNonIgnoredFiles(
testDirSimpleIgnore,
testDirSimpleIgnore,
new Set(['ignore.txt', '.gcloudignore']),
);
expect(await uzf.entriesCount).equal(expectedFiles.length);
for (const entry of Object.values(filesInsideZip)) {
expect(expectedFiles).to.be.include(entry.name);
}
});

it('creates a zipfile with correct files with dir gcloudignore', async function () {
const zf = await zipDir(
testDirNodeIgnore,
path.posix.join(os.tmpdir(), name),
);
const uzf = new StreamZip.async({ file: zf });
const filesInsideZip = await uzf.entries();
const expectedFiles = getNonIgnoredFiles(
testDirNodeIgnore,
testDirNodeIgnore,
new Set([
'node_modules/foo/foo.txt',
'node_modules/bar/bar.txt',
'.gcloudignore',
]),
);
expect(await uzf.entriesCount).equal(expectedFiles.length);
for (const entry of Object.values(filesInsideZip)) {
expect(expectedFiles).to.be.include(entry.name);
}
});
});

function getNonIgnoredFiles(
parentDir: string,
directory: string,
ignore: Set<string> = new Set(),
fileList: string[] = [],
): string[] {
const items = fs.readdirSync(directory);
for (const item of items) {
const stat = fs.statSync(path.posix.join(directory, item));
if (stat.isDirectory())
fileList = getNonIgnoredFiles(
parentDir,
path.posix.join(directory, item),
ignore,
fileList,
);
else {
const fPath = path.posix.relative(
parentDir,
path.posix.join(directory, item),
);
if (!ignore.has(fPath)) fileList.push(fPath);
}
}
return fileList;
}

0 comments on commit bc4a13e

Please sign in to comment.