const path = require('path');
const fs = require('fs');

const dir = path.resolve(__dirname);
const files = fs.readdirSync(dir);
const components = [];
files.forEach(file => {
    if (fs.statSync(path.resolve(__dirname, file)).isDirectory()) {
        components.push(file);
    }
});

const importTmp = "import {{file}} from './{{file}}';";
const comopnentTmp = `const COMPONENTS = [
    {{components}}
];`;
const exportTmp = `export default {
    install(Vue) {
        COMPONENTS.forEach(component => {
            component.install(Vue);
        });
    }
};`;

const output = `${components.map(comp => importTmp.replace(/{{file}}/g, comp)).join('\n')}\n\n${comopnentTmp.replace(/{{components}}/, components.join(',\n    '))}\n\n${exportTmp}\n`;

fs.writeFile(path.resolve(__dirname, 'index.js'), output, (err) => {
    if(err) throw err;
});

fs.writeFile(path.resolve(__dirname, 'index.web.js'), output, (err) => {
    if(err) throw err;
});
