Skip to content

Instantly share code, notes, and snippets.

@manabuyasuda
Last active January 27, 2022 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manabuyasuda/b7abeee706aa3d061e75afb3092d4899 to your computer and use it in GitHub Desktop.
Save manabuyasuda/b7abeee706aa3d061e75afb3092d4899 to your computer and use it in GitHub Desktop.
// `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