navy 6.0.0 → 7.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/lib/__tests__/config-provider.js +75 -0
  2. package/lib/__tests__/config.js +130 -0
  3. package/lib/__tests__/driver-logging.js +148 -0
  4. package/lib/__tests__/driver.js +19 -0
  5. package/lib/__tests__/errors.js +49 -0
  6. package/lib/__tests__/http-proxy.js +214 -0
  7. package/lib/__tests__/index.js +25 -0
  8. package/lib/__tests__/service.js +16 -0
  9. package/lib/cli/__tests__/develop.js +239 -0
  10. package/lib/cli/__tests__/external-ip.js +68 -0
  11. package/lib/cli/__tests__/health.js +257 -0
  12. package/lib/cli/__tests__/https.js +210 -0
  13. package/lib/cli/__tests__/import.js +110 -0
  14. package/lib/cli/__tests__/index.js +118 -0
  15. package/lib/cli/__tests__/lan-ip.js +90 -0
  16. package/lib/cli/__tests__/launch.js +179 -0
  17. package/lib/cli/__tests__/live.js +155 -0
  18. package/lib/cli/__tests__/local-ip.js +72 -0
  19. package/lib/cli/__tests__/logs.js +52 -0
  20. package/lib/cli/__tests__/open.js +65 -0
  21. package/lib/cli/__tests__/program.js +472 -0
  22. package/lib/cli/__tests__/ps.js +345 -0
  23. package/lib/cli/__tests__/refresh-config.js +95 -0
  24. package/lib/cli/__tests__/run.js +54 -0
  25. package/lib/cli/__tests__/status.js +204 -0
  26. package/lib/cli/__tests__/updates.js +243 -0
  27. package/lib/cli/__tests__/wait-for-healthy.js +134 -0
  28. package/lib/cli/config/__tests__/index.js +275 -0
  29. package/lib/cli/config/__tests__/wrapper.js +53 -0
  30. package/lib/cli/config/index.js +19 -37
  31. package/lib/cli/config/wrapper.js +0 -6
  32. package/lib/cli/develop.js +7 -21
  33. package/lib/cli/doctor/__tests__/clean-compose-files.js +78 -0
  34. package/lib/cli/doctor/__tests__/index.js +67 -0
  35. package/lib/cli/doctor/__tests__/invalid-compose-config.js +103 -0
  36. package/lib/cli/doctor/__tests__/invalid-state.js +83 -0
  37. package/lib/cli/doctor/__tests__/util.js +91 -0
  38. package/lib/cli/doctor/clean-compose-files.js +5 -13
  39. package/lib/cli/doctor/index.js +0 -12
  40. package/lib/cli/doctor/invalid-compose-config.js +0 -4
  41. package/lib/cli/doctor/invalid-state.js +0 -6
  42. package/lib/cli/doctor/util.js +0 -10
  43. package/lib/cli/external-ip.js +0 -4
  44. package/lib/cli/health.js +0 -12
  45. package/lib/cli/https.js +9 -25
  46. package/lib/cli/import.js +0 -12
  47. package/lib/cli/index.js +0 -9
  48. package/lib/cli/lan-ip.js +2 -8
  49. package/lib/cli/launch.js +0 -9
  50. package/lib/cli/live.js +6 -16
  51. package/lib/cli/local-ip.js +2 -7
  52. package/lib/cli/logs.js +0 -3
  53. package/lib/cli/open.js +2 -7
  54. package/lib/cli/program.js +73 -101
  55. package/lib/cli/ps.js +1 -21
  56. package/lib/cli/refresh-config.js +0 -7
  57. package/lib/cli/run.js +0 -3
  58. package/lib/cli/status.js +0 -11
  59. package/lib/cli/updates.js +0 -22
  60. package/lib/cli/util/__tests__/get-or-initialise-navy.js +66 -0
  61. package/lib/cli/util/__tests__/import.js +123 -0
  62. package/lib/cli/util/__tests__/index.js +17 -0
  63. package/lib/cli/util/__tests__/merge-action-options.js +47 -0
  64. package/lib/cli/util/__tests__/reconfigure.js +78 -0
  65. package/lib/cli/util/get-or-initialise-navy.js +0 -7
  66. package/lib/cli/util/import.js +0 -9
  67. package/lib/cli/util/index.js +0 -2
  68. package/lib/cli/util/merge-action-options.js +20 -0
  69. package/lib/cli/util/reconfigure.js +0 -4
  70. package/lib/cli/wait-for-healthy.js +0 -21
  71. package/lib/client/registry/__tests__/get-credentials.js +62 -0
  72. package/lib/client/registry/__tests__/get-endpoint.js +124 -0
  73. package/lib/client/registry/__tests__/get-fat-manifest.js +67 -0
  74. package/lib/client/registry/__tests__/get-token.js +66 -0
  75. package/lib/client/registry/__tests__/helpers.js +26 -0
  76. package/lib/client/registry/get-credentials.js +3 -9
  77. package/lib/client/registry/get-endpoint.js +29 -63
  78. package/lib/client/registry/get-fat-manifest.js +2 -9
  79. package/lib/client/registry/get-token.js +2 -13
  80. package/lib/client/registry/helpers.js +0 -4
  81. package/lib/config-provider.js +0 -12
  82. package/lib/config-providers/filesystem/__tests__/index.js +176 -0
  83. package/lib/config-providers/filesystem/index.js +5 -23
  84. package/lib/config-providers/npm/__tests__/index.js +226 -0
  85. package/lib/config-providers/npm/__tests__/util.js +1 -2
  86. package/lib/config-providers/npm/index.js +12 -35
  87. package/lib/config-providers/npm/util.js +0 -3
  88. package/lib/config.js +4 -19
  89. package/lib/domain/__tests__/container-image.js +81 -0
  90. package/lib/domain/__tests__/oci-api-specification.js +23 -0
  91. package/lib/domain/container-image.js +8 -21
  92. package/lib/domain/oci-api-specification.js +3 -5
  93. package/lib/driver-logging.js +0 -19
  94. package/lib/driver.js +0 -4
  95. package/lib/drivers/docker-compose/__tests__/client.js +249 -0
  96. package/lib/drivers/docker-compose/__tests__/index.js +430 -0
  97. package/lib/drivers/docker-compose/client.js +0 -16
  98. package/lib/drivers/docker-compose/index.js +7 -49
  99. package/lib/errors.js +0 -10
  100. package/lib/http-proxy.js +28 -23
  101. package/lib/index.js +1 -9
  102. package/lib/middleware/__tests__/add-service-proxy-config.js +258 -0
  103. package/lib/middleware/__tests__/develop.js +120 -0
  104. package/lib/middleware/__tests__/helpers.js +154 -0
  105. package/lib/middleware/__tests__/port-override.js +125 -0
  106. package/lib/middleware/__tests__/set-env-vars.js +94 -0
  107. package/lib/middleware/__tests__/set-image.js +76 -0
  108. package/lib/middleware/__tests__/set-logging-driver.js +94 -0
  109. package/lib/middleware/__tests__/tag-override.js +92 -0
  110. package/lib/middleware/add-service-proxy-config.js +8 -16
  111. package/lib/middleware/develop.js +2 -5
  112. package/lib/middleware/helpers.js +6 -12
  113. package/lib/middleware/port-override.js +5 -8
  114. package/lib/middleware/set-env-vars.js +6 -6
  115. package/lib/middleware/set-image.js +4 -5
  116. package/lib/middleware/set-logging-driver.js +6 -6
  117. package/lib/middleware/tag-override.js +4 -6
  118. package/lib/navy/__tests__/default-middleware.js +40 -0
  119. package/lib/navy/__tests__/index.js +1612 -0
  120. package/lib/navy/__tests__/middleware.js +71 -0
  121. package/lib/navy/__tests__/plugin-interface.js +121 -0
  122. package/lib/navy/__tests__/state.js +103 -0
  123. package/lib/navy/__tests__/util.js +24 -0
  124. package/lib/navy/default-middleware.js +0 -10
  125. package/lib/navy/index.js +83 -138
  126. package/lib/navy/middleware.js +0 -6
  127. package/lib/navy/plugin-interface.js +2 -10
  128. package/lib/navy/state.js +12 -24
  129. package/lib/navy/util.js +0 -1
  130. package/lib/service.js +2 -3
  131. package/lib/util/__tests__/exec-async.js +83 -0
  132. package/lib/util/__tests__/external-ip.js +97 -2
  133. package/lib/util/__tests__/get-lan-ip.js +46 -0
  134. package/lib/util/__tests__/has-update.js +136 -0
  135. package/lib/util/__tests__/https.js +301 -0
  136. package/lib/util/__tests__/navyrc.js +45 -0
  137. package/lib/util/__tests__/service-host.js +63 -5
  138. package/lib/util/__tests__/table.js +44 -0
  139. package/lib/util/docker-client.js +2 -10
  140. package/lib/util/exec-async.js +0 -4
  141. package/lib/util/external-ip.js +8 -12
  142. package/lib/util/fs.js +1 -6
  143. package/lib/util/get-lan-ip.js +0 -5
  144. package/lib/util/has-update.js +2 -14
  145. package/lib/util/https.js +11 -55
  146. package/lib/util/navyrc.js +0 -5
  147. package/lib/util/service-host.js +0 -17
  148. package/lib/util/table.js +0 -6
  149. package/package.json +14 -13
