const testFolder = './modules';
const fs = require('fs');


let Modules = fs.readdirSync(testFolder).filter(file => 
	fs.lstatSync(testFolder + '/' + file).isDirectory());
writeToFile(testFolder + "/index.js", makeFile(Modules));

Modules.forEach((mod) => {
	try {
		let styles = fs.readdirSync(testFolder + "/" + mod + "/styles")
		.filter(file => file !== 'index.js' && file.includes('.js'))
		.map(file => file.replace('.js', ''));
		writeToFile(testFolder + "/" + mod + "/styles/index.js", makeFile(styles));
	} catch(e) {
		console.log("[-] Module: " + mod + " has no styles");
	}

})











function writeToFile(filename, content){
	fs.writeFile(filename, content, function(err) {
	    if(err) {
	        return console.log(err);
	    }
	    console.log("File: " + filename + " written sucessfully");
	});
}




function makeFile(arr) {
	let indexFile = "";
	// imports first
	arr.forEach((mod) => {
		indexFile += "import " + mod + " from './" + mod + "';\r\n";
	});
	// exports
	indexFile += "\r\nexport default {\r\n";
	indexFile += arr.join(',\r\n');
	indexFile += "\r\n}\r\n";
	return indexFile;
}