-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
215 lines (174 loc) · 5.24 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* jshint node: true, esversion: 6, unused: true */
const path = require('path');
const fs = require('fs-extra');
const gulp = require('gulp');
const sequence = require('gulp-sequence');
const Jimp = require('jimp');
const piexif = require('piexifjs');
const shellton = require('shellton');
const globby = require('globby');
const prettyBytes = require('pretty-bytes');
const prettyMs = require('pretty-ms');
const chalk = require('chalk');
const log = require('fancy-log');
const asynclib = require('async');
const pkg = require('./package.json');
process.title = pkg.name;
const copyright = 'Kiril Vatev';
const repo = 'https://github.com/kirilvatev-photo/portfolio.git';
function size(bytes, useColor = false) {
const color = !useColor ? (v) => v :
bytes < 0 ? chalk.green : chalk.red;
return color(prettyBytes(bytes));
}
function time(ms) {
return chalk.magenta(prettyMs(ms));
}
function addCopyright(buffer, name) {
const start = Date.now();
let data;
try {
const zeroth = {};
const exif = {};
zeroth[piexif.ImageIFD.Copyright] = copyright;
const exifStr = piexif.dump({
'0th': zeroth, 'Exif': exif
});
let dataStr = piexif.insert(exifStr, buffer.toString('binary'));
data = Buffer.from(dataStr, 'binary');
} catch (e) {
// fail the build, but let the process continue
// dealing with the rest of the images
log.error('copyright error:', e);
process.exitCode = 1;
}
let end = Date.now();
if (data) {
log(chalk.grey(`'${chalk.cyan(name)}': added copyright ${time(end - start)}`));
return data;
}
return buffer;
}
function optimizeImage(fileBuffer, name) {
const originalSize = fileBuffer.length;
const start = Date.now();
return Jimp.read(fileBuffer)
.then(img => {
const targetSize = 2500;
const { width, height } = img.bitmap;
if (width > height && width > targetSize) {
img.resize(targetSize, Jimp.AUTO);
} else if (height > targetSize) {
img.resize(Jimp.AUTO, targetSize);
}
img.quality(85);
return img;
})
.then(img => {
return img.getBufferAsync(Jimp.MIME_JPEG);
})
.then(resultBuffer => {
return addCopyright(resultBuffer, name);
})
.then(resultBuffer => {
const newSize = resultBuffer.length;
const end = Date.now();
log(chalk.gray(`'${chalk.cyan(name)}': ${size(originalSize)} -> ${size(newSize)} (${size(newSize - originalSize, true)}) in ${time(end - start)}`));
return resultBuffer;
});
}
gulp.task('clean', () => {
return fs.remove('tmp');
});
gulp.task('build:images', () => {
// We can't use gulp here. The vinyl streams like to
// read files and keep a lot of stuff in memory. Since images
// can be as high as 20-30MB and there can be tens of them,
// that's quite a bit of memory. It often resulted in memory or
// segfault errors on both Windows and Linux. As a bonus, doing
// it manually is also faster
const outdir = path.resolve(__dirname, 'tmp/images');
return fs.ensureDir(outdir)
.then(() => {
return globby('images/*.jpg');
})
.then(files => {
return new Promise((resolve, reject) => {
asynclib.eachLimit(files, 4, (file, next) => {
const name = path.basename(file);
shellton({
// gulp treats non -- parameters as task names,
// so just add -- to the front as a quick hack
task: `gulp build:image:single "--${name}" "--${file}" "--${path.resolve(outdir, name)}"`,
stdout: 'inherit',
stderr: 'inherit'
}, next);
}, (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
});
});
gulp.task('build:image:single', () => {
const name = (process.argv[3] || '').slice(2);
const inpath = (process.argv[4] || '').slice(2);
const outpath = (process.argv[5] || '').slice(2);
if (!name) {
throw new Error('no name was provided');
}
if (!inpath) {
throw new Error('no inpath was provided');
}
if (!outpath) {
throw new Error('no outpath was provided');
}
return fs.readFile(inpath)
.then(buffer => {
return optimizeImage(buffer, name);
})
.then(result => {
return fs.writeFile(outpath, result);
});
});
gulp.task('build:files', () => {
return gulp.src(['public/**/*'])
.pipe(gulp.dest('tmp'));
});
gulp.task('build', ['clean'], sequence(
'build:files',
'build:images'
));
gulp.task('publish', () => {
function exec(line) {
console.log(chalk.green(line));
return new Promise((resolve, reject) => {
shellton({
task: line,
cwd: path.resolve(__dirname, 'tmp'),
stdin: 'inherit',
stdout: 'inherit',
stderr: 'inherit'
}, (err) => err ? reject(err) : resolve());
});
}
function clean() {
return fs.remove('tmp/.git');
}
// using an https repo means that it will ask for
// a username and password when publishing... so that's fun
const script = `
git init
git config user.name "${copyright}"
git config user.email "contact@kirilvatev.com"
git add .
git commit -m "automatic publishing"
git remote add origin ${repo}
git push --force -u origin master:gh-pages
`.trim();
return script.split('\n').map(v => v.trim()).reduce((prom, line) => {
return prom.then(() => exec(line));
}, clean()).then(() => clean());
});