package/lib/navy/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
@@ -9,37 +8,21 @@ exports.Navy = void 0;
9
8
  exports.getLaunchedNavies = getLaunchedNavies;
10
9
  exports.getLaunchedNavyNames = getLaunchedNavyNames;
11
10
  exports.getNavy = getNavy;
12
-
13
11
  var _invariant = _interopRequireDefault(require("invariant"));
14
-
15
12
  var _eventemitter = require("eventemitter2");
16
-
17
13
  var _promiseRetry = _interopRequireDefault(require("promise-retry"));
18
-
19
14
  var _fs = _interopRequireDefault(require("../util/fs"));
20
-
21
15
  var _driver = require("../driver");
22
-
23
16
  var _configProvider = require("../config-provider");
24
-
25
17
  var _util = require("./util");
26
-
27
18
  var _state = require("./state");
28
-
29
19
  var _config = require("../config");
30
-
31
20
  var _errors = require("../errors");
32
-
33
21
  var _pluginInterface = require("./plugin-interface");
34
-
35
22
  var _middleware = require("./middleware");
36
-
37
23
  var _httpProxy = require("../http-proxy");
38
-
39
24
  var _externalIp = require("../util/external-ip");
40
-
41
25
  var _serviceHost = require("../util/service-host");
42
-
43
26
  /**
44
27
  * A Navy instance
45
28
  * @public
@@ -54,6 +37,7 @@ class Navy extends _eventemitter.EventEmitter2 {
54
37
  * The normalised name of the current Navy (name without whitespaces).
55
38
  * @public
56
39
  */
