I am using js-beautify with Gulp to prettify JavaScript on save.
I am unable to use gulp.src
and gulp.dest
pipes. It was giving error
dest.on('unpipe', onunpipe);
So I used the following code and achieved the result I want.
gulp.task("jsbeautify", function () {
const fs = require('fs');
gulp.watch(config.srcTest).on("change", function (file) {
console.log("Updating file");
const destPath = file.path.replace(/\/[^/]*.js/g, " ");
const fileContent = fs.readFileSync(file.path, "utf8");
fs.writeFileSync(file.path, jsBeautify(fileContent, {indent_size: 2}));
});
});
The documentation in js-beautify doesn't provide examples to modify or write to file. So I used fs
.
Is there a better way to do it? Maybe using gulp.src
with pipe.