Last active
January 27, 2022 05:55
-
-
Save manabuyasuda/b7abeee706aa3d061e75afb3092d4899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `npm i -D glob js-beautify` | |
const fs = require('fs') | |
const glob = require('glob') | |
const beautify = require('js-beautify') | |
const beautifyOptions = { | |
indent_size: 2, | |
end_with_newline: true, | |
preserve_newlines: false, | |
max_preserve_newlines: 0, | |
wrap_line_length: 0, | |
wrap_attributes_indent_size: 0, | |
unformatted: ['pre', 'b', 'em'] | |
} | |
glob('./public/**/*.html', (err, files) => { | |
if (err) { | |
console.log(err) | |
return | |
} | |
const lastFileName = files[files.length - 1] | |
files.forEach((file) => { | |
fs.readFile(file, 'utf8', (err, data) => { | |
if (err) { | |
console.log(err) | |
return | |
} | |
// HTMLデータを渡して整形する | |
const result = beautify.html(data, beautifyOptions) | |
// 整形済みHTMLでファイルを上書きする | |
fs.writeFile(file, result, 'utf8', (err) => { | |
if (err) { | |
console.log(err) | |
return | |
} | |
if (file === lastFileName) { | |
console.log('✨ All formatting is complete.') | |
} | |
}) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment