diff --git a/.eslintrc.js b/.eslintrc.js index 2873e2f..ed0fac1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -6,7 +6,7 @@ module.exports = { }, extends: 'eslint:recommended', env: { - browser: true + node: true }, rules: { } diff --git a/lib/gcloud/storage.js b/lib/gcloud/storage.js index 5c23f9d..f2b294c 100644 --- a/lib/gcloud/storage.js +++ b/lib/gcloud/storage.js @@ -1,11 +1,9 @@ /* jshint node: true */ 'use strict'; -const Readable = require('stream').Readable; const { Storage } = require('@google-cloud/storage'); class GoogleCloudStorage { - constructor(credentials) { const options = {}; if (credentials) { @@ -15,25 +13,14 @@ class GoogleCloudStorage { this.storage = new Storage(options); } - upload(bucket, key, file) { - let upload = this.storage.bucket(bucket).file(key); - - if (typeof file === 'string') { - let stream = new Readable(); - stream.push(file); - stream.push(null); - file = stream; - } - - return new Promise(function(resolve, reject){ - file.pipe(upload.createWriteStream()) - .on('error', function(error){ - reject('Failed to upload file ' + key + ' - ' + error.message); - }) - .on('finish', resolve); - }); + upload(bucket, key, filepath) { + return this.storage.bucket(bucket).upload( + filepath, + { + destination: key + } + ); } - } module.exports = GoogleCloudStorage; diff --git a/lib/tasks/upload.js b/lib/tasks/upload.js index 23118d2..98372b8 100644 --- a/lib/tasks/upload.js +++ b/lib/tasks/upload.js @@ -5,6 +5,7 @@ const GoogleCloudStorage = require('../gcloud/storage'); const fs = require('fs'); const path = require('path'); +const util = require('util'); class GoogleCloudStorageUploadTask { @@ -19,34 +20,38 @@ class GoogleCloudStorageUploadTask { this.storage = new GoogleCloudStorage(options.credentials); } - - run() { - return this.uploadHashedZip() - .then(() => this.uploadVersionFile()); + async run() { + await this.uploadHashedZip(); + await this.uploadVersionFile(); } uploadHashedZip() { - let hashedZipPath = this.hashedZipPath; - let bucket = this.bucket; - - this.log('uploading ' + hashedZipPath + ' to ' + bucket, { verbose: true }); - - let file = fs.createReadStream(hashedZipPath); + this.log(`uploading ${this.hashedZipPath} to ${this.bucket} at ${this.hashedZipKey}`, { verbose: true }); - return this.storage.upload(bucket, this.hashedZipKey, file); + return this.storage.upload( + this.bucket, + this.hashedZipKey, + path.resolve(this.hashedZipPath) + ); } - uploadVersionFile() { - let versionFile = { - bucket: this.bucket, - key: this.hashedZipKey - }; + async uploadVersionFile() { + this.log(`uploading version file to ${this.bucket} at ${this.key}`, { verbose: true }); - versionFile = JSON.stringify(versionFile); + const dir = path.dirname(this.hashedZipPath); + const filename = path.resolve(path.join(dir, this.key)); - return this.storage.upload(this.bucket, this.key, versionFile); - } + const writeFile = util.promisify(fs.writeFile); + await writeFile( + filename, + JSON.stringify({ + bucket: this.bucket, + key: this.hashedZipKey + }) + ); + return this.storage.upload(this.bucket, this.key, filename); + } } module.exports = GoogleCloudStorageUploadTask;