1 | import R from "ramda";
|
2 | import fs from "fs-extra";
|
3 | import fsPath from "path";
|
4 | import shell from "shelljs";
|
5 | import Promise from "bluebird";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export const isEmpty = (value) => (R.isNil(value) || R.isEmpty(value));
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export const promises = (list) => {
|
21 | list = R.reject(R.isNil, list);
|
22 | return new Promise((resolve, reject) => {
|
23 | const results = [];
|
24 | const onComplete = (result) => {
|
25 | results.push(result);
|
26 | if (results.length === list.length) { resolve({ results }); }
|
27 | };
|
28 | list.forEach(promise => {
|
29 | promise
|
30 | .then(result => onComplete(result))
|
31 | .catch(err => reject(err));
|
32 | });
|
33 | });
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export const shellAsync = (cmd) => {
|
43 | return new Promise((resolve) => {
|
44 | shell.exec(cmd, (code, output) => {
|
45 | resolve({ code, output });
|
46 | });
|
47 | });
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | export const delay = (msecs, func) => global.setTimout(func, msecs);
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export const loadFile = (path) => {
|
68 | return new Promise((resolve, reject) => {
|
69 | path = fsPath.resolve(path);
|
70 | fs.exists(path, (exists) => {
|
71 | if (exists) {
|
72 | fs.readFile(path, (err, result) => {
|
73 | if (err) {
|
74 | reject(err);
|
75 | } else {
|
76 | resolve({ exists: true, content: result.toString() });
|
77 | }
|
78 | });
|
79 | } else {
|
80 | resolve({ exists: false });
|
81 | }
|
82 | });
|
83 | });
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export const pathExists = (path) => {
|
93 | return new Promise((resolve) => {
|
94 | fs.exists(path, (exists) => resolve(exists));
|
95 | });
|
96 | };
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export const loadJson = (path) => {
|
106 | return new Promise((resolve, reject) => {
|
107 | loadFile(path)
|
108 | .then(result => {
|
109 | try {
|
110 | if (result.exists) {
|
111 | result.json = JSON.parse(result.content);
|
112 | }
|
113 | } catch (err) { reject(err); }
|
114 | resolve(result);
|
115 | })
|
116 | .catch(err => reject(err));
|
117 | });
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export const sortAppsByRoute = (apps) => {
|
132 | apps = apps.map(app => ({ app, route: app.route.toString() }));
|
133 |
|
134 | const wildcard = (app) => app.route.startsWith("*");
|
135 | const notWildcard = (app) => !wildcard(app);
|
136 | const sorted = (filter) => R.pipe(R.filter(filter), R.sortBy(R.prop("route")));
|
137 |
|
138 | let wildcardDomains = sorted(wildcard)(apps);
|
139 | let explicitDomains = sorted(notWildcard)(apps);
|
140 |
|
141 | if (wildcardDomains[0] && wildcardDomains[0].route === "*") {
|
142 | const catchAll = wildcardDomains[0];
|
143 | wildcardDomains = R.remove(0, 1, wildcardDomains);
|
144 | wildcardDomains.push(catchAll);
|
145 | }
|
146 |
|
147 |
|
148 | apps = R.union(explicitDomains, wildcardDomains);
|
149 | return apps.map(item => item.app);
|
150 | };
|