greenlock.js/lib/init.js

193 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-11-19 05:55:29 +00:00
'use strict';
2019-11-18 08:21:31 +00:00
var Init = module.exports;
2019-11-19 05:55:29 +00:00
var fs = require('fs');
var path = require('path');
2019-11-18 08:21:31 +00:00
//var promisify = require("util").promisify;
Init._init = function(opts) {
//var Rc = require("@root/greenlock/rc");
2019-11-19 05:55:29 +00:00
var Rc = require('./rc.js');
2019-11-18 08:21:31 +00:00
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 (opts.packageRoot) {
try {
2019-11-19 05:55:29 +00:00
pkgText = fs.readFileSync(
path.resolve(opts.packageRoot, 'package.json'),
'utf8'
);
opts._hasPackage = true;
2019-11-18 08:21:31 +00:00
} catch (e) {
pkgErr = e;
2019-11-19 05:55:29 +00:00
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`)'
);
2019-11-18 08:21:31 +00:00
}
}
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;
2019-11-19 05:55:29 +00:00
implicitConfig.push('name');
2019-11-18 08:21:31 +00:00
}
// version
myPkg.version = userPkg.version;
if (!myPkg.version) {
myPkg.version = realPkg.version;
2019-11-19 05:55:29 +00:00
implicitConfig.push('version');
2019-11-18 08:21:31 +00:00
}
if (myPkg.name && myPkg.version) {
2019-11-19 05:55:29 +00:00
opts.packageAgent = myPkg.name + '/' + myPkg.version;
2019-11-18 08:21:31 +00:00
}
}
// build author
myPkg.author = opts.maintainerEmail;
if (!myPkg.author) {
2019-11-19 05:55:29 +00:00
myPkg.author =
(userPkg.author && userPkg.author.email) || userPkg.author;
2019-11-18 08:21:31 +00:00
}
if (!myPkg.author) {
2019-11-19 05:55:29 +00:00
implicitConfig.push('author');
myPkg.author =
(realPkg.author && realPkg.author.email) || realPkg.author;
}
2019-11-19 09:24:09 +00:00
if (!opts._init) {
2019-11-19 05:55:29 +00:00
opts.maintainerEmail = myPkg.author;
2019-11-18 08:21:31 +00:00
}
}
if (!opts.packageAgent) {
2019-11-19 05:55:29 +00:00
msgErr =
'missing `packageAgent` and also failed to read `name` and/or `version` from `package.json`';
2019-11-18 08:21:31 +00:00
if (pkgErr) {
2019-11-19 05:55:29 +00:00
msgErr += ': ' + pkgErr.message;
2019-11-18 08:21:31 +00:00
}
throw new Error(msgErr);
}
2019-11-19 09:24:09 +00:00
if (!opts._init) {
2019-11-19 05:55:29 +00:00
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);
}
2019-11-18 08:21:31 +00:00
}
if (opts.packageRoot) {
// Place the rc file in the packageroot
rc = Rc._initSync(opts.packageRoot, opts.manager, opts.configDir);
opts.configDir = rc.configDir;
opts.manager = rc.manager;
}
if (!opts.configDir && !opts.manager) {
2019-11-19 05:55:29 +00:00
throw new Error(
'missing `packageRoot` and `configDir`, but no `manager` was supplied'
);
2019-11-18 08:21:31 +00:00
}
//var mkdirp = promisify(require("@root/mkdirp"));
2019-11-19 08:39:05 +00:00
opts.configFile = path.join(opts.configDir, 'config.json');
2019-11-18 08:21:31 +00:00
var config;
try {
2019-11-19 08:39:05 +00:00
config = JSON.parse(fs.readFileSync(opts.configFile));
2019-11-18 08:21:31 +00:00
} catch (e) {
2019-11-19 05:55:29 +00:00
if ('ENOENT' !== e.code) {
2019-11-18 08:21:31 +00:00
throw e;
}
config = { defaults: {} };
}
2019-11-19 05:55:29 +00:00
opts.manager =
rc.manager ||
(config.defaults && config.defaults.manager) ||
config.manager;
2019-11-18 08:21:31 +00:00
if (!opts.manager) {
2019-11-19 05:55:29 +00:00
opts.manager = '@greenlock/manager';
2019-11-18 08:21:31 +00:00
}
2019-11-19 05:55:29 +00:00
if ('string' === typeof opts.manager) {
2019-11-18 08:21:31 +00:00
opts.manager = {
module: opts.manager
};
}
opts.manager = JSON.parse(JSON.stringify(opts.manager));
2019-11-19 05:55:29 +00:00
var confconf = ['configDir', 'configFile', 'staging', 'directoryUrl'];
2019-11-18 08:21:31 +00:00
Object.keys(opts).forEach(function(k) {
if (!confconf.includes(k)) {
return;
}
2019-11-19 05:55:29 +00:00
if ('undefined' !== typeof opts.manager[k]) {
2019-11-18 08:21:31 +00:00
return;
}
opts.manager[k] = opts[k];
});
/*
var ignore = ["packageRoot", "maintainerEmail", "packageAgent", "staging", "directoryUrl", "manager"];
Object.keys(opts).forEach(function(k) {
if (ignore.includes(k)) {
return;
}
opts.manager[k] = opts[k];
});
*/
// Place the rc file in the configDir itself
//Rc._initSync(opts.configDir, opts.configDir);
return opts;
};
// ex: "John Doe <john@example.com> (https://john.doe)"
// ex: "John Doe <john@example.com>"
// ex: "<john@example.com>"
// ex: "john@example.com"
var looseEmailRe = /(^|[\s<])([^'" <>:;`]+@[^'" <>:;`]+\.[^'" <>:;`]+)/;
function parseMaintainer(maintainerEmail) {
try {
maintainerEmail = maintainerEmail.match(looseEmailRe)[2];
} catch (e) {
maintainerEmail = null;
}
return maintainerEmail;
}