-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
90 lines (75 loc) · 2.82 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
// copied from https://github.com/Arikael/guacamole-common-js.git
import gulp from 'gulp'
import concat from 'gulp-concat'
import insert from 'gulp-insert'
import fs from 'node:fs'
import xml2js from 'xml2js'
import uglify from 'gulp-uglify'
import rename from 'gulp-rename'
import {extract} from 'tar'
import axios from 'axios'
const tmpDir = './tmp'
const zipFile = `${tmpDir}/guacamole.tar.gz`
const packageJsonFile = 'package.json'
const distDir = './dist'
let taggedDir = ''
gulp.task('getGuacamole', async function () {
return new Promise(async function(resolve, _) {
fs.mkdirSync(tmpDir, {recursive: true})
const file = fs.createWriteStream(zipFile);
const tagResult = await axios.get('https://api.github.com/repos/apache/guacamole-client/tags')
const tag = tagResult.data[0].name
taggedDir = `${tmpDir}/guacamole-client-${tag}`
const fileResult = await axios.get(`https://github.com/apache/guacamole-client/archive/refs/tags/${tag}.tar.gz`, {
responseType: 'stream'
})
fileResult.data.pipe(file)
file.on('finish', () => {
const comp = fs.createReadStream(zipFile);
extract({
cwd: './tmp',
file: comp.path,
sync: true
});
resolve()
});
})
})
gulp.task('clean', function(callback) {
if(fs.existsSync(distDir)) {
fs.rmSync(distDir, {recursive: true})
}
if(fs.existsSync(tmpDir)) {
fs.rmSync(tmpDir, {recursive: true, force: true})
}
callback()
})
gulp.task('updateVersion', function (callback) {
const xmlFile = fs.readFileSync(`${taggedDir}/guacamole-common-js/pom.xml`);
xml2js.parseString(xmlFile, function (parseErr, result) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8'))
packageJson.version = result['project']['version'][0]
fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2))
callback()
}, null)
})
function createJs(format, exportCode) {
const dir = `${distDir}/${format}`
return gulp.src(`${taggedDir}/guacamole-common-js/src/main/webapp/modules/*.js`)
.pipe(concat('index.js'))
.pipe(insert.append(exportCode))
.pipe(gulp.dest(dir))
.pipe(uglify())
.pipe(rename('index.min.js'))
.pipe(gulp.dest(dir));
}
gulp.task('createEsm', function () {
return createJs('esm', 'export default Guacamole;')
});
gulp.task('createCjs', function () {
return createJs('cjs', 'module.exports = Guacamole;')
});
gulp.task('createPlainJs', function () {
return createJs('js', '')
});
gulp.task('default', gulp.series('clean', 'getGuacamole', 'updateVersion', 'createEsm', 'createCjs', 'createPlainJs'))