@appium/docutils
Version:
Documentation generation utilities for Appium and related projects
124 lines • 4.75 kB
JavaScript
;
/**
* Handles reading of a config file for docutils
* @module
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findConfig = findConfig;
exports.loadConfig = loadConfig;
const lilconfig_1 = require("lilconfig");
const lodash_1 = __importDefault(require("lodash"));
const node_path_1 = __importDefault(require("node:path"));
const YAML = __importStar(require("yaml"));
const yargs_parser_1 = __importDefault(require("yargs-parser"));
const helpers_1 = require("yargs/helpers");
const constants_1 = require("../constants");
const logger_1 = require("../logger");
const util_1 = require("../util");
const log = (0, logger_1.getLogger)('config');
/**
* `lilconfig` loader for YAML
*/
const loadYaml = lodash_1.default.rearg(YAML.parse, [2, 0, 1]);
/**
* Controls how we load/find a config file.
*
* Takes _raw_ args from the CLI, and uses `yargs-parser` to parse them as to not interfere with the
* main usage of args.
*
* We're looking for various things in the CLI args:
* - `--no-config` - if this is present, we don't load a config file
* - `--log-level` - if this is present, we set the log level
* - `--verbose` - same as above
* - `--config` - if this is present, we load the config file at the given path
* - `--help`, `--version` - do nothing
* @param argv Raw CLI args
* @returns
*/
async function findConfig(argv = (0, helpers_1.hideBin)(process.argv)) {
const preArgs = (0, yargs_parser_1.default)(argv);
// if --verbose is used, set the log level to debug.
// otherwise use --log-level or the default.
let logLevel;
if (preArgs.verbose) {
logLevel = 'debug';
}
else {
// if the loglevel is valid, use it, otherwise use the default
logLevel = (0, logger_1.isLogLevelString)(preArgs.logLevel) ? preArgs.logLevel : constants_1.DEFAULT_LOG_LEVEL;
}
(0, logger_1.initLogger)(logLevel);
if (preArgs.noConfig) {
log.debug('Not loading config because --no-config was provided');
}
return preArgs.noConfig || preArgs.help || preArgs.version
? {}
: await loadConfig(preArgs.config);
}
/**
* Loads a config file or finds and loads one if none provided
* @param filepath Config file path, if provided
* @param cwd Current working directory
* @returns A config object or an empty object. Could be anything; `yargs` will validate it.
*/
async function loadConfig(filepath, cwd = process.cwd()) {
const relativePath = (0, util_1.relative)(cwd);
const searcher = (0, lilconfig_1.lilconfig)(constants_1.NAME_BIN, {
loaders: {
'.yaml': loadYaml,
'.yml': loadYaml,
},
});
const result = filepath
? await searcher.load(node_path_1.default.normalize(filepath))
: await searcher.search(cwd);
if (result === null) {
log.debug('No config found');
return {};
}
if (result.isEmpty) {
log.debug('Config loaded at %s but it was empty', result.filepath);
return {};
}
const relFilepath = relativePath(result.filepath);
log.success('Loaded config from %s', relFilepath);
log.debug('Config contents: %O', result.config);
return result.config;
}
//# sourceMappingURL=config.js.map