1 | /*───────────────────────────────────────────────────────────────────────────*\
|
2 | │ Copyright (C) 2016 PayPal │
|
3 | │ │
|
4 | │ Licensed under the Apache License, Version 2.0 (the "License"); │
|
5 | │ you may not use this file except in compliance with the License. │
|
6 | │ You may obtain a copy of the License at │
|
7 | │ │
|
8 | │ http://www.apache.org/licenses/LICENSE-2.0 │
|
9 | │ │
|
10 | │ Unless required by applicable law or agreed to in writing, software │
|
11 | │ distributed under the License is distributed on an "AS IS" BASIS, │
|
12 | │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
13 | │ See the License for the specific language governing permissions and │
|
14 | │ limitations under the License. │
|
15 | \*───────────────────────────────────────────────────────────────────────────*/
|
16 | import shush from 'shush';
|
17 | import async from 'async';
|
18 | import Shortstop from 'shortstop'
|
19 | import { path as createPath } from 'shortstop-handlers';
|
20 |
|
21 |
|
22 | export default class Handlers {
|
23 |
|
24 | static resolveConfig(data) {
|
25 | return new Promise((resolve, reject) => {
|
26 | let usedHandler = false;
|
27 |
|
28 | let shorty = Shortstop.create();
|
29 | shorty.use('config', function (key) {
|
30 | usedHandler = true;
|
31 |
|
32 | let keys = key.split('.');
|
33 | let result = data;
|
34 |
|
35 | while (result && keys.length) {
|
36 | let prop = keys.shift();
|
37 | if (!result.hasOwnProperty(prop)) {
|
38 | return undefined;
|
39 | }
|
40 | result = result[prop];
|
41 | }
|
42 |
|
43 | return keys.length ? null : result;
|
44 | });
|
45 |
|
46 | async.doWhilst(
|
47 | function exec(cb) {
|
48 | usedHandler = false;
|
49 | shorty.resolve(data, function (err, result) {
|
50 | if (err) {
|
51 | cb(err);
|
52 | return;
|
53 | }
|
54 | data = result;
|
55 | cb();
|
56 | });
|
57 | },
|
58 |
|
59 | function test() {
|
60 | return usedHandler;
|
61 | },
|
62 |
|
63 | function complete(err) {
|
64 | if (err) {
|
65 | reject(err);
|
66 | return;
|
67 | }
|
68 | resolve(data);
|
69 | }
|
70 | );
|
71 | });
|
72 | }
|
73 |
|
74 | static resolveImport(data, basedir) {
|
75 | return new Promise((resolve, reject) => {
|
76 | let path = createPath(basedir);
|
77 | let shorty = Shortstop.create();
|
78 |
|
79 | shorty.use('import', function (file, cb) {
|
80 | try {
|
81 | file = path(file);
|
82 | return shorty.resolve(shush(file), cb);
|
83 | } catch (err) {
|
84 | cb(err);
|
85 | }
|
86 | });
|
87 |
|
88 | shorty.resolve(data, function (err, data) {
|
89 | if (err) {
|
90 | reject(err);
|
91 | return;
|
92 | }
|
93 | resolve(data);
|
94 | });
|
95 | });
|
96 | }
|
97 |
|
98 | static resolveCustom(data, protocols) {
|
99 | return new Promise(function (resolve, reject) {
|
100 | let shorty = Shortstop.create();
|
101 |
|
102 | for (let protocol of Object.keys(protocols)) {
|
103 | let impls = protocols[protocol];
|
104 |
|
105 | if (Array.isArray(impls)) {
|
106 | for (let impl of impls) {
|
107 | shorty.use(protocol, impl);
|
108 | }
|
109 | } else {
|
110 | shorty.use(protocol, impls);
|
111 | }
|
112 | }
|
113 |
|
114 | shorty.resolve(data, function (err, data) {
|
115 | if (err) {
|
116 | reject(err);
|
117 | return;
|
118 | }
|
119 | resolve(data);
|
120 | });
|
121 | });
|
122 |
|
123 | }
|
124 | }
|