Skip to content

Commit

Permalink
v2.0.0 --semi flag and default to no semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
axelpale committed Jan 1, 2018
1 parent 228cb73 commit ae30b2b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 138 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions bin/genversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ program
.version(v)
.usage('[options] <target>')
.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) {
Expand All @@ -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
Expand All @@ -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
Expand Down
106 changes: 0 additions & 106 deletions lib/detectSemicolon.js

This file was deleted.

54 changes: 31 additions & 23 deletions lib/genversion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

var detectSemicolon = require('./detectSemicolon')
var makeAbsolute = require('./makeAbsolute')
var path = require('path')
var firstline = require('firstline')
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
})
})
}
2 changes: 1 addition & 1 deletion lib/version.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// generated by genversion
module.exports = '1.2.0'
module.exports = '2.0.0'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ae30b2b

Please sign in to comment.