'use strict';

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    m = require('../common.js');

gulp.task('status',
    done => {

        const git = require('gulp-git');
        try {
            var details = m.getReleaseDetails();
        } catch (err) {
            gutil.log(gutil.colors.red("BUILD FAILURE:"), gutil.colors.magenta("ReleaseDetails.json") , err);
            process.exit(2);
        }

        git.status({quiet: true}, function (err, stdout) {
            if (err) throw err;

            var modifiedFiles = (stdout.match(/modified:/g) || []).length;

            if (stdout.indexOf("ReleaseDetails.json") < 0) {
                gutil.log(gutil.colors.red("BUILD FAILURE:"), "Please modify the", gutil.colors.magenta("ReleaseDetails.json"),
                    "file to specify the contents of this release");
                process.exit(2);
            } else if (modifiedFiles > 1) {
                gutil.log(gutil.colors.red("BUILD FAILURE:"), "There are",
                    gutil.colors.green('+' + (modifiedFiles - 1)), "modified file(s) other than",
                    gutil.colors.magenta("ReleaseDetails.json."),
                "Please commit pending files before doing a release.");
                process.exit(2);
            } else {
                gutil.log(gutil.colors.magenta("ReleaseDetails.json")," Modified!");
            }

            done();
        });

    });