UNPKG

clang-format

Version:

node wrapper around clang-format

43 lines (39 loc) 1.41 kB
var spawn = require('child_process').spawn; var os = require('os'); var fs = require('fs'); /** * Start a child process running the native clang-format binary. * @param file a Vinyl virtual file reference * @param enc the encoding to use for reading stdout * @param style valid argument to clang-format's '-style' flag * @param done callback invoked when the child process terminates * @returns {Stream} the formatted code */ function clangFormat(file, enc, style, done) { return spawnClangFormat(['-style=' + style, file.path], done, ['ignore', 'pipe', process.stderr]) .stdout; } /** * Spawn the clang-format binary with given arguments */ function spawnClangFormat(args, done, stdio) { var nativeBinary = __dirname + '/bin/' + os.platform() + "_" + os.arch() + '/clang-format'; if (!fs.existsSync(nativeBinary)) { message = "FATAL: This module doesn't bundle the clang-format executable for your platform. " + "(" + os.platform() + "_" + os.arch() + ")\n" + "Consider installing it with your native package manager instead.\n"; throw new Error(message); } var child_process = spawn(nativeBinary, args, { stdio: stdio, cwd: __dirname, }); child_process.on('close', function(exit) { if (exit) { done(exit); } }); return child_process; } module.exports = clangFormat; module.exports.spawnClangFormat = spawnClangFormat;