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');
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'

View File

@ -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)

View File

@ -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) {

View File

@ -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);

View File

@ -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
};

View File

@ -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);

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,
// contact for security and critical bug notices
maintainerEmail: pkg.author,
//maintainerEmail: pkg.author,
// where to find .greenlockrc and set default paths
packageRoot: __dirname

View File

@ -1,18 +1,20 @@
'use strict';
var app = require('./app.js');
require('greenlock-express')
.init(function() {
return {
.init({
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
cluster: false
};
})
.ready(function(glx) {
var app = require('./app.js');
// Serves on 80 and 443
// 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 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);

View File

@ -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,46 +63,53 @@ 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;
}
}
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);
}
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";
'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) {
// Place the rc file in the 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];