-
Notifications
You must be signed in to change notification settings - Fork 17
命令行使用
Sun,Haohao edited this page Aug 24, 2015
·
3 revisions
lark 命令行是一个为了协助快速开发 lark 项目而创建的项目,您可以通过 lark 命令 快速创建项目、开发热重启、开发测试以及开发完之后打包发布的一整套从创建、开发到部署的方案。
$ npm install lark -g
lark is a tool for managing lark app.
Usage:
lark command [arguments]
The commands are:
-h, --help output usage information
-V, --version output the version number
-e, --environment switch project's environment
n, new create an application base on lark framework
a, api create an api application base on lark framework
g, generate source code generator
destroy destroy the code generated by `generate` command
r, run, server run the lark app in development
s, start run the lark app in production
benchmark profile the project
test run project's unit tests
new 命令是新建一个 Web 项目,我们在命令行下执行 lark new <项目名> 创建一个新的项目。但是注意当前目录下不能存在和项目名相同的文件夹
$ lark new lark-web-demo
$ lark api lark-api-demo
由于 Node.js 的运行机制,每一次我们修改代码,都要去手动重启项目。run 命令会监听文件的变化,当发现文件更新,会自动重启。用来开发时使用。
监控文件的功能尚未实现
$ lark run
start 命令是对 pm2命令的封装,用来在线上使用,会默认开启cluster模式
watch 命令也是对 pm2命令的封装,该命令会通过pm2的watch模式启动app,在这种模式下,app目录下的所有文件被改动都会引起app的重启。
pm2的watch模式用到了nodejs的fs.watch接口,这是一个unstable的借口,在某些平台上无法使用
包含 controller、api、model、pageservice、dataservice
# 根据路由生成对应目录以及 js
$ lark generate controller user/list
# 生成restapi
$ lark generate rest user
# 生成 pageservice
$ lark generate pageservice user
# 生成 model
$ lark generate model user
lark 命令行解析基于 command.js开发。commandjs非常灵活,很方便开发复杂的命令行工具,另外还可以提供自动的 help 命令生成
var program = require('commander');
program
.version('0.0.1')
.option('-C, --chdir <path>', 'change the working directory')
.option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
.option('-T, --no-tests', 'ignore test hook')
program
.command('exec <cmd>')
.alias('ex')
.description('execute the given remote cmd')
.option("-e, --exec_mode <mode>", "Which exec mode to use")
.action(function(cmd, options){
console.log('exec "%s" using %s mode', cmd, options.exec_mode);
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ deploy exec sequential');
console.log(' $ deploy exec async');
console.log();
});
program.parse(process.argv);
为了方便一些文件的操作,还引入了shell.js。shelljs提供了跨平台的shell 命令,可以很方便的开发命令行工具。
require('shelljs/global');
if (!which('git')) {
echo('Sorry, this script requires git');
exit(1);
}
// Copy files to release dir
mkdir('-p', 'out/Release');
cp('-R', 'stuff/*', 'out/Release');