wip: replace rc/init handling

This commit is contained in:
AJ ONeal 2019-11-18 22:55:29 -07:00
parent b8d30b2b91
commit 8e632aee63
11 changed files with 153 additions and 83 deletions

View File

@ -8,7 +8,7 @@ var cli = require('./lib/cli.js');
var Flags = require('./lib/flags.js'); var Flags = require('./lib/flags.js');
Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { Flags.init().then(function({ flagOptions, greenlock, mconf }) {
var myFlags = {}; var myFlags = {};
[ [
'subject', 'subject',
@ -34,11 +34,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) {
cli.parse(myFlags); cli.parse(myFlags);
cli.main(function(argList, flags) { cli.main(function(argList, flags) {
Flags.mangleFlags(flags, mconf); Flags.mangleFlags(flags, mconf);
main(argList, flags, rc, greenlock); main(argList, flags, greenlock);
}, args); }, args);
}); });
async function main(_, flags, rc, greenlock) { async function main(_, flags, greenlock) {
if (!flags.subject || !flags.altnames) { if (!flags.subject || !flags.altnames) {
console.error( console.error(
'--subject and --altnames must be provided and should be valid domains' '--subject and --altnames must be provided and should be valid domains'

View File

@ -8,7 +8,7 @@ var cli = require('./lib/cli.js');
var Flags = require('./lib/flags.js'); var Flags = require('./lib/flags.js');
Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { Flags.init().then(function({ flagOptions, greenlock, mconf }) {
var myFlags = {}; var myFlags = {};
['all', 'subject', 'servername' /*, 'servernames', 'altnames'*/].forEach( ['all', 'subject', 'servername' /*, 'servernames', 'altnames'*/].forEach(
function(k) { function(k) {
@ -19,11 +19,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) {
cli.parse(myFlags); cli.parse(myFlags);
cli.main(function(argList, flags) { cli.main(function(argList, flags) {
Flags.mangleFlags(flags, mconf); Flags.mangleFlags(flags, mconf);
main(argList, flags, rc, greenlock); main(argList, flags, greenlock);
}, args); }, args);
}); });
async function main(_, flags, rc, greenlock) { async function main(_, flags, greenlock) {
var servernames = [flags.subject] var servernames = [flags.subject]
.concat([flags.servername]) .concat([flags.servername])
//.concat(flags.servernames) //.concat(flags.servernames)

View File

@ -10,7 +10,6 @@ var Flags = require('./lib/flags.js');
Flags.init({ forceSave: true }).then(function({ Flags.init({ forceSave: true }).then(function({
flagOptions, flagOptions,
rc,
greenlock, greenlock,
mconf mconf
}) { }) {
@ -38,11 +37,11 @@ Flags.init({ forceSave: true }).then(function({
cli.parse(myFlags); cli.parse(myFlags);
cli.main(function(argList, flags) { cli.main(function(argList, flags) {
Flags.mangleFlags(flags, mconf, null, { forceSave: true }); Flags.mangleFlags(flags, mconf, null, { forceSave: true });
main(argList, flags, rc, greenlock); main(argList, flags, greenlock);
}, args); }, args);
}); });
async function main(_, flags, rc, greenlock) { async function main(_, flags, greenlock) {
greenlock.manager greenlock.manager
.defaults(flags) .defaults(flags)
.catch(function(err) { .catch(function(err) {

View File

@ -3,9 +3,7 @@
var P = require('../plugins.js'); var P = require('../plugins.js');
var args = process.argv.slice(3); var args = process.argv.slice(3);
var cli = require('./lib/cli.js'); var cli = require('./lib/cli.js');
//var path = require('path'); var Init = require('../lib/init.js');
//var pkgpath = path.join(__dirname, '..', 'package.json');
//var pkgpath = path.join(process.cwd(), 'package.json');
var Flags = require('./lib/flags.js'); var Flags = require('./lib/flags.js');
@ -17,10 +15,7 @@ var myFlags = {};
cli.parse(myFlags); cli.parse(myFlags);
cli.main(async function(argList, flags) { cli.main(async function(argList, flags) {
var path = require('path'); var pkgRoot = process.cwd();
var pkgpath = path.join(process.cwd(), 'package.json');
var pkgdir = path.dirname(pkgpath);
//var rcpath = path.join(pkgpath, '.greenlockrc');
var manager = flags.manager; var manager = flags.manager;
if (['fs', 'cloud'].includes(manager)) { if (['fs', 'cloud'].includes(manager)) {
@ -32,7 +27,7 @@ cli.main(async function(argList, flags) {
flags.manager = flags.managerOpts; flags.manager = flags.managerOpts;
delete flags.managerOpts; delete flags.managerOpts;
flags.manager.manager = manager; flags.manager.module = manager;
try { try {
P._loadSync(manager); P._loadSync(manager);
@ -49,12 +44,16 @@ cli.main(async function(argList, flags) {
} }
} }
var GreenlockRc = require('../greenlockrc.js'); var opts = Init._init({
//var rc = await GreenlockRc(pkgpath, manager, flags.manager); packageRoot: pkgRoot,
await GreenlockRc(pkgpath, manager, flags.manager); manager: flags.manager,
writeGreenlockJs(pkgdir, flags); maintainerEmail: flags.maintainerEmail,
writeServerJs(pkgdir, flags); _mustPackage: true
writeAppJs(pkgdir); });
//writeGreenlockJs(pkgdir, flags);
writeServerJs(opts.packageRoot, flags);
writeAppJs(opts.packageRoot);
/* /*
rc._bin_mode = true; rc._bin_mode = true;
@ -66,6 +65,7 @@ cli.main(async function(argList, flags) {
*/ */
}, args); }, args);
/*
function writeGreenlockJs(pkgdir, flags) { function writeGreenlockJs(pkgdir, flags) {
var greenlockJs = 'greenlock.js'; var greenlockJs = 'greenlock.js';
var fs = require('fs'); var fs = require('fs');
@ -92,15 +92,13 @@ function writeGreenlockJs(pkgdir, flags) {
fs.writeFileSync(path.join(pkgdir, greenlockJs), tmpl); fs.writeFileSync(path.join(pkgdir, greenlockJs), tmpl);
console.info("created '%s'", greenlockJs); console.info("created '%s'", greenlockJs);
} }
*/
function writeServerJs(pkgdir, flags) { function writeServerJs(pkgdir, flags) {
var serverJs = 'server.js'; var serverJs = 'server.js';
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var tmpl = fs.readFileSync( var tmpl;
path.join(__dirname, 'tmpl/server.tmpl.js'),
'utf8'
);
try { try {
fs.accessSync(path.join(pkgdir, serverJs)); fs.accessSync(path.join(pkgdir, serverJs));
@ -111,7 +109,22 @@ function writeServerJs(pkgdir, flags) {
} }
if (flags.cluster) { if (flags.cluster) {
tmpl = fs.readFileSync(
path.join(__dirname, 'tmpl/cluster.tmpl.js'),
'utf8'
);
tmpl = tmpl.replace(/cluster: false/g, 'cluster: true'); tmpl = tmpl.replace(/cluster: false/g, 'cluster: true');
} else {
tmpl = fs.readFileSync(
path.join(__dirname, 'tmpl/server.tmpl.js'),
'utf8'
);
}
if (flags.maintainerEmail) {
tmpl = tmpl
.replace(/pkg.author/g, JSON.stringify(flags.maintainerEmail))
.replace(/\/\/maintainerEmail/g, 'maintainerEmail');
} }
fs.writeFileSync(path.join(pkgdir, serverJs), tmpl); fs.writeFileSync(path.join(pkgdir, serverJs), tmpl);

View File

@ -3,9 +3,8 @@
var Flags = module.exports; var Flags = module.exports;
var path = require('path'); var path = require('path');
//var pkgpath = path.join(__dirname, '..', 'package.json'); var pkgRoot = process.cwd();
var pkgpath = path.join(process.cwd(), 'package.json'); var Init = require('../../lib/init.js');
var GreenlockRc = require('../../greenlockrc.js');
// These are ALL options // These are ALL options
// The individual CLI files each select a subset of them // The individual CLI files each select a subset of them
@ -174,17 +173,19 @@ Flags.flags = function(mconf, myOpts) {
}; };
Flags.init = async function(myOpts) { Flags.init = async function(myOpts) {
var rc = await GreenlockRc(pkgpath);
rc._bin_mode = true;
var Greenlock = require('../../'); var Greenlock = require('../../');
// this is a copy, so it's safe to modify // this is a copy, so it's safe to modify
rc.packageRoot = path.dirname(pkgpath); var greenlock = Greenlock.create({
var greenlock = Greenlock.create(rc); packageRoot: pkgRoot,
_mustPackage: true,
_init: true,
_bin_mode: true
});
var mconf = await greenlock.manager.defaults(); var mconf = await greenlock.manager.defaults();
var flagOptions = Flags.flags(mconf, myOpts); var flagOptions = Flags.flags(mconf, myOpts);
return { return {
flagOptions, flagOptions,
rc,
greenlock, greenlock,
mconf mconf
}; };

View File

@ -8,7 +8,7 @@ var cli = require('./lib/cli.js');
var Flags = require('./lib/flags.js'); var Flags = require('./lib/flags.js');
Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { Flags.init().then(function({ flagOptions, greenlock, mconf }) {
var myFlags = {}; var myFlags = {};
['subject'].forEach(function(k) { ['subject'].forEach(function(k) {
myFlags[k] = flagOptions[k]; myFlags[k] = flagOptions[k];
@ -17,11 +17,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) {
cli.parse(myFlags); cli.parse(myFlags);
cli.main(function(argList, flags) { cli.main(function(argList, flags) {
Flags.mangleFlags(flags, mconf); Flags.mangleFlags(flags, mconf);
main(argList, flags, rc, greenlock); main(argList, flags, greenlock);
}, args); }, args);
}); });
async function main(_, flags, rc, greenlock) { async function main(_, flags, greenlock) {
if (!flags.subject) { if (!flags.subject) {
console.error('--subject must be provided as a valid domain'); console.error('--subject must be provided as a valid domain');
process.exit(1); process.exit(1);

30
bin/tmpl/cluster.tmpl.js Normal file
View File

@ -0,0 +1,30 @@
'use strict';
require('greenlock-express')
.init(function() {
// var pkg = require('./package.json');
return {
// where to find .greenlockrc and set default paths
packageRoot: __dirname,
// name & version for ACME client user agent
//packageAgent: pkg.name + '/' + pkg.version,
// contact for security and critical bug notices
//maintainerEmail: pkg.author,
// contact for security and critical bug notices
configDir: './greenlock.d',
// whether or not to run at cloudscale
cluster: true
};
})
.ready(function(glx) {
var app = require('./app.js');
// Serves on 80 and 443
// Get's SSL certificates magically!
glx.serveApp(app);
});

View File

@ -6,7 +6,7 @@ module.exports = require('@root/greenlock').create({
packageAgent: pkg.name + '/' + pkg.version, packageAgent: pkg.name + '/' + pkg.version,
// contact for security and critical bug notices // contact for security and critical bug notices
maintainerEmail: pkg.author, //maintainerEmail: pkg.author,
// where to find .greenlockrc and set default paths // where to find .greenlockrc and set default paths
packageRoot: __dirname packageRoot: __dirname

View File

@ -1,18 +1,20 @@
'use strict'; 'use strict';
var app = require('./app.js');
require('greenlock-express') require('greenlock-express')
.init(function() { .init({
return {
packageRoot: __dirname, packageRoot: __dirname,
// contact for security and critical bug notices
//maintainerEmail: pkg.author,
// contact for security and critical bug notices
configDir: './greenlock.d',
// whether or not to run at cloudscale // whether or not to run at cloudscale
cluster: false cluster: false
};
}) })
.ready(function(glx) {
var app = require('./app.js');
// Serves on 80 and 443 // Serves on 80 and 443
// Get's SSL certificates magically! // Get's SSL certificates magically!
glx.serveApp(app); .serve(app);
});

View File

@ -4,7 +4,7 @@ var args = process.argv.slice(3);
var cli = require('./lib/cli.js'); var cli = require('./lib/cli.js');
var Flags = require('./lib/flags.js'); var Flags = require('./lib/flags.js');
Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { Flags.init().then(function({ flagOptions, greenlock, mconf }) {
var myFlags = {}; var myFlags = {};
[ [
'subject', 'subject',
@ -31,11 +31,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) {
cli.main(async function(argList, flags) { cli.main(async function(argList, flags) {
var sconf = await greenlock._config({ servername: flags.subject }); var sconf = await greenlock._config({ servername: flags.subject });
Flags.mangleFlags(flags, mconf, sconf); Flags.mangleFlags(flags, mconf, sconf);
main(argList, flags, rc, greenlock); main(argList, flags, greenlock);
}, args); }, args);
}); });
async function main(_, flags, rc, greenlock) { async function main(_, flags, greenlock) {
if (!flags.subject) { if (!flags.subject) {
console.error('--subject must be provided as a valid domain'); console.error('--subject must be provided as a valid domain');
process.exit(1); process.exit(1);

View File

@ -1,14 +1,14 @@
"use strict"; 'use strict';
var Init = module.exports; var Init = module.exports;
var fs = require("fs"); var fs = require('fs');
var path = require("path"); var path = require('path');
//var promisify = require("util").promisify; //var promisify = require("util").promisify;
Init._init = function(opts) { Init._init = function(opts) {
//var Rc = require("@root/greenlock/rc"); //var Rc = require("@root/greenlock/rc");
var Rc = require("./rc.js"); var Rc = require('./rc.js');
var pkgText; var pkgText;
var pkgErr; var pkgErr;
var msgErr; var msgErr;
@ -23,10 +23,23 @@ Init._init = function(opts) {
if (opts.packageRoot) { if (opts.packageRoot) {
try { try {
pkgText = fs.readFileSync(path.resolve(opts.packageRoot, "package.json"), "utf8"); pkgText = fs.readFileSync(
path.resolve(opts.packageRoot, 'package.json'),
'utf8'
);
opts._hasPackage = true;
} catch (e) { } catch (e) {
pkgErr = e; pkgErr = e;
console.warn("`packageRoot` should be the root of the package (probably `__dirname`)"); if (opts._mustPackage) {
console.error(
'Should be run from package root (the same directory as `package.json`)'
);
process.exit(1);
return;
}
console.warn(
'`packageRoot` should be the root of the package (probably `__dirname`)'
);
} }
} }
@ -50,46 +63,53 @@ Init._init = function(opts) {
myPkg.name = userPkg.name; myPkg.name = userPkg.name;
if (!myPkg.name) { if (!myPkg.name) {
myPkg.name = realPkg.name; myPkg.name = realPkg.name;
implicitConfig.push("name"); implicitConfig.push('name');
} }
// version // version
myPkg.version = userPkg.version; myPkg.version = userPkg.version;
if (!myPkg.version) { if (!myPkg.version) {
myPkg.version = realPkg.version; myPkg.version = realPkg.version;
implicitConfig.push("version"); implicitConfig.push('version');
} }
if (myPkg.name && myPkg.version) { if (myPkg.name && myPkg.version) {
opts.packageAgent = myPkg.name + "/" + myPkg.version; opts.packageAgent = myPkg.name + '/' + myPkg.version;
} }
} }
// build author // build author
myPkg.author = opts.maintainerEmail; myPkg.author = opts.maintainerEmail;
if (!myPkg.author) { if (!myPkg.author) {
myPkg.author = (userPkg.author && userPkg.author.email) || userPkg.author; myPkg.author =
(userPkg.author && userPkg.author.email) || userPkg.author;
} }
if (!myPkg.author) { if (!myPkg.author) {
implicitConfig.push("author"); implicitConfig.push('author');
myPkg.author = (realPkg.author && realPkg.author.email) || realPkg.author; myPkg.author =
(realPkg.author && realPkg.author.email) || realPkg.author;
} }
if (opts._init) {
opts.maintainerEmail = myPkg.author; opts.maintainerEmail = myPkg.author;
} }
}
if (!opts.packageAgent) { if (!opts.packageAgent) {
msgErr = "missing `packageAgent` and also failed to read `name` and/or `version` from `package.json`"; msgErr =
'missing `packageAgent` and also failed to read `name` and/or `version` from `package.json`';
if (pkgErr) { if (pkgErr) {
msgErr += ": " + pkgErr.message; msgErr += ': ' + pkgErr.message;
} }
throw new Error(msgErr); throw new Error(msgErr);
} }
if (opts._init) {
opts.maintainerEmail = parseMaintainer(opts.maintainerEmail); opts.maintainerEmail = parseMaintainer(opts.maintainerEmail);
if (!opts.maintainerEmail) { if (!opts.maintainerEmail) {
msgErr = msgErr =
"missing or malformed `maintainerEmail` (or `author` from `package.json`), which is used as the contact for support notices"; 'missing or malformed `maintainerEmail` (or `author` from `package.json`), which is used as the contact for support notices';
throw new Error(msgErr); throw new Error(msgErr);
} }
}
if (opts.packageRoot) { if (opts.packageRoot) {
// Place the rc file in the packageroot // Place the rc file in the packageroot
@ -99,38 +119,43 @@ Init._init = function(opts) {
} }
if (!opts.configDir && !opts.manager) { if (!opts.configDir && !opts.manager) {
throw new Error("missing `packageRoot` and `configDir`, but no `manager` was supplied"); throw new Error(
'missing `packageRoot` and `configDir`, but no `manager` was supplied'
);
} }
//var mkdirp = promisify(require("@root/mkdirp")); //var mkdirp = promisify(require("@root/mkdirp"));
var configFile = path.join(opts.configDir, "config.json"); var configFile = path.join(opts.configDir, 'config.json');
var config; var config;
try { try {
config = JSON.parse(fs.readFileSync(configFile)); config = JSON.parse(fs.readFileSync(configFile));
} catch (e) { } catch (e) {
if ("ENOENT" !== e.code) { if ('ENOENT' !== e.code) {
throw e; throw e;
} }
config = { defaults: {} }; config = { defaults: {} };
} }
opts.manager = rc.manager || (config.defaults && config.defaults.manager) || config.manager; opts.manager =
rc.manager ||
(config.defaults && config.defaults.manager) ||
config.manager;
if (!opts.manager) { if (!opts.manager) {
opts.manager = "@greenlock/manager"; opts.manager = '@greenlock/manager';
} }
if ("string" === typeof opts.manager) { if ('string' === typeof opts.manager) {
opts.manager = { opts.manager = {
module: opts.manager module: opts.manager
}; };
} }
opts.manager = JSON.parse(JSON.stringify(opts.manager)); opts.manager = JSON.parse(JSON.stringify(opts.manager));
var confconf = ["configDir", "configFile", "staging", "directoryUrl"]; var confconf = ['configDir', 'configFile', 'staging', 'directoryUrl'];
Object.keys(opts).forEach(function(k) { Object.keys(opts).forEach(function(k) {
if (!confconf.includes(k)) { if (!confconf.includes(k)) {
return; return;
} }
if ("undefined" !== typeof opts.manager[k]) { if ('undefined' !== typeof opts.manager[k]) {
return; return;
} }
opts.manager[k] = opts[k]; opts.manager[k] = opts[k];