diff --git a/src/io-methods/google-cloud-storage.js b/src/io-methods/google-cloud-storage.js index b3aeca0..fb2dfd2 100644 --- a/src/io-methods/google-cloud-storage.js +++ b/src/io-methods/google-cloud-storage.js @@ -1,10 +1,12 @@ const { Storage } = require("@google-cloud/storage"); +const { first } = require("lodash"); const storage = new Storage(); const regex = /^(?:google|gs):\/\/(.+?)\/(.+)$/; const { getCacheControl } = require("../cache-control"); +const { getEmptyManifest } = require("../modify"); exports.isGooglePath = function (path) { return regex.test(path); @@ -18,15 +20,16 @@ function parseFilePath(filePath) { return { bucketName, fileName }; } -exports.readManifest = function (filePath) { - return Promise.resolve().then(() => { - const { bucketName, fileName } = parseFilePath(filePath); - return storage - .bucket(bucketName) - .file(fileName) - .download() - .then((data) => data.toString("utf-8")); - }); +exports.readManifest = async function (filePath) { + const { bucketName, fileName } = parseFilePath(filePath); + const file = await storage.bucket(bucketName).file(fileName); + const exists = first(await file.exists()); + + if (!exists) { + await exports.writeManifest(filePath, JSON.stringify(getEmptyManifest())); + } + + return file.download().then((data) => data.toString("utf-8")); }; exports.writeManifest = function (filePath, data) {