/*
 * A quick and dirty way of updating files in `lib` from `node_modules`. Intended to just make React & Redux updates
 * a bit easier for now, should be replaced with something more robust as part of BSERVDEV-12748
 */
const fs = require('fs-extra');
const path = require('path');

const packages = {
    'native-promise-only': {
        files: ['npo.js'],
        dest: 'lib'
    },
    'react': {
        files: ['dist/react-with-addons.js', 'dist/react-with-addons.min.js'],
        dest: 'lib/react',
    },
    'react-dom': {
        files: ['dist/react-dom.js'],
        dest: 'lib/react',
    },
    'react-redux': {
        files: ['dist/react-redux.js', 'dist/react-redux.min.js'],
        dest: 'lib/redux',
    },
    'redux': {
        files: ['dist/redux.js', 'dist/redux.min.js'],
        dest: 'lib/redux',
    },
};

module.exports = function(gulp, paths) {
    const nodeModules = path.join(path.dirname(paths.PACKAGE_JSON), 'node_modules');
    return function(cb) {
        Object.keys(packages).forEach(pkg => {
            const packagePath = path.join(nodeModules, pkg);
            const destBasePath = path.resolve(path.join(paths.FRONTEND_SRC, packages[pkg].dest));
            packages[pkg].files.forEach(file => {
                const srcFilePath = path.join(packagePath, file);
                const dstFilePath = path.resolve(destBasePath, path.basename(file));
                console.log(`Copying ${srcFilePath} => ${dstFilePath}`);
                fs.copySync(srcFilePath, dstFilePath);
            });
        });
        cb();
    };
};