const gulp = require('gulp');
const { exec } = require('child_process');

function buildServer(cb) {
    exec('npx webpack --config webpack.server.js', (err, stdout, stderr) => {
        if (stdout) process.stdout.write(stdout);
        if (stderr) process.stderr.write(stderr);
        cb(err);
    });
}

function buildClient(cb) {
    exec('npx webpack --config webpack.client.js', (err, stdout, stderr) => {
        if (stdout) process.stdout.write(stdout);
        if (stderr) process.stderr.write(stderr);
        cb(err);
    });
}

function watchServer(cb) {
    exec('npx webpack --config webpack.server.js --watch', (err, stdout, stderr) => {
        if (stdout) process.stdout.write(stdout);
        if (stderr) process.stderr.write(stderr);
    });
    cb();
}

function watchClient(cb) {
    exec('npx webpack --config webpack.client.js --watch', (err, stdout, stderr) => {
        if (stdout) process.stdout.write(stdout);
        if (stderr) process.stderr.write(stderr);
    });
    cb();
}

exports.buildServer = buildServer;
exports.buildClient = buildClient;
exports.build = gulp.parallel(buildServer, buildClient);
exports.watch = gulp.parallel(watchServer, watchClient);
exports.default = exports.build;
