This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (60 loc) · 2.51 KB
/
index.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
var through = require('through2');
var path = require('path');
var gulp = require('gulp-util')
var Snoowrap = require('snoowrap');
module.exports = function redeploy(options) {
options = options || {}
var subreddit = options.subreddit || process.env.REDDIT_SUB;
var message = options.message || 'Updated by redeploy.';
var reddit = new Snoowrap({
userAgent: options.userAgent || 'redeploy (https://github.com/VolunteerLiveTeam/redeploy)',
clientId: options.clientID || process.env.REDDIT_CLIENT_ID,
clientSecret: options.clientSecret || process.env.REDDIT_CLIENT_SECRET,
username: options.username || process.env.REDDIT_USERNAME,
password: options.password || process.env.REDDIT_PASSWORD
});
redeploy = {}
function validate(file, callback) {
if (!subreddit)
return callback(new gulp.PluginError('redeploy', 'No subreddit specified! Pass in the "subreddit" option or use the REDDIT_SUB environment variable.'));
if (!file || !file.contents)
return callback(null, file);
if (file.isStream())
return callback(new gulp.PluginError('redeploy', 'File streaming is not supported.'));
}
redeploy.css = function css() {
return through.obj(function(file, encoding, callback) {
validate(file, callback);
reddit.getSubreddit(subreddit)
.updateStylesheet({
css: String(file.contents),
reason: message
}).then(function() {
gulp.log('deployed', file.relative, 'to /r/' + subreddit)
return callback(null, file);
}, function(error) {
return callback(new gulp.PluginError('redeploy', 'Error deploying to /r/' + subreddit + ': ' + error));
});
});
}
redeploy.images = function images() {
return through.obj(function(file, encoding, callback) {
validate(file, callback);
imagePath = path.parse(file.path);
if (!['.jpg', '.png'].includes(imagePath.ext))
return callback(new gulp.PluginError('redeploy', 'Unable to deploy images that are not .png or .jpg.'))
reddit.getSubreddit(subreddit)
.uploadStylesheetImage({
name: imagePath.name,
file: file.path,
imageType: imagePath.ext
}).then(function() {
gulp.log('uploaded', file.relative, 'to /r/' + subreddit)
return callback(null, file);
}, function(error) {
return callback(new gulp.PluginError('redeploy', 'Error deploying ' + file.relative + ' to /r/' + subreddit + ': ' + error));
});
});
}
return redeploy;
}