From 8e632aee63d8e6542daa57690cbc06704ac9f829 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 18 Nov 2019 22:55:29 -0700 Subject: [PATCH] wip: replace rc/init handling --- bin/add.js | 6 +-- bin/config.js | 6 +-- bin/defaults.js | 5 +-- bin/init.js | 49 ++++++++++++++--------- bin/lib/flags.js | 17 ++++---- bin/remove.js | 6 +-- bin/tmpl/cluster.tmpl.js | 30 ++++++++++++++ bin/tmpl/greenlock.tmpl.js | 2 +- bin/tmpl/server.tmpl.js | 28 +++++++------ bin/update.js | 6 +-- lib/init.js | 81 +++++++++++++++++++++++++------------- 11 files changed, 153 insertions(+), 83 deletions(-) create mode 100644 bin/tmpl/cluster.tmpl.js diff --git a/bin/add.js b/bin/add.js index f289abc..1155ff1 100644 --- a/bin/add.js +++ b/bin/add.js @@ -8,7 +8,7 @@ var cli = require('./lib/cli.js'); var Flags = require('./lib/flags.js'); -Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { +Flags.init().then(function({ flagOptions, greenlock, mconf }) { var myFlags = {}; [ 'subject', @@ -34,11 +34,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { cli.parse(myFlags); cli.main(function(argList, flags) { Flags.mangleFlags(flags, mconf); - main(argList, flags, rc, greenlock); + main(argList, flags, greenlock); }, args); }); -async function main(_, flags, rc, greenlock) { +async function main(_, flags, greenlock) { if (!flags.subject || !flags.altnames) { console.error( '--subject and --altnames must be provided and should be valid domains' diff --git a/bin/config.js b/bin/config.js index 2e73056..0df784f 100644 --- a/bin/config.js +++ b/bin/config.js @@ -8,7 +8,7 @@ var cli = require('./lib/cli.js'); var Flags = require('./lib/flags.js'); -Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { +Flags.init().then(function({ flagOptions, greenlock, mconf }) { var myFlags = {}; ['all', 'subject', 'servername' /*, 'servernames', 'altnames'*/].forEach( function(k) { @@ -19,11 +19,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { cli.parse(myFlags); cli.main(function(argList, flags) { Flags.mangleFlags(flags, mconf); - main(argList, flags, rc, greenlock); + main(argList, flags, greenlock); }, args); }); -async function main(_, flags, rc, greenlock) { +async function main(_, flags, greenlock) { var servernames = [flags.subject] .concat([flags.servername]) //.concat(flags.servernames) diff --git a/bin/defaults.js b/bin/defaults.js index dbe3a5f..8c445bd 100644 --- a/bin/defaults.js +++ b/bin/defaults.js @@ -10,7 +10,6 @@ var Flags = require('./lib/flags.js'); Flags.init({ forceSave: true }).then(function({ flagOptions, - rc, greenlock, mconf }) { @@ -38,11 +37,11 @@ Flags.init({ forceSave: true }).then(function({ cli.parse(myFlags); cli.main(function(argList, flags) { Flags.mangleFlags(flags, mconf, null, { forceSave: true }); - main(argList, flags, rc, greenlock); + main(argList, flags, greenlock); }, args); }); -async function main(_, flags, rc, greenlock) { +async function main(_, flags, greenlock) { greenlock.manager .defaults(flags) .catch(function(err) { diff --git a/bin/init.js b/bin/init.js index 454b6cb..7fcb4c6 100644 --- a/bin/init.js +++ b/bin/init.js @@ -3,9 +3,7 @@ var P = require('../plugins.js'); var args = process.argv.slice(3); var cli = require('./lib/cli.js'); -//var path = require('path'); -//var pkgpath = path.join(__dirname, '..', 'package.json'); -//var pkgpath = path.join(process.cwd(), 'package.json'); +var Init = require('../lib/init.js'); var Flags = require('./lib/flags.js'); @@ -17,10 +15,7 @@ var myFlags = {}; cli.parse(myFlags); cli.main(async function(argList, flags) { - var path = require('path'); - var pkgpath = path.join(process.cwd(), 'package.json'); - var pkgdir = path.dirname(pkgpath); - //var rcpath = path.join(pkgpath, '.greenlockrc'); + var pkgRoot = process.cwd(); var manager = flags.manager; if (['fs', 'cloud'].includes(manager)) { @@ -32,7 +27,7 @@ cli.main(async function(argList, flags) { flags.manager = flags.managerOpts; delete flags.managerOpts; - flags.manager.manager = manager; + flags.manager.module = manager; try { P._loadSync(manager); @@ -49,12 +44,16 @@ cli.main(async function(argList, flags) { } } - var GreenlockRc = require('../greenlockrc.js'); - //var rc = await GreenlockRc(pkgpath, manager, flags.manager); - await GreenlockRc(pkgpath, manager, flags.manager); - writeGreenlockJs(pkgdir, flags); - writeServerJs(pkgdir, flags); - writeAppJs(pkgdir); + var opts = Init._init({ + packageRoot: pkgRoot, + manager: flags.manager, + maintainerEmail: flags.maintainerEmail, + _mustPackage: true + }); + + //writeGreenlockJs(pkgdir, flags); + writeServerJs(opts.packageRoot, flags); + writeAppJs(opts.packageRoot); /* rc._bin_mode = true; @@ -66,6 +65,7 @@ cli.main(async function(argList, flags) { */ }, args); +/* function writeGreenlockJs(pkgdir, flags) { var greenlockJs = 'greenlock.js'; var fs = require('fs'); @@ -92,15 +92,13 @@ function writeGreenlockJs(pkgdir, flags) { fs.writeFileSync(path.join(pkgdir, greenlockJs), tmpl); console.info("created '%s'", greenlockJs); } +*/ function writeServerJs(pkgdir, flags) { var serverJs = 'server.js'; var fs = require('fs'); var path = require('path'); - var tmpl = fs.readFileSync( - path.join(__dirname, 'tmpl/server.tmpl.js'), - 'utf8' - ); + var tmpl; try { fs.accessSync(path.join(pkgdir, serverJs)); @@ -111,7 +109,22 @@ function writeServerJs(pkgdir, flags) { } if (flags.cluster) { + tmpl = fs.readFileSync( + path.join(__dirname, 'tmpl/cluster.tmpl.js'), + 'utf8' + ); 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); diff --git a/bin/lib/flags.js b/bin/lib/flags.js index b19ebcd..0c269c5 100644 --- a/bin/lib/flags.js +++ b/bin/lib/flags.js @@ -3,9 +3,8 @@ var Flags = module.exports; var path = require('path'); -//var pkgpath = path.join(__dirname, '..', 'package.json'); -var pkgpath = path.join(process.cwd(), 'package.json'); -var GreenlockRc = require('../../greenlockrc.js'); +var pkgRoot = process.cwd(); +var Init = require('../../lib/init.js'); // These are ALL options // The individual CLI files each select a subset of them @@ -174,17 +173,19 @@ Flags.flags = function(mconf, myOpts) { }; Flags.init = async function(myOpts) { - var rc = await GreenlockRc(pkgpath); - rc._bin_mode = true; var Greenlock = require('../../'); + // this is a copy, so it's safe to modify - rc.packageRoot = path.dirname(pkgpath); - var greenlock = Greenlock.create(rc); + var greenlock = Greenlock.create({ + packageRoot: pkgRoot, + _mustPackage: true, + _init: true, + _bin_mode: true + }); var mconf = await greenlock.manager.defaults(); var flagOptions = Flags.flags(mconf, myOpts); return { flagOptions, - rc, greenlock, mconf }; diff --git a/bin/remove.js b/bin/remove.js index b1f23cb..9f4c838 100644 --- a/bin/remove.js +++ b/bin/remove.js @@ -8,7 +8,7 @@ var cli = require('./lib/cli.js'); var Flags = require('./lib/flags.js'); -Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { +Flags.init().then(function({ flagOptions, greenlock, mconf }) { var myFlags = {}; ['subject'].forEach(function(k) { myFlags[k] = flagOptions[k]; @@ -17,11 +17,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { cli.parse(myFlags); cli.main(function(argList, flags) { Flags.mangleFlags(flags, mconf); - main(argList, flags, rc, greenlock); + main(argList, flags, greenlock); }, args); }); -async function main(_, flags, rc, greenlock) { +async function main(_, flags, greenlock) { if (!flags.subject) { console.error('--subject must be provided as a valid domain'); process.exit(1); diff --git a/bin/tmpl/cluster.tmpl.js b/bin/tmpl/cluster.tmpl.js new file mode 100644 index 0000000..4f2d027 --- /dev/null +++ b/bin/tmpl/cluster.tmpl.js @@ -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); + }); diff --git a/bin/tmpl/greenlock.tmpl.js b/bin/tmpl/greenlock.tmpl.js index 80d3937..787faf8 100644 --- a/bin/tmpl/greenlock.tmpl.js +++ b/bin/tmpl/greenlock.tmpl.js @@ -6,7 +6,7 @@ module.exports = require('@root/greenlock').create({ packageAgent: pkg.name + '/' + pkg.version, // contact for security and critical bug notices - maintainerEmail: pkg.author, + //maintainerEmail: pkg.author, // where to find .greenlockrc and set default paths packageRoot: __dirname diff --git a/bin/tmpl/server.tmpl.js b/bin/tmpl/server.tmpl.js index 3a04849..e120af9 100644 --- a/bin/tmpl/server.tmpl.js +++ b/bin/tmpl/server.tmpl.js @@ -1,18 +1,20 @@ 'use strict'; +var app = require('./app.js'); + require('greenlock-express') - .init(function() { - return { - packageRoot: __dirname, + .init({ + packageRoot: __dirname, - // whether or not to run at cloudscale - cluster: false - }; + // 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: false }) - .ready(function(glx) { - var app = require('./app.js'); - - // Serves on 80 and 443 - // Get's SSL certificates magically! - glx.serveApp(app); - }); + // Serves on 80 and 443 + // Get's SSL certificates magically! + .serve(app); diff --git a/bin/update.js b/bin/update.js index b8fdd7c..7fca94c 100644 --- a/bin/update.js +++ b/bin/update.js @@ -4,7 +4,7 @@ var args = process.argv.slice(3); var cli = require('./lib/cli.js'); var Flags = require('./lib/flags.js'); -Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { +Flags.init().then(function({ flagOptions, greenlock, mconf }) { var myFlags = {}; [ 'subject', @@ -31,11 +31,11 @@ Flags.init().then(function({ flagOptions, rc, greenlock, mconf }) { cli.main(async function(argList, flags) { var sconf = await greenlock._config({ servername: flags.subject }); Flags.mangleFlags(flags, mconf, sconf); - main(argList, flags, rc, greenlock); + main(argList, flags, greenlock); }, args); }); -async function main(_, flags, rc, greenlock) { +async function main(_, flags, greenlock) { if (!flags.subject) { console.error('--subject must be provided as a valid domain'); process.exit(1); diff --git a/lib/init.js b/lib/init.js index dbdd963..2a015c6 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,14 +1,14 @@ -"use strict"; +'use strict'; var Init = module.exports; -var fs = require("fs"); -var path = require("path"); +var fs = require('fs'); +var path = require('path'); //var promisify = require("util").promisify; Init._init = function(opts) { //var Rc = require("@root/greenlock/rc"); - var Rc = require("./rc.js"); + var Rc = require('./rc.js'); var pkgText; var pkgErr; var msgErr; @@ -23,10 +23,23 @@ Init._init = function(opts) { if (opts.packageRoot) { 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) { 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,45 +63,52 @@ Init._init = function(opts) { myPkg.name = userPkg.name; if (!myPkg.name) { myPkg.name = realPkg.name; - implicitConfig.push("name"); + implicitConfig.push('name'); } // version myPkg.version = userPkg.version; if (!myPkg.version) { myPkg.version = realPkg.version; - implicitConfig.push("version"); + implicitConfig.push('version'); } if (myPkg.name && myPkg.version) { - opts.packageAgent = myPkg.name + "/" + myPkg.version; + opts.packageAgent = myPkg.name + '/' + myPkg.version; } } // build author myPkg.author = opts.maintainerEmail; if (!myPkg.author) { - myPkg.author = (userPkg.author && userPkg.author.email) || userPkg.author; + myPkg.author = + (userPkg.author && userPkg.author.email) || userPkg.author; } if (!myPkg.author) { - implicitConfig.push("author"); - myPkg.author = (realPkg.author && realPkg.author.email) || realPkg.author; + implicitConfig.push('author'); + myPkg.author = + (realPkg.author && realPkg.author.email) || realPkg.author; + } + if (opts._init) { + opts.maintainerEmail = myPkg.author; } - opts.maintainerEmail = myPkg.author; } 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) { - msgErr += ": " + pkgErr.message; + msgErr += ': ' + pkgErr.message; } throw new Error(msgErr); } - opts.maintainerEmail = parseMaintainer(opts.maintainerEmail); - if (!opts.maintainerEmail) { - msgErr = - "missing or malformed `maintainerEmail` (or `author` from `package.json`), which is used as the contact for support notices"; - throw new Error(msgErr); + if (opts._init) { + opts.maintainerEmail = parseMaintainer(opts.maintainerEmail); + if (!opts.maintainerEmail) { + msgErr = + 'missing or malformed `maintainerEmail` (or `author` from `package.json`), which is used as the contact for support notices'; + throw new Error(msgErr); + } } if (opts.packageRoot) { @@ -99,38 +119,43 @@ Init._init = function(opts) { } 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 configFile = path.join(opts.configDir, "config.json"); + var configFile = path.join(opts.configDir, 'config.json'); var config; try { config = JSON.parse(fs.readFileSync(configFile)); } catch (e) { - if ("ENOENT" !== e.code) { + if ('ENOENT' !== e.code) { throw e; } 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) { - opts.manager = "@greenlock/manager"; + opts.manager = '@greenlock/manager'; } - if ("string" === typeof opts.manager) { + if ('string' === typeof opts.manager) { opts.manager = { module: 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) { if (!confconf.includes(k)) { return; } - if ("undefined" !== typeof opts.manager[k]) { + if ('undefined' !== typeof opts.manager[k]) { return; } opts.manager[k] = opts[k];