1 | "use strict";
|
2 | var __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 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const path_1 = __importDefault(require("path"));
|
16 |
|
17 | const uniq = arr => arr.filter((elem, pos, a) => a.indexOf(elem) === pos);
|
18 | const 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 | };
|
25 | exports.reversePathsToWalk = reversePathsToWalk;
|
26 | const configLookup = (file, folder, path = path_1.default) => uniq(reversePathsToWalk({ folder, path }).map(p => path.join(p, file)));
|
27 | exports.configLookup = configLookup;
|
28 | class 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 | }
|
46 | exports.ConfigResolver = ConfigResolver;
|
47 |
|
\ | No newline at end of file |