Skip to content

Commit

Permalink
add cli for mmstate
Browse files Browse the repository at this point in the history
  • Loading branch information
qianjun.yang committed May 10, 2016
1 parent 9633235 commit fc823ed
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 916 deletions.
103 changes: 70 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
## mmState CLI && demo list

### CLI

```
npm install mmstate #安装
mmstate new projectName #创建一个默认使用avalon 1.7的mmState项目
mmstate new -h
-v, --avalonVersion [avalonVersion] avalon的版本
-l, --loader [loader] 打包方式,requirejs or webpack,avalon 2只能用webpack打包
cd projectName
mmstate serve #启动服务器
mmstate update #更新模板库,linux上可能需要 suo
mmstate build #编译
```


### 例子



** 需要服务器环境才能跑起来【commonjs need fekit】

1,打开页面看效果
Expand All @@ -12,43 +39,53 @@

requirejs和webpack版本都支持打包以及按需加载

###requirejs打包:r.js
###requirejs打包:gulpfile.js

build.js配置
build task配置

```
({
appDir: "www",
baseUrl: "script/",
dir: "build",
map: {
"*": {
"css": "require-css/css",
"avalon": "empty:" // 不打包avalon
}
},
skipDirOptimize: true, // 只处理modules的配置
optimizeCss: "none",
//separateCSS: true,
//buildCSS: false,
modules: [
{
name: "common"
},
{
name: "pages/stateBlog",
exclude: ["text"]
},
{
name: "pages/stateDetail",
exclude: ["text"]
gulp.task('build', function() {
var requirejs = require('requirejs');
var config = {
appDir: "www",
baseUrl: "script/",
dir: "build",
map: {
"*": {
"css": "require-css/css",
"text": "lib/text",
"avalon": "empty:" // 不打包avalon
}
},
{
name: "pages/stateList",
exclude: ["text"]
}
]
})
skipDirOptimize: true, // 只处理modules的配置
optimizeCss: "none",
//separateCSS: true,
//buildCSS: false,
modules: [
{
name: "common"
},
{
name: "pages/stateBlog",
exclude: ["text"]
},
{
name: "pages/stateDetail",
exclude: ["text"]
},
{
name: "pages/stateList",
exclude: ["text"]
}
]
};
requirejs.optimize(config, function (buildResponse) {
var contents = fs.readFileSync(config.out, 'utf8');
}, function(err) {
});
});
```


Expand Down
84 changes: 66 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var program = require('commander'),
http = require('https'),
which = require('os').tmpdir().indexOf('\\') != -1 ? 'where' : 'which',
tpldir = __dirname + '/__tpldir',
infoPath= tpldir + '/__info.json'
infoPath= tpldir + '/__info.json',
open = commandChecker('open', !'show no log')


function error(msg) {
Expand Down Expand Up @@ -36,13 +37,13 @@ function loadInfo() {
return false
}

function commandChecker(cmd) {
function commandChecker(cmd, printLog) {
try {
child_process.execSync(which + ' ' + cmd).toString()
success(cmd + '已安装')
if (printLog !== false) success(cmd + '已安装')
return true
} catch(e) {
error(cmd + ' is required, run "npm install -g ' + cmd + '"')
if (printLog !== false) error(cmd + ' is required, run "npm install -g ' + cmd + '"')
return false
}
}
Expand Down Expand Up @@ -106,24 +107,71 @@ program

program
.command('serve')
// .option('-p, --port [port]', '端口,默认8080,使用gulp-dev-server或者webpack-dev-server该项配置无效')
.description('serve 启动服务')
.action(function (options) {
var cwd = process.cwd(),
__info = cwd + '/__info.json'
try {
__info = fs.readJsonSync(__info)
if (__info.loader !== 'requirejs') {
var ps = child_process.spawn('gulp', ['run-webpack'])
ps.stdout.on('data', function (data) {
log(data.toString());
});

ps.stderr.on('data', function (data) {
error(data.toString());
});
__info = cwd + '/__info.json',
options = options || {},
port = options.port || 8080
if (commandChecker('gulp')) {
try {
__info = fs.readJsonSync(__info)
var args = ['gulp', ['webserver']],
url = 'http://localhost:' + port
// if (__info.loader == 'requirejs') {
// if (commandChecker('python', !'show no log')) {
// success('检测到python,使用python启动服务,端口:' + port)
// args = ['python', ['-m', 'SimpleHTTPServer', port]]
// } else {
// try {
// var express = require('express'),
// app = express()
// success('检测到express,使用express启动服务,端口:' + port)
// app.use(express.static(cwd + '/www'))
// app.listen(port)
// args = null
// } catch(e) {
// return error('检测到系统未安装python或者express,无法启动服务')
// }
// }
// }
if (args) {
var ps = child_process.spawn(args[0], args[1], {cwd: cwd + '/www'})
ps.stdout.on('data', function (data) {
log(data.toString())
})
ps.stderr.on('data', function (data) {
var msg = data.toString()
if (msg.match(/HTTP\/[0-9]\.[0-9]" [0-9]{3}/g)) {
log(msg)
} else {
error(msg)
}
})
}
} catch(e) {
return error('启动失败' + error)
}
} catch(e) {
return error('启动失败' + error)
}
})

program
.command('build')
.option('-t, --task [task]', 'gulp任务名,默认是build')
.description('build 编译项目')
.action(function(options) {
options = options || {}
var task = options.task || 'build',
cwd = process.cwd()
if (commandChecker('gulp')) {
var ps = child_process.spawn('gulp', [task], {cwd: cwd})
ps.stdout.on('data', function (data) {
log(data.toString())
})
ps.stderr.on('data', function (data) {
error(data.toString())
})
}
})

Expand Down
32 changes: 0 additions & 32 deletions requirejs/build.js

This file was deleted.

55 changes: 55 additions & 0 deletions requirejs/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var gulp = require('gulp');
var webserver = require('gulp-webserver');

gulp.task('webserver', function() {
gulp
.src('./www')
.pipe(webserver({
livereload: true,
port: 8080,
open: true
}));
});

gulp.task('build', function() {
var requirejs = require('requirejs');

var config = {
appDir: "www",
baseUrl: "script/",
dir: "build",
map: {
"*": {
"css": "require-css/css",
"text": "lib/text",
"avalon": "empty:" // 不打包avalon
}
},
skipDirOptimize: true, // 只处理modules的配置
optimizeCss: "none",
//separateCSS: true,
//buildCSS: false,
modules: [
{
name: "common"
},
{
name: "pages/stateBlog",
exclude: ["text"]
},
{
name: "pages/stateDetail",
exclude: ["text"]
},
{
name: "pages/stateList",
exclude: ["text"]
}
]
};

requirejs.optimize(config, function (buildResponse) {
var contents = fs.readFileSync(config.out, 'utf8');
}, function(err) {
});
});
3 changes: 3 additions & 0 deletions requirejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "avalon app base on mmState",
"main": "main.js",
"dependencies": {
"gulp": "^3.9.1",
"gulp-webserver": "^0.9.1",
"requirejs": "^2.2.0"
},
"devDependencies": {},
"scripts": {
Expand Down
10 changes: 0 additions & 10 deletions requirejs/require-css/LICENSE

This file was deleted.

Loading

0 comments on commit fc823ed

Please sign in to comment.