[6a3a178] | 1 | const http = require('http')
|
---|
| 2 | const cfg = require('./config')
|
---|
| 3 | const logger = require('./logger')
|
---|
| 4 | const helper = require('./helper')
|
---|
| 5 |
|
---|
| 6 | exports.stop = function (cliOptionsOrConfig, done) {
|
---|
| 7 | cliOptionsOrConfig = cliOptionsOrConfig || {}
|
---|
| 8 | const log = logger.create('stopper')
|
---|
| 9 | done = helper.isFunction(done) ? done : process.exit
|
---|
| 10 |
|
---|
| 11 | let config
|
---|
| 12 | if (cliOptionsOrConfig instanceof cfg.Config) {
|
---|
| 13 | config = cliOptionsOrConfig
|
---|
| 14 | } else {
|
---|
| 15 | logger.setupFromConfig({
|
---|
| 16 | colors: cliOptionsOrConfig.colors,
|
---|
| 17 | logLevel: cliOptionsOrConfig.logLevel
|
---|
| 18 | })
|
---|
| 19 | const deprecatedCliOptionsMessage =
|
---|
| 20 | 'Passing raw CLI options to `stopper(config, done)` is deprecated. Use ' +
|
---|
| 21 | '`parseConfig(configFilePath, cliOptions, {promiseConfig: true, throwErrors: true})` ' +
|
---|
| 22 | 'to prepare a processed `Config` instance and pass that as the ' +
|
---|
| 23 | '`config` argument instead.'
|
---|
| 24 | log.warn(deprecatedCliOptionsMessage)
|
---|
| 25 | try {
|
---|
| 26 | config = cfg.parseConfig(
|
---|
| 27 | cliOptionsOrConfig.configFile,
|
---|
| 28 | cliOptionsOrConfig,
|
---|
| 29 | {
|
---|
| 30 | promiseConfig: false,
|
---|
| 31 | throwErrors: true
|
---|
| 32 | }
|
---|
| 33 | )
|
---|
| 34 | } catch (parseConfigError) {
|
---|
| 35 | // TODO: change how `done` falls back to exit in next major version
|
---|
| 36 | // SEE: https://github.com/karma-runner/karma/pull/3635#discussion_r565399378
|
---|
| 37 | done(1)
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | const request = http.request({
|
---|
| 42 | hostname: config.hostname,
|
---|
| 43 | path: config.urlRoot + 'stop',
|
---|
| 44 | port: config.port,
|
---|
| 45 | method: 'GET'
|
---|
| 46 | })
|
---|
| 47 |
|
---|
| 48 | request.on('response', function (response) {
|
---|
| 49 | if (response.statusCode === 200) {
|
---|
| 50 | log.info('Server stopped.')
|
---|
| 51 | done(0)
|
---|
| 52 | } else {
|
---|
| 53 | log.error(`Server returned status code: ${response.statusCode}`)
|
---|
| 54 | done(1)
|
---|
| 55 | }
|
---|
| 56 | })
|
---|
| 57 |
|
---|
| 58 | request.on('error', function (e) {
|
---|
| 59 | if (e.code === 'ECONNREFUSED') {
|
---|
| 60 | log.error(`There is no server listening on port ${config.port}`)
|
---|
| 61 | done(1, e.code)
|
---|
| 62 | } else {
|
---|
| 63 | throw e
|
---|
| 64 | }
|
---|
| 65 | })
|
---|
| 66 | request.end()
|
---|
| 67 | }
|
---|