Skip to content

Commit

Permalink
Merge pull request #20 from darylwright/master
Browse files Browse the repository at this point in the history
Added support for backticks in generated code
  • Loading branch information
axelpale authored Jan 2, 2024
2 parents b5513e1 + 5b32978 commit 751d9d0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Directly from `$ genversion --help`:
-v, --verbose increased output verbosity
-s, --semi use semicolons in generated code
-d, --double use double quotes in generated code
-b, --backtick use backticks in generated code
-e, --es6 use es6 syntax in generated code
-u, --strict add "use strict" in generated code
-p, --source <path> search for package.json along a custom path
Expand All @@ -113,6 +114,10 @@ End each generated line of code with a semicolon as required by some style guide

Use double quotes `"` instead of single quotes `'` as required by some style guides.

### -b, --backtick

Use backticks `` ` `` instead of single `'` or double `"` quotes as required by some style guides.

### -e, --es6

Use ECMAScript 6 `export const` statement instead of `module.exports` in the generated code.
Expand Down Expand Up @@ -157,6 +162,7 @@ Check if it is possible to generate the version module into `targetPath`.
- *source:* optional string. An absolute or relative path to a file or directory. Genversion searches for the source package.json along this path. Defaults to the value of `targetPath`.
- *useSemicolon:* optional boolean. Defaults to `false`.
- *useDoubleQuotes:* optional boolean. Defaults to `false`.
- *useBackticks:* optional boolean. Defaults to `false`.
- *useEs6Syntax:* optional boolean. Defaults to `false`.
- *useStrict:* optional boolean. Defaults to `false`.
- *callback:* function (err, doesExist, isByGenversion, isUpToDate), where:
Expand Down Expand Up @@ -189,6 +195,7 @@ Read the version property from the nearest `package.json` along the `targetPath`
- *source:* optional string. An absolute or relative path to a file or directory. Genversion searches for the source package.json along this path. Defaults to the value of `targetPath`.
- *useSemicolon:* optional boolean. Defaults to `false`.
- *useDoubleQuotes:* optional boolean. Defaults to `false`.
- *useBackticks:* optional boolean. Defaults to `false`.
- *useEs6Syntax:* optional boolean. Defaults to `false`.
- *useStrict:* optional boolean. Defaults to `false`.
- *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 version module fails.
Expand Down Expand Up @@ -264,6 +271,7 @@ To configure VSCode debugger for genversion development, create a file `.vscode/
"args": [
"--semi",
"--double",
"--backtick",
"--es6",
"--strict",
"--check-only",
Expand Down
2 changes: 2 additions & 0 deletions bin/genversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ program
.option('-v, --verbose', 'increased output verbosity')
.option('-s, --semi', 'use semicolons in generated code')
.option('-d, --double', 'use double quotes in generated code')
.option('-b, --backtick', 'use backticks in generated code')
.option('-e, --es6', 'use es6 syntax in generated code')
.option('-u, --strict', 'add "use strict" in generated code')
.option('-p, --source <path>', 'search for package.json along a custom path')
Expand All @@ -31,6 +32,7 @@ program
const opts = {
useSemicolon: cliOpts.semi,
useDoubleQuotes: cliOpts.double,
useBackticks: cliOpts.backtick,
useEs6Syntax: cliOpts.es6,
useStrict: cliOpts.strict,
source: cliOpts.source
Expand Down
10 changes: 10 additions & 0 deletions lib/genversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ exports.dry = (targetPath, opts, callback) => {
// useDoubleQuotes
// boolean. Set true to use double quotes in generated code
// instead of single quotes.
// useBackticks
// boolean. Set true to use backticks in generated code
// instead of single or double quotes.
// useEs6Syntax
// boolean. Set true to use ES6 export syntax
// useStrict:
Expand Down Expand Up @@ -147,6 +150,10 @@ exports.dry = (targetPath, opts, callback) => {
opts.useDoubleQuotes = false // default
}

if (typeof opts.useBackticks !== 'boolean') {
opts.useBackticks = false // default
}

if (typeof opts.useEs6Syntax !== 'boolean') {
opts.useEs6Syntax = false // default
}
Expand Down Expand Up @@ -205,6 +212,9 @@ exports.generate = (targetPath, opts, callback) => {
// useDoubleQuotes
// boolean. Set true to use double quotes in generated code
// instead of single quotes.
// useBackticks
// boolean. Set true to use backticks in generated code
// instead of single or double quotes.
// useEs6Syntax
// boolean. Set true to use ES6 export syntax
// useStrict:
Expand Down
7 changes: 6 additions & 1 deletion lib/versionTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ exports.createContent = (version, opts) => {
// useDoubleQuotes
// boolean. Set true to use double quotes in generated code
// instead of single quotes.
// useBackticks
// boolean. Set true to use backticks in generated code
// instead of single or double quotes.
// useEs6Syntax:
// boolean. True to use ES6 export syntax in generated code
// useStrict:
Expand All @@ -30,7 +33,9 @@ exports.createContent = (version, opts) => {
//
let content = SIGNATURE + '\n'

const Q = opts.useDoubleQuotes ? '"' : '\''
let Q = opts.useDoubleQuotes ? '"' : '\''
Q = opts.useBackticks ? '`' : Q

const SEMI = opts.useSemicolon ? ';' : ''

// In some cases 'use strict' is required in the file
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"command-line-test": "^1.0.10",
"eslint": "^7.32.0",
"fs-extra": "^10.0.1",
"mocha": "^8.4.0",
"mocha": "^10.2.0",
"should": "^13.1.0",
"standard": "^16.0.4"
},
Expand Down
34 changes: 33 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('genversion cli', () => {
})
})

describe('flags --es6 --semi --double --strict', () => {
describe('flags --es6 --semi --double --backtick --strict', () => {
it('should allow --es6 flag', (done) => {
const clit = new CliTest()

Expand Down Expand Up @@ -162,6 +162,38 @@ describe('genversion cli', () => {
})
})

it('should allow --backtick flag', (done) => {
const clit = new CliTest()

clit.exec(GENERATE_COMMAND + ' --backtick ' + P, (err, response) => {
if (err) {
console.error(err, response)
return
}

readTemp().should.equal(SIGNATURE +
'module.exports = `' + pjson.version + '`\n')

return done()
})
})

it('should have --backtick flag take precedence over --double flag', (done) => {
const clit = new CliTest()

clit.exec(GENERATE_COMMAND + ' --backtick --double ' + P, (err, response) => {
if (err) {
console.error(err, response)
return
}

readTemp().should.equal(SIGNATURE +
'module.exports = `' + pjson.version + '`\n')

return done()
})
})

it('should allow --semi and --strict flag', (done) => {
const clit = new CliTest()

Expand Down

0 comments on commit 751d9d0

Please sign in to comment.