-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
241 lines (197 loc) · 5.78 KB
/
Copy pathgulpfile.js
File metadata and controls
241 lines (197 loc) · 5.78 KB
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
239
240
241
const fs = require( 'fs' );
const gulp = require( 'gulp' );
const glob = require( 'glob' );
const autoprefixer = require( 'gulp-autoprefixer' );
const browserify = require( 'browserify' );
const babelify = require( 'babelify' );
const buffer = require( 'vinyl-buffer' );
const sass = require( 'gulp-sass' )( require('sass') );
const source = require( 'vinyl-source-stream' );
const sourcemaps = require( 'gulp-sourcemaps' );
const es = require( 'event-stream' );
const child_process = require( 'child_process' );
const package = require( './package.json' );
const config = {
sass : {
precision: 8,
stopOnError: false,
functions: {
'base64Encode($string)': $string => {
var buffer = new Buffer( $string.getValue() );
return sass.types.String( buffer.toString('base64') );
}
},
includePaths:[
'./node_modules/bootstrap/scss',
'./src/scss/',
],
watchPaths: './src/scss/**/*.scss',
// importers:[]
},
js: {
exclude:[
'./src/js/lib/'
]
},
destPath: e => {
if ( ['style.css','editor-style.css'].includes( e.basename ) ) {
return './';
}
return e.extname.replace( /^\./, './' );
}
}
gulp.task('i18n:make-pot',cb => {
//child_process.execSync(`wp i18n make-pot . languages/${package.name}.pot --domain=${package.name} --include=src/js/*.js,*.php --exclude=*.*`);
child_process.execSync(`wp i18n make-pot . languages/${package.name}.pot --domain=${package.name} --exclude=tmp,node_modules,vendor`)
cb();
})
gulp.task('i18n:make-json',cb => {
// rm -f languages/*.json
glob.sync('./languages/*.json').map( fs.unlinkSync );
glob.sync('./languages/*.po').length && child_process.execSync( "wp i18n make-json languages/*.po --no-purge" );
cb();
});
/**
* wp i18n make-json lack textdomain prefix in themes
* @see https://github.com/wp-cli/i18n-command/issues/197
*/
gulp.task('i18n:theme-fix-json',cb => {
// rm -f languages/*.json
glob.sync('./languages/*.json')
.map( entry => {
let basename = path.basename(entry);
if ( basename.indexOf( package.wpSkeleton.textdomain ) === -1 ) {
fs.renameSync( entry, path.join(
path.dirname(entry),
`${package.wpSkeleton.textdomain}-${basename}`
) );
}
})
cb();
});
gulp.task('i18n:strings-from-json', cb => {
// rm -f languages/*.json
const xt = require('./src/run/lib/json-extract.js');
let strings = [], php = '';
const common_mapping = {
title:xt.map_string,
description:xt.map_string,
label:xt.map_string,
labels:xt.map_values,
}
const acf_mapping = {
title:(k,v) => {
// this: mapping object
//v = v.replace(/\r\n/,"\n")
if ( 'string' === typeof v && v.indexOf('Inc: ') !== 0 ) {
return xt.map_string.apply(null,arguments)
}
},
description:xt.map_string,
label:xt.map_string,
instructions:xt.map_string,
prepend:xt.map_string,
append:xt.map_string,
message:xt.map_string,
choices:xt.map_values,
placeholder:xt.map_string,
prefix_label:xt.map_string,
suffix_label:xt.map_string
}
// acf
strings = [];
// php += xt.generate_php_lines(
// xt.parse_files( glob.sync('./json/section-types.json'), common_mapping, [] ),
// textdomain,
// 'section-types'
// )
php += '<?php\n';
php += '/* Generated file - do not edit */\n';
php += '\n';
php += xt.generate_php_lines(
xt.parse_files( glob.sync('./json/post-type/*.json'), common_mapping, [] ),
package.name,
'post-types'
)
php += xt.generate_php_lines(
xt.parse_files( glob.sync('./json/taxonomy/*.json'), common_mapping, [] ),
package.name,
'taxonomy'
)
// acf
php += xt.generate_php_lines(
xt.parse_files( glob.sync(`./json/acf/*.json`), acf_mapping, [] ),
package.name,
'acf-fields'
)
fs.writeFileSync( './src/php/json-strings.php', php );
cb();
});
function js_task(debug) {
return cb => {
let tasks = glob.sync("./src/js/**/index.js")
.filter( p => ! config.js.exclude.find( ex => p.indexOf(ex) !== -1 ) )
.map( entry => {
let target = entry.replace(/(\.\/src\/js\/|\/index)/g,'');
return browserify({
entries: [entry],
debug: debug,
paths:['./src/js/lib']
})
.transform( babelify.configure({}) )
.transform( 'browserify-shim' )
.plugin('tinyify')
.bundle()
.pipe(source(target))
.pipe( gulp.dest( config.destPath ) );
} );
return es.merge(tasks).on('end',cb)
}
}
function scss_task(debug) {
const conf = config.sass
conf.outputStyle = debug ? 'expanded' : 'compressed'
return cb => {
let g = gulp.src( config.sass.watchPaths ); // fuck gulp 4 sourcemaps!
if ( debug ) { // lets keep ye olde traditions
g = g.pipe( sourcemaps.init( ) )
}
g = g.pipe(
sass( conf )
)
.pipe( autoprefixer( {
// browsers: package.browserlist
// flexbox:'no-2009',
} ) );
if ( debug ) {
g = g.pipe( sourcemaps.write( ) )
}
return g.pipe( gulp.dest( config.destPath ) );
}
}
gulp.task('build:js', js_task( false ) );
gulp.task('build:scss', scss_task( false ) );
gulp.task('dev:js', js_task( true ) );
gulp.task('dev:scss', scss_task( true ) );
gulp.task('watch:scss',cb => {
cb()
})
gulp.task('watch', cb => {
gulp.watch( config.sass.watchPaths, gulp.parallel('dev:scss','watch:scss'));
gulp.watch('./src/js/**/*.js',gulp.parallel('dev:js'));
// gulp.watch('./languages/*.pot',gulp.parallel('i18n:fix-pot'));
// gulp.watch('./languages/*.po',gulp.parallel('i18n:make-json'));
});
gulp.task('dev',gulp.series('dev:scss','dev:js','watch'));
gulp.task('i18n', gulp.series( 'i18n:strings-from-json','i18n:make-pot'/*,'i18n:fix-pot','i18n:make-json'*/));
gulp.task('build', gulp.series('build:js','build:scss' /*, 'i18n'*/));
gulp.task('default',cb => {
console.log('run either `gulp build` or `gulp dev`');
cb();
});
if ( process.argv.length > 3 && parseInt( process.argv[3] ) ) {
gulp.task( process.argv[3], gulp.series('dev'));
}
module.exports = {
build:gulp.series('build')
}