From 9f14eec9e8b4ffb504000a7df7929fb997f17a80 Mon Sep 17 00:00:00 2001 From: Nikolas Poniros Date: Sat, 30 Oct 2021 17:16:58 +0200 Subject: [PATCH 1/2] Feat: add CLI interface --- README.md | 34 +++++++++++++++++ package.json | 6 ++- src/bin/index.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/bin/index.js diff --git a/README.md b/README.md index ab9c172..08c00c8 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,37 @@ format(`SELECT foo FROM bar`); |`spaces`|number|`4`|Number of spaces to indent the code.|`spaces`| |`stripComments`|boolean|`false`|Remove any comment from SQL code.|`nocomment`| |`tabs`|boolean|`false`|Use tabs instead of spaces. When `true`, the `spaces` option is ignored.|`tabs`| + +## CLI Usage + +```bash +$ npm install pg-formatter -g +$ pg-formatter --help +Formats SQL files + +Options: + --version Show version number [boolean] + --anonymize Obscure all literals in queries, useful to hide + confidential data before formatting. + [boolean] [default: false] + --comma-break Add a newline after each comma in an insert statement. + [boolean] [default: false] + --function-case Change the case of the function names. + [string] [choices: "unchanged", "lowercase", "uppercase", "capitalize"] + [default: "unchanged"] + -i, --inplace Override input file with formatted content. + [boolean] [default: false] + --keyword-case Change the case of the reserved keyword. + [string] [choices: "unchanged", "lowercase", "uppercase", "capitalize"] + [default: "unchanged"] + --no-rc-file Do not read ~/.pg_format automatically. + [boolean] [default: false] + --placeholder Regex to find code that must not be changed. [string] + --spaces Number of spaces to indent the code. + [number] [default: 4] + --strip-comments Remove any comment from SQL code. + [boolean] [default: false] + --tabs Use tabs instead of spaces. When true, the spaces option + is ignored. [boolean] [default: false] + --help Show help [boolean] +``` diff --git a/package.json b/package.json index 4f4adef..8d964ca 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,12 @@ "src/**/*" ] }, + "bin": { + "pg-formatter": "dist/bin/index.js" + }, "dependencies": { - "shell-quote": "^1.7.2" + "shell-quote": "^1.7.2", + "yargs": "^17.2.1" }, "description": "PostgreSQL SQL syntax beautifier.", "devDependencies": { diff --git a/src/bin/index.js b/src/bin/index.js new file mode 100644 index 0000000..8509b82 --- /dev/null +++ b/src/bin/index.js @@ -0,0 +1,97 @@ +#!/usr/bin/env node + +import fs from 'fs'; +import yargs from 'yargs'; +import format from '../format'; + +const argv = yargs + .usage('Formats SQL files') + .options({ + anonymize: { + default: false, + description: 'Obscure all literals in queries, useful to hide confidential data before formatting.', + type: 'boolean', + }, + 'comma-break': { + default: false, + description: 'Add a newline after each comma in an insert statement.', + type: 'boolean', + }, + 'function-case': { + choices: ['unchanged', 'lowercase', 'uppercase', 'capitalize'], + default: 'unchanged', + description: 'Change the case of the function names.', + type: 'string', + }, + inplace: { + alias: 'i', + default: false, + description: 'Override input file with formatted content.', + type: 'boolean', + }, + 'keyword-case': { + choices: ['unchanged', 'lowercase', 'uppercase', 'capitalize'], + default: 'unchanged', + description: 'Change the case of the reserved keyword.', + type: 'string', + }, + 'no-rc-file': { + default: false, + description: 'Do not read ~/.pg_format automatically.', + type: 'boolean', + }, + placeholder: { + description: 'Regex to find code that must not be changed.', + type: 'string', + }, + spaces: { + default: 4, + description: 'Number of spaces to indent the code.', + type: 'number', + }, + 'strip-comments': { + default: false, + description: 'Remove any comment from SQL code.', + type: 'boolean', + }, + tabs: { + default: false, + description: 'Use tabs instead of spaces. When true, the spaces option is ignored.', + type: 'boolean', + }, + }) + .check((parameters) => { + const filePaths = parameters._; + if (filePaths.length === 0) { + throw new Error('No files given'); + } else { + return true; + } + }) + .help() + .wrap(80) + .parse(); + +const files = argv._; + +if (files.length === 0) { + throw new Error('No files given'); +} + +const options = ['anonymize', 'commaBreak', 'functionCase', 'keywordCase', 'noRcFile', 'placeholder', 'spaces', 'stripComments', 'tabs']; +const config = {}; + +for (const option of options) { + config[option] = argv[option]; +} + +files.forEach((file) => { + const data = fs.readFileSync(file); + const result = format(data, config); + + if (argv.inplace) { + fs.writeFileSync(file, result); + } else { + process.stdout.write(result); + } +}); From d90ddac8b787b94c48a69e369104692c6de9ba1c Mon Sep 17 00:00:00 2001 From: Nikolas Poniros Date: Wed, 3 Nov 2021 17:35:00 +0100 Subject: [PATCH 2/2] Lock pg-formatter version to v.4.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8d964ca..c1fe7f7 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ }, "scripts": { "build": "rm -fr ./dist && babel ./src --out-dir ./dist --copy-files --source-maps", - "build-pg-formatter": "rm -fr ./src/pg-formatter && mkdir -p ./src/pg-formatter && curl -LkSs http://github.com/darold/pgFormatter/tarball/master/ | tar xz --strip-components=1 -C ./src/pg-formatter", + "build-pg-formatter": "rm -fr ./src/pg-formatter && mkdir -p ./src/pg-formatter && curl -LkSs https://github.com/darold/pgFormatter/archive/refs/tags/v4.4.tar.gz | tar xz --strip-components=1 -C ./src/pg-formatter", "lint": "eslint ./src ./test && flow", "test": "ava --serial --verbose" },