greenlock.js-ARCHIVED/lib/common.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-12-15 15:21:27 +00:00
'use strict';
var fs = require('fs');
2015-12-19 19:59:50 +00:00
var path = require('path');
2015-12-15 15:21:27 +00:00
var PromiseA = require('bluebird');
2015-12-19 19:59:50 +00:00
var homeRe = new RegExp("^~(\\/|\\\|\\" + path.sep + ")");
2015-12-19 10:18:32 +00:00
var re = /^[a-zA-Z0-9\.\-]+$/;
var punycode = require('punycode');
2015-12-19 19:59:50 +00:00
module.exports.isValidDomain = function (domain) {
2015-12-19 10:18:32 +00:00
if (re.test(domain)) {
return domain;
}
domain = punycode.toASCII(domain);
if (re.test(domain)) {
return domain;
}
return '';
};
2016-08-04 22:49:35 +00:00
module.exports.tplConfigDir = function (configDir, defaults) {
2015-12-19 19:59:50 +00:00
var homedir = require('homedir')();
2015-12-17 08:46:40 +00:00
Object.keys(defaults).forEach(function (key) {
if ('string' === typeof defaults[key]) {
defaults[key] = defaults[key].replace(':config', configDir).replace(':conf', configDir);
2015-12-19 19:59:50 +00:00
defaults[key] = defaults[key].replace(homeRe, homedir + path.sep);
2015-12-17 08:46:40 +00:00
}
});
};
2016-08-04 22:49:35 +00:00
module.exports.merge = function (/*defaults, args*/) {
var allDefaults = Array.prototype.slice.apply(arguments);
var args = args.shift();
2015-12-17 08:46:40 +00:00
var copy = {};
2016-08-04 22:49:35 +00:00
allDefaults.forEach(function (defaults) {
Object.keys(defaults).forEach(function (key) {
copy[key] = defaults[key];
});
2015-12-17 08:46:40 +00:00
});
2016-08-04 22:49:35 +00:00
2015-12-17 08:46:40 +00:00
Object.keys(args).forEach(function (key) {
copy[key] = args[key];
});
return copy;
};
2016-08-04 22:49:35 +00:00
module.exports.tplCopy = function (copy) {
var url = require('url');
var acmeLocation = url.parse(copy.server);
var acmeHostpath = path.join(acmeLocation.hostname, acmeLocation.pathname);
copy.accountsDir = copy.accountsDir || path.join(copy.configDir, 'accounts', acmeHostpath);
// TODO move these defaults elsewhere?
//args.renewalDir = args.renewalDir || ':config/renewal/';
args.renewalPath = args.renewalPath || ':config/renewal/:hostname.conf';
// Note: the /directory is part of the server url and, as such, bleeds into the pathname
// So :config/accounts/:server/directory is *incorrect*, but the following *is* correct:
args.accountsDir = args.accountsDir || ':config/accounts/:server';
hargs.renewalDir = hargs.renewalDir || ':config/renewal/';
copy.renewalPath = copy.renewalPath || path.join(copy.configDir, 'renewal', copy.domains[0] + '.conf');
2015-12-19 19:59:50 +00:00
var homedir = require('homedir')();
var tpls = {
hostname: (copy.domains || [])[0]
, server: (copy.server || '').replace('https://', '').replace(/(\/)$/, '')
, conf: copy.configDir
, config: copy.configDir
};
2015-12-17 08:46:40 +00:00
Object.keys(copy).forEach(function (key) {
if ('string' === typeof copy[key]) {
Object.keys(tpls).sort(function (a, b) {
return b.length - a.length;
}).forEach(function (tplname) {
2015-12-20 10:41:17 +00:00
if (!tpls[tplname]) {
// what can't be templated now may be templatable later
return;
}
copy[key] = copy[key].replace(':' + tplname, tpls[tplname]);
copy[key] = copy[key].replace(homeRe, homedir + path.sep);
});
2015-12-17 08:46:40 +00:00
}
});
//return copy;
};