40
+
57
41
  constructor(name) {
58
42
  super({
59
43
  maxListeners: Number.MAX_SAFE_INTEGER,
@@ -66,7 +50,6 @@ class Navy extends _eventemitter.EventEmitter2 {
66
50
  this._registeredCommands = {};
67
51
  this._registeredMiddleware = [];
68
52
  }
69
-
70
53
  async ensurePluginsLoaded() {
71
54
  if (this._pluginsLoaded) return;
72
55
  const navyFile = await this.getNavyFile();
@@ -74,136 +57,112 @@ class Navy extends _eventemitter.EventEmitter2 {
74
57
  await (0, _pluginInterface.loadPlugins)(this, navyFile);
75
58
  this._pluginsLoaded = true;
76
59
  }
60
+
77
61
  /**
78
62
  * Returns the current State for this Navy.
79
63
  * @public
80
64
  */
81
-
82
-
83
65
  async getState() {
84
66
  return await (0, _state.getState)(this.normalisedName);
85
67
  }
68
+
86
69
  /**
87
70
  * Returns an instance of the driver in use by this navy.
88
71
  * Returns null if the navy hasn't been launched yet.
89
72
  */
90
-
91
-
92
73
  async getDriver() {
93
74
  const envState = await this.getState();
94
-
95
75
  if (!envState || !envState.driver) {
96
76
  return null;
97
77
  }
98
-
99
78
  const createDriver = (0, _driver.resolveDriverFromName)(envState.driver);
100
-
101
79
  if (!createDriver) {
102
80
  return null;
103
81
  }
104
-
105
82
  return createDriver(this);
106
83
  }
84
+
107
85
  /**
108
86
  * Same as getDriver but will throw an error if the driver cannot be determined.
109
87
  */
110
-
111
-
112
88
  async safeGetDriver() {
113
89
  const driver = await this.getDriver();
114
-
115
90
  if (!(await this.isInitialised())) {
116
91
  throw new _errors.NavyNotInitialisedError(this.name);
117
92
  }
118
-
119
93
  (0, _invariant.default)(driver, "NO_DRIVER: Couldn't determine driver for navy %s", this.name);
120
94
  return driver;
121
95
  }
122
-
123
96
  async getConfigProvider() {
124
97
  const envState = await this.getState();
125
-
126
98
  if (!envState || !envState.configProvider) {
127
99
  return null;
128
100
  }
129
-
130
101
  const createConfigProvider = (0, _configProvider.resolveConfigProviderFromName)(envState.configProvider);
131
-
132
102
  if (!createConfigProvider) {
133
103
  return null;
134
104
  }
135
-
136
105
  return createConfigProvider(this);
137
106
  }
138
-
139
107
  async getNavyFile() {
140
108
  if (!(await this.isInitialised())) {
141
109
  throw new _errors.NavyNotInitialisedError(this.name);
142
110
  }
143
-
144
111
  const configProvider = await this.getConfigProvider();
145
112
  (0, _invariant.default)(configProvider, "NO_CONFIG_PROVIDER: Couldn't determine config provider for navy %s", this.name);
146
113
  const navyFilePath = await configProvider.getNavyFilePath();
147
-
148
114
  try {
149
- // $FlowIgnore: entry point to Navyfile.js has to be dynamic
115
+ // $FlowFixMe[unsupported-syntax]: entry point to Navyfile.js has to be dynamic
150
116
  return require(navyFilePath);
151
117
  } catch (ex) {
152
118
  return null;
153
119
  }
154
120
  }
121
+
155
122
  /**
156
123
  * Saves a new State for this Navy.
157
124
  * @public
158
125
  */
159
-
160
-
161
126
  async saveState(state) {
162
127
  await (0, _state.saveState)(this.normalisedName, state);
163
128
  await (0, _middleware.middlewareRunner)(this, state);
164
129
  }
130
+
165
131
  /**
166
132
  * Registers a custom CLI command with the given name and callback function.
167
133
  * Any command registered can be run using `navy run [name]`.
168
134
  * @public
169
135
  */
170
-
171
-
172
136
  registerCommand(name, action) {
173
137
  this._registeredCommands[name] = action;
174
138
  }
139
+
175
140
  /**
176
141
  * Registers a middleware reducer function. See [Writing Plugins](docs/writing-plugins.md).
177
142
  * @public
178
143
  */
179
-
180
-
181
144
  registerMiddleware(middlewareFn) {
182
145
  this._registeredMiddleware.push(middlewareFn);
183
146
  }
184
-
185
147
  async invokeCommand(name, args) {
186
148
  if (!this._registeredCommands[name]) {
187
149
  throw new _errors.NavyError('Unknown command "' + name + '"');
188
150
  }
189
-
190
151
  await this._registeredCommands[name](args);
191
152
  }
153
+
192
154
  /**
193
155
  * Returns whether this Navy is initialised and ready to launch services.
194
156
  * @public
195
157
  */
196
-
197
-
198
158
  async isInitialised() {
199
159
  return (await this.getState()) != null;
200
160
  }
161
+
201
162
  /**
202
163
  * Initialises this Navy with the given State.
203
164
  * @public
204
165
  */
205
-
206
-
207
166
  async initialise(opts) {
208
167
  const state = {
209
168
  services: {},
@@ -212,22 +171,20 @@ class Navy extends _eventemitter.EventEmitter2 {
212
171
  };
213
172
  await (0, _state.saveState)(this.normalisedName, state);
214
173
  }
174
+
215
175
  /**
216
176
  * Deletes the state for this Navy. It won't be possible to interact with this Navy after calling this function
217
177
  * unless you re-initialise it.
218
178
  * @public
219
179
  */
220
-
221
-
222
180
  async delete() {
223
181
  await (0, _state.deleteState)(this.normalisedName);
224
182
  }
183
+
225
184
  /**
226
185
  * Launches the given services for this Navy.
227
186
  * @public
228
187
  */
229
-
230
-
231
188
  async launch(serviceNames, opts) {
232
189
  if (!serviceNames) serviceNames = await this.getLaunchedServiceNames();
233
190
  const availableServices = await (await this.safeGetDriver()).getAvailableServiceNames();
@@ -235,158 +192,146 @@ class Navy extends _eventemitter.EventEmitter2 {
235
192
  if (availableServices.indexOf(serviceName) !== -1) {
236
193
  return true;
237
194
  }
238
-
239
195
  console.log(`Unknown service name: ${serviceName}`);
240
196
  return false;
241
197
  });
242
198
  const allServiceNamesInvalid = serviceNames.length > 0 && validServiceNames.length === 0;
243
-
244
199
  if (allServiceNamesInvalid) {
245
200
  return;
246
201
  }
247
-
248
202
  const state = await this.getState();
249
-
250
203
  if (state) {
251
204
  await (0, _middleware.middlewareRunner)(this, state);
252
205
  }
253
-
254
206
  await (await this.safeGetDriver()).launch(validServiceNames, opts);
255
- await (0, _httpProxy.reconfigureHTTPProxy)();
207
+ await (0, _httpProxy.reconfigureHTTPProxy)({
208
+ navyFile: await this.getNavyFile()
209
+ });
256
210
  }
211
+
257
212
  /**
258
213
  * @deprecated
259
214
  */
260
-
261
-
262
215
  async relaunch(opts) {
263
216
  return await this.reconfigure(opts);
264
217
  }
218
+
265
219
  /**
266
220
  * Relaunches (reconfigures) all of the services running in this Navy.
267
221
  * This will pick up any changes to the compose configuration.
268
222
  * @public
269
223
  */
270
-
271
-
272
224
  async reconfigure(opts) {
273
225
  const services = await this.getLaunchedServiceNames();
274
-
275
226
  if (services.length === 0) {
276
227
  return;
277
228
  }
278
-
279
229
  await this.launch(undefined, opts);
280
230
  }
231
+
281
232
  /**
282
233
  * Destroys all of the services in this Navy and deletes the state.
283
234
  * @public
284
235
  */
285
-
286
-
287
236
  async destroy() {
288
237
  if (!(await this.isInitialised())) {
289
238
  throw new _errors.NavyNotInitialisedError(this.name);
290
239
  }
291
-
292
240
  await (0, _httpProxy.reconfigureHTTPProxy)({
293
- navies: (await getLaunchedNavyNames()).filter(navy => navy !== this.normalisedName)
241
+ navies: (await getLaunchedNavyNames()).filter(navy => navy !== this.normalisedName),
242
+ navyFile: await this.getNavyFile()
294
243
  });
295
-
296
244
  try {
297
245
  await (await this.safeGetDriver()).destroy();
298
246
  } catch (ex) {}
299
-
300
247
  await this.delete();
301
248
  }
249
+
302
250
  /**
303
251
  * Returns a list of launched services for this Navy.
304
252
  * @public
305
253
  */
306
-
307
-
308
254
  async ps() {
309
255
  return await (await this.safeGetDriver()).ps();
310
256
  }
257
+
311
258
  /**
312
259
  * Starts the given services.
313
260
  * @public
314
261
  */
315
-
316
-
317
262
  async start(services) {
318
263
  if (!services) services = await this.getLaunchedServiceNames();
319
- await (0, _httpProxy.reconfigureHTTPProxy)();
264
+ await (0, _httpProxy.reconfigureHTTPProxy)({
265
+ navyFile: await this.getNavyFile()
266
+ });
320
267
  await (await this.safeGetDriver()).start(services);
321
268
  }
269
+
322
270
  /**
323
271
  * Stops the given services.
324
272
  * @public
325
273
  */
326
-
327
-
328
274
  async stop(services) {
329
275
  if (!services) services = await this.getLaunchedServiceNames();
330
276
  await (await this.safeGetDriver()).stop(services);
331
277
  }
278
+
332
279
  /**
333
280
  * Restarts the given services.
334
281
  * @public
335
282
  */
336
-
337
-
338
283
  async restart(services) {
339
284
  if (!services) services = await this.getLaunchedServiceNames();
340
285
  await (await this.safeGetDriver()).restart(services);
341
286
  }
287
+
342
288
  /**
343
289
  * Forcefully stops the given services.
344
290
  * @public
345
291
  */
346
-
347
-
348
292
  async kill(services) {
349
293
  if (!services) services = await this.getLaunchedServiceNames();
350
294
  await (await this.safeGetDriver()).kill(services);
351
295
  }
296
+
352
297
  /**
353
298
  * Removes the given services. Requires the services to be stopped first.
354
299
  * @public
355
300
  */
356
-
357
-
358
301
  async rm(services) {
359
302
  if (!services) services = await this.getLaunchedServiceNames();
360
303
  await (await this.safeGetDriver()).rm(services);
361
304
  }
305
+
362
306
  /**
363
307
  * Makes sure the images for the given services are up to date, and restarts any running
364
308
  * services with new images.
365
309
  * @public
366
310
  */
367
-
368
-
369
311
  async update(services) {
370
312
  // update all images by default
371
313
  if (!services) services = await this.getAvailableServiceNames();
372
314
  await (await this.safeGetDriver()).update(services);
373
315
  }
374
-
375
316
  async spawnLogStream(services) {
376
317
  if (!services) services = await this.getLaunchedServiceNames();
377
318
  await (await this.safeGetDriver()).spawnLogStream(services);
378
319
  }
320
+
379
321
  /**
380
322
  * Locks down the given service to the given docker image tag.
381
323
  * @public
382
324
  */
383
-
384
-
385
325
  async useTag(service, tag) {
386
- const state = (await this.getState()) || {};
387
- await this.saveState({ ...state,
388
- services: { ...state.services,
389
- [service]: { ...(state.services || {})[service],
326
+ const state = await this.getState();
327
+ if (!state) throw new _errors.NavyNotInitialisedError(this.name);
328
+ const stateServices = state.services || {};
329
+ await this.saveState({
330
+ ...state,
331
+ services: {
332
+ ...stateServices,
333
+ [service]: {
334
+ ...stateServices[service],
390
335
  _tag: tag
391
336
  }
392
337
  }
@@ -394,17 +339,21 @@ class Navy extends _eventemitter.EventEmitter2 {
394
339
  await this.kill([service]);
395
340
  await this.update([service]); // pull and launch
396
341
  }
342
+
397
343
  /**
398
344
  * Resets the version of the given service if `useTag` was used.
399
345
  * @public
400
346
  */
401
-
402
-
403
347
  async resetTag(service) {
404
- const state = (await this.getState()) || {};
405
- await this.saveState({ ...state,
406
- services: { ...state.services,
407
- [service]: { ...(state.services || {})[service],
348
+ const state = await this.getState();
349
+ if (!state) throw new _errors.NavyNotInitialisedError(this.name);
350
+ const stateServices = state.services || {};
351
+ await this.saveState({
352
+ ...state,
353
+ services: {
354
+ ...stateServices,
355
+ [service]: {
356
+ ...stateServices[service],
408
357
  _tag: undefined
409
358
  }
410
359
  }
@@ -414,13 +363,19 @@ class Navy extends _eventemitter.EventEmitter2 {
414
363
  noDeps: true
415
364
  });
416
365
  }
417
-
418
366
  async usePort(service, privatePort, externalPort) {
419
- const state = (await this.getState()) || {};
420
- await this.saveState({ ...state,
421
- services: { ...state.services,
422
- [service]: { ...(state.services || {})[service],
423
- _ports: { ...((state.services || {})[service] || {})._ports,
367
+ const state = await this.getState();
368
+ if (!state) throw new _errors.NavyNotInitialisedError(this.name);
369
+ const stateServices = state.services || {};
370
+ const existingService = stateServices[service] || {};
371
+ await this.saveState({
372
+ ...state,
373
+ services: {
374
+ ...stateServices,
375
+ [service]: {
376
+ ...existingService,
377
+ _ports: {
378
+ ...existingService._ports,
424
379
  [privatePort]: externalPort
425
380
  }
426
381
  }
@@ -431,13 +386,19 @@ class Navy extends _eventemitter.EventEmitter2 {
431
386
  noDeps: true
432
387
  });
433
388
  }
434
-
435
389
  async resetPort(service, privatePort) {
436
- const state = (await this.getState()) || {};
437
- await this.saveState({ ...state,
438
- services: { ...state.services,
439
- [service]: { ...(state.services || {})[service],
440
- _ports: { ...((state.services || {})[service] || {})._ports,
390
+ const state = await this.getState();
391
+ if (!state) throw new _errors.NavyNotInitialisedError(this.name);
392
+ const stateServices = state.services || {};
393
+ const existingService = stateServices[service] || {};
394
+ await this.saveState({
395
+ ...state,
396
+ services: {
397
+ ...stateServices,
398
+ [service]: {
399
+ ...existingService,
400
+ _ports: {
401
+ ...existingService._ports,
441
402
  [privatePort]: undefined
442
403
  }
443
404
  }
@@ -448,12 +409,11 @@ class Navy extends _eventemitter.EventEmitter2 {
448
409
  noDeps: true
449
410
  });
450
411
  }
412
+
451
413
  /**
452
414
  * Waits for the given services to be healthy. Resolves when all services are healthy.
453
415
  * @public
454
416
  */
455
-
456
-
457
417
  async waitForHealthy(services, progressCallback, retryConfig = {
458
418
  factor: 1.1,
459
419
  retries: 30,
@@ -462,24 +422,20 @@ class Navy extends _eventemitter.EventEmitter2 {
462
422
  if (services == null) {
463
423
  services = (await this.ps()).filter(service => service && service.raw && service.raw.State.Health).map(service => service.name);
464
424
  }
465
-
466
425
  try {
467
426
  await (0, _promiseRetry.default)(retryConfig, async retry => {
468
427
  const ps = await this.ps();
469
428
  const specifiedServices = ps.filter(service => services && services.indexOf(service.name) !== -1);
470
429
  const servicesWithoutHealthInfo = specifiedServices.filter(service => !service.raw || !service.raw.State.Health).map(service => service.name);
471
-
472
430
  if (servicesWithoutHealthInfo.length > 0) {
473
431
  throw new _errors.NavyError('The specified services don\'t have health checks: ' + servicesWithoutHealthInfo.join(', '));
474
432
  }
475
-
476
433
  const serviceHealth = specifiedServices.map(service => ({
477
434
  service: service.name,
478
435
  health: service.raw && service.raw.State.Health.Status
479
436
  }));
480
437
  if (progressCallback) progressCallback(serviceHealth);
481
438
  const unhealthy = serviceHealth.filter(service => service.health !== 'healthy');
482
-
483
439
  if (unhealthy.length !== 0) {
484
440
  retry('Timed out waiting for services to be healthy. Unhealthy services: ' + unhealthy.map(s => s.service).join(', '));
485
441
  }
@@ -487,77 +443,67 @@ class Navy extends _eventemitter.EventEmitter2 {
487
443
  } catch (e) {
488
444
  throw new _errors.NavyError(e.message);
489
445
  }
490
-
491
446
  return true;
492
447
  }
448
+
493
449
  /**
494
450
  * Returns the external IP for accessing Docker and running services.
495
451
  * @public
496
452
  */
497
-
498
-
499
453
  async externalIP() {
500
454
  const config = await (0, _config.getConfig)();
501
455
  return await (0, _externalIp.getExternalIP)(config.externalIP);
502
456
  }
457
+
503
458
  /**
504
459
  * Returns the external port for the given service and internal private port.
505
460
  * @public
506
461
  */
507
-
508
-
509
462
  async port(service, privatePort, index = 1) {
510
463
  return await (await this.safeGetDriver()).port(service, privatePort, index);
511
464
  }
465
+
512
466
  /**
513
467
  * Returns the URL for the given service which can be used to access it if it exposes
514
468
  * a HTTP server.
515
469
  * @public
516
470
  */
517
-
518
-
519
471
  async url(service) {
520
472
  const ps = await (await this.safeGetDriver()).ps(service);
521
473
  return (0, _serviceHost.getUrlFromService)(ps.pop()) || (0, _serviceHost.createUrlForService)(service, this.normalisedName, await this.externalIP());
522
474
  }
475
+
523
476
  /**
524
477
  * Returns an array of the names of the launched services for this Navy.
525
478
  * @public
526
479
  */
527
-
528
-
529
480
  async getLaunchedServiceNames() {
530
481
  return await (await this.safeGetDriver()).getLaunchedServiceNames();
531
482
  }
483
+
532
484
  /**
533
485
  * Returns an array of the names of all of the possible services for this Navy.
534
486
  * @public
535
487
  */
536
-
537
-
538
488
  async getAvailableServiceNames() {
539
489
  return await (await this.safeGetDriver()).getAvailableServiceNames();
540
490
  }
541
-
542
491
  }
492
+
543
493
  /**
544
494
  * Returns a `Navy` instance from the given Navy name.
545
495
  * @public
546
496
  */
547
-
548
-
549
497
  exports.Navy = Navy;
550
-
551
498
  function getNavy(navyName) {
552
499
  (0, _invariant.default)(navyName, "NO_NAVY_PROVIDED: No Navy provided");
553
500
  return new Navy(navyName);
554
501
  }
502
+
555
503
  /**
556
504
  * Returns an array of `Navy` instances which are currently imported and launched.
557
505
  * @public
558
506
  */
559
-
560
-
561
507
  async function getLaunchedNavies() {
562
508
  try {
563
509
  const navyNames = await _fs.default.readdirAsync((0, _state.pathToNavys)());
@@ -566,12 +512,11 @@ async function getLaunchedNavies() {
566
512
  return [];
567
513
  }
568
514
  }
515
+
569
516
  /**
570
517
  * Returns an array of names of Navies which are currently imported and launched.
571
518
  * @public
572
519
  */
573
-
574
-
575
520
  async function getLaunchedNavyNames() {
576
521
  try {
577
522
  return await _fs.default.readdirAsync((0, _state.pathToNavys)());
@@ -1,23 +1,17 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.middlewareRunner = middlewareRunner;
9
-
10
8
  var _bluebird = _interopRequireDefault(require("bluebird"));
11
-
12
9
  var _defaultMiddleware = _interopRequireDefault(require("./default-middleware"));
13
-
14
10
  async function middlewareRunner(navy, state) {
15
11
  const driver = await navy.getDriver();
16
-
17
12
  if (!driver) {
18
13
  return;
19
14
  }
20
-
21
15
  await navy.ensurePluginsLoaded();
22
16
  const config = await _bluebird.default.reduce([...(0, _defaultMiddleware.default)(navy), ...navy._registeredMiddleware], async (prevConfig, middlewareFn) => await middlewareFn(prevConfig, state), await driver.getConfig());
23
17
  await driver.writeConfig(config);
@@ -1,38 +1,30 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.loadPlugins = loadPlugins;
9
-
10
8
  var _bluebird = _interopRequireDefault(require("bluebird"));
11
-
12
9
  var _invariant = _interopRequireDefault(require("invariant"));
13
-
14
10
  const resolve = _bluebird.default.promisify(require('resolve'));
15
-
16
11
  async function loadPlugins(navy, navyFile) {
17
12
  const configProvider = await navy.getConfigProvider();
18
13
  (0, _invariant.default)(configProvider, "NO_CONFIG_PROVIDER: Couldn't determine config provider for navy %s", navy.name);
19
14
  const basedir = await configProvider.getNavyPath();
20
-
21
15
  if (!navyFile.plugins) {
22
16
  return [];
23
17
  }
24
-
25
18
  let pluginPaths;
26
-
27
19
  try {
28
20
  pluginPaths = await Promise.all(navyFile.plugins.map(async pluginName => resolve(pluginName, {
29
21
  basedir
30
22
  })));
31
23
  } catch (ex) {
32
24
  (0, _invariant.default)(false, "PLUGIN_RESOLVE_ERR: Could not resolve some of the plugins in Navyfile.js: %s", navyFile.plugins.join(', '));
33
- } // $FlowIgnore: entry point to plugin has to be dynamic
34
-
25
+ }
35
26
 
27
+ // $FlowFixMe[unsupported-syntax]: entry point to plugin has to be dynamic
36
28
  const plugins = pluginPaths.map(pluginPath => require(pluginPath));
37
29
  return plugins.map((Plugin, index) => {
38
30
  (0, _invariant.default)(typeof Plugin === 'function', "PLUGIN_DOESNT_EXPORT_FUNCTION: Plugin %s doesn't export a function as the entrypoint", navyFile.plugins[index]);