const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');

const args = process.argv.slice(2);
const destinationFolder = args[0] || 'lib';

const declarationFolder = 'src';

glob(`${declarationFolder}/**/*.d.ts`, (err, files) => {
    if (err) {
        throw err;
    }

    files.map(file => {
        const destinationPath = file
            .split('/')
            .slice(1)
            .join('/');

        // eslint-disable-next-line no-console
        console.log('Copied', file, ' -> ', destinationPath);

        fs.ensureFile(path.join(destinationFolder, destinationPath), err => {
            if (err) {
                throw err;
            }

            fs.copyFile(file, path.join(destinationFolder, destinationPath), err => {
                if (err) {
                    throw err;
                }
            });
        });
    });
});
