nae client(using node.js to create shell cmd)

28
NAE Client Node.js 编写shell script 郑新林(剪巽)| @literal

Upload: fisher-zheng

Post on 13-Jul-2015

1.118 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Nae client(using Node.js to create shell cmd)

NAE Client用Node.js 编写shell script

郑新林(剪巽)| @literal

Page 2: Nae client(using Node.js to create shell cmd)

NAE (Node App Engine)

Page 3: Nae client(using Node.js to create shell cmd)

NAE (Node App Engine)

Page 4: Nae client(using Node.js to create shell cmd)

NAE 在线编辑器

每个应用需要一个 package.json 文件

.naeignore 文件:控制同步时的排除文件

NAE (Node App Engine)

Page 5: Nae client(using Node.js to create shell cmd)

NAE (Node App Engine)

NAE 上APP有两种模式 : Dev , Online

在线编辑器操作的代码是dev模式下的代码

dev调试之后,需要通过发布才能到online模式

dev : appname.dev.cnodejs.net

online : appname.cnodejs.net

Page 6: Nae client(using Node.js to create shell cmd)

NAE (Node App Engine)

为哈没有shell工具?

我不打算在浏览器里写代码XD !

我控shell !

我是姚明!

Page 7: Nae client(using Node.js to create shell cmd)

NAE Client (design)

作为一个client,我要提供哪些功能呢? (压力好大~)

格式 :

nae cmd appname

cmd:

auth

start [appname]

stop [appname]

restart [appname]

debug [appname]

sdown [appname]

sup [appname]

pub [appname]

update

Page 8: Nae client(using Node.js to create shell cmd)

NAE Client (design)

通信:socket 问答式

协议:json head + binary body

Page 9: Nae client(using Node.js to create shell cmd)

NAE Client (design)

大洋葱文件传输

{cmd:file_add,app:xxx,v:’1.1’,length:123}

010101011010110101010100101011001010101010100101010102

Page 10: Nae client(using Node.js to create shell cmd)

NAE Client (dev)

寻找技术实现方案

npm install read

命令行参数获取

npm install optimist

命令行 用户输入

socket 连接 require(“net”)

文件操作 require(“fs”)

命令行运行 shell / npm package

Page 11: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v1)

第一版: shell + node.js

#!/bin/sh

echo “[Enter Email]:”

read email

if [ ! –z $email ] ;then

fi

.....

xxxx/nae.js argv1 argv2 ....

#!/usr/bin/env node

// javascript

// write more , do more!

$ ln path/nae /usr/local/bin/nae

#!/bin/sh

# TODO 安装、更新用的shell脚本

Page 12: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v1)

第一版 开试

mocha 被上传了,咋整 ?

需要一个控制同步到机制,.naeignore 出场 !!!

Page 13: Nae client(using Node.js to create shell cmd)

NAE Client (dev, v1 )

改进大洋葱

.naeignore 制定同步排除列表

node_module/mocha/*

node_module/xxxx/*

config.json

精简diff 列表,引入xfs模块

Mkdir xxx

Mkdir xxx/xxx => mkdir xxx/xxx

Rmdir xxx/xxx/

Rmdir xxx/ => rmdir xxx

Page 14: Nae client(using Node.js to create shell cmd)

NAE Client (dev, v1 )

Nodeclub , 第一场考验,挂了~

现象:5000k+的文件,sdown死活跑2000个左右挂掉,为什么?

原因:EMFILE 已达到进程可同时打开的文件数上限

结论:For循环遍历文件,获取文件索引,是不安全的,

方案:改成队列! 20个并发

文件错误码说明:

http://club.cnodejs.org/topic/4f181d70817ae4105c003b9c

现象:formidle上传文件,fs.rename(),提示tmp文件不存在,为什么?

PS 另外一个文件操作坑坑

原因:fs.rename () 无法跨磁盘移动文件,报错 文件不存在

结论:给node提 issue(增加了EXDEV错误码)

方案:修改tmp目录 | fs.writeStream()

Page 15: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v1)

兼容了

linux/unix shell

windows git-bash

缺点

安装像原先的 npm install

更新比较麻烦,尤其是windows下

Page 16: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v1)

还行吧

能用了,只是家里win7,没装虚拟机~

呃,什么? 容我想想~

Page 17: Nae client(using Node.js to create shell cmd)

NAE Client

node v0.6.* 开始,npm开始强大起来

npm对平台的支持变好;

从 mocha,jake等模块中得到启发;

于是开始收集资料,准备将client升级成 npm模块

程序还没开始,先占坑 : nae

Page 18: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v2)

$ cd your_module

$ npm init # 初始化,按wizard一部一部

Npm 的使用

{

“name” : “nae”,

“version” : “0.0.1”,

“main” : “index.js”,

“bin” : { “nae” : “bin/nae.js” },

“engines” : “>0.6.6”,

“author” : “NAE”,

“description”: “nae client ,”,

“dependencies” : {}

}

$ npm publish

Page 19: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v2)

捕获用户输入,替代shell的read

$ npm install read

var Read = require(‘read’);

Read({prompt:”Enter Email”},function(err, user){

Read({prompt:”Enter Password,

silent: true},function(err, user){

console.log(“密码明文,么加密~”)

});

});

Page 20: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v2)

处理OS平台差异

// *nix

process.env[‘HOME’]

// windows

process.env[‘HOMEDRIVE’] + process.env[‘HOMEPATH’]

获取用户目录,保存auth信息

文件路径 : \ 和 /

filepath.replace(/\\/g , ‘/’);

Page 21: Nae client(using Node.js to create shell cmd)

NAE Client (dev , v2)

迁移完毕!

除了异步写起来需要异步一下思路,其他很好

可以和“难写”的shell script 说 byebye了

$ npm publish

呃,忘了改帮助文档了,又不想升版本号,咋办咋办?

$ npm unpublish [email protected]

0.0.1版本就这么灰飞烟灭了~

Page 22: Nae client(using Node.js to create shell cmd)

NAE Client (inaugurate)

$ npm install nae

亲,全国包邮

Page 23: Nae client(using Node.js to create shell cmd)

NAE Client (usage)

本地开发,编辑代码 (代码有版本库控制,更加)

$ nae pub

开始使用NAE Client

$ nae auth

$ cd workspace

$ nae sdown app1 # 下载app1,将会在workspace下建立app1/

$ cd app1

$ nae sup ## 上传修改,有排除的文件,则修改.naeignore

$ nae debug ## 连接nae的dev模式,stdout,stderr将被返回,

## app重启,获得调试信息

线上调试

发布代码上线

Page 24: Nae client(using Node.js to create shell cmd)

NAE Client

这个可以有!

哈哈哈哈哈 !

囧死我了~

Page 25: Nae client(using Node.js to create shell cmd)

NAE Client (TODO)

命令改进

$ nae sdown ## 本地目录名称 必须和 appname 相同 ?

$ nae sup ## sup、debug 能否组合?

你的建议?

Page 26: Nae client(using Node.js to create shell cmd)

Node.js : the new shell script

process 提供平台、进程的信息,以及进程的控制

child_process 调用 shell 命令

fs 、http、net 提供文件、网络支持

模块 optimist 提供shell式的参数支持

模块 read 提供输入交互

模块 xfs增强文件操作

模块 log4js 打印漂亮的输出信息

Page 27: Nae client(using Node.js to create shell cmd)

FAQ

Page 28: Nae client(using Node.js to create shell cmd)

Thank you

部分图片来自:

http://sinovision.net/news.php?act=details&news_id=82607

http://golang.org/doc/