'use strict';

var gulp = require('gulp'),
    gutil = require('gulp-util');

require('./release-status');

const doRelease = done => {
    const git = require('gulp-git');
    var m = require('../common.js');
    var buildConfig = m.config.buildConfig;
    var version = m.getPackageJson().version;
    var name = m.getPackageJson().name;
    var details = m.getReleaseDetails();
    var msgHead = name.replace('-', '_').toUpperCase();
    var commitMsg = msgHead + "_VERSION_" + version;

    if (details && details.changes) {
        var changes = details.changes;
        commitMsg += "\n";
        for (var i = 0; i < changes.length; i++) {
            commitMsg += "\n\t" + changes[i];
        }
    }


    var tagMsg = "Tagging with " + msgHead + '_' + version;

    gulp.src([
        './package.json',
        './yarn.lock',
        './projects/types/package.json',
        './projects/angular-shared/package.json',
        './projects/shared/package.json',
        './client/app/modules/formbird-services/package.json',
        './projects/styles/package.json',
        './ReleaseDetails.json'
    ])
        .pipe(git.add())
        .pipe(git.commit(commitMsg))
        .on('end', function () {
            gutil.log(tagMsg);

            git.tag(version, msgHead, function (err) {
                if (err) throw err;

                done();
            });
        });
};

gulp.task('release', gulp.series(['status', 'bump', 'dist', doRelease]));
