Skip to content

Instantly share code, notes, and snippets.

@indiefolk
Created February 5, 2016 14:24
Show Gist options
  • Save indiefolk/d1c8c8c1603510cc6a67 to your computer and use it in GitHub Desktop.
Save indiefolk/d1c8c8c1603510cc6a67 to your computer and use it in GitHub Desktop.
Node script to restructure app module files
/**
* Node Generate Modules
*
* Node script to restructure app module files
*
* Move nested modules from individual project directories to a
* central modules directory shared by the app.
*
* Auto generate related module files - scss, html template, html demo page
*/
var del = require('del');
var dive = require('dive');
var fs = require('fs-extra');
// Delete target directory if already exists
del('./app/modules', function () {
fs.mkdirSync('./app/modules');
dive('./app/section/modules/', { all: true }, function(err, file, stat) {
if (err) throw err;
console.log(file);
// Get filename by regex on path
var filename = file.match(/[^/]*(?=\.[^.]+($|\?))/)[0];
var filenameWithExtension = file.match(/[^\/]+(?=\.*$)/)[0];
// Create directory in modules directory
fs.mkdirSync('./app/modules/' + filename);
// Copy file to modules directory
fs.copySync(file, './app/modules/' + filename + '/' + filenameWithExtension);
// Create empty scss, template, index pages
fs.writeFileSync('./app/modules/' + filename + '/' + filename + '.scss', '');
fs.writeFileSync('./app/modules/' + filename + '/' + filename + '.html', '');
fs.writeFileSync('./app/modules/' + filename + '/' + 'index.html', '');
}, function() {
console.log('complete');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment