@report-toolkit/fs
Version:
See docs at [https://ibm.github.io/report-toolkit](https://ibm.github.io/report-toolkit)
129 lines (111 loc) • 4.18 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var common = require('@report-toolkit/common');
var path = _interopDefault(require('path'));
var cosmiconfig = _interopDefault(require('cosmiconfig'));
var xdgBasedir = _interopDefault(require('xdg-basedir'));
var globalDirs = _interopDefault(require('global-dirs'));
var os = _interopDefault(require('os'));
var fs = require('fs');
const {
SHORT_NAMESPACE
} = common.constants;
const RC_FILENAME = `.${SHORT_NAMESPACE}rc.js`;
const RC_NAMESPACE = SHORT_NAMESPACE;
var constants = /*#__PURE__*/Object.freeze({
__proto__: null,
RC_FILENAME: RC_FILENAME,
RC_NAMESPACE: RC_NAMESPACE
});
const {
RTKERR_MISSING_CONFIG
} = common.error;
const {
map,
mergeMap,
filter,
concatMap,
concatMapTo,
of,
pipeIf,
switchMapTo,
take,
throwRTkError
} = common.observable;
const debug = common.createDebugPipe('cli', 'loaders', 'config');
const EXTRA_SEARCH_DIRS = [os.homedir(), path.join(globalDirs.npm.prefix, 'etc')];
if (os.platform() !== 'win32') {
EXTRA_SEARCH_DIRS.push(...xdgBasedir.configDirs, '/etc');
}
const getExplorer = common._.memoize(opts => // @ts-ignore
cosmiconfig(RC_NAMESPACE, common._.defaultsDeep({
// @ts-ignore
loaders: {
noExt: cosmiconfig.loadJs
}
}, opts)));
/**
*
* @param {import('cosmiconfig').Options} [opts] - Extra opts for cosmiconfig
* @returns {import('rxjs').OperatorFunction<string,object>}
*/
function toConfigFromSearchPath(opts = {}) {
const explorer = getExplorer(opts);
return observable => observable.pipe(mergeMap(searchpath => explorer.search(searchpath)), pipeIf(common._.isObject, debug(config => `found config at ${config.filepath}`), map(common._.get('config.config'))));
}
/**
*
* @param {import('cosmiconfig').Options} [opts] - Extra opts for cosmiconfig
* @returns {import('rxjs').OperatorFunction<string,object>}
*/
function toConfigFromFilepath(opts = {}) {
const explorer = getExplorer(opts);
return observable => observable.pipe(mergeMap(filepath => explorer.load(filepath)), map(common._.get('config.config')));
}
const fromFilesystemToConfig = ({
config: rawConfigOrFilepath = {},
searchPath = process.cwd(),
search = true
} = {}) => {
const allSearchPaths = [searchPath, ...EXTRA_SEARCH_DIRS];
return of(rawConfigOrFilepath).pipe(pipeIf(common._.isString, toConfigFromFilepath()), pipeIf(
/** @param {object} rawConfig */
rawConfig => common._.isEmpty(rawConfig) && search, concatMapTo(allSearchPaths), debug(allSearchPath => `searching in ${allSearchPath} for config`), concatMap(allSearchPath => of(allSearchPath).pipe(pipeIf(allSearchPath === searchPath, toConfigFromSearchPath()), pipeIf(allSearchPath !== searchPath, toConfigFromSearchPath({
stopDir: allSearchPath
})))), filter(common._.negate(common._.isEmpty)), take(1)), pipeIf(common._.isEmpty && common._.isString(rawConfigOrFilepath), switchMapTo(throwRTkError(RTKERR_MISSING_CONFIG, `No config file found at ${rawConfigOrFilepath}`))));
};
const {
bindNodeCallback,
map: map$1,
mergeMap: mergeMap$1,
toObjectFromJSON
} = common.observable;
const debug$1 = common.createDebugPipe('fs', 'report-loader');
/**
* @type {(filepath: string, encoding:string)=>Observable<any>}
*/
const readFile = bindNodeCallback(fs.readFile);
/**
* @returns {import('rxjs').OperatorFunction<string,{filepath: string,rawReport:import('@report-toolkit/common/diagnostic-report').DiagnosticReport}>}
*/
function toObjectFromFilepath() {
return filepath$ => filepath$.pipe(mergeMap$1(
/**
* @param {string} filepath
*/
filepath => readFile(filepath, 'utf8').pipe(toObjectFromJSON(), map$1(rawReport => ({
filepath,
rawReport
})), debug$1(({
filepath
}) => `loaded report at filepath ${filepath}`))));
}
/**
* @template T
* @typedef {import('rxjs').Observable<T>} Observable
*/
exports.constants = constants;
exports.fromFilesystemToConfig = fromFilesystemToConfig;
exports.toObjectFromFilepath = toObjectFromFilepath;
//# sourceMappingURL=report-toolkit-fs.cjs.js.map