From 48b892c323da76e79e641b0567e50f79319c27f2 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 16 Nov 2019 16:50:12 -0700 Subject: [PATCH] wip: move to configDir --- greenlock-shim.js | 155 ++++------------------------------------------ init.js | 131 +++++++++++++++++++++++++++++++++++++++ rc.js | 39 ++++++++++++ 3 files changed, 182 insertions(+), 143 deletions(-) create mode 100644 init.js create mode 100644 rc.js diff --git a/greenlock-shim.js b/greenlock-shim.js index 3044708..77687b4 100644 --- a/greenlock-shim.js +++ b/greenlock-shim.js @@ -5,19 +5,10 @@ var fs = require("fs"); module.exports.create = function(opts) { var Greenlock = require("@root/greenlock"); + var Init = require("./init.js"); var greenlock = opts.greenlock; - var pkgText; - var pkgErr; - var msgErr; - //var emailErr; - var realPkg; - var userPkg; - var myPkg = {}; - // we want to be SUPER transparent that we're reading from package.json - // we don't want anything unexpected - var implicitConfig = []; - var rc; + /* if (!greenlock && opts.packageRoot) { try { greenlock = require(path.resolve(opts.packageRoot, "greenlock.js")); @@ -27,115 +18,19 @@ module.exports.create = function(opts) { } } } + */ if (!greenlock) { - if (opts.packageRoot) { - try { - pkgText = fs.readFileSync(path.resolve(opts.packageRoot, "package.json"), "utf8"); - } catch (e) { - pkgErr = e; - console.warn("`packageRoot` should be the root of the package (probably `__dirname`)"); - } - } - - if (pkgText) { - try { - realPkg = JSON.parse(pkgText); - } catch (e) { - pkgErr = e; - } - } - - userPkg = opts.package; - - if (realPkg || userPkg) { - userPkg = userPkg || {}; - realPkg = realPkg || {}; - - // build package agent - if (!opts.packageAgent) { - // name - myPkg.name = userPkg.name; - if (!myPkg.name) { - myPkg.name = realPkg.name; - implicitConfig.push("name"); - } - - // version - myPkg.version = userPkg.version; - if (!myPkg.version) { - myPkg.version = realPkg.version; - implicitConfig.push("version"); - } - if (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; - } - if (!myPkg.author) { - implicitConfig.push("author"); - myPkg.author = (realPkg.author && realPkg.author.email) || realPkg.author; - } - opts.maintainerEmail = myPkg.maintainerEmail; - } - - if (!opts.packageAgent) { - msgErr = "missing `packageAgent` and also failed to read `name` and/or `version` from `package.json`"; - if (pkgErr) { - 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); - } - - opts.packageAgent = addGreenlockAgent(opts); - - if (opts.packageRoot) { - try { - rc = JSON.parse(fs.readFileSync(path.resolve(opts.packageRoot, ".greenlockrc"))); - } catch (e) { - if ("ENOENT" !== e.code) { - throw e; - } - rc = {}; - } - - if (opts.configFile && opts.configFile !== rc.configFile) { - if (rc.configFile) { - console.info("changing `configFile` from '%s' to '%s'", rc.configFile, opts.configFile); - } - rc.configFile = opts.configFile; - - if (!rc.manager) { - rc.manager = "@greenlock/manager"; - } - - fs.writeFileSync(path.resolve(opts.packageRoot, ".greenlockrc"), JSON.stringify(rc)); - } - } - - if (!greenlock) { - greenlock = Greenlock.create(opts); - } - - try { - if (opts.notify) { - greenlock._defaults.notify = opts.notify; - } - } catch (e) { - console.error("Developer Error: notify not attached correctly"); + opts = Init._init(opts); + greenlock = Greenlock.create(opts); + } + + try { + if (opts.notify) { + greenlock._defaults.notify = opts.notify; } + } catch (e) { + console.error("Developer Error: notify not attached correctly"); } // re-export as top-level function to simplify rpc with workers @@ -164,29 +59,3 @@ module.exports.create = function(opts) { return greenlock; }; - -function addGreenlockAgent(opts) { - // Add greenlock as part of Agent, unless this is greenlock - var packageAgent = opts.packageAgent || ""; - if (!/greenlock(-express|-pro)?/i.test(packageAgent)) { - var pkg = require("./package.json"); - packageAgent += " Greenlock_Express/" + pkg.version; - } - - return packageAgent.trim(); -} - -// ex: "John Doe (https://john.doe)" -// ex: "John Doe " -// ex: "" -// ex: "john@example.com" -var looseEmailRe = /(^|[\s<])([^'" <>:;`]+@[^'" <>:;`]+\.[^'" <>:;`]+)/; -function parseMaintainer(maintainerEmail) { - try { - maintainerEmail = maintainerEmail.match(looseEmailRe)[2]; - } catch (e) { - maintainerEmail = null; - } - - return maintainerEmail; -} diff --git a/init.js b/init.js new file mode 100644 index 0000000..007ce1b --- /dev/null +++ b/init.js @@ -0,0 +1,131 @@ +"use strict"; + +var Init = module.exports; + +var fs = require("fs"); +var path = require("path"); + +Init.init = function(opts) { + //var Rc = require("@root/greenlock/rc"); + var Rc = require("./rc.js"); + var pkgText; + var pkgErr; + var msgErr; + //var emailErr; + var realPkg; + var userPkg; + var myPkg = {}; + // we want to be SUPER transparent that we're reading from package.json + // we don't want anything unexpected + var implicitConfig = []; + + if (opts.packageRoot) { + try { + pkgText = fs.readFileSync(path.resolve(opts.packageRoot, "package.json"), "utf8"); + } catch (e) { + pkgErr = e; + console.warn("`packageRoot` should be the root of the package (probably `__dirname`)"); + } + } + + if (pkgText) { + try { + realPkg = JSON.parse(pkgText); + } catch (e) { + pkgErr = e; + } + } + + userPkg = opts.package; + + if (realPkg || userPkg) { + userPkg = userPkg || {}; + realPkg = realPkg || {}; + + // build package agent + if (!opts.packageAgent) { + // name + myPkg.name = userPkg.name; + if (!myPkg.name) { + myPkg.name = realPkg.name; + implicitConfig.push("name"); + } + + // version + myPkg.version = userPkg.version; + if (!myPkg.version) { + myPkg.version = realPkg.version; + implicitConfig.push("version"); + } + if (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; + } + if (!myPkg.author) { + implicitConfig.push("author"); + myPkg.author = (realPkg.author && realPkg.author.email) || realPkg.author; + } + opts.maintainerEmail = myPkg.author; + } + + if (!opts.packageAgent) { + msgErr = "missing `packageAgent` and also failed to read `name` and/or `version` from `package.json`"; + if (pkgErr) { + 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); + } + + opts.packageAgent = addGreenlockAgent(opts); + + if (opts.packageRoot) { + // Place the rc file in the packageroot + opts.configDir = Rc._initSync(opts.packageRoot, opts.configDir); + } + + if (!opts.configDir) { + throw new Error("missing `packageRoot` and `configDir`"); + } + + // Place the rc file in the configDir itself + //Rc._initSync(opts.configDir, opts.configDir); +}; + +function addGreenlockAgent(opts) { + // Add greenlock as part of Agent, unless this is greenlock + var packageAgent = opts.packageAgent || ""; + if (!/greenlock(-express|-pro)?/i.test(packageAgent)) { + var pkg = require("./package.json"); + packageAgent += " Greenlock_Express/" + pkg.version; + } + + return packageAgent.trim(); +} + +// ex: "John Doe (https://john.doe)" +// ex: "John Doe " +// ex: "" +// ex: "john@example.com" +var looseEmailRe = /(^|[\s<])([^'" <>:;`]+@[^'" <>:;`]+\.[^'" <>:;`]+)/; +function parseMaintainer(maintainerEmail) { + try { + maintainerEmail = maintainerEmail.match(looseEmailRe)[2]; + } catch (e) { + maintainerEmail = null; + } + + return maintainerEmail; +} diff --git a/rc.js b/rc.js new file mode 100644 index 0000000..87fde7b --- /dev/null +++ b/rc.js @@ -0,0 +1,39 @@ +"use strict"; + +var Rc = module.exports; +var fs = require("fs"); +var path = require("path"); + +Rc._initSync = function(dirname, configDir) { + // dirname / opts.packageRoot + var rcpath = path.resolve(dirname, ".greenlockrc"); + var rc; + + try { + rc = JSON.parse(fs.readFileSync(rcpath)); + } catch (e) { + if ("ENOENT" !== e.code) { + throw e; + } + rc = {}; + } + + if (!configDir) { + configDir = rc.configDir; + } + + if (configDir && configDir !== rc.configDir) { + if (rc.configDir) { + console.info("changing `configDir` from '%s' to '%s'", rc.configDir, configDir); + } + rc.configDir = configDir; + /* if (!rc.manager) { rc.manager = "@greenlock/manager"; } */ + fs.writeFileSync(rcpath, JSON.stringify(rc)); + } else if (!rc.configDir) { + configDir = path.resolve(dirname, "greenlock.d"); + rc.configDir = configDir; + fs.writeFileSync(rcpath, JSON.stringify(rc)); + } + + return configDir; +};