From 5b3297866c537351510edfac71c4f78aed7f159e Mon Sep 17 00:00:00 2001 From: Daryl Wright Date: Mon, 20 Mar 2023 15:45:39 -0300 Subject: [PATCH] Added support for backticks in generated code --- README.md | 8 ++++++++ bin/genversion.js | 2 ++ lib/genversion.js | 10 ++++++++++ lib/versionTools.js | 7 ++++++- package.json | 2 +- test/cli.test.js | 34 +++++++++++++++++++++++++++++++++- 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bfe9818..037d832 100644 --- a/README.md +++ b/README.md @@ -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 search for package.json along a custom path @@ -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. @@ -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: @@ -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. @@ -264,6 +271,7 @@ To configure VSCode debugger for genversion development, create a file `.vscode/ "args": [ "--semi", "--double", + "--backtick", "--es6", "--strict", "--check-only", diff --git a/bin/genversion.js b/bin/genversion.js index 60ec721..bdb515c 100755 --- a/bin/genversion.js +++ b/bin/genversion.js @@ -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 ', 'search for package.json along a custom path') @@ -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 diff --git a/lib/genversion.js b/lib/genversion.js index ecd898f..f682771 100644 --- a/lib/genversion.js +++ b/lib/genversion.js @@ -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: @@ -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 } @@ -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: diff --git a/lib/versionTools.js b/lib/versionTools.js index fbc9fe1..23584ad 100644 --- a/lib/versionTools.js +++ b/lib/versionTools.js @@ -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: @@ -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 diff --git a/package.json b/package.json index fb89268..a32db38 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/cli.test.js b/test/cli.test.js index 404bcfe..eeca953 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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() @@ -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()