UNPKG

2.2 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const path_1 = __importDefault(require("path"));
16// inline fp methods due to perf
17const uniq = arr => arr.filter((elem, pos, a) => a.indexOf(elem) === pos);
18const reversePathsToWalk = ({ folder, path }) => {
19 const resolved = path.resolve(folder);
20 const parts = resolved.split(path.sep);
21 const results = parts.map((_, idx, arr) => arr.slice(0, idx + 1).join(path.sep));
22 results[0] = results[0] || '/';
23 return results.reverse();
24};
25exports.reversePathsToWalk = reversePathsToWalk;
26const configLookup = (file, folder, path = path_1.default) => uniq(reversePathsToWalk({ folder, path }).map(p => path.join(p, file)));
27exports.configLookup = configLookup;
28class ConfigResolver {
29 constructor(configFile, io) {
30 this.configFile = configFile;
31 this.io = io;
32 }
33 resolve(from) {
34 return __awaiter(this, void 0, void 0, function* () {
35 const configCandidates = configLookup(this.configFile, from);
36 const { exists, load, none } = this.io;
37 for (const candidate of configCandidates) {
38 if (yield exists(candidate)) {
39 return load(candidate);
40 }
41 }
42 return none(from);
43 });
44 }
45}
46exports.ConfigResolver = ConfigResolver;
47//# sourceMappingURL=config.js.map
\No newline at end of file