@hotglue/cli
Version:
hotglue CLI tools
105 lines (83 loc) • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.writeConfigFile = exports.staticOptions = exports.getProjectConfig = exports.getProfileConfigFilePath = exports.getProfileConfig = exports.getCombinedConfig = void 0;
var _os = require("os");
var _promises = require("fs/promises");
var _path = _interopRequireDefault(require("path"));
var _cosmiconfig = require("cosmiconfig");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
Precedence of CLI configuration (from highest to lowest):
per cli execution: pass config file (--config /path/to/.hotgluerc) option with your command
per project: load from current project's package.json or a config folder in any folder in the path (i.e. /path/to/project/.hotgluerc)
per user: ~/.hotglue/config.yaml
any built-in or default config settings
*/
const staticOptions = {
hg: {
appName: 'hotglue',
clientApiBaseUri: process.env.HOTGLUE_CLIENT_API_BASE_URI || 'https://client-api.hotglue.xyz',
defaultConfigFileName: 'config.yaml'
}
};
exports.staticOptions = staticOptions;
const defaultOptions = {// absolute path or a relative path using dot notation only
// downloadFolder: path.resolve(process.cwd(), 'downloads'),
};
const getProfileConfigFilePath = () => _path.default.resolve((0, _os.homedir)(), `.${staticOptions.hg.appName}`, staticOptions.hg.defaultConfigFileName);
exports.getProfileConfigFilePath = getProfileConfigFilePath;
const writeConfigFile = async (file, data) => {
await (0, _promises.mkdir)(_path.default.dirname(file), {
recursive: true
});
await (0, _promises.writeFile)(file, data);
};
exports.writeConfigFile = writeConfigFile;
const getProfileConfig = async () => {
const {
appName
} = staticOptions.hg;
try {
const result = await (0, _cosmiconfig.cosmiconfig)(appName).load(getProfileConfigFilePath());
return result || {}; // return { ...result.config };
} catch (err) {
// no profile config found; app not configured
return {};
}
};
exports.getProfileConfig = getProfileConfig;
const getProjectConfig = async () => {
const {
appName
} = staticOptions.hg;
const cosmic = (0, _cosmiconfig.cosmiconfig)(appName, {
searchPlaces: [// primary
`${appName}.yaml`, `.${appName}.yaml`, `.${appName}rc.yaml`, `.${appName}rc.yml`, // secondary
`.${appName}rc`, `.${appName}rc.json`, `.${appName}rc.js` // `package.json`,
] // loaders: [],
// packageProp: `${appName}`,
// stopDir: '',
// cache: true,
// transform: (res) => res
});
try {
const result = await cosmic.search();
return result || {}; // return (result && result.config) || {};
} catch (err) {
console.log(err); // use debug()
throw new Error(`Malformed configuration file: ${err.message}`);
}
};
exports.getProjectConfig = getProjectConfig;
const getCombinedConfig = async () => {
const profileConfig = await getProfileConfig();
const projectConfig = await getProjectConfig();
const result = { ...defaultOptions,
...profileConfig.config,
...projectConfig.config
}; // todo: post-process config (i.e. template replacement)
return result;
};
exports.getCombinedConfig = getCombinedConfig;