Skip to content

Commit

Permalink
feat: add CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus authored Nov 3, 2021
2 parents a25830a + d90ddac commit da5746b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -68,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"
},
Expand Down
97 changes: 97 additions & 0 deletions src/bin/index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});

0 comments on commit da5746b

Please sign in to comment.