Last active
August 14, 2018 00:24
-
-
Save alexweber/68b59b1d3c971b81a61de477065cd6eb to your computer and use it in GitHub Desktop.
create plugin script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const requestify = require('requestify'); | |
const homedir = require('os').homedir(); | |
const cfg = require(homedir + '/.maya/mayarc.json'); | |
// Load default dev token and namespace prefix from glboal user config. | |
const token = cfg.token; | |
const prefix = cfg.prefix; | |
////// START EDITING HERE /////// | |
const name = 'Awesome Plugin'; | |
const service = 'spectacular-service'; | |
// var service = false; | |
const offline = true; | |
const firebaseUrl = 'https://MY-AWESOME-PLUGIN.firebaseio.com/'; | |
const firebaseSecret = 'TOPSECRET'; | |
////// STOP HERE MEOW /////// | |
const namespace = slugify(prefix + name); | |
const baseUrl = 'https://api.zenginehq.com/v1/plugins/'; | |
const options = { headers: { 'Authorization': 'Bearer ' + token } }; | |
const data = { | |
namespace: namespace, | |
privacy: 'private', | |
name: name, | |
description: 'Custom dev plugin', | |
supportUrl: 'http://wizehive.com', | |
publish: false, | |
}; | |
if (firebaseUrl && firebaseSecret) { | |
data.firebaseUrl = firebaseUrl; | |
data.firebaseSecret = firebaseSecret; | |
} | |
const serviceData = { | |
route: '/' + service, | |
offline: offline | |
}; | |
// Create plugin. | |
return requestify.post(baseUrl, data, options).then(function (response) { | |
const body = response.getBody(); | |
return { | |
id: body.data.id, | |
route: body.data.route, | |
namespace: body.data.namespace | |
}; | |
}).catch(function (err) { | |
console.log(err); | |
}).then(function (data) { | |
// Create backend service if we need one. | |
if (service) { | |
return requestify.post(baseUrl + data.id + '/services', serviceData, options).then(function (response) { | |
var body = response.getBody(); | |
data.serviceId = body.data.id; | |
return data; | |
}).catch(function (err) { | |
console.log(err); | |
}); | |
} | |
return data; | |
}).then(function (data) { | |
// Finally publish the plugin. | |
return requestify.put(baseUrl + data.id, { publish: true }, options).then(function () { | |
console.log(data); | |
}).catch(function (err) { | |
console.log(data); | |
console.log(err); | |
}); | |
}); | |
/** | |
* Transforms a string into a camelized slug. | |
* | |
* @param {string} text | |
* @return {string} | |
* | |
* Based on https://gist.github.com/eek/9c4887e80b3ede05c0e39fee4dce3747 | |
*/ | |
function slugify (text) { | |
var slug = text.toString().trim() | |
.normalize('NFD') // separate accent from letter | |
.replace(/[\u0300-\u036f]/g, '') // remove all separated accents | |
.replace(/(\-|\_)/g, '') // remove hipens and underscores | |
.replace(/\s+/g, '') // remove spaces | |
.replace(/&/g, '-and-') // replace & with 'and' | |
.replace(/[^\w\-]+/g, ''); // remove all non-word chars | |
return slug.charAt(0).toLowerCase() + slug.substr(1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "create-plugin", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"author": "Viking Octopus", | |
"private": true, | |
"dependencies": { | |
"requestify": "^0.2.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment