Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated deprecated dependencies #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 60 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
"use strict";

const fs = require('fs');
const path = require('path');
const fsp = require('fs-promise');
const exec = require('child_process').exec;
const fs = require("fs");
const { remove } = require("fs-extra");
const path = require("path");
const { exec } = require("child_process");
const { Storage } = require("@google-cloud/storage");

const storage = new Storage();

function AppNotFoundError(message) {
let error = new Error(message);
error.name = 'AppNotFoundError';
const error = new Error(message);
error.name = "AppNotFoundError";

return error;
}

let gcloud = require('gcloud');

let storage = gcloud.storage();

/*
* Downloader class that downloads the latest version of the deployed
* app from GCloud Storage and unzips it.
*/
class GCloudStorageDownloader {
constructor(options) {
this.ui = options.ui;
this.configBucket = options.bucket;
this.configBucket = options.bucket;
this.configKey = options.key;
}

download() {
if (!this.configBucket || !this.configKey) {
this.ui.writeError('no gcloud Storage bucket or key; not downloading app');
this.ui.writeError(
"no gcloud Storage bucket or key; not downloading app"
);
return Promise.reject(new AppNotFoundError());
}

return this.fetchCurrentVersion()
.then(() => this.removeOldApp())
.then(() => this.downloadAppZip())
Expand All @@ -42,76 +43,77 @@ class GCloudStorageDownloader {
}

removeOldApp() {
this.ui.writeLine('removing ' + this.outputPath);
return fsp.remove(this.outputPath);
this.ui.writeLine("removing " + this.outputPath);
return remove(this.outputPath);
}

fetchCurrentVersion() {
let bucket = this.configBucket;
let key = this.configKey;

this.ui.writeLine('fetching current app version from ' + bucket + '/' + key);

let stream = this.readStream(bucket, key);

return this.streamToPromise(stream)
.then(data => {
let config = JSON.parse(data);
this.ui.writeLine('got config', config);

this.appBucket = config.bucket;
this.appKey = config.key;
this.zipPath = path.basename(config.key);
this.outputPath = outputPathFor(this.zipPath);
});
const bucket = this.configBucket;
const key = this.configKey;

this.ui.writeLine(
"fetching current app version from " + bucket + "/" + key
);

const stream = this.readStream(bucket, key);

return this.streamToPromise(stream).then((data) => {
let config = JSON.parse(data);
this.ui.writeLine("got config", config);

this.appBucket = config.bucket;
this.appKey = config.key;
this.zipPath = path.basename(config.key);
this.outputPath = outputPathFor(this.zipPath);
});
}

readStream(bucket, key) {
let file = storage.bucket(bucket).file(key);
readStream(bucket, key) {
const file = storage.bucket(bucket).file(key);
return file.createReadStream();
}

streamToPromise(stream) {
return new Promise(function(resolve, reject){
let data = '';
stream.on('error', reject)
.on('data', chunk => data+=chunk )
.on('end', function() {
return new Promise(function (resolve, reject) {
let data = "";
stream
.on("error", reject)
.on("data", (chunk) => (data += chunk))
.on("end", function () {
resolve(data);
});
});
}

downloadAppZip() {
return new Promise((res, rej) => {
let bucket = this.appBucket;
let key = this.appKey;
const bucket = this.appBucket;
const key = this.appKey;

let zipPath = this.zipPath;
let file = fs.createWriteStream(zipPath);
let stream = this.readStream(bucket, key);
const zipPath = this.zipPath;
const file = fs.createWriteStream(zipPath);
const stream = this.readStream(bucket, key);

this.ui.writeLine("saving gcloud Storage object " + bucket + "/" + key + " to " + zipPath);
this.ui.writeLine(
"saving gcloud Storage object " + bucket + "/" + key + " to " + zipPath
);

stream.pipe(file)
.on('close', res)
.on('error', rej);
stream.pipe(file).on("close", res).on("error", rej);
});
}

unzipApp() {
let zipPath = this.zipPath;
const zipPath = this.zipPath;

return this.exec('unzip ' + zipPath)
.then(() => {
this.ui.writeLine("unzipped " + zipPath);
});
return this.exec("unzip " + zipPath).then(() => {
this.ui.writeLine("unzipped " + zipPath);
});
}

installNPMDependencies() {
return this.exec(`cd ${this.outputPath} && npm install`)
.then(() => this.ui.writeLine('installed npm dependencies'))
.catch(() => this.ui.writeError('unable to install npm dependencies'));
.then(() => this.ui.writeLine("installed npm dependencies"))
.catch(() => this.ui.writeError("unable to install npm dependencies"));
}

exec(command) {
Expand All @@ -130,10 +132,10 @@ class GCloudStorageDownloader {
}

function outputPathFor(zipPath) {
let name = path.basename(zipPath, '.zip');
const name = path.basename(zipPath, ".zip");

// Remove MD5 hash
return name.split('-').slice(0, -1).join('-');
return name.split("-").slice(0, -1).join("-");
}

module.exports = GCloudStorageDownloader;
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "fastboot-gcloud-storage-downloader",
"version": "0.1.0",
"version": "0.1.2",
"description": "A FastBoot App Server downloader for gcloud Storage",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/EmberSherpa/fastboot-gcloud-storage-downloader.git"
Expand All @@ -24,7 +21,7 @@
},
"homepage": "https://github.com/EmberSherpa/fastboot-gcloud-storage-downloader#readme",
"dependencies": {
"fs-promise": "0.5.0",
"gcloud": "0.32.0"
"@google-cloud/storage": "^5.5.0",
"fs-extra": "^9.0.1"
}
}
Loading