From ae30b2b9dd2727d5107c202f4adb44730862a35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Akseli=20Pal=C3=A9n?= Date: Mon, 1 Jan 2018 20:18:19 +0200 Subject: [PATCH] v2.0.0 --semi flag and default to no semicolon --- README.md | 16 +++++-- bin/genversion.js | 11 +++-- lib/detectSemicolon.js | 106 ----------------------------------------- lib/genversion.js | 54 ++++++++++++--------- lib/version.js | 2 +- package.json | 2 +- 6 files changed, 53 insertions(+), 138 deletions(-) delete mode 100644 lib/detectSemicolon.js diff --git a/README.md b/README.md index f24899a..3097e45 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ Directly from `$ genversion --help`: Options: -V, --version output the version number - -v, --verbose Output the new version. + -v, --verbose output the new version + -s, --semi use semicolons in generated code -h, --help output usage information @@ -100,25 +101,32 @@ Check if it is possible to generate the version module into targetPath. }); -### genversion.generate(targetPath, callback) +### genversion.generate(targetPath, opts, callback) Read the version from the nearest package.json along the targetPath and generate a version module into targetPath. **Parameters:** - *targetPath:* string. An absolute or relative file path. Relative to `process.cwd()`. +- *opts:* optional options. Available keys are: + - *useSemicolon:* optional boolean. - *callback:* function (err, version). Parameter *version* is the version string read from package.json. Parameter *err* is non-null if package.json cannot be found, its version is not a string, or writing the module fails. -**Example:** +**Examples:** gv.generate('lib/version.js', function (err, version) { if (err) { throw err; } - console.log('Sliding into', version, 'like a sledge.'); }); + gv.generate('src/v.js', { useSemicolon: true }, function (err) { + if (err) { throw err } + console.log('Generated version file with a semicolon.') + }) + + ### genversion.version diff --git a/bin/genversion.js b/bin/genversion.js index 2f167b7..e902741 100755 --- a/bin/genversion.js +++ b/bin/genversion.js @@ -13,7 +13,8 @@ program .version(v) .usage('[options] ') .description('Generates a version module at the target filepath.') - .option('-v, --verbose', 'Output the new version.', increaseVerbosity, 0) + .option('-v, --verbose', 'output the new version', increaseVerbosity, 0) + .option('-s, --semi', 'use semicolons in generated code') .action(function (target) { gv.check(target, function (err, doesExist, isByGenversion) { if (err) { @@ -23,7 +24,9 @@ program if (doesExist) { if (isByGenversion) { - gv.generate(target, function (errg, version) { + gv.generate(target, { + useSemicolon: program.semi + }, function (errg, version) { if (errg) { console.error(errg) return @@ -44,7 +47,9 @@ program } } else { // OK, file does not exist. - gv.generate(target, function (errg, version) { + gv.generate(target, { + useSemicolon: program.semi + }, function (errg, version) { if (errg) { console.error(errg) return diff --git a/lib/detectSemicolon.js b/lib/detectSemicolon.js deleted file mode 100644 index 0497810..0000000 --- a/lib/detectSemicolon.js +++ /dev/null @@ -1,106 +0,0 @@ -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) - }) -} diff --git a/lib/genversion.js b/lib/genversion.js index 366a896..d9cdb73 100644 --- a/lib/genversion.js +++ b/lib/genversion.js @@ -1,5 +1,4 @@ -var detectSemicolon = require('./detectSemicolon') var makeAbsolute = require('./makeAbsolute') var path = require('path') var firstline = require('firstline') @@ -49,12 +48,16 @@ exports.check = function (targetPath, callback) { }) } -exports.generate = function (targetPath, callback) { +exports.generate = function (targetPath, opts, callback) { // Generate version submodule file to targetPath with utf-8 encoding. // // Parameters: // targetPath // string. absolute or relative path + // opts + // optional object with optional properties + // useSemicolon + // bool, true to use semicolons in generated code // callback // function (err, version) // err @@ -70,7 +73,20 @@ exports.generate = function (targetPath, callback) { } if (typeof callback !== 'function') { - throw new Error('Unexpected callback argument') + if (typeof opts !== 'function') { + throw new Error('Unexpected callback argument') + } else { + callback = opts + opts = {} + } + } + + if (typeof opts !== 'object') { + throw new Error('Unexpected opts argument') + } + + if (typeof opts.useSemicolon !== 'boolean') { + opts.useSemicolon = false // default } absTarget = makeAbsolute(targetPath) @@ -93,34 +109,26 @@ exports.generate = function (targetPath, callback) { return callback(err) } - // Ensure directory exists + // Ensure directory exists before writing file mkdirp(path.dirname(absTarget), function (errp) { if (errp) { return callback(errp) } - // Some code styles use semicolons, others do not. - // It is important to do this detection after mkdirp. Otherwise - // an error about missing dir occurs. - detectSemicolon(absTarget, function (errs, useSemicolon) { - if (errs) { - return callback(errs) - } + var content = SIGNATURE + '\nmodule.exports = \'' + version + '\'' - var content = SIGNATURE + '\nmodule.exports = \'' + version + '\'' + // Some code styles use semicolons, others do not. + if (opts.useSemicolon) { + content += ';\n' + } else { + content += '\n' + } - if (useSemicolon) { - content += ';\n' - } else { - content += '\n' + fs.writeFile(absTarget, content, 'utf8', function (errw) { + if (errw) { + return callback(errw) } - - fs.writeFile(absTarget, content, 'utf8', function (errw) { - if (errw) { - return callback(errw) - } - return callback(null, version) - }) + return callback(null, version) }) }) } diff --git a/lib/version.js b/lib/version.js index 0c8962b..036544e 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1,2 +1,2 @@ // generated by genversion -module.exports = '1.2.0' +module.exports = '2.0.0' diff --git a/package.json b/package.json index 22137f9..554e82c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "genversion", - "version": "1.2.0", + "version": "2.0.0", "description": "A command line utility to read version from package.json and attach it into your module as a property", "keywords": [ "release",