Clojure China

[翻译] 通过 ClojureScript 和 Node 运行 CLI app

#1

翻译 CLI app with ClojureScript on Node

首先有安装 clojure, 从而得到 clj 这个命令行程序:

brew install clojure

下载 core.cljs 文件放到正确的目录结构当中:

git clone https://gist.github.com/a6427534ea76cd4e9222a76eb398b289.git inc
cd inc
mkdir -p src/inc
mv core.cljs src/inc

在开发环境:

clj -m cljs.main --repl-env node
cljs.user=> (require '[inc.core])
cljs.user=> (inc.core/handle-input "1")
2
nil
;; change something in src/inc/core.cljs
cljs.user=> (require '[inc.core] :reload)
;; try the change

编译出最终代码:

clj -m cljs.main --target node --output-to inc.js -O simple -c inc.core
chmod +x inc.js

试一下!

$ time echo 1 | ./inc.js
2
echo 1  0.00s user 0.00s system 38% cpu 0.002 total
./inc.js  0.13s user 0.03s system 94% cpu 0.173 total

$ time ./inc.js 1
2
./inc.js 1  0.13s user 0.03s system 96% cpu 0.168 total

$ ./inc.js hello
Not a number: hello

$ echo $?
1

666!

2赞
#2

:+1::heart_eyes:

1赞