How to use portfinder - 10 common examples

To help you get started, we’ve selected a few portfinder examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SirM2z / vue2-all-test / build / dev-server.js View on Github external
// serve pure static assets
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))

const uri = config.dev.openBrowserUriHost === 'http://localhost:'? config.dev.openBrowserUriHost + port : config.dev.openBrowserUriHost

var _resolve
var _reject
var readyPromise = new Promise((resolve, reject) => {
  _resolve = resolve
  _reject = reject
})

var server
var portfinder = require('portfinder')
portfinder.basePort = port

console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
  portfinder.getPort((err, port) => {
    if (err) {
      _reject(err)
    }
    process.env.PORT = port
    var uri = config.dev.openBrowserUriHost === 'http://localhost:'? config.dev.openBrowserUriHost + port : config.dev.openBrowserUriHost
      console.log('> Listening at ' + uri + '\n')
      // when env is testing, don't need open it
      if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
        opn(uri)
      }
      server = app.listen(port)
      _resolve()
github urbanairship / frock / tests / api-integration / index.js View on Github external
test(`setup ${__filename}`, t => {
  t.plan(1)

  frockfile = JSON.parse(fs.readFileSync(path.join(__dirname, 'frockfile.json')))

  portfinder.getPorts(3, (err, foundPorts) => {
    if (err) {
      t.fail(err)
    }

    ports = foundPorts

    // mangle the found ports into the loaded frockfile
    frockfile.servers[0].port = ports[0]
    frockfile.servers[1].port = ports[1]
    frockfile.sockets[0].port = ports[2]

    frock = lib(frockfile, {pwd})

    frock.run(err => {
      if (err) {
        t.fail(err)
github libp2p / js-libp2p-switch / test / circuit-relay.node.js View on Github external
before((done) => createInfos(5, (err, infos) => {
      expect(err).to.not.exist()

      getPorts(6, (err, ports) => {
        expect(err).to.not.exist()

        bootstrapPeer = infos[0]
        tcpPeer1 = infos[1]
        tcpPeer2 = infos[2]
        wsPeer1 = infos[3]
        wsPeer2 = infos[4]

        // Setup the addresses of our nodes
        bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
        bootstrapPeer.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
        tcpPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
        tcpPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}`)
        wsPeer1.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
        wsPeer2.multiaddrs.add(`/ip4/0.0.0.0/tcp/${ports.shift()}/ws`)
github yyssc / ssc-grid / docs / dev-run.js View on Github external
/* eslint-disable no-console, no-process-exit */

import 'colors';
import portfinder from 'portfinder';
import { exec } from 'child-process-promise';
// import ip from 'ip';

portfinder.basePort = 4000;

const SIGINT = 'SIGINT';
let processMap = {};

function output(prefix, message) {
  let formattedMessage = message.toString().trim().split('\n')
    .reduce((acc, line) => `${acc}${ acc !== '' ? '\n' : '' }${prefix} ${line}`, '');

  console.log(formattedMessage);
}

function listen({stdout, stderr}, name) {
  stdout.on('data', data => output(`[${name}] `.grey, data));
  stderr.on('data', data => output(`[${name}] `.grey, data));
}
github yyssc / ssc-grid / docs / dev-run.js View on Github external
return;
    })
    .then(() => console.log('Shutdown: '.cyan + name.green))
    .catch(err => {
      if (catchExec(name, err)) {
        // Restart if not explicitly shutdown
        runCmd(name, cmd, options);
      }
    });
}

console.log('Starting docs in Development mode'.cyan);

process.on(SIGINT, shutdown);

portfinder.getPorts(2, {}, (portFinderErr, [docsPort, webpackPort]) => {
  if (portFinderErr) {
    console.log('Failed to acquire ports'.red);
    process.exit(1);
  }

  // runCmd('webpack-dev-server', `nodemon --watch webpack --watch webpack.docs.js --exec webpack-dev-server -- --config webpack.docs.js --color --port ${webpackPort} --debug --hot --host ${ip.address()}`);
  runCmd('webpack-dev-server', `nodemon --watch webpack --watch webpack.docs.js --exec webpack-dev-server -- --config webpack.docs.js --color --port ${webpackPort} --debug --hot --host 127.0.0.1`);

  runCmd('docs-server', 'nodemon --watch docs --watch src --exec babel-node docs/server.js', {
    env: {
      PORT: docsPort,
      WEBPACK_DEV_PORT: webpackPort,
      ...process.env
    }
  });
});
github react-bootstrap / react-bootstrap / docs / dev-run.js View on Github external
processMap[name] = childProcess;
    })
    .then(() => console.log('Shutdown: '.cyan + name.green))
    .catch((err) => {
      if (catchExec(name, err)) {
        // Restart if not explicitly shutdown
        runCmd(name, cmd, options);
      }
    });
}

console.log('Starting docs in Development mode'.cyan);

process.on(SIGINT, shutdown);

portfinder.getPorts(2, {}, (portFinderErr, [docsPort, webpackPort]) => {
  if (portFinderErr) {
    console.log('Failed to acquire ports'.red);
    process.exit(1);
  }

  runCmd('webpack-dev-server', `nodemon --watch webpack --watch webpack.docs.js --exec webpack-dev-server -- --config webpack.docs.js --color --port ${webpackPort} --debug --hot --host ${ip.address()}`);

  runCmd('docs-server', 'nodemon --watch docs --watch src --exec babel-node docs/server.js', {
    env: {
      PORT: docsPort,
      WEBPACK_DEV_PORT: webpackPort,
      ...process.env,
    },
  });
});
github jake / screenhole-web / build / dev-server.js View on Github external
// serve pure static assets
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))

const uri = 'http://localhost:' + port

var _resolve
var _reject
var readyPromise = new Promise((resolve, reject) => {
  _resolve = resolve
  _reject = reject
})

var server
var portfinder = require('portfinder')
portfinder.basePort = port

console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
  portfinder.getPort((err, port) => {
    if (err) {
      _reject(err)
    }
    process.env.PORT = port
    var uri = 'http://localhost:' + port
    console.log('> Listening at ' + uri + '\n')
    // when env is testing, don't need open it
    if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
      opn(uri)
    }
    server = app.listen(port)
    _resolve()
github wxsms / blog / build / dev-server.js View on Github external
// serve static posts
app.use('/posts', express.static(__dirname + '/../dist/posts/'))
app.use('/feed.xml', express.static(__dirname + '/../dist/feed.xml'))

const uri = 'http://localhost:' + port

var _resolve
var _reject
var readyPromise = new Promise((resolve, reject) => {
  _resolve = resolve
  _reject = reject
})

var server
var portfinder = require('portfinder')
portfinder.basePort = port

console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
  portfinder.getPort((err, port) => {
    if (err) {
      _reject(err)
    }
    process.env.PORT = port
    var uri = 'http://localhost:' + port
    console.log('> Listening at ' + uri + '\n')
    // when env is testing, don't need open it
    if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
      opn(uri)
    }
    server = app.listen(port)
    _resolve()
github YaoZeyuan / vue-multi-page / build / dev-server.js View on Github external
// 直接打开最新添加的项目,方便调试
let projectList = config.projectConfig.project
let lastedProjectIndex = Object.keys(projectList).length - 1
let lastedProjectUri = uri + '/' + config.projectConfig.staticRoot + '/html/' + Object.keys(projectList)[lastedProjectIndex] + '/index.html'
console.log(`lasted_project_uri => ${lastedProjectUri}`)

var _resolve
var _reject
var readyPromise = new Promise((resolve, reject) => {
  _resolve = resolve
  _reject = reject
})

var server
var portfinder = require('portfinder')
portfinder.basePort = port

console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
  portfinder.getPort((err, port) => {
    if (err) {
      _reject(err)
    }
    process.env.PORT = port
    console.log('> Listening at ' + uri + '\n')
    // when env is testing, don't need open it
    if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
      opn(lastedProjectUri)
    }
    server = app.listen(port)
    _resolve()
  })
github KuangPF / vue-cli-analysis / packages / @vue / cli-service / lib / commands / serve.js View on Github external
}

    // entry arg
    const entry = args._[0]
    if (entry) {
      webpackConfig.entry = {
        app: api.resolve(entry)
      }
    }

    // resolve server options
    const useHttps = args.https || projectDevServerOptions.https || defaults.https
    const protocol = useHttps ? 'https' : 'http'
    const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
    portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
    const port = await portfinder.getPortPromise()
    const rawPublicUrl = args.public || projectDevServerOptions.public
    const publicUrl = rawPublicUrl
      ? /^[a-zA-Z]+:\/\//.test(rawPublicUrl)
        ? rawPublicUrl
        : `${protocol}://${rawPublicUrl}`
      : null

    const urls = prepareURLs(
      protocol,
      host,
      port,
      isAbsoluteUrl(options.baseUrl) ? '/' : options.baseUrl
    )

    const proxySettings = prepareProxy(
      projectDevServerOptions.proxy,

portfinder

A simple tool to find an open port on the current machine

MIT
Latest version published 2 years ago

Package Health Score

74 / 100
Full package analysis