-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
157 lines (141 loc) Β· 3.41 KB
/
gulpfile.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
var gulp = require('gulp');
var webpack = require('webpack-stream');
var uglify = require('gulp-uglify');
var path = require('path');
var webshot = require('gulp-webshot');
var fs = require('fs-extra');
var gutil = require("gulp-util");
var env = require('./src/env.js');
var webpackConfig = {
plugins: [
new webpack.webpack.DefinePlugin((function(){
var rt = {};
Object.keys(process.env).map(function(key){
rt['process.env.'+key] = '"'+process.env[key]+'"';
})
return rt;
})())
],
module: {
loaders: [
{
test: /\.css$/, loader: "raw"
},
{
test: /\.txt$/, loader: "raw"
},
{
test: /\.mustache$/, loader: "raw"
},
{
test: /.js?$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
output: {
filename: 'textbook.js',
library: 'Textbook',
libraryTarget: 'umd'
},
resolve: {
root: path.resolve('./src')
}
};
gulp.task('tweet',function(done){
var Twit = require('twit')
if(env.twitterConsumerKey){
var T = new Twit({
consumer_key: env.twitterConsumerKey,
consumer_secret: env.twitterConsumerSecret,
access_token: env.twitterAccess,
access_token_secret: env.twitterSecret
});
var b64content = fs.readFileSync('build/book.jpg', { encoding: 'base64' });
T.post('media/upload', { media_data: b64content }, function (err, data, response) {
var params = { status: '', media_ids: [data.media_id_string] }
T.post('statuses/update', params, function (err, data, response) {
if(err) {
throw new gutil.PluginError({
plugin: 'tweet',
message: err
});
}
else {
done()
}
});
});
} else {
throw new gutil.PluginError({
plugin: 'tweet',
message: 'Environment is undefined'
});
}
})
gulp.task('webshot',function(done){
var webshot = require('webshot');
var options = {
renderDelay: 20000,
siteType: 'file',
onLoadFinished: function(){
for(var k in window.Textbook.textbooks[0]) {
console.log(k.toUpperCase()+' - '+JSON.stringify(window.Textbook.textbooks[0][k]))
}
},
onConsoleMessage: function(text){
gutil.log(text);
},
phantomPath: require('phantomjs2').path,
phantomConfig: {
'local-to-remote-url-access':'true',
'web-security':'false'
},
userAgent: 'Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3',
errorIfJSException: true,
quality: 75,
screenSize: {
width: 800,
height: 1100
}
};
webshot('build/index.html', 'build/book.jpg', options, function(err) {
if(err) {
console.error(err);
}
else {
done();
}
});
});
gulp.task('_makefiles',function(done){
fs.removeSync('build');
fs.mkdirsSync('build');
fs.copySync('src/template/index.html','build/index.html');
var data = fs.readFileSync('build/index.html');
data = data.toString();
data = data.replace('{{base}}','file://'+__dirname+'/build/index.html');
fs.writeFileSync('build/index.html', data);
done();
});
gulp.task('_makejs', function() {
return gulp.src('src/index.js')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('build/'));
});
gulp.task('watch',function() {
webpackConfig.devtool = 'source-map';
webpackConfig.watch = true;
return gulp.src('src/index.js')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('build/'));
})
gulp.task('default',
gulp.series('_makefiles','_makejs')
);
gulp.task('shitpost',
gulp.series('default','webshot','tweet')
);