UNPKG

3.36 kBJavaScriptView Raw
1import R from "ramda";
2import fs from "fs-extra";
3import fsPath from "path";
4import shell from "shelljs";
5import Promise from "bluebird";
6
7
8
9/**
10 * Determines whether the given value is Null/Undefined or Empty.
11 */
12export const isEmpty = (value) => (R.isNil(value) || R.isEmpty(value));
13
14
15
16/**
17 * Waits for a set of promises to complete.
18 * @return {Promise}
19 */
20export 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 * Invokes a shell command asynchronously returning a promise.
40 * @return {Promise}
41 */
42export 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 * Sets a timeout for the specified amount of time.
54 * @param {integer} msecs: The number of milliseconds to wait.
55 * @param {Function} func: The function to invoke.
56 */
57export const delay = (msecs, func) => global.setTimout(func, msecs);
58
59
60
61
62
63/**
64 * Loads JSON from the given path.
65 * @return {Promise}
66 */
67export 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 * Determines whether a file exists at the given path.
90 * @return {Promise}
91 */
92export const pathExists = (path) => {
93 return new Promise((resolve) => {
94 fs.exists(path, (exists) => resolve(exists));
95 });
96};
97
98
99
100
101/**
102 * Loads JSON from the given path.
103 * @return {Promise}
104 */
105export 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 * Sorts a set of app definitions, moving the wild-card domains to the end.
128 * @param apps: An array of app definitions.
129 * @return {Array}.
130 */
131export 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 // Finish up.
148 apps = R.union(explicitDomains, wildcardDomains);
149 return apps.map(item => item.app);
150 };