-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin
executable file
·139 lines (115 loc) · 3.68 KB
/
bin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
var core = require('canner-core');
var genghis = require('genghis');
var argv = require('minimist')(process.argv.slice(2));
var fs = require('fs-extra');
var path = require('path');
var root = process.cwd();
var load = argv._[0];
var date = argv._[1];
var time = argv._[2];
var project = argv._[3];
var w = argv.w;
var twstatBlog = {};
twstatBlog.build = function(load, w) {
if(load)
var root = path.resolve(process.cwd(), load);
var blogPage = require(path.join(root, 'page.js'));
var blogPosts = require(path.join(root, 'blog.json'));
var blog_setting_path = path.join(root, 'blog.json');
var post_path = path.resolve(blogPosts.post_settings.path);
if(!w) {
// clear blog posts
core.build(blogPage, {
cwd: root,
output: root
})
.then(function() {
genghis.build(blog_setting_path)
.catch(function(err) {
console.error(err)
})
.done();
})
.catch(function(err) {
console.error(err);
})
.done();
}else {
// clear blog posts
fs.removeSync(post_path);
return core.watch(blogPage, {
cwd: root,
serve: root,
output: root,
reloader: function() {
return blogPage;
},
changeFilter: function (row, f, curr, prev) {
if(path.extname(f) === '.html') {
return (f === path.resolve(root, row.layout));
}else if (path.extname(f) === '.md') {
return (f === path.resolve(root, row.data.content));
}
}
})
.then(function() {
return genghis.build(blog_setting_path, true)
.catch(function(err) {
console.error(err);
})
.done();
})
.catch(function(err) {
console.error(err);
})
}
}
twstatBlog.init = function(load, date, time, project) {
var date_reg = /(\d{4}\/\d{2}\/\d{2})/g;
var time_reg = /(\d{2}):(\d{2}):(\d{2})/g;
var parse_d = date_reg.exec(date);
var parse_t = time_reg.exec(time);
if(parse_d && parse_t) {
var list_path = path.join(process.cwd(), 'blog.json')
var lists = require(list_path);
lists.posts.unshift({
"title": project,
"date": date + " " + time,
"categories": [""],
"tags": [""],
"cover": "/assets/article_images/" + date + "/" + project + "-cover.png",
"content": "./posts/" + date + "-" + project + ".md",
"url_name": project,
"author": "taiwanstat"
})
var new_file = path.join(process.cwd(), "./posts/" + date.replace(/\//g, '-') + "-" + project + ".md");
if(!fs.existsSync(new_file))
fs.copy(path.join(__dirname, 'init.md'), new_file, function(err) {if(err) return console.error(err)})
console.log('Init your project: ' + new_file);
fs.writeFileSync(list_path, JSON.stringify(lists, null, 2))
console.log('Add your project to blog.json: ' + list_path)
}else if(!parse_d && parse_t) {
throw new Error('Please check your date strings, format should be like "2015/08/03"')
}else if(parse_d && !parse_t) {
throw new Error('Please check your time strings, format should be like "22:07:39"')
}else if(!parse_d && !parse_t) {
throw new Error('Please check your date strings, format should be like "2015/08/03", and time should be like "22:07:39"')
}
}
if(load === 'init' && date && time && project) {
// init project
twstatBlog.init(load, date, time, project)
}else if (load === 'init' && (!project || !date || !time)) {
throw new Error("Usage: twstatBlog init <YYYY/MM/DD> <hh:mm:ss> <project name>");
}else {
if(load && w){
twstatBlog.build(load, w);
}else if(load && !w){
twstatBlog.build(load);
}else if(!load && w) {
twstatBlog.build('./', w)
}else if(!load && !w) {
twstatBlog.build('./')
}
}