• Stars
    star
    50
  • Rank 558,144 (Top 12 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run Elm code from the command line

run-elm

npm Travis branch AppVeyor branch

Run Elm code from the command line

Ever want to quickly try out some Elm code that's too complex for the REPL or too much of a hassle to bootstrap with the Elm architecture and the browser? run-elm has you covered. run-elm allows you to quickly define a module that you can run from the command line!

Prerequisites

run-elm requires Node versions >= 6.

run-elm also expects a current version of the Elm binary installed globally. You can install it with:

npm install -g elm

Installation

$ npm install -g run-elm

Usage

Write some Elm code, ensuring you declare a module name that matches the filename. Then, add an output constant of type String for whatever you want to print to the command line:

module Hello exposing (..)


output : String
output =
    "Hello World!"

Finally, run the code:

$ run-elm Hello.elm
Hello World!

You should see your message printed to the console.

Command Line Arguments

run-elm also supports command line arguments to output as a list of strings.

module Main exposing (output)


output : List String -> String
output args =
    String.join "-" args ++ "!"

Running this:

run-elm Main.elm hello world

Results in this:

hello-world!

run-elm automatically detects whether output is of type String or List String -> String, so passing no extra arguments to the command will simply make args an empty list and will not produce an error. Similarly, if output is String, extra command arguments are simply ignored as redundant.

Customizing Output Name

Specify a custom name for the output constant or function by adding --output-name=myCustomConstantOrFunction to the command:

module Hello exposing (..)


myCustomConstant : String
myCustomConstant =
    "Hello World!"
$ run-elm --output-name=myCustomConstant Hello.elm
Hello World!

Running Modules in Elm Project Subdirectories

By default, run-elm assumes that your Elm module is located in the root of its project directory. This means that Elm searches for elm-package.json and elm-stuff in the directory where the Elm file is and attempts to create these assets if they are missing.

However, this behaviour may be undesired if the Elm module you are trying to run is located in a subdirectory of your project. In this case, you can pass option --project-dir=/path/to/project to run-elm and thus help it pick the correct elm-package.json and elm-stuff. In this case, is important that elm-package.json mentions the subdirectory with your module in "source-directories" field. Otherwise, the module will not be visible from the project directory and the command will fail. See Elm docs for details.

If --project-dir is specified, it has to be one of the parent directories to your file. If the given directory does not contain elm-package.json and elm-stuff, these assets are created automatically. However, the command will likely fail because "source-directories" is set to ["."] by default, which does not include subdirectories.

Setting report format

Passing --report=json to run-elm makes Elm compilation errors in stderr easier to parse. Please note that only the last one or two lines in stderr may be valid JSON strings because there exist additional messages such as Error: Compilation failed.

Using custom elm-make instance

Passing --path-to-elm-make=/path/to/elm-make allows you to choose a custom elm-make instance (e.g. the one installed locally). Otherwise, a globally available elm-make command is used to compile Elm code.

Meta

Typing run-elm --help prints usage instructions, which can be handy for recalling what arguments and options the command accepts.

You can also check what version of run-elm you have installed by typing run-elm --version.

Node Module API

If you are using run-elm in a Node project, you don’t have to spawn a separate process and can use import / require instead.

import runElm from 'run-elm';
// or
// const runElm = require('run-elm').default;

(async () => {
  const { output, debugLog } = await runElm('path/to/Main.elm');
  // output: string
  // debugLog: string[]

  const anotherRunElmResult = await runElm(
    '/path/to/project/subdirectory/Custom.elm',
    {
      outputName: 'customOutput',
      projectDir: '/path/to/project',
      report: 'json',
      pathToElmMake: '/path/to/elm-make'
    }
  );
})();

More Repositories

1

redux-saga-test-plan

Test Redux Saga with an easy plan.
JavaScript
1,244
star
2

revalidate

Elegant and composable validations
JavaScript
363
star
3

chroma

Ruby gem for color manipulation and palette generation
Ruby
231
star
4

redux-saga-router

A router for Redux Saga
JavaScript
154
star
5

marionette.component

Manage and create components for your Marionette.js application
JavaScript
36
star
6

fp-basics-in-es6

Code samples for "Functional Programming Basics in ES6" talk
JavaScript
35
star
7

redux-resource

Redux action creator for managing RESTful resources
JavaScript
32
star
8

combos

Generate all possible permutations of an object's key-value pairs
JavaScript
27
star
9

rise-of-async-js-talk

Code samples, demos, and resources for "The Rise of Async JavaScript" talk
JavaScript
26
star
10

react-bind-closures

Bind closures to stateless React components to avoid creating closures at render time
JavaScript
22
star
11

programming-elm.com

Source for programming-elm.com
Elm
19
star
12

arch-elm

Code for talk "Toward a Better Front-end Architecture: Elm"
Elm
19
star
13

fsm-iterator

A finite state machine iterator for JavaScript
JavaScript
16
star
14

website

Source for my personal website
JavaScript
15
star
15

marionette.polymerview

JavaScript
13
star
16

effective-react-testing

JavaScript
12
star
17

whitesimilarity

Ruby gem implementation of the White Similarity Algorithm
C
12
star
18

building-web-apps-with-elm-tutorial

Elm
11
star
19

elm-workshop

Setup instructions and finished demos for the "Building Web Apps with Elm" workshop
9
star
20

perchance

A simple maybe monad for JavaScript
JavaScript
9
star
21

redux-binary

Redux reducer and actions for handling binary state
JavaScript
9
star
22

elm-stream

Fast and simple stream library for Elm
Elm
8
star
23

react-classify

Classify functional React components to use component lifecycle hooks
JavaScript
8
star
24

dotfiles

Vim Script
7
star
25

redux-glue

Glue together Redux actions to create testable, sequenced actions
JavaScript
6
star
26

public-apis

Simple web app for finding public APIs
JavaScript
6
star
27

building-resilient-api-driven-elm

Elm
5
star
28

photomosaic

Web app for computing photomosaics in the browser
JavaScript
5
star
29

babel-plugin-transform-underscore-arrow-functions

Transform arrow functions with an underscore param into param-less functions
JavaScript
4
star
30

react-revalidate

Validate React component props with revalidate
JavaScript
4
star
31

elm-demos

Example Elm demo apps to complement future Elm blog posts
Elm
3
star
32

conways-game-of-life

Conway's Game of Life implemented in Elm
Elm
3
star
33

migrate-js-to-elm

JavaScript
3
star
34

modular-react-and-redux-talk

Code samples for the "Modular React and Redux" talk
JavaScript
2
star
35

redux-revalidate

Validate your Redux store state with revalidate
JavaScript
2
star
36

meteor-rps

JavaScript
2
star
37

advanced-cypress-testing

JavaScript
2
star
38

_redux-saga-router_5

JavaScript
2
star
39

measurb

Handle units of measurement in Ruby with ease!
Ruby
2
star
40

fp-in-javascript

Many common functional programming patterns succinctly implemented in JavaScript
JavaScript
2
star
41

marionette.attrsync

JavaScript
1
star
42

node-koa-es6

JavaScript
1
star
43

backbone.directattributes

Direct attributes for Backbone models
1
star
44

pat-packet-visits-ruby-rails

Ruby
1
star
45

playit

Control audio players on websites with your keyboard's Play/Pause key
JavaScript
1
star
46

state-side-effects-and-redux

Code samples for "State, Side Effects, and Redux. Oh my!" talk
JavaScript
1
star
47

es7-and-beyond-talk

JavaScript
1
star
48

bundle-with-webpack-talk

Resources for the talk "Bundle the Web with Webpack"
JavaScript
1
star
49

dynamically-sassy-demos

Ruby
1
star
50

orchestrating-apps-angular

JavaScript
1
star