This repository has been archived by the owner on Oct 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
238 lines (210 loc) · 6.54 KB
/
gulpfile.babel.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*eslint strict: [1, "global"]*/
'use strict';
require('babel-polyfill');
var cp = require('child_process');
var del = require('del');
var fs = require('fs');
var glob = require('glob');
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var cssBase64 = require('gulp-css-base64');
var eslint = require('gulp-eslint');
var imagemin = require('gulp-imagemin');
var plumber = require('gulp-plumber');
var rollup = require('gulp-rollup');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var merge = require('merge-stream');
var runSequence = require('run-sequence');
var Ssh = require('simple-ssh');
var SRCS = {
html: 'lib/views/**/**.html',
img : 'src/images/**/*.+(jpg|jpeg|png|gif|webp)',
js : ['gulpfile.babel.js', 'src/javascripts/**/*.js'],
sass: 'src/stylesheets/**/!(_)*.+(sass|scss)',
};
glob = promisify(glob);
// {{{ Util
function promisify(func) {
return function (...args) {
var me = this;
return new Promise((resolve, reject) => {
function done(err, ...args) {
if (err) {
return reject(err);
}
resolve.apply(me, args);
}
args.push(done);
func.apply(me, args);
});
};
}
function promiseSequence(deferreds) {
return deferreds.reduce(
(promise, deferred) => promise.then(deferred),
Promise.resolve(null)
);
}
function exec(cmd) {
return new Promise((resolve, reject) => {
cp.exec(cmd, (err, stdout, stderr) => {
console.log(stdout);
if (err) {
console.error(stderr);
return reject(err);
}
resolve(stdout);
});
});
}
function spawn(cmd, options) {
var proc = cp.spawn(cmd, options, {stdio : 'inherit'});
return new Promise((resolve, reject) => {
proc.on('exit', (code) => {
if (0 !== code) {
return reject(new Error(`${cmd} ${options.join(' ')} ends with ${code}`));
}
resolve();
}).on('error', (err) => reject(err));
});
}
// }}}
gulp.task('build', ['clean'], (done) => runSequence(['build:copy-assets', 'build:imagemin'], ['build:css', 'build:js'], 'seiji', done));
gulp.task('build:copy-assets', () => {
function build(src, dest = '') {
return gulp.src(src).pipe(gulp.dest(`assets/${dest}`));
}
return merge([
build(
[
'node_modules/Base64/base64.min.js',
'src/bower_components/TAKETORI-JS/taketori.css',
'src/bower_components/TAKETORI-JS/taketori.js',
'src/fonts/Vertical_Latin_Calligraphic.otf',
"src/fonts/Yuraru ru Soin 01'.ttf",
]
),
build(
[
'src/bower_components/font-awsome/css/font-awesome.min.css',
'src/bower_components/font-awsome/fonts/*',
],
'fonts'
),
]);
});
gulp.task('build:css', () => {
return gulp.src(SRCS.sass).
pipe(plumber()).
pipe(sass({outputStyle: 'compressed'})).
pipe(autoprefixer({browsers: ['last 2 version']})).
pipe(cssBase64({
baseDir : '.',
maxWeightResource: 32768 * 5,
})).
pipe(gulp.dest('assets'));
});
gulp.task('build:imagemin', () => {
return gulp.src(SRCS.img).
pipe(imagemin({
optimizationLevel: 7,
progressive : true,
})).
pipe(gulp.dest('assets'));
});
gulp.task('build:js', () => {
function build(src, dest = '') {
return gulp.src(src).
pipe(plumber()).
pipe(rollup()).
pipe(babel()).
pipe(gulp.dest(`assets/${dest}`));
}
return merge([
build('src/javascripts/layout.js'),
build('src/javascripts/index.js'),
build('src/javascripts/feed.js'),
build('src/javascripts/vertical_latin/layout.js', 'vertical_latin/'),
]);
});
gulp.task('clean', () => {
return del(['assets/+(!.keep|**)']).
then((paths) => console.log('Del ' + paths.join(', ')))
});
gulp.task('deploy', ['build'], async () => {
var ssh = new Ssh({
agentForward: true,
key : fs.readFileSync(process.env.HOME + '/.ssh/id_rsa'),
host : 'c4se2.sakura.ne.jp',
port : 22,
user : 'c4se2',
});
function sshExec(cmd) {
return new Promise((resolve, reject) => {
ssh.exec(
cmd,
{
out: (stdout) => console.log(stdout),
err: (stderr) => console.error(stderr),
}
).
on('end', () => resolve()).
start();
});
}
if ('' !== await exec('git status --porcelain')) {
throw new Error('Please commit all changes');
}
if ('' !== await exec('git push -n origin master')) {
throw new Error('Please push all changes');
}
if ('master\n' !== await exec("git branch | awk '/^\\*/{print $2}'")) {
throw new Error('Please `git checkout master`');
}
await sshExec(
'cd ~/www;' +
'git pull --ff-only origin master;' +
'composer install --no-dev;' +
'touch .env;' +
'' // 'set SERVER_ENV=production; vendor/bin/phpmig migrate';
);
return exec('rsync -azh --delete --stats assets [email protected]:/home/c4se2/www');
});
gulp.task('test', ['test:js', 'test:php']);
gulp.task('test:js', () => {
return gulp.src(SRCS.js).
pipe(eslint()).
pipe(eslint.format()).
pipe(eslint.failOnError());
});
gulp.task('test:php', () => exec('vendor/bin/phing test'));
// 他のタスクと同時に実行してはならない
gulp.task('seiji', (done) => runSequence(['seiji:translate', 'seiji:uniseiji-font'], 'seiji:propose', done));
// 他のタスクと同時に実行してはならない
gulp.task('seiji:propose', async () => {
return promiseSequence(
(await glob(SRCS.html)).map((filename) => {
return () => spawn('bin/seiji_proposer', [filename]);
})
);
});
gulp.task('seiji:translate', async () => {
return Promise.all([
(await glob(SRCS.html)).map((filename) => {
return () => exec(`bin/seiji_translator ${filename}`);
})
]);
});
gulp.task('seiji:uniseiji-font', () => exec('bin/uniseiji_font'));
gulp.task('watch', () => {
gulp.watch(SRCS.html , ['seiji:translate' ]);
gulp.watch(SRCS.img , ['build:imagemin' ]);
gulp.watch(SRCS.js , ['build:js'/*, 'test:js'*/]);
gulp.watch(SRCS.sass , ['build:css' ]);
gulp.watch(['index.php', 'lib/**/**.php', 'tests/**/**.php'], ['test:php' ]);
});
gulp.task('default', () => exec('gulp --tasks'));
// vim:fdm=marker: