-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic semicolon detection (to be discarded)
- Loading branch information
Showing
8 changed files
with
291 additions
and
150 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,72 @@ | ||
#!/usr/bin/env node | ||
|
||
var gv = require('../lib/genversion'); | ||
var v = require('../lib/version'); | ||
var program = require('commander'); | ||
var path = require('path'); | ||
|
||
var gv = require('../lib/genversion') | ||
var v = require('../lib/version') | ||
var program = require('commander') | ||
var path = require('path') | ||
|
||
var increaseVerbosity = function (verb, total) { | ||
return total + 1; | ||
}; | ||
|
||
return total + 1 | ||
} | ||
|
||
program | ||
.version(v) | ||
.usage('[options] <target>') | ||
.description('Generates a version module at the target filepath.') | ||
.option('-v, --verbose', 'Output the new version.', increaseVerbosity, 0) | ||
.action(function (target) { | ||
|
||
gv.check(target, function (err, doesExist, isByGenversion) { | ||
if (err) { | ||
console.error(err.toString()); | ||
process.exit(1); | ||
console.error(err.toString()) | ||
process.exit(1) | ||
} | ||
|
||
if (doesExist) { | ||
if (isByGenversion) { | ||
gv.generate(target, function (errg, version) { | ||
if (errg) { | ||
console.error(errg); | ||
return; | ||
console.error(errg) | ||
return | ||
} | ||
|
||
if (program.verbose >= 1) { | ||
console.log('File ' + path.basename(target) + | ||
' was successfully updated to ' + version); | ||
' was successfully updated to ' + version) | ||
} | ||
}); | ||
}) | ||
} else { | ||
// FAIL, unknown file, do not replace | ||
console.error( | ||
'ERROR: File ' + target + ' is not generated by genversion and ' + | ||
'therefore will not be replaced. Please ensure that the file can ' + | ||
'be destroyed and remove it manually before retry.' | ||
); | ||
) | ||
} | ||
} else { | ||
// OK, file does not exist. | ||
gv.generate(target, function (errg, version) { | ||
if (errg) { | ||
console.error(errg); | ||
return; | ||
console.error(errg) | ||
return | ||
} | ||
|
||
if (program.verbose >= 1) { | ||
console.log('File ' + path.basename(target) + | ||
' was successfully generated with version ' + version); | ||
' was successfully generated with version ' + version) | ||
} | ||
}); | ||
}) | ||
} | ||
}); | ||
|
||
}); | ||
}) | ||
}) | ||
|
||
program.on('--help', function () { | ||
// Additional newline. | ||
console.log(''); | ||
}); | ||
console.log('') | ||
}) | ||
|
||
program.parse(process.argv); | ||
program.parse(process.argv) | ||
|
||
if (program.args.length === 0) { | ||
console.error('ERROR: Missing argument <target>'); | ||
program.outputHelp(); | ||
console.error('ERROR: Missing argument <target>') | ||
program.outputHelp() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
var findPackage = require('find-package') | ||
var path = require('path') | ||
var find = require('find') | ||
var findInFiles = require('find-in-files') | ||
|
||
var countSemiFiles = function (dirPath, excludePath, callback) { | ||
// Find the number of js files. | ||
// If there is more files with trailing semicolons than no semis | ||
// then we use semis. | ||
// | ||
find.file(/\.js$/, dirPath, function (files) { | ||
// | ||
// Do not count in the possibly existing version file. | ||
files = files.filter(function (fp) { | ||
return fp !== excludePath | ||
}) | ||
|
||
findInFiles.find(';\n', dirPath, /\.js$/).then(function (results) { | ||
var num, key, numSemi | ||
|
||
num = files.length | ||
numSemi = 0 | ||
|
||
for (key in results) { | ||
if (results.hasOwnProperty(key)) { | ||
if (key !== excludePath) { | ||
numSemi += 1 | ||
} | ||
} | ||
} | ||
|
||
return callback(null, { | ||
numFiles: num, | ||
numFilesWithSemis: numSemi | ||
}) | ||
|
||
// var mostSemi = false | ||
// | ||
// if (num === 0) { | ||
// mostSemi = false // arbitrary | ||
// } else { | ||
// if (numSemi >= Math.ceil(num / 2)) { | ||
// mostSemi = true | ||
// } else { | ||
// mostSemi = false | ||
// } | ||
// } | ||
// | ||
// console.log('mostSemi', mostSemi) | ||
// | ||
// return callback(null, mostSemi) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = function (targetPath, callback) { | ||
// Detect if the project uses semicolons or not. | ||
// | ||
// Parameters | ||
// targetPath | ||
// string. Absolute path to initial location to begin search. | ||
// callback | ||
// function (err, useSemicolon) | ||
// Parameters | ||
// err | ||
// useSemicolon | ||
// bool. True if semicolon is used | ||
// | ||
var pjson = findPackage(targetPath, true) // true adds .paths property | ||
|
||
// If project uses standardjs, no semicolon. | ||
if (pjson.dependencies) { | ||
if (pjson.dependencies.hasOwnProperty('standard')) { | ||
return callback(null, false) | ||
} | ||
} | ||
|
||
var dirPath = path.dirname(targetPath) | ||
|
||
// TODO If there is no files, try bin/, lib/, src/ | ||
|
||
// Find the number of js files. | ||
// If there is more files with trailing semicolons than no semis | ||
// then we use semis. | ||
countSemiFiles(dirPath, targetPath, function (errs, results) { | ||
if (errs) { | ||
return callback(errs) | ||
} | ||
|
||
var num = results.numFiles | ||
var numSemi = results.numFilesWithSemis | ||
var mostSemi | ||
|
||
if (num === 0) { | ||
mostSemi = false // arbitrary | ||
} else { | ||
if (numSemi >= Math.ceil(num / 2)) { | ||
mostSemi = true | ||
} else { | ||
mostSemi = false | ||
} | ||
} | ||
|
||
return callback(null, mostSemi) | ||
}) | ||
} |
Oops, something went wrong.