Files
tankz/tasks/gulp-publish.js
2019-09-02 17:07:31 +03:00

111 lines
3.5 KiB
JavaScript

const gulp = require('gulp');
const fs = require('fs');
const path = require('path');
const through = require('through2');
class Package {
constructor(platform, type, version) {
this.platform = platform;
this.type = type;
this.version = version;
}
get key() {
return [this.platform, this.type].join(':');
}
static getPlatform(filename) {
for (const [platform, r] of [
['android', /^.*?\.apk$/],
['debian', /^.*?\.deb$/],
['linux', /^.*?linux\.tar\.gz$/],
['windows', /^.*?windows\.zip$/],
['windows', /^.*?\.exe$/],
]) {
if (r.test(filename)) {
return platform;
}
}
return null;
}
static getType(filename) {
for (const [type, r] of [
['apk', /^.*?\.apk$/],
['deb', /^.*?\.deb$/],
['archive', /^.*?\.tar\.gz$/],
['archive', /^.*?\.zip$/],
['installer', /^.*?\.exe$/],
]) {
if (r.test(filename)) {
return type;
}
}
return null;
}
static getVersion(filename) {
const m = /(\d+)\.(\d+).(\d+)/.exec(filename);
if (m) {
return parseInt(m[1]) * 10000 + parseInt(m[2]) * 100 + parseInt(m[3]);
}
return null;
}
static parseFilename(filename) {
const platform = this.getPlatform(filename);
const type = this.getType(filename);
const version = this.getVersion(filename);
if (platform && type && version) {
return new Package(platform, type, version);
}
return null;
}
}
module.exports = (name, version, publishDir) => function publish(cb) {
const packages = {};
return gulp.series([
function copy() {
return gulp.src('target/client/@(android|archive|debian|windows)/*')
.pipe(through.obj(function (file, enc, cb) {
const pack = Package.parseFilename(file.path);
if (pack) {
this.push(file);
}
cb(null);
}))
.pipe(gulp.dest(publishDir))
},
function generate() {
return gulp.src(`${publishDir}/*/*`)
.pipe(through.obj(function (file, enc, cb) {
const path = file.path.replace(file.base + '/', '');
const pack = Package.parseFilename(file.path);
if (pack) {
if (!packages[pack.key] || packages[pack.key].version < pack.version) {
packages[pack.key] = {
platform: pack.platform,
type: pack.type,
version: pack.version,
path: path,
filename: path.split('/').pop(),
url: `${publishDir}/${path}`,
}
}
}
cb(null);
})).on('end', function () {
fs.writeFileSync(path.join(publishDir, 'packages.json'), JSON.stringify({
name: name,
version: version,
packages: Object.values(packages),
}, null, 4));
})
}
])(cb);
};
module.exports.Package = Package;