-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathgulpfile.js
151 lines (139 loc) · 5.33 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
/***
* Copyright (C) 2018 Qli5. All Rights Reserved.
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*************************************************/
/*************** Build Configs ***************/
const gitPath = 'D:\\Program Files\\Git\\bin\\git';
const npmPath = 'npm';
const includeSourcemap = false;
/*************************************************/
const fs = require('fs');
const spawn = require('child_process').spawn;
const rollup = require('rollup');
const gulp = require('gulp');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const babel = require('gulp-babel');
const replace = require('gulp-replace');
const babelCore = require("babel-core");
const jsxac = require('jsx-append-child');
gulp.task('default', ['build']);
gulp.task('build', ['biliTwin.user.js', 'biliTwinBabelCompiled.user.js']);
gulp.task('biliTwin.user.js', ['./src/bilitwin.js'], () => {
if (!includeSourcemap) {
return gulp.src(['./src/bilitwin.meta.js', './src/bilitwin.js'])
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(concat('biliTwin.user.js'))
.pipe(gulp.dest('.'));
}
else {
return gulp.src(['./src/bilitwin.meta.js', './src/bilitwin.js'])
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(concat('biliTwin.user.js'))
.pipe(sourcemaps.write('.', {
sourceMappingURLPrefix: 'https://raw.githubusercontent.com/liqi0816/bilitwin/develop'
}))
.pipe(gulp.dest('.'));
}
});
gulp.task('biliTwinBabelCompiled.user.js', ['biliTwin.user.js'], async () => new Promise((resolve, reject) => {
babelCore.transformFile('biliTwin.user.js', {
presets: ['env'],
plugins: [["babel-plugin-transform-builtin-extend", { globals: ["DataView"] }]]
}, (err, { code }) => {
if (err) return reject(err);
fs.readFile('./src/bilitwin.meta.js', (err, file) => {
if (err) return reject(err);
meta = file.toString();
gulp.src(['./src/bilitwin-babel.entry.js'])
.pipe(replace('"${metadata}"', meta))
.pipe(replace(/(\/\/[ ]?@name\s+)(?=\w)/, '$1(Babel)'))
.pipe(replace(/(\/\/[ ]?@description\s+)(?=\w)/, '$1(国产浏览器专用)'))
.pipe(replace('"${transpiledCode}"', code))
.pipe(rename('biliTwinBabelCompiled.user.js'))
.pipe(gulp.dest('.'))
.on('end', resolve)
.on('error', reject);
})
});
}));
gulp.task('./src/bilitwin.js', ['./src/ui/ui.js', './src/assconverter/interface.js', './src/flvass2mkv/interface.js'], async () => {
const bundle = await rollup.rollup({
input: './src/bilitwin.entry.js'
});
return bundle.write({
file: './src/bilitwin.js',
format: 'es',
sourcemap: includeSourcemap,
})
});
gulp.task('./src/ui/ui.js', () => {
return gulp.src('./src/ui/ui.entry.js')
.pipe(babel({
plugins: [jsxac],
sourceMaps: 'inline'
}))
.pipe(rename('ui.js'))
.pipe(gulp.dest('./src/ui/'));
});
gulp.task('./src/assconverter/interface.js', async () => new Promise((resolve, reject) => {
fs.access('./src/assconverter/interface.js', fs.constants.R_OK, err => {
if (err) {
spawn(gitPath, ['submodule', 'init'], { shell: true }).once('close', err => {
if (err) return reject(err);
else {
spawn(gitPath, ['submodule', 'update'], { shell: true }).once('close', err => {
if (err) return reject(err);
resolve();
});
}
});
}
else {
resolve();
}
})
}));
gulp.task('./src/flvass2mkv/interface.js', async () => new Promise((resolve, reject) => {
fs.access('./src/flvass2mkv/interface.js', fs.constants.R_OK, err => {
if (err) {
spawn(npmPath, ['install'], { cwd: 'src/flvass2mkv', shell: true }).once('close', err => {
if (err) return reject(err);
resolve();
});
}
else {
resolve();
}
})
}));
gulp.task('bump-minor-version', async () => {
await new Promise((resolve, reject) => {
gulp.src('./src/bilitwin.meta.js')
.pipe(replace(/(\/\/[ ]?@version\s+)([\d\.]+)/, ({ }, $1, version) => {
const arr = version.split('.', 3);
arr[1] = (parseInt(arr[1]) + 1).toString();
return `${$1}${arr.join('.')}`;
}))
.pipe(gulp.dest('./src/'))
.on('end', resolve)
.on('error', reject);
});
await new Promise((resolve, reject) => {
spawn(npmPath, ['--no-git-tag-version', 'version', 'minor'], { shell: true }).once('close', err => {
if (err) return reject(err);
resolve();
});
});
});
gulp.task('release', ['bump-minor-version', 'build'])
gulp.task('watch', () => {
return gulp.watch(['src/*.js', 'src/*/*.js'], { delay: 5000 }, ['build']);
});