const {src, dest, series} = require('gulp');
const del = require('del');
const ts = require('gulp-typescript');
const exec = require('child_process').exec;
const sass = require('gulp-sass');
sass.compiler = require('node-sass');
let tsSource = ts.createProject('tsconfig.json')

function clean(cb) {
    del(['build/*']);
    cb();
}

function validateConfigSources(){

}

function conf() {
    return src(['src/assets/**/*', '!src/assets/**/*.scss'], {base: 'src/assets'})
        .pipe(dest('build/assets/'));
}

function style() {
    return src('node_modules/bootstrap/dist/css/bootstrap.css')
        .pipe(dest('build/assets/pages/styles/'));
}

function scss() {
    return src('src/assets/pages/styles/*.scss',)
        .pipe(sass.sync().on('error', sass.logError))
        .on('error', () => {
            process.exit(1)
        })
        .pipe(dest('build/assets/pages/styles/'));
}

function root() {
    return src('src/*.js')
        .pipe(dest('build/'));
}

function pkg() {
    return src('package*')
        .pipe(dest('build/'));
}

function typescript() {
    return tsSource.src()
        .pipe(tsSource()).on('error', () => {
            process.exit(1)
        })
        .js
        .pipe(dest('build/util/'));
}

function dependencies(cb) {
    // Installs dependencies into build folder
    exec('npm --prefix ./build install ./build --production', './node_modules/.bin/electron-rebuild -m ./build/node_modules',
        (err, stdout, stderr) => {
            console.log(stdout);
            console.log(stderr)
            cb(err);
        });
}

exports.clean = clean;

exports.default = series(conf, style, scss, root, pkg, typescript, dependencies);

exports.runFast = series(conf, style, scss, root, pkg)