GithubHelp home page GithubHelp logo

the-cc-dev / browser-run Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juliangruber/browser-run

0.0 0.0 0.0 172 KB

The easiest way of running code in a browser environment

Makefile 2.38% JavaScript 96.21% HTML 1.41%

browser-run's Introduction

browser-run

The easiest way of running code in a browser environment.

Bundles electronjs by default!

build status downloads

Usage

$ echo "console.log('Hey from ' + location); window.close()" | browser-run
Hey from http://localhost:53227/
$

Or use browser-run programmatically:

var run = require('browser-run');

var browser = run();
browser.pipe(process.stdout);
browser.end('console.log(location); window.close()');

Example with browserify

$ browserify main.js | browser-run

or

var browserify = require('browserify');
var browser = require('browser-run');

browserify('main.js').bundle().pipe(browser()).pipe(process.stdout);

CLI

$ browser-run --help
Run JavaScript in a browser.
Write code to stdin and receive console output on stdout.
Usage: browser-run [OPTIONS]

Options:
  --browser, -b  Browser to use. Always available: electron. Available if installed: chrome, firefox, ie, phantom, safari  [default: "electron"]
  --port         Starts listening on that port and waits for you to open a browser                                       
  --static       Serve static assets from this directory                                                                 
  --mock         Path to code to handle requests for mocking a dynamic back-end                                          
  --input        Input type. Defaults to 'javascript', can be set to 'html'.                                             
  --node         Enable nodejs apis in electron                                                                          
  --basedir      Set this if you need to require node modules in node mode                                               
  --help         Print help 

Custom html file

By using --input html or { input: 'html' } you can provide a custom html file for browser-run to use. Keep in mind though that it always needs to have <script src="/reporter.js"></script> above other script tags so browser-run is able to properly forward your console.logs etc to the terminal.

Dynamic back-end mock

By using --mock mock.js or { mock: 'mock.js'} you can provide a custom server-side implementation and handle all requests that are sent to paths beginning with /mock

mock.js needs to export a function that accepts req and res arguments for handling requests.

Example:

module.exports = function(req,res){
  if (req.url === '/mock/echo') {
    req.pipe(res)
  }
}

API

run([opts])

Returns a duplex stream and starts a webserver.

opts can be:

  • port: If speficied, no browser will be started, so you can point one yourself to http://localhost/<port>
  • browser: Browser to use. Defaults to electron. Available if installed:
    • chrome
    • firefox
    • ie
    • phantom
    • safari
  • static: Serve static files from this directory
  • mock: Path to code to handle requests for mocking a dynamic back-end
  • input: Input type. Defaults to javascript, can be set to html.
  • node: Enable nodejs integration in electron
  • basedir: Set this if you need to require node modules in node mode

If only an empty string is written to it, an error will be thrown as there is nothing to execute.

If you call window.close() inside the script, the browser will exit.

run#stop()

Stop the underlying webserver.

Headless testing

To use the default electron browser on travis, add this to your travis.yml:

addons:
  apt:
    packages:
      - xvfb
install:
  - export DISPLAY=':99.0'
  - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
  - npm install

Source.

For gnu/linux installations without a graphical environment:

$ sudo apt-get install xvfb # or equivalent
$ export DISPLAY=':99.0'
$ Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
$ browser-run ...

There is also an example docker machine here.

Installation

With npm do

$ npm install browser-run    # for library
$ npm install -g browser-run # for cli

Sponsors

This module is proudly supported by my Sponsors!

Do you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my Patreon. Not sure how much of my modules you're using? Try feross/thanks!

License

(MIT)

browser-run's People

Contributors

apaleslimghost avatar btakita avatar greenkeeper[bot] avatar greenkeeperio-bot avatar jcblw avatar juliangruber avatar kumavis avatar naugtur avatar peteruithoven avatar rtsao avatar rubennorte avatar zeke avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.