-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
159 lines (134 loc) · 3.43 KB
/
Gruntfile.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// see: https://gruntjs.com/sample-gruntfile
module.exports = function(grunt) {
var config = {
pkg: grunt.file.readJSON('package.json'),
pathConfig: {
raw: 'raw',
posts: 'source/_posts',
},
clean: {
posts: {
src: ['<%= pathConfig.posts %>/'],
},
},
copy: {
main: {
files: [{
expand: true,
cwd: '<%= pathConfig.raw %>',
src: '**/*.md',
dest: '<%= pathConfig.posts %>',
flatten: true,
filter: function(filepath) {
// var patterns = ['---\ntitle:'];
var patterns = ['^---$'];
var matchRegex = function(filepath, patterns) {
var content = grunt.file.read(filepath);
return patterns.some(function(pattern){
var regex = new RegExp(pattern, 'm');
// var regex = new RegExp(pattern);
return regex.test(content);
});
};
return matchRegex(filepath, patterns);
},
}],
},
},
watch: {
raw: {
files: ['<%= pathConfig.raw %>/**/*.md'],
tasks: ['copy:main'],
options: {
// spawn: false,
},
},
},
bgShell: {
hexoServer: {
cmd: 'hexo server',
bg: true,
},
},
shell: {
gitClone: {
command: 'git clone [email protected]:qw8880000/JustBook.git <%= pathConfig.raw %>/JustBook'
},
gitPull: {
command: 'git pull'
},
gitPullRaw: {
command: 'cd ./raw/JustBook && git pull'
},
gitPush: {
command: 'git add ./ && git ci -m "new post" && git push'
},
gitPushRaw: {
command: 'cd ./raw/JustBook && git add ./ && git ci -m "new post" && git push'
},
hexoGenerate: {
command: 'hexo g',
},
hexoClean: {
command: 'hexo clean',
},
},
rewrite: {
abbrlink: {
src: '<%= pathConfig.raw %>/**/*.md',
editor: function(contents, filepath){
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(contents);
var hashValue = hash.digest('hex');
return contents.replace(/abbrlink: @@abbrlink/g, "abbrlink: " + hashValue.substring(0, 16));
}
},
},
zip: {
dist: {
src: [
'public/**/*',
],
dest: 'blog.zip'
}
}
};
grunt.initConfig(config);
// output some log when watched files are modified
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
});
// load all grunt tasks matching the ['grunt-*', '@*/grunt-*'] patterns
// so we don't need to excute 'grunt.loadNpmTasks('grunt-*')'
require('load-grunt-tasks')(grunt);
grunt.registerTask('init', [
'shell:gitClone'
]);
grunt.registerTask('push', [
'shell:gitPush',
'shell:gitPushRaw',
]);
grunt.registerTask('pull', [
'shell:gitPull',
'shell:gitPullRaw',
]);
grunt.registerTask('RawToPosts', [
'shell:gitPullRaw',
'rewrite:abbrlink',
'copy:main',
]);
grunt.registerTask('default', [
'clean:posts',
'RawToPosts',
'bgShell:hexoServer',
'watch',
]);
grunt.registerTask('build', [
'clean:posts',
'RawToPosts',
'shell:hexoClean',
'shell:hexoGenerate',
'zip:dist'
]);
};