• Stars
    star
    261
  • Rank 156,630 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

Straightforward command line tool to setup a development environment for modern JavaScript.

kotatsu

kotatsu is a straightforward CLI tool aiming either at running node.js scripts or serving JavaScript/TypeScript web applications in a modern environment (modules, ES2015, Hot Module Replacement, etc.).

Its goal is to relieve developers from the really heavy stack that we now face on a daily basis when working with modern JavaScript.

The idea is to let developers new to the stack forget about it as long as they can while enabling seasoned developers to setup their environment very fast and to start customizing the stack progressively only when this is really needed.

Typical use cases for kotatsu are hot-reloaded express APIs written in ES2015, hot-reloaded React or deku applications etc. Check the use cases for a quick glance of what can be achieved.

Note that kotatsu currently uses webpack under the hood to perform its magic.

Summary

Installation

Kotatsu can be installed globally (you should avoid this!) or within your node.js project using npm:

# Within your project
npm install --save-dev kotatsu

# Globally:
[sudo] npm install -g kotatsu

Usage

Usage: kotatsu <command> {options} [entry]

Commands:
  cli.js start     Start a node.js script.
  cli.js serve     Serve a client-side application.
  cli.js monitor   Monitor a terminating node.js script.
  cli.js run       Run the given node.js script.
  cli.js build     Build your code for client or server.
  cli.js scaffold  Scaffold some typical boilerplate files.

