'use strict';

const spawn = require('cross-spawn').spawn;
const gutil = require('gulp-util');
const gulp = require('gulp');

gulp.task('test-client', function (done) {
  const importLocal = require('import-local');
  if (!importLocal(__filename)) {
    if (process.env.NODE_ENV == null) {
      process.env.NODE_ENV = 'test';
    }
    const buf = process.argv;
    process.argv = [];

    const ng = spawn('ng', ['test', '--watch=false'], { stdio: 'inherit' });
    ng.on('exit', function (code) {
        const exitCode = code.toString(); // call toString because the raw data is a buffer
        if (code.toString() !== '0') {
          gutil.log('gulp test exited with a non error code: ' + exitCode);
        } else {
          gutil.log('gulp test-client done.');
        }
        
        process.argv = buf;
        done();
    });
  }
});
