clang-format
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -18,9 +18,5 @@ #!/usr/bin/env node | ||
const spawn_opts = {encoding: 'utf-8', stdio: ['pipe', 'pipe', 'inherit']}; | ||
const binary = | ||
spawn('git', ['config', '--get', 'clangFormat.binary'], spawn_opts) | ||
.stdout.trim(); | ||
const style = | ||
spawn('git', ['config', '--get', 'clangFormat.style'], spawn_opts) | ||
.stdout.trim(); | ||
var gitConfigWrong = false; | ||
const binary = spawn('git', ['config', '--get', 'clangFormat.binary'], spawn_opts).stdout.trim(); | ||
const style = spawn('git', ['config', '--get', 'clangFormat.style'], spawn_opts).stdout.trim(); | ||
let gitConfigWrong = false; | ||
@@ -51,5 +47,8 @@ if (binary !== 'node_modules/.bin/clang-format') { | ||
function main(args) { | ||
let clangFormatPath; | ||
let configCheck; | ||
try { | ||
var clangFormatPath = path.dirname(require.resolve('clang-format')); | ||
var configCheck = checkGitConfig(); | ||
clangFormatPath = path.dirname(require.resolve('clang-format')); | ||
configCheck = checkGitConfig(); | ||
@@ -60,3 +59,3 @@ if (configCheck !== 0) return configCheck; | ||
// different place | ||
var clangFormatPath = '.'; | ||
clangFormatPath = '.'; | ||
// And we don't run the configCheck, because the clang-format binary is also | ||
@@ -63,0 +62,0 @@ // in a different place |
129
index.js
#!/usr/bin/env node | ||
'use strict'; | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var path = require('path'); | ||
var resolve = require('resolve').sync; | ||
var spawn = require('child_process').spawn; | ||
var glob = require('glob'); | ||
var async = require('async'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const resolve = require('resolve').sync; | ||
const spawn = require('child_process').spawn; | ||
const glob = require('glob'); | ||
const async = require('async'); | ||
var VERSION = require('./package.json').version; | ||
var LOCATION = __filename; | ||
const VERSION = require('./package.json').version; | ||
const LOCATION = __filename; | ||
// Glob pattern option name | ||
const GLOB_OPTION = '--glob='; | ||
function errorFromExitCode(exitCode) { | ||
return new Error('clang-format exited with exit code ' + exitCode + '.'); | ||
return new Error(`clang-format exited with exit code ${exitCode}.`); | ||
} | ||
/** | ||
* Start a child process running the native clang-format binary. | ||
* Starts a child process running the native clang-format binary. | ||
* | ||
* @param file a Vinyl virtual file reference | ||
@@ -28,4 +32,4 @@ * @param enc the encoding to use for reading stdout | ||
function clangFormat(file, enc, style, done) { | ||
var args = ['-style=' + style, file.path]; | ||
var result = spawnClangFormat(args, done, ['ignore', 'pipe', process.stderr]); | ||
let args = [`-style=${style}`, file.path]; | ||
let result = spawnClangFormat(args, done, ['ignore', 'pipe', process.stderr]); | ||
if (result) { // must be ChildProcess | ||
@@ -47,20 +51,16 @@ result.stdout.setEncoding(enc); | ||
// loading below to work. | ||
var nativeBinary; | ||
if (os.platform() === 'win32') { | ||
nativeBinary = __dirname + '/bin/win32/clang-format.exe'; | ||
} else { | ||
nativeBinary = __dirname + '/bin/' + os.platform() + '_' + os.arch() + '/clang-format'; | ||
} | ||
if (!fs.existsSync(nativeBinary)) { | ||
var message = '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'; | ||
setImmediate(done.bind(new Error(message))); | ||
let nativeBinary; | ||
try { | ||
nativeBinary = getNativeBinary(); | ||
} catch (e) { | ||
setImmediate(done.bind(e)); | ||
return; | ||
} | ||
if (args.indexOf('-version') !== -1 || args.indexOf('--version') !== -1) { | ||
if (args.find(a => a === '-version' || a === '--version')) { | ||
// Print our version. | ||
// This makes it impossible to format files called '-version' or '--version'. That's a feature. | ||
// minimist & Co don't support single dash args, which we need to match binary clang-format. | ||
console.log('clang-format NPM version', VERSION, 'at', LOCATION); | ||
console.log(`clang-format NPM version ${VERSION} at ${LOCATION}`); | ||
args = ['--version']; | ||
@@ -70,15 +70,7 @@ } | ||
// extract glob, if present | ||
var filesGlob = args.filter(function(arg) { | ||
return arg.indexOf('--glob=') === 0; | ||
}) | ||
.map(function(arg) { | ||
return arg.replace('--glob=', ''); | ||
}) | ||
.shift(); | ||
const filesGlob = getGlobArg(args); | ||
if (filesGlob) { | ||
// remove glob from arg list | ||
args = args.filter(function(arg) { | ||
return arg.indexOf('--glob=') === -1; | ||
}); | ||
args = args.filter(arg => arg.indexOf(GLOB_OPTION) === -1); | ||
@@ -92,3 +84,4 @@ glob(filesGlob, function(err, files) { | ||
// split file array into chunks of 30 | ||
var i, j, chunks = [], chunkSize = 30; | ||
let i, j, chunks = [], chunkSize = 30; | ||
for (i = 0, j = files.length; i < j; i += chunkSize) { | ||
@@ -102,3 +95,3 @@ chunks.push(files.slice(i, i + chunkSize)); | ||
return function(callback) { | ||
var clangFormatProcess = spawn(nativeBinary, args.concat(chunk), {stdio: stdio}); | ||
const clangFormatProcess = spawn(nativeBinary, args.concat(chunk), {stdio: stdio}); | ||
clangFormatProcess.on('close', function(exit) { | ||
@@ -118,3 +111,4 @@ if (exit !== 0) | ||
console.log('\n'); | ||
console.log('ran clang-format on', files.length, files.length === 1 ? 'file' : 'files'); | ||
console.log( | ||
`ran clang-format on ${files.length} ${files.length === 1 ? 'file' : 'files'}`); | ||
done(); | ||
@@ -124,3 +118,3 @@ }); | ||
} else { | ||
var clangFormatProcess = spawn(nativeBinary, args, {stdio: stdio}); | ||
const clangFormatProcess = spawn(nativeBinary, args, {stdio: stdio}); | ||
clangFormatProcess.on('close', function(exit) { | ||
@@ -139,13 +133,12 @@ if (exit) { | ||
// Find clang-format in node_modules of the project of the .js file, or cwd. | ||
var nonDashArgs = process.argv.filter(function(arg, idx) { | ||
return idx > 1 && arg[0] != '-'; | ||
}); | ||
const nonDashArgs = process.argv.filter((arg, idx) => idx > 1 && arg[0] != '-'); | ||
// Using the last file makes it less likely to collide with clang-format's argument parsing. | ||
var lastFileArg = nonDashArgs[nonDashArgs.length - 1]; | ||
var basedir = lastFileArg ? path.dirname(lastFileArg) : // relative to the last .js file given. | ||
process.cwd(); // or relative to the cwd() | ||
var resolvedClangFormat; | ||
var clangFormatLocation; | ||
const lastFileArg = nonDashArgs[nonDashArgs.length - 1]; | ||
const basedir = lastFileArg ? path.dirname(lastFileArg) : // relative to the last .js file given. | ||
process.cwd(); // or relative to the cwd() | ||
let resolvedClangFormat; | ||
let clangFormatLocation; | ||
try { | ||
clangFormatLocation = resolve('clang-format', {basedir: basedir}); | ||
clangFormatLocation = resolve('clang-format', {basedir}); | ||
resolvedClangFormat = require(clangFormatLocation); | ||
@@ -155,3 +148,3 @@ } catch (e) { | ||
} | ||
var actualSpawnFn; | ||
let actualSpawnFn; | ||
if (!resolvedClangFormat) { | ||
@@ -162,3 +155,3 @@ actualSpawnFn = spawnClangFormat; | ||
} else { | ||
throw new Error('Incompatible clang-format loaded from ' + clangFormatLocation); | ||
throw new Error(`Incompatible clang-format loaded from ${clangFormatLocation}`); | ||
} | ||
@@ -175,2 +168,35 @@ // Run clang-format. | ||
/** | ||
* @returns the native `clang-format` binary for the current platform | ||
* @throws when the `clang-format` executable can not be found | ||
*/ | ||
function getNativeBinary() { | ||
let nativeBinary; | ||
if (os.platform() === 'win32') { | ||
nativeBinary = `${__dirname}/bin/win32/clang-format.exe`; | ||
} else { | ||
nativeBinary = `${__dirname}/bin/${os.platform()}_${os.arch()}/clang-format`; | ||
} | ||
if (!fs.existsSync(nativeBinary)) { | ||
const message = '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); | ||
} | ||
return nativeBinary; | ||
} | ||
/** | ||
* Filters the arguments to return the value of the `--glob=` option. | ||
* | ||
* @returns The value of the glob option or null if not found | ||
*/ | ||
function getGlobArg(args) { | ||
const found = args.find(a => a.startsWith(GLOB_OPTION)); | ||
return found ? found.substring(GLOB_OPTION.length) : null; | ||
} | ||
module.exports = clangFormat; | ||
@@ -180,3 +206,4 @@ module.exports.version = VERSION; | ||
module.exports.spawnClangFormat = spawnClangFormat; | ||
module.exports.getNativeBinary = getNativeBinary; | ||
if (require.main === module) main(); |
{ | ||
"name": "clang-format", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "node wrapper around clang-format", | ||
@@ -16,3 +16,2 @@ "repository": { | ||
"scripts": { | ||
"precommit": "node bin/check-clang-format.js \"bin/git-clang-format\"", | ||
"test": "./test.sh" | ||
@@ -23,3 +22,4 @@ }, | ||
"Martin Probst <martinprobst@google.com>", | ||
"Filipe Silva <filipematossilva@gmail.com>" | ||
"Filipe Silva <filipematossilva@gmail.com>", | ||
"Victor Berchet <victor@suumit.com>" | ||
], | ||
@@ -31,6 +31,3 @@ "license": "Apache-2.0", | ||
"resolve": "^1.1.6" | ||
}, | ||
"devDependencies": { | ||
"husky": "^0.14.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9453866
0
248