|
| 1 | +/* |
| 2 | +Copyright 2019 Javier Brea |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| 9 | +*/ |
| 10 | + |
| 11 | +"use strict"; |
| 12 | + |
| 13 | +// TODO, deprecate this programmatic initialization. Use Core instead. |
| 14 | + |
| 15 | +const Core = require("./core/Core"); |
| 16 | +const AdminApi = require("./api/Api"); |
| 17 | +const InquirerCli = require("./cli/Cli"); |
| 18 | + |
| 19 | +class ProgrammaticCli { |
| 20 | + constructor(options = {}) { |
| 21 | + const createInquirerCli = core => { |
| 22 | + this._inquirerCli = new InquirerCli(core); |
| 23 | + return this._inquirerCli; |
| 24 | + }; |
| 25 | + |
| 26 | + this._core = new Core({ |
| 27 | + onlyProgrammaticOptions: true, |
| 28 | + plugins: [AdminApi, createInquirerCli] |
| 29 | + }); |
| 30 | + this._options = { ...options }; |
| 31 | + this._cliStarted = false; |
| 32 | + this._coreStarted = false; |
| 33 | + this._core.tracer.warn( |
| 34 | + "Deprecation warning: Cli constructor will be deprecated. Use @mocks-server/core instead" |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + async _startCore(cliEnabled) { |
| 39 | + if (!this._coreStarted) { |
| 40 | + this._coreStarted = true; |
| 41 | + await this._core.init(this._options); |
| 42 | + this._core.settings.set("cli", cliEnabled); |
| 43 | + return this._core.start(); |
| 44 | + } |
| 45 | + return Promise.resolve(); |
| 46 | + } |
| 47 | + |
| 48 | + async start() { |
| 49 | + await this._startCore(true); |
| 50 | + if (!this._cliStarted && !this._core.settings.get("cli")) { |
| 51 | + this._core.settings.set("cli", true); |
| 52 | + this._cliStarted = true; |
| 53 | + return this._inquirerCli.start(); |
| 54 | + } |
| 55 | + return Promise.resolve(); |
| 56 | + } |
| 57 | + |
| 58 | + async initServer() { |
| 59 | + return this._startCore(false); |
| 60 | + } |
| 61 | + |
| 62 | + stopListeningServerWatch() { |
| 63 | + return this._inquirerCli.stopListeningServerWatch(); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +module.exports = ProgrammaticCli; |
0 commit comments