Skip to content

Commit

Permalink
[bin] [ux] Better handling of pkg deployment #14
Browse files Browse the repository at this point in the history
  * Ensures ENOENT package error is sent to user
  * Adds confirmation step to deploy
  * Adds yes/no CLI helper
  • Loading branch information
Marak committed Dec 3, 2017
1 parent 0781d02 commit 3ccb6a6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
66 changes: 47 additions & 19 deletions bin/hook-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,54 @@ var argv = require('minimist')(process.argv.slice(2));

var deployPath = argv._[0] || process.cwd();

console.log('attempting to deploy: ' + deployPath);
console.log('connecting to: ' + config.uri);
var prompt = require('mschema-prompt');

client.hook.deploy({ path: deployPath }, function (err, result) {
if (err) {
console.log('There was an issue communicating with hook.io');
console.log('Error: ' + err.message);
return;
}
if (result.status === 404) {
console.log(result);
return;
var handlers = {
'confirm' : {
default: "yes",
required: true,
label: 'Deploy ' + deployPath + " ?",
conform: require('../lib/helpers/cli/conform-yes-no')
}
if (result.status === "created") {
console.log('created new service at: '+ config.uri + '/' + result.hook.owner + '/' + result.hook.name);
return;
}

prompt(handlers, _deploy);

function _deploy (err, input) {
if (err) {
console.log(err.message);
process.exit();
}
if (result.status === "updated") {
console.log('updated service: ' + config.uri + '/' + result.hook.owner + '/' + result.hook.name);
return;
if (!input.confirm) {
console.log('cancelled');
process.exit();
}
console.log(result);
});
console.log('attempting to deploy: ' + deployPath);
console.log('connecting to: ' + config.uri);

client.hook.deploy({ path: deployPath }, function (err, result) {
if (err) {
console.log('Error: ' + err.message);
if (err.code === "ENOENT") {
console.log('A valid package.json file is required in order to deploy a new service');
console.log('You can run `hook-init` to generate a new package.json file')
} else {
console.log('There was an issue communicating with hook.io');
}
return;
}
if (result.status === 404) {
console.log(result);
return;
}
if (result.status === "created") {
console.log('created new service at: '+ config.uri + '/' + result.hook.owner + '/' + result.hook.name);
return;
}
if (result.status === "updated") {
console.log('updated service: ' + config.uri + '/' + result.hook.owner + '/' + result.hook.name);
return;
}
console.log(result);
});
}
11 changes: 11 additions & 0 deletions lib/helpers/cli/conform-yes-no.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function conform (val, cb) {
var check = new RegExp(/y[es]*|n[o]?|1/i)
if (check.test(val)) {
if (/y(?:es)?|1/i.test(val)) {
return cb(null, true);
} else {
return cb(null, false);
}
}
return cb(new Error('yes or no answer required'));
}

0 comments on commit 3ccb6a6

Please sign in to comment.