UNPKG

fformat

Version:

This script helps people in formatting js, css and html files recursively by just mentioning the root path of your project.

52 lines (44 loc) 1.37 kB
var beautify = require('js-beautify'); var fs = require('fs'); var files = []; function formatFile(file) { fs.readFile(file, 'utf8', function (err, data) { var formattedCode; if (err) { throw err; } if (file.indexOf('.js') > -1 || file.indexOf('.ts') > -1) { formattedCode = beautify(data); } else if (file.indexOf('.html') > -1) { formattedCode = beautify.html(data); } else if (file.indexOf('.css') > -1) { formattedCode = beautify.css(data); } fs.writeFile(file, formattedCode, 'utf8', function (err) { if (err) { throw err } console.log(file + ' formatted successfully'); }); }); } function fformat(sourceDirPath) { recurrFolder(sourceDirPath); function recurrFolder(filePath) { var repeatFilePaths = fs.readdirSync(filePath); repeatFilePaths.forEach(function (repeatPath) { var xpath = filePath + '/' + repeatPath; // console.log('recurr xpath: ', xpath); if (fs.statSync(xpath).isDirectory()) { recurrFolder(xpath); } else { // console.log('file found: ', xpath); files.push(xpath); formatFile(xpath); } }); }; // console.log('files: ', files); // files.forEach(function (file) { // formatFile(file, callback) // }) } module.exports = fformat;