greenlock.js-ARCHIVED/plugins.js

332 lines
8.0 KiB
JavaScript
Raw Permalink Normal View History

2019-10-20 08:51:19 +00:00
'use strict';
var P = module.exports;
var spawn = require('child_process').spawn;
var spawnSync = require('child_process').spawnSync;
var promisify = require('util').promisify;
// Exported for CLIs and such to override
P.PKG_DIR = __dirname;
2019-10-20 08:51:19 +00:00
2019-10-29 05:18:13 +00:00
P._loadStore = function(storeConf) {
2019-10-31 22:26:18 +00:00
return P._loadHelper(storeConf.module).then(function(plugin) {
return P._normalizeStore(storeConf.module, plugin.create(storeConf));
});
2019-10-29 05:18:13 +00:00
};
P._loadChallenge = function(chConfs, typ01) {
2019-10-31 22:26:18 +00:00
return P._loadHelper(chConfs[typ01].module).then(function(plugin) {
var ch = P._normalizeChallenge(
chConfs[typ01].module,
plugin.create(chConfs[typ01])
);
ch._type = typ01;
return ch;
});
2019-10-29 05:18:13 +00:00
};
P._loadHelper = function(modname) {
2019-10-31 22:26:18 +00:00
try {
return Promise.resolve(require(modname));
} catch (e) {
console.error("Could not load '%s'", modname);
console.error('Did you install it?');
console.error('\tnpm install --save %s', modname);
throw e;
2019-10-31 22:26:18 +00:00
// Fun experiment, bad idea
/*
2019-10-20 08:51:19 +00:00
return P._install(modname).then(function() {
return require(modname);
});
*/
2019-10-31 22:26:18 +00:00
}
2019-10-20 08:51:19 +00:00
};
2019-10-29 05:18:13 +00:00
P._normalizeStore = function(name, store) {
2019-10-31 22:26:18 +00:00
var acc = store.accounts;
var crt = store.certificates;
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
var warned = false;
function warn() {
if (warned) {
return;
}
warned = true;
console.warn(
"'" +
name +
"' may have incorrect function signatures, or contains deprecated use of callbacks"
);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
// accs
if (acc.check && 2 === acc.check.length) {
warn();
acc._thunk_check = acc.check;
acc.check = promisify(acc._thunk_check);
}
if (acc.set && 3 === acc.set.length) {
warn();
acc._thunk_set = acc.set;
acc.set = promisify(acc._thunk_set);
}
if (2 === acc.checkKeypair.length) {
warn();
acc._thunk_checkKeypair = acc.checkKeypair;
acc.checkKeypair = promisify(acc._thunk_checkKeypair);
}
if (3 === acc.setKeypair.length) {
warn();
acc._thunk_setKeypair = acc.setKeypair;
acc.setKeypair = promisify(acc._thunk_setKeypair);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
// certs
if (2 === crt.check.length) {
warn();
crt._thunk_check = crt.check;
crt.check = promisify(crt._thunk_check);
}
if (3 === crt.set.length) {
warn();
crt._thunk_set = crt.set;
crt.set = promisify(crt._thunk_set);
}
if (2 === crt.checkKeypair.length) {
warn();
crt._thunk_checkKeypair = crt.checkKeypair;
crt.checkKeypair = promisify(crt._thunk_checkKeypair);
}
if (2 === crt.setKeypair.length) {
warn();
crt._thunk_setKeypair = crt.setKeypair;
crt.setKeypair = promisify(crt._thunk_setKeypair);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
return store;
2019-10-29 05:18:13 +00:00
};
P._normalizeChallenge = function(name, ch) {
2019-10-31 22:26:18 +00:00
var gch = {};
var warned = false;
function warn() {
if (warned) {
return;
}
warned = true;
console.warn(
"'" +
name +
"' may have incorrect function signatures, or contains deprecated use of callbacks"
);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
var warned2 = false;
function warn2() {
if (warned2) {
return;
}
warned2 = true;
console.warn(
"'" +
name +
"' did not return a Promise when called. This should be fixed by the maintainer."
);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
function wrappy(fn) {
return function(_params) {
return Promise.resolve().then(function() {
var result = fn.call(ch, _params);
if (!result || !result.then) {
warn2();
}
return result;
});
};
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
// init, zones, set, get, remove
if (ch.init) {
if (2 === ch.init.length) {
warn();
ch._thunk_init = ch.init;
ch.init = promisify(ch._thunk_init);
}
gch.init = wrappy(ch.init);
}
if (ch.zones) {
if (2 === ch.zones.length) {
warn();
ch._thunk_zones = ch.zones;
ch.zones = promisify(ch._thunk_zones);
}
gch.zones = wrappy(ch.zones);
}
if (2 === ch.set.length) {
warn();
ch._thunk_set = ch.set;
ch.set = promisify(ch._thunk_set);
}
gch.set = wrappy(ch.set);
if (2 === ch.remove.length) {
warn();
ch._thunk_remove = ch.remove;
ch.remove = promisify(ch._thunk_remove);
}
gch.remove = wrappy(ch.remove);
if (ch.get) {
if (2 === ch.get.length) {
warn();
ch._thunk_get = ch.get;
ch.get = promisify(ch._thunk_get);
}
gch.get = wrappy(ch.get);
}
2019-10-29 05:18:13 +00:00
2019-10-31 22:26:18 +00:00
return gch;
2019-10-29 05:18:13 +00:00
};
2019-10-20 08:51:19 +00:00
P._loadSync = function(modname) {
2019-10-31 22:26:18 +00:00
try {
return require(modname);
} catch (e) {
console.error("Could not load '%s'", modname);
console.error('Did you install it?');
console.error('\tnpm install --save %s', modname);
throw e;
}
/*
2019-10-20 08:51:19 +00:00
try {
mod = require(modname);
} catch (e) {
P._installSync(modname);
mod = require(modname);
}
2019-10-31 12:20:49 +00:00
*/
2019-10-20 08:51:19 +00:00
};
P._installSync = function(moduleName) {
2019-10-31 22:26:18 +00:00
var npm = 'npm';
var args = ['install', '--save', moduleName];
var out = '';
var cmd;
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
try {
cmd = spawnSync(npm, args, {
cwd: P.PKG_DIR,
windowsHide: true
});
} catch (e) {
console.error(
"Failed to start: '" +
npm +
' ' +
args.join(' ') +
"' in '" +
P.PKG_DIR +
"'"
);
console.error(e.message);
process.exit(1);
}
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
if (!cmd.status) {
return;
}
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
out += cmd.stdout.toString('utf8');
out += cmd.stderr.toString('utf8');
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
if (out) {
console.error(out);
console.error();
console.error();
}
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
console.error(
"Failed to run: '" +
npm +
' ' +
args.join(' ') +
"' in '" +
P.PKG_DIR +
"'"
);
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
console.error(
'Try for yourself:\n\tcd ' + P.PKG_DIR + '\n\tnpm ' + args.join(' ')
);
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
process.exit(1);
2019-10-20 08:51:19 +00:00
};
P._install = function(moduleName) {
2019-10-31 22:26:18 +00:00
return new Promise(function(resolve) {
if (!moduleName) {
throw new Error('no module name given');
}
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
var npm = 'npm';
var args = ['install', '--save', moduleName];
var out = '';
var cmd = spawn(npm, args, {
cwd: P.PKG_DIR,
windowsHide: true
});
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
cmd.stdout.on('data', function(chunk) {
out += chunk.toString('utf8');
});
cmd.stdout.on('data', function(chunk) {
out += chunk.toString('utf8');
});
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
cmd.on('error', function(e) {
console.error(
"Failed to start: '" +
npm +
' ' +
args.join(' ') +
"' in '" +
P.PKG_DIR +
"'"
);
console.error(e.message);
process.exit(1);
});
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
cmd.on('exit', function(code) {
if (!code) {
resolve();
return;
}
2019-10-20 08:51:19 +00:00
2019-10-31 22:26:18 +00:00
if (out) {
console.error(out);
console.error();
console.error();
}
console.error(
"Failed to run: '" +
npm +
' ' +
args.join(' ') +
"' in '" +
P.PKG_DIR +
"'"
);
console.error(
'Try for yourself:\n\tcd ' +
P.PKG_DIR +
'\n\tnpm ' +
args.join(' ')
);
process.exit(1);
});
});
2019-10-20 08:51:19 +00:00
};
if (require.main === module) {
2019-10-31 22:26:18 +00:00
P._installSync(process.argv[2]);
2019-10-20 08:51:19 +00:00
}