greenlock.js-ARCHIVED/lib/rc.js

78 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2019-11-18 08:21:31 +00:00
'use strict';
var Rc = module.exports;
var fs = require('fs');
var path = require('path');
// This is only called if packageRoot is specified
// (which it should be most of the time)
Rc._initSync = function(dirname, manager, configDir) {
if (!dirname) {
return {};
}
// 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 = {};
}
2019-11-19 08:39:05 +00:00
var changed = true;
2019-11-18 08:21:31 +00:00
// In the general case the manager should be specified in the
// config file, which is in the config dir, but for the specific
// case in which all custom plugins are being used and no config
// dir is needed, we allow the manager to be read from the rc.
// ex: manager: { module: 'name', xxxx: 'xxxx' }
if (manager) {
if (rc.manager) {
if (
2019-11-19 09:24:09 +00:00
('string' === typeof rc.manager && rc.manager !== manager) ||
('string' !== typeof rc.manager &&
rc.manager.module !== manager.module)
2019-11-18 08:21:31 +00:00
) {
2019-11-19 08:39:05 +00:00
changed = true;
2019-11-18 08:21:31 +00:00
console.info(
"changing `manager` from '%s' to '%s'",
2019-11-19 08:39:05 +00:00
rc.manager.module || rc.manager,
manager.module || manager
2019-11-18 08:21:31 +00:00
);
}
}
rc.manager = manager;
}
if (!configDir) {
configDir = rc.configDir;
}
if (configDir && configDir !== rc.configDir) {
if (rc.configDir) {
console.info(
"changing `configDir` from '%s' to '%s'",
rc.configDir,
configDir
);
}
2019-11-19 08:39:05 +00:00
changed = true;
2019-11-18 08:21:31 +00:00
rc.configDir = configDir;
} else if (!rc.configDir) {
2019-11-19 08:39:05 +00:00
changed = true;
configDir = './greenlock.d';
2019-11-18 08:21:31 +00:00
rc.configDir = configDir;
2019-11-19 08:39:05 +00:00
}
if (changed) {
2019-11-18 08:21:31 +00:00
fs.writeFileSync(rcpath, JSON.stringify(rc));
}
return rc;
};