forked from OnsenUI/onsen.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·237 lines (206 loc) · 6.17 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var argv = require('yargs').argv;
var gutil = require('gulp-util');
var del = require('del');
var fs = require('fs');
var path = require('path');
var merge = require('merge-stream');
var siteGenerator = require('./modules/metalsmith');
var gettext = require('./modules/gettext');
var parallelize = require('concurrent-transform');
var transifex = require('./modules/gulp-transifex');
//--
var lang = argv.lang === 'en' ? 'en' : 'ja';
var env = argv.production ? 'production' : 'staging';
gutil.log('Language: --lang=' + lang);
gutil.log('Environment: ' + env);
gutil.log('Source: \'./src/documents_' + lang + '\'');
gutil.log('Destination: \'./out_' + lang + '\'');
// Print stack trace on unhandled rejection
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', reason);
});
//////////////////////////////
// generate
//////////////////////////////
gulp.task('generate', gulp.series(less, metalsmith, blog, authors));
//////////////////////////////
// blog
//////////////////////////////
function blog(done) {
siteGenerator(lang, env === 'staging').blog(done);
}
//////////////////////////////
// authors
//////////////////////////////
function authors(done) {
siteGenerator(lang, env === 'staging').authors(done);
}
//////////////////////////////
// metalsmith
//////////////////////////////
function metalsmith(done) {
siteGenerator(lang, env === 'staging').site(done);
}
//////////////////////////////
// i18n
//////////////////////////////
var createTransifexClient = function() {
if (!process.env.TRANSIFEX && !config.TRANSIFEX) {
gutil.log(gutil.colors.bold.red("No TRANSIFEX variable found in env or config.json!"));
gutil.log(gutil.colors.red("Transifex sync is not available."));
return null;
}
var userpw = process.env.TRANSIFEX || config.TRANSIFEX;
return transifex.createClient({
user: userpw.split(":")[0],
password: userpw.split(":")[1],
project: "onsen-ui-website",
local_path: "src/i18n/",
});
}
gulp.task('i18n-extract', function() {
return gulp.src('src/documents_en/v2/guide/**/*')
.pipe(gettext.extract())
.pipe(gulp.dest('src/i18n/gettext/v2/guide'));
});
gulp.task('i18n-translate', function() {
return gulp.src('src/i18n/gettext/v2/guide/**/*.po')
.on('data', (vinylFile) => { gutil.log(`Processing ${gutil.colors.cyan(vinylFile.history[0])}...`); })
.pipe(gettext.translate())
.pipe(gulp.dest('src/documents_ja/v2/guide'));
});
//////////////////////////////
// imagemin
//////////////////////////////
gulp.task('imagemin-core', function() {
return gulp.src('src/files/images/**/*')
.pipe($.imagemin())
.pipe(gulp.dest('src/files/images/'));
});
gulp.task('imagemin-blog', function() {
return gulp.src('blog/content/images/**/*')
.pipe($.imagemin())
.pipe(gulp.dest('blog/content/images/'));
});
gulp.task('imagemin', gulp.series('imagemin-core', 'imagemin-blog'));
//////////////////////////////
// less
//////////////////////////////
function less() {
return gulp.src(['src/less/main.less', 'src/less/blog.less'])
.pipe($.plumber())
.pipe($.less())
.pipe($.autoprefixer({
browsers: ['last 4 versions'],
cascade: false
}))
.pipe($.cssmin())
.pipe(gulp.dest('./out_' + lang + '/css/'));
}
//////////////////////////////
// clean
//////////////////////////////
function clean(done) {
del([
'out_' + lang + '/*'
], done);
}
//////////////////////////////
// serve
//////////////////////////////
function serve(done) {
browserSync({
server: {
baseDir: 'out_' + lang,
index: 'index.html'
},
port: argv.port ? parseInt(argv.port) : 3000,
notify: false,
open: false,
injectChanges: true
});
var options = {
debounceDelay: 400
};
gulp.watch([
'src/documents_' + lang + '/**/*',
'dist/v1/OnsenUI/build/docs/' + lang + '/partials/*/*.html',
'dist/v2/OnsenUI/build/docs/' + lang + '/partials/*/*.html',
'src/layouts/*',
'src/misc/*',
'src/partials/*',
'src/files/**/*',
], options,
gulp.series(metalsmith, blog, authors, () => browserSync.reload())
);
gulp.watch([
'src/less/*'
], options,
gulp.series(less, () => browserSync.reload())
);
if (lang === 'en') {
gulp.watch([
'blog/*',
'blog/posts/*',
'blog/authors/*',
'blog/content/**/*',
'src/partials/*',
'src/layouts/blog.html.eco'
], options,
gulp.series(blog, () => browserSync.reload())
);
} else if (lang === 'ja') {
gulp.watch([
'blog_ja/*',
'blog_ja/posts/*',
'blog_ja/authors/*',
'blog_ja/content/**/*',
'src/partials/*',
'src/layouts/blog_ja.html.eco'
], options,
gulp.series(blog, () => browserSync.reload())
);
}
done();
}
exports.serve = gulp.series('generate', serve);
//////////////////////////////
// deploy
//////////////////////////////
exports.deploy = gulp.series(clean, 'generate', deployAws);
function deployAws() {
var aws,
aws_config = 'aws_' + lang + (env == 'production' ? '_prod' : '') + '.json';
if (fs.existsSync(path.join(__dirname, aws_config))) {
gutil.log("Loading from AWS config file.");
aws = JSON.parse(fs.readFileSync(path.join(__dirname, aws_config)));
} else if (process.env.AWS_KEY) {
gutil.log("Loading from environment variable.");
aws = {
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET,
region: process.env.AWS_REGION,
params: {
Bucket: process.env.AWS_BUCKET
}
};
}
if (!aws) {
throw new Error('AWS configuration is missing! Please create a config file, or set it in the environment before trying to deploy!');
}
var dst = 'out_' + lang;
var publisher = $.awspublish.create(aws);
var site = gulp.src([
dst + '/**',
'!' + dst + '/dist',
]);
var headers = env == 'production' ? {'Cache-Control': 'max-age=7200, no-transform, public'} : {'Cache-Control': 'no-cache'};
var stream = merge(site)
.pipe(parallelize(publisher.publish(headers), 10))
.pipe(publisher.sync())
.pipe($.awspublish.reporter());
return stream;
}