build_version.js
const exec = require('child_process').exec;
const clone = require('../node_modules/gulp-html-replace/node_modules/clone');
const Transform = require('../node_modules/gulp-html-replace/node_modules/readable-stream/transform');
const Parser = require('../node_modules/gulp-html-replace/lib/parser');
const common = require('../node_modules/gulp-html-replace/lib/common');
const argv = require('yargs').argv;
const privateToken = 'XXXXXXXXXXXXXXXX-XXX';
const url = 'https://127.0.0.1';
const packageName = 'frontend';
/**
* Bash execute
* @param command {String}
* @param callback {Function}
*/
function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
}
/**
* Getting build version from GitLab
*/
module.exports = function (options) {
const tasks = common.parseTasks(options);
const config = {
keepUnassigned: false,
keepBlockTags: false,
resolvePaths: false
};
return new Transform({
objectMode: true,
transform(file, enc, callback) {
let parser = new Parser(clone(tasks), config, file);
if (file.isBuffer()) {
parser.write(file.contents);
parser.end();
let contents = new Buffer(0);
parser.on('data', (data) => {
contents = Buffer.concat([contents, data]);
});
parser.once('end', () => {
getGitInfo((error, data) => {
if (error) {
console.log(error);
return callback(error);
}
const namespace = data.path_with_namespace;
const version = data.current_branch;
const logObj = JSON.parse(JSON.stringify({
'Namespace': namespace,
'Version': version
}));
let logStr = '';
for (let i in logObj) {
logStr += `${i}: ${ logObj[i] }\\n`;
}
logStr = logStr.trim().replace(/\\n$/, '');
const scriptHTML = `<script>console.log('${ logStr }');</script>`;
contents = Buffer.concat([contents, new Buffer(scriptHTML, 'binary')]);
file.contents = contents;
callback(null, file);
});
});
return;
}
callback('file is not a buffer', file);
}
});
};
/**
* Getting data from current branch
* @param cb {Function}
*/
function getGitInfo(cb) {
execute(`curl --noproxy '*' --header "PRIVATE-TOKEN: ${ privateToken }" "${ url }/api/v3/projects/starred" -k`, data => {
execute(`git rev-parse --abbrev-ref HEAD`, branchName => {
try {
const json = JSON.parse(data);
const front = json.find(obj => obj.name === packageName);
if (!front) {
throw 'package not found';
}
branchName = branchName || '';
let res = {
time: front.last_activity_at.trim(),
path_with_namespace: front.path_with_namespace.trim(),
default_branch: front.default_branch.trim(),
current_branch: branchName.trim()
};
cb(null, res);
} catch (error) {
cb({error});
}
});
});
}
gitlab-version.js
'use strict';
const gulp = require('gulp');
const buildVersion = require('./build_version');
gulp.task('gitlab-ver', () => {
return gulp
.src('www/index.html')
.pipe(buildVersion())
.pipe(gulp.dest(outDist));
});