mails 0.0.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +1 -3
- package/LICENSE +1 -1
- package/README.en.md +32 -36
- package/README.md +46 -49
- package/dist/render.js +42 -0
- package/dist/render.js.map +1 -0
- package/libs/render.js +21 -0
- package/package.json +17 -13
- package/README.sample.md +0 -58
- package/bin/cli +0 -3
- package/cli.js +0 -141
- package/index.js +0 -86
- package/package.sample.json +0 -20
- package/styles/basic.css +0 -77
- package/styles/ink.css +0 -548
- package/styles/mails.css +0 -86
- package/templates/basic.html +0 -171
- package/templates/hero.html +0 -228
- package/templates/layout.html +0 -20
- package/templates/newsletter.html +0 -336
- package/templates/one.html +0 -143
- package/templates/sidebar-hero.html +0 -277
- package/templates/sidebar.html +0 -249
package/.npmignore
CHANGED
package/LICENSE
CHANGED
package/README.en.md
CHANGED
|
@@ -1,75 +1,72 @@
|
|
|
1
1
|
##  mails 
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Send beautiful emails made easy, with responsive templates built in.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Mails supports [ink](http://zurb.com/ink) responsive email template now!
|
|
6
6
|
|
|
7
7
|
### Installation
|
|
8
|
-
|
|
8
|
+
```bash
|
|
9
9
|
$ npm install mails
|
|
10
|
-
|
|
10
|
+
```
|
|
11
11
|
|
|
12
12
|
### Example
|
|
13
|
-
|
|
13
|
+
```js
|
|
14
14
|
var mails = require('mails');
|
|
15
15
|
|
|
16
|
-
//
|
|
16
|
+
// Use built in templates
|
|
17
17
|
mail.render('basic', {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
name: 'mySite',
|
|
19
|
+
banner: 'http://mysite.com/banner.jpg'
|
|
20
20
|
}, function(err,html){
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
if (!err) {
|
|
22
|
+
console.log(html);
|
|
23
|
+
} else {
|
|
24
|
+
console.log(err);
|
|
25
|
+
}
|
|
26
26
|
});
|
|
27
|
-
|
|
27
|
+
```
|
|
28
28
|
### Built-in themes
|
|
29
|
-
|
|
29
|
+
Mails supports several kinds of themes, they are:
|
|
30
30
|
|
|
31
31
|
- ink themes:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
- basic
|
|
33
|
+
- hero
|
|
34
|
+
- newsletter
|
|
35
|
+
- sidebar
|
|
36
|
+
- sidebar-hero
|
|
37
37
|
- mails theme:
|
|
38
|
-
|
|
38
|
+
- one
|
|
39
39
|
|
|
40
40
|
#### built-in theme useage:
|
|
41
|
-
|
|
41
|
+
```js
|
|
42
42
|
var mails = require('mails');
|
|
43
43
|
|
|
44
44
|
mails.render('basic', {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
name: 'mySite',
|
|
46
|
+
banner: 'http://mysite.com/banner.jpg'
|
|
47
47
|
}, function(err, html){
|
|
48
|
-
|
|
48
|
+
// do sth.
|
|
49
49
|
});
|
|
50
|
-
|
|
50
|
+
```
|
|
51
51
|
#### built-in theme variables:
|
|
52
|
-
|
|
52
|
+
Take a look:
|
|
53
53
|
(screenshots coming soon...)
|
|
54
54
|
|
|
55
55
|
### Templates as NPM modules
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
You can also push templates as NPM modules, like this:
|
|
57
|
+
```js
|
|
58
58
|
var mails = require('mails');
|
|
59
59
|
|
|
60
|
-
//
|
|
60
|
+
// Using templates named `single` under module `mails-mailmao`
|
|
61
61
|
mails.render('mails-mailmao/single', {
|
|
62
62
|
name: 'mySite',
|
|
63
63
|
banner: 'http://mysite.com/banner.jpg'
|
|
64
64
|
}, function(err, html){
|
|
65
65
|
// do sth.
|
|
66
66
|
});
|
|
67
|
-
|
|
67
|
+
```
|
|
68
68
|
please feel free to publish your templates to NPM.
|
|
69
69
|
|
|
70
|
-
### API
|
|
71
|
-
check this file: `index.js`
|
|
72
|
-
|
|
73
70
|
### Contributing
|
|
74
71
|
- Fork this repo
|
|
75
72
|
- Clone your repo
|
|
@@ -80,7 +77,7 @@ check this file: `index.js`
|
|
|
80
77
|
- Open a pull request, and enjoy <3
|
|
81
78
|
|
|
82
79
|
### MIT license
|
|
83
|
-
Copyright (c) 2013 turing
|
|
80
|
+
Copyright (c) 2013 turing <o.u.turing@gmail.com>
|
|
84
81
|
|
|
85
82
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
86
83
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -100,7 +97,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
100
97
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
101
98
|
THE SOFTWARE.
|
|
102
99
|
|
|
103
|
-
|
|
104
100
|
---
|
|
105
101
|

|
|
106
102
|
generated using [docor](https://github.com/turingou/docor.git) @ 0.1.0. brought to you by [turingou](https://github.com/turingou)
|
package/README.md
CHANGED
|
@@ -1,68 +1,62 @@
|
|
|
1
|
-
##  mails 
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mails 使发送邮件变得更简单与优雅,提高模板复用率。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
使用 Mails 内建的标准模板 `mails-default` 来发送邮件,或者快速发布自己的邮件主题,Mails 推荐您把邮件主题发布到 NPM
|
|
6
|
+
|
|
7
|
+
Mails 内建的标准邮件模板由 [Ink](http://zurb.com/ink) 改造而来,对二次开发表现友好,也对各个终端的兼容表现优秀。稍作修改,亦可以适配智能手机等小屏幕终端。
|
|
6
8
|
|
|
7
9
|
### 如何安装
|
|
8
|
-
|
|
9
|
-
$ npm install mails
|
|
10
|
-
|
|
10
|
+
```bash
|
|
11
|
+
$ npm install mails --save
|
|
12
|
+
```
|
|
11
13
|
|
|
12
14
|
### 快速指引
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
Mails 包括三个部分:
|
|
15
17
|
|
|
16
|
-
- 一个邮件模板加载器
|
|
18
|
+
- 一个邮件模板加载器
|
|
17
19
|
- 一个支持实时编辑(live reload)的邮件模板设计工具(CLI)
|
|
18
20
|
- 一个快速生成邮件主题项目文件的脚手架工具(CLI)
|
|
19
21
|
|
|
20
|
-
如果你是邮件主题开发者,希望分享自己的主题,或者将他们发布到 NPM
|
|
22
|
+
如果你是邮件主题开发者,希望分享自己的主题,或者将他们发布到 NPM,你可能会想要使用到第二个与第三个工具;
|
|
23
|
+
如果你只是使用 mails 发送邮件的程序员,并不需要了解后两者如何运作,只需了解API的使用方法即可:
|
|
21
24
|
|
|
22
25
|
### 范例代码
|
|
23
26
|
最快上手使用 mails,我们来看一段渲染邮件的代码:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// 使用 basic
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
````
|
|
40
|
-
### 内建邮件主题
|
|
41
|
-
mails 内建邮件主题包括以下几种,各个邮件的变量列表请查阅 `templates/*.html` 各个模板文件:
|
|
42
|
-
|
|
43
|
-
- basic
|
|
44
|
-
- hero
|
|
45
|
-
- newsletter
|
|
46
|
-
- sidebar
|
|
47
|
-
- sidebar-hero
|
|
27
|
+
```js
|
|
28
|
+
import mails from 'mails'
|
|
29
|
+
|
|
30
|
+
// 使用 mails-default/basic 别名渲染邮件模板,并填入对应的变量:
|
|
31
|
+
mails.render('basic', {
|
|
32
|
+
name: 'The First Email To You',
|
|
33
|
+
banner: 'http://mysite.com/banner.jpg'
|
|
34
|
+
}).then(function(html){
|
|
35
|
+
console.log(html) // 得到最终的邮件 html 代码
|
|
36
|
+
}).catch(err => {
|
|
37
|
+
console.log(err)
|
|
38
|
+
})
|
|
39
|
+
```
|
|
48
40
|
|
|
49
41
|
### 加载 NPM 模块作为主题
|
|
50
|
-
你可以将 mails 提供的标准邮件主题进行简单的二次开发,然后发布到
|
|
42
|
+
你可以将 mails 提供的标准邮件主题进行简单的二次开发,然后发布到 NPM 作为您的邮件主题,使他人获益。
|
|
51
43
|
|
|
52
44
|
在这个例子中,我们使用 `mails-flat` 模块中的 `message` 主题进行邮件渲染:
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
```js
|
|
46
|
+
import mails from 'mails'
|
|
47
|
+
|
|
48
|
+
mails.render('mails-flat/message', {})
|
|
49
|
+
.then(function(html) {
|
|
50
|
+
console.log(html)
|
|
51
|
+
})
|
|
52
|
+
```
|
|
55
53
|
|
|
56
|
-
mails.render('mails-flat/message', { ...... }, function(err, html){
|
|
57
|
-
console.log(html);
|
|
58
|
-
});
|
|
59
|
-
````
|
|
60
54
|
#### 如何进行主题的二次开发
|
|
61
55
|
|
|
62
|
-
1. 首先,将 mails 作为全局模块安装:
|
|
56
|
+
1. 首先,将 mails-cli 作为全局模块安装:
|
|
63
57
|
|
|
64
58
|
````
|
|
65
|
-
$
|
|
59
|
+
$ npm install mails-cli -g
|
|
66
60
|
````
|
|
67
61
|
|
|
68
62
|
2. 建立新项目文件夹,初始化邮件主题:
|
|
@@ -76,23 +70,26 @@ $ mails init
|
|
|
76
70
|
|
|
77
71
|
````
|
|
78
72
|
$ vi ./package.json
|
|
79
|
-
$ { "view engine": "swig" }
|
|
73
|
+
$ { "view engine": "swig" } // 比如使用swig作为模板引擎
|
|
80
74
|
````
|
|
81
75
|
|
|
82
76
|
4. 使用 live reload 设计工具进行开发
|
|
83
77
|
|
|
84
78
|
````
|
|
85
|
-
|
|
79
|
+
// ./package.json 也是存放模板变量的文件
|
|
80
|
+
$ mails watch ./package.json [-p] [port]
|
|
81
|
+
````
|
|
82
|
+
使用 mails 提供的 live reload 工具进行开发,程序会监听当前文件夹下文件的变动,包括样式表文件,模板文件和提供模板变量的 json 文件。打开相应的端口服务,指定相应文件的url即可访问,比如:
|
|
83
|
+
````
|
|
84
|
+
$ GET http://localhost:3001/message.html
|
|
86
85
|
````
|
|
87
|
-
|
|
88
86
|
|
|
89
87
|
#### 如何发布主题到 NPM
|
|
90
88
|
在使用 `npm publish` 发布到社区之前,确认以下几项:
|
|
91
89
|
- 确保你的项目中 `package.json` 符合 NPM 社区规范
|
|
92
90
|
- 确保 `package.json` 文件中有 `view engine` 字段并且符合邮件模板引擎
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
查看这个文件: `index.js`
|
|
91
|
+
- 确保填写了相应的依赖模块名
|
|
92
|
+
- 建议在 readme.md 中附上一张模板最终渲染效果的截图
|
|
96
93
|
|
|
97
94
|
### 贡献我的代码
|
|
98
95
|
- Fork this repo
|
|
@@ -104,7 +101,7 @@ $ mails watch ./package.json
|
|
|
104
101
|
- Open a pull request, and enjoy <3
|
|
105
102
|
|
|
106
103
|
### MIT license
|
|
107
|
-
Copyright (c) 2013 turing
|
|
104
|
+
Copyright (c) 2013 turing <o.u.turing@gmail.com>
|
|
108
105
|
|
|
109
106
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
110
107
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
|
8
|
+
|
|
9
|
+
var _juice = require('juice');
|
|
10
|
+
|
|
11
|
+
var _juice2 = _interopRequireDefault(_juice);
|
|
12
|
+
|
|
13
|
+
var _bluebird = require('bluebird');
|
|
14
|
+
|
|
15
|
+
var _bluebird2 = _interopRequireDefault(_bluebird);
|
|
16
|
+
|
|
17
|
+
var _pkghubRender = require('pkghub-render');
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 根据给定的主题名称或者文件名称渲染邮件
|
|
21
|
+
* @example
|
|
22
|
+
* render('mails-flat/message', {...}).then(inlineHTML).catch(err)
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var _pkghubRender2 = _interopRequireDefault(_pkghubRender);
|
|
26
|
+
|
|
27
|
+
exports['default'] = function (template) {
|
|
28
|
+
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
|
29
|
+
|
|
30
|
+
if (template.indexOf('/') === -1) template = 'mails-default/' + template;
|
|
31
|
+
|
|
32
|
+
return (0, _pkghubRender2['default'])(template, data).then(function (html) {
|
|
33
|
+
try {
|
|
34
|
+
return _bluebird2['default'].resolve((0, _juice2['default'])(html));
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return _bluebird2['default'].reject(err);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
module.exports = exports['default'];
|
|
42
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../libs/render.js"],"names":[],"mappings":";;;;;;;;qBAAkB,OAAO;;;;wBACL,UAAU;;;;4BACX,eAAe;;;;;;;;;;qBAOnB,UAAS,QAAQ,EAAa;MAAX,IAAI,yDAAG,EAAE;;AACzC,MAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAC9B,QAAQ,sBAAoB,QAAQ,AAAE,CAAA;;AAExC,SAAO,+BAAO,QAAQ,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,UAAA,IAAI,EAAI;AACzC,QAAI;AACF,aAAO,sBAAQ,OAAO,CAAC,wBAAM,IAAI,CAAC,CAAC,CAAA;KACpC,CAAC,OAAO,GAAG,EAAE;AACZ,aAAO,sBAAQ,MAAM,CAAC,GAAG,CAAC,CAAA;KAC3B;GACF,CAAC,CAAA;CACH","file":"render.js","sourcesContent":["import juice from 'juice'\nimport Promise from 'bluebird'\nimport render from 'pkghub-render'\n\n/**\n * 根据给定的主题名称或者文件名称渲染邮件\n * @example\n * render('mails-flat/message', {...}).then(inlineHTML).catch(err)\n */\nexport default function(template, data = {}) {\n if (template.indexOf('/') === -1)\n template = `mails-default/${template}`\n\n return render(template, data).then(html => {\n try {\n return Promise.resolve(juice(html))\n } catch (err) {\n return Promise.reject(err)\n }\n })\n}"]}
|
package/libs/render.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import juice from 'juice'
|
|
2
|
+
import Promise from 'bluebird'
|
|
3
|
+
import render from 'pkghub-render'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 根据给定的主题名称或者文件名称渲染邮件
|
|
7
|
+
* @example
|
|
8
|
+
* render('mails-flat/message', {...}).then(inlineHTML).catch(err)
|
|
9
|
+
*/
|
|
10
|
+
export default function(template, data = {}) {
|
|
11
|
+
if (template.indexOf('/') === -1)
|
|
12
|
+
template = `mails-default/${template}`
|
|
13
|
+
|
|
14
|
+
return render(template, data).then(html => {
|
|
15
|
+
try {
|
|
16
|
+
return Promise.resolve(juice(html))
|
|
17
|
+
} catch (err) {
|
|
18
|
+
return Promise.reject(err)
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mails",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "
|
|
6
|
-
"author": "turing",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Send beautiful emails made easy with sexy built-in templates",
|
|
5
|
+
"main": "dist/mails.js",
|
|
6
|
+
"author": "turing <o.u.turing@Gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"test": "
|
|
9
|
+
"test": "node_modules/mocha/bin/mocha tests/*.js --require tests/babelhook",
|
|
10
|
+
"build": "node_modules/.bin/babel libs --out-dir dist --source-maps --watch"
|
|
10
11
|
},
|
|
11
|
-
"bin": "./bin/cli",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "https://github.com/turingou/mails.git"
|
|
@@ -22,11 +22,15 @@
|
|
|
22
22
|
"url": "https://github.com/turingou/mails/issues"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
"bluebird": "^2.9.34",
|
|
26
|
+
"juice": "1.3.3",
|
|
27
|
+
"mails-default": "0.0.1",
|
|
28
|
+
"pkghub-render": "0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"babel": "^5.8.3",
|
|
32
|
+
"mails-flat": "0.0.1",
|
|
33
|
+
"mocha": "^2.2.5",
|
|
34
|
+
"should": "^7.0.2"
|
|
31
35
|
}
|
|
32
|
-
}
|
|
36
|
+
}
|
package/README.sample.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
##  mails-themename 
|
|
2
|
-
|
|
3
|
-
another email templates for mails
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
### Installation
|
|
8
|
-
````
|
|
9
|
-
$ npm install mails-themename
|
|
10
|
-
````
|
|
11
|
-
|
|
12
|
-
### Example
|
|
13
|
-
````javascript
|
|
14
|
-
var mails = require('mails');
|
|
15
|
-
|
|
16
|
-
mails.render('mails-themename/message', function(err, html){
|
|
17
|
-
console.log(html);
|
|
18
|
-
});
|
|
19
|
-
````
|
|
20
|
-
|
|
21
|
-
### Theme variables
|
|
22
|
-
|
|
23
|
-
mails-flat using `swig` to create htmls, checkout variables in `message.html` and `package.json`
|
|
24
|
-
|
|
25
|
-
### Contributing
|
|
26
|
-
- Fork this repo
|
|
27
|
-
- Clone your repo
|
|
28
|
-
- Install dependencies
|
|
29
|
-
- Checkout a feature branch
|
|
30
|
-
- Feel free to add your features
|
|
31
|
-
- Make sure your features are fully tested
|
|
32
|
-
- Open a pull request, and enjoy <3
|
|
33
|
-
|
|
34
|
-
### MIT license
|
|
35
|
-
Copyright (c) 2013 turing
|
|
36
|
-
|
|
37
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
38
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
39
|
-
in the Software without restriction, including without limitation the rights
|
|
40
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
41
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
42
|
-
furnished to do so, subject to the following conditions:
|
|
43
|
-
|
|
44
|
-
The above copyright notice and this permission notice shall be included in
|
|
45
|
-
all copies or substantial portions of the Software.
|
|
46
|
-
|
|
47
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
48
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
49
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
50
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
51
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
52
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
53
|
-
THE SOFTWARE.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-

|
|
58
|
-
generated using [docor](https://github.com/turingou/docor.git) @ 0.1.0. brought to you by [turingou](https://github.com/turingou)
|
package/bin/cli
DELETED
package/cli.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
var fs = require('fs'),
|
|
2
|
-
path = require('path'),
|
|
3
|
-
mails = require('./index'),
|
|
4
|
-
Tao = require('tao'),
|
|
5
|
-
optimist = require('optimist'),
|
|
6
|
-
argv = optimist.argv,
|
|
7
|
-
consoler = require('consoler'),
|
|
8
|
-
exeq = require('exeq'),
|
|
9
|
-
sys = require('./package.json');
|
|
10
|
-
|
|
11
|
-
exports.socket = function(port, html) {
|
|
12
|
-
var socket = "<script src=\"http://localhost:" + (port + 1) + "/socket.io/socket.io.js\"></script><script>var socket = io.connect('http://localhost:" + (port + 1) + "');socket.on('updated', function (result) { window.location.reload(); });</script>";
|
|
13
|
-
return html + socket;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
exports.serve = function(dir, params, callback) {
|
|
17
|
-
|
|
18
|
-
// 初始化服务
|
|
19
|
-
var server = new Tao({
|
|
20
|
-
dir: dir,
|
|
21
|
-
port: params.port,
|
|
22
|
-
socket: true
|
|
23
|
-
});
|
|
24
|
-
// 直接访问时渲染页面
|
|
25
|
-
server.use(function(req, res, next) {
|
|
26
|
-
if (req.url === '/') {
|
|
27
|
-
res.end('ok');
|
|
28
|
-
} else {
|
|
29
|
-
next();
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
server.use(function(req, res, next) {
|
|
33
|
-
var file = req._parsedUrl.pathname,
|
|
34
|
-
afterfix = file.substr(file.lastIndexOf('.') + 1);
|
|
35
|
-
fs.exists(path.join(dir, file), function(exist) {
|
|
36
|
-
if (exist) {
|
|
37
|
-
if (afterfix !== 'json') {
|
|
38
|
-
mails._render({
|
|
39
|
-
template: path.join(dir, file),
|
|
40
|
-
data: JSON.parse(fs.readFileSync(params.pkg)),
|
|
41
|
-
engine: {
|
|
42
|
-
name: params.data['view engine'],
|
|
43
|
-
_engine: params.engine
|
|
44
|
-
}
|
|
45
|
-
}, function(err, html) {
|
|
46
|
-
if (!err) {
|
|
47
|
-
res.end(exports.socket(server.configs.port, html));
|
|
48
|
-
} else {
|
|
49
|
-
res.end('render error: ' + err.toString());
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
} else {
|
|
53
|
-
res.end(fs.readFileSync(path.join(dir, file)));
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
res.end('404');
|
|
57
|
-
}
|
|
58
|
-
})
|
|
59
|
-
});
|
|
60
|
-
// 检测到模板变动时刷新页面
|
|
61
|
-
server.watch(function(event, file, stat) {
|
|
62
|
-
if (params.engine && event !== 'removed') {
|
|
63
|
-
server.emit('updated', {
|
|
64
|
-
stat: 'ok',
|
|
65
|
-
file: file
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
server.run();
|
|
70
|
-
return server;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// mails(1)
|
|
74
|
-
exports.cli = function() {
|
|
75
|
-
|
|
76
|
-
var arguments = argv._,
|
|
77
|
-
command = arguments[0],
|
|
78
|
-
pkg = arguments[1],
|
|
79
|
-
dir = process.cwd(),
|
|
80
|
-
self = this;
|
|
81
|
-
|
|
82
|
-
if (command == 'watch') {
|
|
83
|
-
if (pkg) {
|
|
84
|
-
pkg = pkg.toString();
|
|
85
|
-
fs.readFile(path.resolve(dir, pkg), function(err, file) {
|
|
86
|
-
if (!err) {
|
|
87
|
-
try {
|
|
88
|
-
var data = JSON.parse(file);
|
|
89
|
-
if (data['view engine']) {
|
|
90
|
-
try {
|
|
91
|
-
var engine = require(data['view engine']),
|
|
92
|
-
port = argv.p && !isNaN(parseInt(argv.p, 10)) ? parseInt(argv.p, 10) : 3001;
|
|
93
|
-
self.server = exports.serve(dir, {
|
|
94
|
-
port: port,
|
|
95
|
-
engine: engine,
|
|
96
|
-
data: data,
|
|
97
|
-
pkg: path.resolve(dir, pkg)
|
|
98
|
-
});
|
|
99
|
-
consoler.success('Mails is watching: http://localhost:' + port);
|
|
100
|
-
} catch (err) {
|
|
101
|
-
consoler.error('view engine required');
|
|
102
|
-
consoler.error(err);
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
} else {
|
|
106
|
-
consoler.error('view engine required');
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
} catch (err) {
|
|
110
|
-
consoler.error('configs format must be json');
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
} else {
|
|
114
|
-
consoler.log('404', 'configs not found');
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
})
|
|
118
|
-
} else {
|
|
119
|
-
consoler.error('configs required');
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
} else if (command == 'init') {
|
|
123
|
-
var init = exeq([
|
|
124
|
-
'git clone ' + sys.repository.url + ' .',
|
|
125
|
-
'rm -rf .git',
|
|
126
|
-
'rm -rf bin',
|
|
127
|
-
'rm index.js cli.js package.json README.md README.en.md LICENSE',
|
|
128
|
-
'cp ./package.sample.json ./package.json',
|
|
129
|
-
'rm package.sample.json',
|
|
130
|
-
'cp ./README.sample.md ./README.md',
|
|
131
|
-
'rm README.sample.md',
|
|
132
|
-
'npm install'
|
|
133
|
-
]);
|
|
134
|
-
init.on('done', function(count) {
|
|
135
|
-
consoler.success('another mails theme created, enjoy!');
|
|
136
|
-
});
|
|
137
|
-
init.run();
|
|
138
|
-
} else {
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
}
|