Skip to content

Instantly share code, notes, and snippets.

@tomazy
Created April 25, 2017 06:09
Show Gist options
  • Save tomazy/bc69cdee7b2966867f7cd62c8cd47892 to your computer and use it in GitHub Desktop.
Save tomazy/bc69cdee7b2966867f7cd62c8cd47892 to your computer and use it in GitHub Desktop.
automate adding codeship env vars from .env file or json
function codeShipSetVars(vars) {
const $blocks = getBlocks();
const keys = Object.keys(vars);
function getBlocks() {
return $('.form_block.environment_variable');
}
function add() {
getBlocks().last().find('button.-good').click();
}
function next(done) {
const name = keys.shift();
const $inputs = getBlocks().last().find('input');
$inputs.first().val(name);
$inputs.last().val(vars[name]);
add();
return keys.length > 0;
}
function waitFor(predicate, cb) {
const int = setInterval(() => {
if (predicate()) {
clearInterval(int);
cb();
}
}, 100);
}
function loop() {
console.log('in loop')
const cnt = getBlocks().length;
if (next()) {
waitFor(function() {
console.log('count', getBlocks().length);
return getBlocks().length > cnt;
}, loop);
}
}
loop();
}
function varsFromDotEnvContent(dotEnvContent) {
const lines = dotEnvContent.split('\n');
return lines
.filter(l => !!l)
.reduce((vars, line) => {
const [_, name, value] = line.match(/(\w+)=(.+)/);
vars[name] = value[0] === '"' ? JSON.parse(value) : value;
return vars;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment