Skip to content

Commit

Permalink
Works now?
Browse files Browse the repository at this point in the history
  • Loading branch information
John98Zakaria committed Apr 24, 2023
1 parent 6cc760b commit 0a17556
Show file tree
Hide file tree
Showing 6 changed files with 1,738 additions and 68 deletions.
26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": [
"standard-with-typescript",
"prettier"
],
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"project": [
"tsconfig.json"
]
},
"rules": {
"indent": [
"warn",
4
],
"@typescript-eslint/explicit-function-return-type": "off"
}
}
5 changes: 4 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{}
{
"singleQuote": true,
"tabWidth": 4
}
34 changes: 34 additions & 0 deletions lib/gcp-specific.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type Bucket, type File, Storage } from '@google-cloud/storage';

export interface GCPBucketOptions {
googleProject: string;
bucketName: string;
}

export async function getGCSBucket(options: GCPBucketOptions) {
const bucketURL = options.bucketName;
const storageClient = new Storage({
projectId: options.googleProject,
retryOptions: {
autoRetry: true,
maxRetries: 4,
},
});
const bucket = storageClient.bucket(bucketURL);

const [bucketExists] = await bucket.exists();
if (!bucketExists) {
throw Error(`The given Bucket ${bucketURL} does not exist`);
}

return bucket;
}

export function constructGCSFile(bucket: Bucket, filename: string) {
return bucket.file(filename);
}

export async function bucketFileExists(bucketFile: File): Promise<boolean> {
const [exists] = await bucketFile.exists();
return exists;
}
81 changes: 37 additions & 44 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
import { createCustomRunner, initEnv } from "nx-remotecache-custom";
import { Storage } from "@google-cloud/storage";

const ENV_GCP_BUCKET_URL = "gs://";

const getEnv = (key: string) => process.env[key];

async function getGCPFileClient(filename: string, options: AzureBlobRunnerOptions) {
const bucketURL = process.env.GCP_BUCKET_URL;
const storageClient = new Storage();
const bucket = storageClient.bucket(bucketURL);
const bucketFile = bucket.file(filename);


if (!await bucket.exists()) {
throw Error(
`The given Bucket ${bucketURL} does not exist`
);
}

return bucketFile;
}

interface AzureBlobRunnerOptions {
connectionString: string;
accountKey: string;
accountName: string;
container: string;
azureUrl: string;
sasUrl: string;
}

export default createCustomRunner<AzureBlobRunnerOptions>(async (options) => {
initEnv(options);
const GCPFile = async (filename: string) => await getGCPFileClient(filename, options);

return {
name: "Azure Blob Storage",
fileExists: async (filename) => (await GCPFile(filename)).exists(),
retrieveFile: async (filename) =>
(await blob(filename).download()).readableStreamBody!,
storeFile: (filename, stream) => blob(filename).uploadStream(stream)
};
});
import { createCustomRunner, initEnv } from 'nx-remotecache-custom';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import type { GCPBucketOptions } from './gcp-specific';
import {
bucketFileExists,
constructGCSFile,
getGCSBucket,
} from './gcp-specific';
import type { RemoteCacheImplementation } from 'nx-remotecache-custom/types/remote-cache-implementation';

export default createCustomRunner<GCPBucketOptions>(
async (options): Promise<RemoteCacheImplementation> => {
initEnv(options);
const bucket = await getGCSBucket(options);

return {
name: 'Google Cloud Bucket',
fileExists: async (filename) => {
const bucketFile = constructGCSFile(bucket, filename);
return await bucketFileExists(bucketFile);
},
retrieveFile: async (filename) => {
const bucketFile = constructGCSFile(bucket, filename);
const downloadedFile = bucketFile.download();
return Readable.from(await downloadedFile);
},
storeFile: async (filename, stream) => {
const uploadStream = constructGCSFile(
bucket,
filename
).createWriteStream();
await pipeline(stream, uploadStream);
},
};
}
);
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "nx-remotecache-gcp",
"version": "1.0.0",
"description": "Remote caching for @nrwl/nx using Google Cloud Buckets",
"main": "index.js",
"typings": "index.d.ts",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "rm -rf dist && tsc && cp package.json dist/package.json && cp README.md dist/README.md && cp CHANGELOG.md dist/CHANGELOG.md",
"prepare": "yarn build",
"release": "yarn run build && cd dist && yarn publish && cd ..",
"local-release": "yarn run build && cd dist && yarn publish --registry=http://localhost:4873/ && cd ..",
"link": "yarn run build && cd dist && yarn link && cd .."
"link": "yarn run build && cd dist && yarn link -A . && cd .."
},
"repository": {
"type": "git",
Expand All @@ -34,6 +34,15 @@
"devDependencies": {
"@types/node": "^18.16.0",
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.59.1",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.8.0",
"eslint-config-standard-with-typescript": "latest",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"prettier": "^2.8.8",
"typescript": "^5.0.4"
},
"dependencies": {
Expand Down
Loading

0 comments on commit 0a17556

Please sign in to comment.