Options:
  -c, --config            Optional webpack config that will be merged with kotatsu's one (useful if
                          you need specific loaders).
  -d, --devtool           Webpack devtool spec to use to compute source maps.               [string]
  -m, --mount-node        Id of the mount node in the generated HMTL index.[string] [default: "app"]
  -o, --output            Output path (either directory or filename).                       [string]
  -p, --port              Port that the server should listen to.                     [default: 3000]
  -s, --source-maps       Should source maps be computed for easier debugging?
                                                                           [boolean] [default: true]
      --cors              Should the server allow CORS?                    [boolean] [default: true]
      --index             Path to a custom HMTL index file. Will default to `./index.html` if
                          present.                                                          [string]
      --jsx               Does your code uses JSX syntax?                 [boolean] [default: false]
      --pragma            JSX pragma.                                                       [string]
      --sass              Whether to transpile scss files (requires `sass` or `node-sass`).
                                                                          [boolean] [default: false]
      --typescript, --ts  Whether to support TypeScript (requires `typescript`). Enabled by default
                          if target entry has .ts or .tsx extension.      [boolean] [default: false]
      --open              Whether to open your browser on the served application.
                                                                          [boolean] [default: false]
      --presets           Babel presets separated by a comma (example:
                          @babel/preset-stage-2,@babel/preset-react).                       [string]
      --production        Whether to build for production (minify + define).
                                                                          [boolean] [default: false]
      --progress          Should it display the compilation's progress?   [boolean] [default: false]
      --proxy             Proxy information (example: --proxy /api http://localhost:4000)   [string]
      --public            Mounting a path to a public folder (example: --public /data ./src/data).
                          Can be used several times. Works with directories and single files.
                                                                                            [string]
  -r, --html5-routing     Whether to enable HTML5 routing, i.e. redirect every unknown url on the
                          index page to avoid reload issues.              [boolean] [default: false]
      --quiet             Disable logs.                                   [boolean] [default: false]
      --version           Show version number                                              [boolean]
  -h, --help              Show help                                                        [boolean]

Examples:
  kotatsu start script.js                           Launching the given script with HMR.
  kotatsu start -c webpack.config.js script.js      Using a specific webpack config.
  kotatsu start --no-source-maps script.js          Disabling source maps.
  kotatsu start script.js -- --path test.js         Passing arguments to the script.

  kotatsu serve entry.js                            Serving the given app.
  kotatsu serve --jsx entry.jsx                     Serving the given app with JSX code.
  kotatsu serve --port 8000 entry.jsx               Serving the app on a different port.
  kotatsu serve --proxy /api http://localhost:4000  Proxying an API.
  kotatsu serve --public /data ./src/data           Serving local static files.
  kotatsu serve --sass entry.js                     Supporting SASS stylesheets.
  kotatsu serve --typescript entry.ts               Serving a TypeScript app.

  kotatsu build server entry.js -o ./               Build the given server script.
  kotatsu build client --production entry.js -o ./  Build the given client app for production.

  kotatsu scaffold index.html                       Dump a boilerplate html file in stdout.

If this is your first time using kotatsu, you should really check the use cases below to see how it could fit your workflow.

Package config

If you don't want to repeat yourself in your npm scripts know that you can use the kotatsu key of your project's package.json to keep a global configuration for the CLI:

{
  "scripts": {
    "dev": "kotatsu serve ./entry.js"
  },
  "kotatsu": {
    "progress": true,
    "public": ["/data", "./src/data"]
  }
}

TypeScript

kotatsu supports TypeScript out of the box. If you want to use TypeScript in your project, just ensure you have a valid tsconfig.json file and that you have installed the typescript dependencies.

If your entry does not have the .ts or .tsx extension you will need to use the --typescript flag else everything should work automatically.

Style

kotatsu lets you import CSS files out of the box. If you need to import SCSS files, you can use the --sass flag but be sure to install sass (or node-sass) for it to work.

JSON and YAML

JSON and YAML imports are automatically dealt with.

Use cases

Interval

This example does not really serve a real-life purpose but merely shows you how to hot-reload a very simple node.js script.

The idea here is to create a script that will continuously print a required string into the console every 2 seconds:

1. Creating the necessary files

// file: interval.js
var string = require('./string.js');

setInterval(function() {
  console.log(string);
}, 2000);

// Here is the twist: whenever the `string` dependency is updated, we will swap it:
if (module.hot) {
  module.hot.accept('./string.js', function() {
    string = require('./string.js');
  });
}
// file: string.js
module.exports = 'ping';

2. Using kotatsu to start the script

kotatsu start interval.js

Now the script will start and you should see it logging ping into the console every two seconds.

Now edit the string.js file and the script will automatically update and log the new exported value of the file.

3. Let's use the same script in the browser

You would rather run this script in the browser?

kotatsu serve interval.js

Now go to localhost:3000 and you should be able to observe the same kind of results in the console.

Remarks

This example serves another purposes: showing you that kotatsu is meant to be used on long-running scripts such as servers or UIs. If what you need is to code a terminating script, check the monitor command instead.

If you need more information about module.hot and Hot Module Replacement (HMR), go check webpack's docs on the subject.

Express

Let's setup a very simple hot-reloaded express app:

1. Installing necessary dependencies

npm i --save express
npm i --save-dev kotatsu

2. Creating our app

// file: app.js
var express = require('express');

var app = express();

app.get('/', function(req, res) {
  return res.send('Hello World!');
});

module.exports = app;

3. Creating our startup script

// file: start.js
var app = require('./app.js'),
    http = require('http');

var server = http.createServer(app);

server.listen(3000);

if (module.hot) {

  // This will handle HMR and reload the server
  module.hot.accept('./app.js', function() {
    server.removeListener('request', app);
    app = require('./app.js');
    server.on('request', app);
    console.log('Server reloaded!');
  });
}

4. Using kotatsu

Launching our app with HMR so we can work comfortably.

kotatsu start ./start.js

You can now edit the express app live and it will automatically update without having to reload the script.

Deku

1. Installing necessary dependencies

npm i --save deku
npm i --save-dev kotatsu

2. Creating our main component

// file: App.jsx
import {element} from 'deku';

export default function App() {
  return <div>Hello World!</div>;
}

3. Creating our application's entry

// file: main.jsx
import {dom, element} from 'deku';
import InitalApp from './App.jsx';

const mountNode = document.getElementById('app'),
      render = dom.createRenderer(mountNode);

function refresh(Component) {
  render(<Component />);
}
refresh(InitalApp);

// Let's handle our code's updates
if (module.hot) {
  module.hot.accept('./App.jsx', function() {
    const NextApp = require('./App.jsx');
    refresh(NextApp);
  });
}

4. Using kotatsu

Now let's run a server to host our app:

kotatsu serve --jsx --pragma element main.jsx

Note that kotatsu will serve for you a HTML index file looking quite like this:

<!DOCTYPE html>
<html>
  <head>
    <title>kotatsu</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript" src="/build/bundle.js"></script>
  </body>
</html>

If you need a custom one, just use the --index argument.

Now visit localhost:3000 and you are ready to develop.

React

React is a bit more tricky because we need to install a Babel 6 preset (react-hmre) to handle hot-loading.

1. Installing necessary dependencies

npm i --save react react-dom
npm i --save-dev kotatsu babel-preset-react babel-preset-react-hmre

2. Creating our main component

// file: App.jsx
import React, {Component} from 'react';

export default class App extends Component {
  render() {
    return <div>Hello World!</div>;
  }
}

3. Creating our application's entry

// file: main.jsx
import React from 'react';
import {render} from 'react-dom';
import App from './App.jsx';

const mountNode = document.getElementById('app');

render(<App />, mountNode);

4. Using kotatsu

Now let's run a server to host our app:

kotatsu serve --jsx ./main.jsx

Note that kotatsu will serve for you a HTML index file looking quite like this:

<!DOCTYPE html>
<html>
  <head>
    <title>kotatsu</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript" src="/build/bundle.js"></script>
  </body>
</html>

If you need a custom one, just use the --index argument.

Now visit localhost:3000 and you are ready to develop.

Node API

var kotatsu = require('kotatsu');

Every method of the library uses the same configuration object (similar to the CLI arguments):

required

  • entry string: Path towards the entry.

optional

  • args array: array of arguments to pass to the child script.
  • cwd string [process.cwd()]: current working directory.
  • config object: a webpack config object.
  • cors boolean [true]: should the server allow CORS?
  • devtool string: a webpack devtool spec.
  • index string: path of the HTML index file to serve.
  • jsx boolean [false]: should we handle JSX?
  • mountNode string ['app']: id of the mount node in the generated HTML index file.
  • open boolean [false]: Whether to open the app in you web browser as soon as served.
  • output string [.kotatsu]: path of the built file.
  • port integer [3000]: port that the server should listen to.
  • pragma string: custom JSX pragma.
  • presets array: Babel 6 presets to apply.
  • production boolan: Whether to build for production, i.e. minify output and define NODE_ENV as production.
  • progress boolean [false]: should the compiler display a progress bar?
  • proxy array: proxy information.
  • quiet boolean [false]: if true, will disable all console logs.
  • server function: function called with the express app in case you want to apply custom middlewares etc.
  • sourceMaps boolean [true]: should it compute source maps?

start

var watcher = kotatsu.start({
  entry: 'script.js',
  ...
})

serve

var server = kotatsu.serve({
  entry: 'script.js',
  ...
})

monitor

var watcher = kotatsu.monitor({
  entry: 'script.js',
  ...
})

run

kotatsu.run({
  entry: 'script.js',
  ...
});

build

// side can be either 'front' or 'back'
kotatsu.build(side, {
  entry: 'script.js',
  ...
}, callback);

What on earth is a kotatsu?

A kotatsu is a low Japanase table covered by a heavy blanket with an underneath heat source that keeps you warm in the cold season.

Inspiration

kotatsu is widely inspired by the following modules:

License

MIT

More Repositories

1

react-blessed

A react renderer for blessed.
JavaScript
4,440
star
2

baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
JavaScript
3,148
star
3

mnemonist

Curated collection of data structures for the JavaScript/TypeScript language.
JavaScript
2,261
star
4

talisman

Straightforward fuzzy matching, information retrieval and NLP building blocks for JavaScript.
JavaScript
700
star
5

baobab-react

React integration for Baobab.
JavaScript
310
star
6

clj-fuzzy

A handy collection of algorithms dealing with fuzzy strings and phonetics.
Clojure
260
star
7

pandemonium

Typical random-related functions for JavaScript and TypeScript.
JavaScript
149
star
8

obliterator

Higher order iterator library for JavaScript and TypeScript.
JavaScript
53
star
9

mtgnode

A realtime web application for Magic the Gathering.
JavaScript
49
star
10

gexf

Gexf library for JavaScript
JavaScript
34
star
11

decypher

A handful of cypher utilities for Node.js
JavaScript
33
star
12

fog

A fuzzy matching & clustering library for python.
Python
25
star
13

colback

JavaScript asynchronous paradigm shift in the blink of an eye.
JavaScript
17
star
14

dolman

Light express app wrapper to develop an API comfortably.
JavaScript
16
star
15

vimeo-srt

A simplistic jQuery plugin to display srt subtitles along with an embedded vimeo video.
JavaScript
10
star
16

furuikeya

Procedural Haiku generation with Twitter API 1.1
Python
10
star
17

mtgparser

A compilation of handy parsers for popular Magic the Gathering deck formats.
JavaScript
9
star
18

agent-smith

Simple neo4jmyadmin powered by sigma.js
JavaScript
8
star
19

courses

Miscellaneous pages needing to be served.
CSS
8
star
20

takoyaki

Fuzzy clustering interface prototype.
JavaScript
7
star
21

recettes

Livre de recettes en format Markdown.
7
star
22

djax-client

A straightforward services client powered by djax.
JavaScript
7
star
23

ebbe

Collection of typical helper functions for python.
Python
6
star
24

deku-hmr-example

Simple example of HMR setup for deku.
JavaScript
5
star
25

sigma-experiments

Various experiments related to sigma v2.
TypeScript
5
star
26

clj-cmudict

Clojure wrapper for the CMU Pronouncing Dictionary.
Clojure
5
star
27

rhetorical

JavaScript library dealing with figures of speech, word play and literature.
JavaScript
4
star
28

colifrapy

Command Line Framework for Python
Python
4
star
29

yomguithereal.github.io

A blog.
HTML
4
star
30

levenshtein-lte1

A very fast JavaScript implementation of Levenshtein distance for the k <= 1 case.
JavaScript
3
star
31

helpers

Miscellaneous helper functions for JavaScript.
JavaScript
3
star
32

sabretache

A javascript library to discover similarities in html documents.
JavaScript
2
star
33

react-utilities

Some helpful utilities to work with React.
JavaScript
2
star
34

sigma-visualizer

A minimalist sigma usage.
JavaScript
2
star
35

plickle

Create, parse and execute Gherkin-like DSLs quick and easy.
JavaScript
2
star
36

baobab-deku

Simple helpers to use Baobab along with deku
JavaScript
2
star
37

StreamlineD3

Providing developers with a simple way to create dynamic D3.js visualizations with live data.
JavaScript
2
star
38

phylactery

Curated collection of data structures for Python.
Python
2
star
39

eslint-config

Just an eslint config.
JavaScript
1
star
40

npm-gexf-dependencies

Basic tool for converting the results of npm ls --json into a gexf file.
JavaScript
1
star
41

modular

Some files linked to modular synths.
1
star
42

sandcrawler-logger

A logger plugin for sandcrawler.
JavaScript
1
star
43

Yomguithereal

1
star
44

special-agent

Thin wrapper around a compilation of common user agent strings that one can query easily through tags.
JavaScript
1
star
45

python-daj

Read and write data without further ado.
Python
1
star
46

persil

Threadsafe & persistent python collections relying on SQLite.
Python
1
star
47

scuttlebutt-sigma

An attempt at visualizing scuttlebutt networks using sigma.js.
JavaScript
1
star
48

symlarjs

calculate and verify similarity between strings
JavaScript
1
star
49

afscrap

Aufeminin.com scraper for social researches.
JavaScript
1
star