@mometa/materials-resolver
Version:
Resolve materials config
148 lines • 5.9 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveLibMatConfig = exports.resolveAsyncConfig = exports.mapObjectSkip = void 0;
const nps = __importStar(require("path"));
const fs = __importStar(require("fs"));
const isObject = (value) => typeof value === 'object' && value !== null;
const isPromise = (val) => (val && val instanceof Promise) || typeof val.then === 'function';
// Customized for this use-case
const isObjectCustom = (value) => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date);
exports.mapObjectSkip = Symbol('mapObjectSkip');
const _mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
options = Object.assign({ deep: false, target: {} }, options);
if (isSeen.has(object)) {
return isSeen.get(object);
}
isSeen.set(object, options.target);
const { target } = options;
delete options.target;
const mapArray = (array) => array.map((element) => (isObjectCustom(element) ? _mapObject(element, mapper, options, isSeen) : element));
if (Array.isArray(object)) {
return mapArray(object);
}
for (const [key, value] of Object.entries(object)) {
const mapResult = mapper(key, value, object, target);
if (mapResult === exports.mapObjectSkip) {
continue;
}
let [newKey, newValue, { shouldRecurse = true } = {}] = mapResult;
// Drop `__proto__` keys.
if (newKey === '__proto__') {
continue;
}
if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
newValue = Array.isArray(newValue) ? mapArray(newValue) : _mapObject(newValue, mapper, options, isSeen);
}
target[newKey] = newValue;
}
return target;
};
function mapObject(object, mapper, options) {
if (!isObject(object)) {
throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
}
return _mapObject(object, mapper, options);
}
function resolveAsyncConfig(config) {
return __awaiter(this, void 0, void 0, function* () {
config = yield Promise.resolve(config);
if (Array.isArray(config)) {
config = yield Promise.all(config);
}
const tasks = [];
const newConfig = mapObject(config, (key, val, raw, target) => {
if (isPromise(val)) {
tasks.push(Promise.resolve(val).then((resolved) => {
target[key] = resolved;
}));
return [key, val, { shouldRecurse: false }];
}
if (Array.isArray(val)) {
tasks.push(Promise.all(val).then((resolved) => {
target[key] = resolved;
}));
return [key, val];
}
return [key, val];
}, {
deep: true
});
yield Promise.all(tasks);
return newConfig;
});
}
exports.resolveAsyncConfig = resolveAsyncConfig;
const myRequire = (p) => {
var _a;
const mod = require(resolvePath(p));
if (mod.__esModule) {
return (_a = mod.default) !== null && _a !== void 0 ? _a : mod;
}
return mod;
};
const resolvePath = (p) => {
let pkgName;
try {
pkgName = require.resolve(nps.join(p, 'package.json'));
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
if (pkgName) {
const pkg = JSON.parse(fs.readFileSync(pkgName, 'utf-8'));
if (pkg.mometa && typeof pkg.mometa === 'string') {
return require.resolve(nps.join(p, pkg.mometa));
}
}
return require.resolve(p);
};
function resolveLibMatConfig(path) {
return __awaiter(this, void 0, void 0, function* () {
let config;
if (nps.isAbsolute(path) || path.startsWith('.')) {
config = myRequire(nps.resolve(path));
}
else if (path.startsWith('@')) {
config = myRequire(path);
}
else if (resolvePath(`@mometa-mat/${path}`)) {
config = myRequire(`@mometa-mat/${path}`);
}
else {
config = myRequire(path);
}
return resolveAsyncConfig(config);
});
}
exports.resolveLibMatConfig = resolveLibMatConfig;
//# sourceMappingURL=resolve-async-config.js.map