UNPKG

nx-remotecache-gcp

Version:

Remote caching for @nrwl/nx using Google Cloud Buckets

151 lines (147 loc) 5.34 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // lib/index.ts var lib_exports = {}; __export(lib_exports, { default: () => lib_default }); module.exports = __toCommonJS(lib_exports); var import_nx_remotecache_custom = require("nx-remotecache-custom"); var import_stream = require("stream"); var import_promises2 = require("stream/promises"); // lib/gcp-specific.ts var import_storage = require("@google-cloud/storage"); var import_promises = require("fs/promises"); function buildConfiguration(options) { var _a, _b, _c; const bucketName = (_a = process.env.NXCACHE_GCP_BUCKET_NAME) != null ? _a : options == null ? void 0 : options.bucketName; const projectId = (_b = process.env.NXCACHE_GCP_PROJECT) != null ? _b : options == null ? void 0 : options.googleProject; const googleApplicationCredentialsPath = (_c = process.env.NX_GOOGLE_APPLICATION_CREDENTIALS_PATH) != null ? _c : options == null ? void 0 : options.googleApplicationCredentialsPath; return { bucketName, googleProject: projectId, googleApplicationCredentialsPath }; } function verifyConfiguration(options) { const bucketName = options == null ? void 0 : options.bucketName; const projectId = options == null ? void 0 : options.googleProject; if (bucketName === void 0) { throw new Error( "You forgot to specify a google bucket name, please check your nx.json or set the environment variable NXCACHE_GCP_BUCKET_NAME" ); } if (projectId === void 0) { throw new Error( "You forgot to specify a google project, please check your nx.json or set the environment variable NXCACHE_GCP_BUCKET_NAME" ); } return { bucketName, googleProject: projectId, googleApplicationCredentialsPath: options.googleApplicationCredentialsPath }; } async function getGCSBucket(configuration) { var _a; const bucketName = (_a = process.env.NXCACHE_GCP_BUCKET_NAME) != null ? _a : configuration.bucketName; const googleStorageOptions = { projectId: configuration.googleProject, retryOptions: { autoRetry: true, maxRetries: 4 } }; if (configuration.googleApplicationCredentialsPath) { try { googleStorageOptions.credentials = JSON.parse( await (0, import_promises.readFile)( configuration.googleApplicationCredentialsPath, "utf-8" ) ); } catch (error) { if (error instanceof Error && "code" in error && error.code === "ENOENT") { throw Error( `Was unable to find the authentication file located at ${configuration.googleApplicationCredentialsPath}` ); } if (error instanceof SyntaxError) { throw Error( `The provided file at ${configuration.googleApplicationCredentialsPath} does not contain valid JSON` ); } throw error; } } const storageClient = new import_storage.Storage(googleStorageOptions); const bucket = storageClient.bucket(bucketName); const [bucketExists] = await bucket.exists(); if (!bucketExists) { throw Error(`The given Bucket ${bucketName} does not exist`); } return bucket; } function constructGCSFileReference(bucket, filename) { return bucket.file(filename); } async function bucketFileExists(bucketFile) { const [exists] = await bucketFile.exists(); return exists; } // lib/index.ts var lib_default = (0, import_nx_remotecache_custom.createCustomRunner)( async (options) => { let bucket; (0, import_nx_remotecache_custom.initEnv)(options); const configuration = buildConfiguration(options); const verifiedConfiguration = verifyConfiguration(configuration); const getBucket = async () => { if (!bucket) { bucket = await getGCSBucket(verifiedConfiguration); } return bucket; }; return { name: "Google Cloud Bucket", fileExists: async (filename) => { const bucketFile = constructGCSFileReference( await getBucket(), filename ); return await bucketFileExists(bucketFile); }, retrieveFile: async (filename) => { const bucketFile = constructGCSFileReference( await getBucket(), filename ); const downloadedFile = bucketFile.download(); return import_stream.Readable.from(await downloadedFile); }, storeFile: async (filename, stream) => { const uploadStream = constructGCSFileReference( await getBucket(), filename ).createWriteStream(); await (0, import_promises2.pipeline)(stream, uploadStream); } }; } );