Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nightvisi0n committed Apr 5, 2023
0 parents commit 47c7715
Show file tree
Hide file tree
Showing 10 changed files with 2,974 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
*.log
*.tgz
.nyc_output/
coverage/
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 4
}
24 changes: 24 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"git": {
"tagName": "v${version}",
"commitMessage": "Release v${version}",
"requireCleanWorkingDir": true,
"requireUpstream": true,
"requireCommits": false,
"addUntrackedFiles": false,
"commit": true,
"tag": true,
"push": true,
"pushTags": true
},
"npm": {
"publish": true,
"publishPath": ".",
"ignoreVersion": false,
"tag": "latest"
},
"github": {
"release": true,
"assets": ["CHANGELOG.md", "*.tgz"]
}
}
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Julian Neureuther <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# pg-formatter-ts

`pg-formatter-ts` is a TypeScript library that wraps the pgFormatter CLI tool for formatting PostgreSQL SQL code.
With a simple and easy-to-use API, it provides the ability to format SQL code programmatically with various formatting options.

Compared to the existing [`pg-formatter`](https://github.com/gajus/pg-formatter) library, `pg-formatter-ts` offers improved TypeScript support and better integration with modern TypeScript/JavaScript projects. This library utilizes the same underlying CLI tool, but provides a more streamlined and developer-friendly API.
Additionally, `pg-formatter-ts` supports all available CLI options of pgFormatter, allowing for greater customization and control over the formatting process.

## Installation

```bash
yarn install pg-formatter-ts
```

## Usage

```typescript
import { format, PgFormatOptions } from "pg-formatter-ts";

const sqlCode = "SELECT id, name, age FROM users WHERE age >= 18;";
const options: PgFormatOptions = {
keywordCase: 2,
spaces: 2,
};

format(sqlCode, options).then((formattedCode) => {
console.log(formattedCode);
});
```

## Options

`pg-formatter-ts` accepts an optional `PgFormatOptions` object as a parameter for the `format` function. The options available are:

```typescript
interface PgFormatOptions {
anonymize?: boolean;
commaStart?: boolean;
commaBreak?: boolean;
config?: string;
wrapComment?: boolean;
debug?: boolean;
commaEnd?: boolean;
functionCase?: number;
format?: string;
nogrouping?: boolean;
help?: boolean;
inplace?: boolean;
keepNewline?: boolean;
noExtraLine?: boolean;
maxlength?: number;
multiline?: boolean;
nocomment?: boolean;
numbering?: boolean;
output?: string;
placeholder?: string;
redshift?: boolean;
spaces?: number;
separator?: string;
formatType?: boolean;
tabs?: boolean;
keywordCase?: number;
typeCase?: number;
version?: boolean;
wrapLimit?: number;
wrapAfter?: number;
noRcfile?: boolean;
extraFunction?: string;
extraKeyword?: string;
noSpaceFunction?: boolean;
}
```

For a detailed explanation of each option, refer to the [pgFormatter documentation](https://github.com/darold/pgFormatter).

## Disclaimer

The code for this library and this README were generated by an AI called GPT-4, developed by OpenAI. While GPT-4 strives for accuracy, it is possible that the code and documentation may contain errors or inconsistencies. Please feel free to report any issues or submit pull requests with improvements, as your contributions are welcome and appreciated.

## License

This project is licensed under the [MIT License](https://opensource.org/license/mit/).
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "pg-formatter-ts",
"version": "0.0.0",
"description": "A simple wrapper library for the pg_format CLI tool.",
"main": "dist/pgFormatter.js",
"types": "dist/pgFormatter.d.ts",
"repository": "[email protected]:nightvisi0n/pg-formatter-ts.git",
"author": "Julian Neureuther <[email protected]>",
"license": "MIT",
"scripts": {
"build": "tsc",
"release": "release-it",
"format": "prettier --write ."
},
"devDependencies": {
"@types/node": "^18.15.11",
"prettier": "^2.8.7",
"release-it": "^15.10.1",
"typescript": "^5.0.3"
}
}
88 changes: 88 additions & 0 deletions src/pgFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { execFile } from "child_process";
import { resolve as resolvePath } from "path";
import { camelCaseToKebabCase } from "./utils";

export interface PgFormatOptions {
anonymize?: boolean;
commaStart?: boolean;
commaBreak?: boolean;
config?: string;
wrapComment?: boolean;
debug?: boolean;
commaEnd?: boolean;
functionCase?: number;
format?: string;
nogrouping?: boolean;
help?: boolean;
inplace?: boolean;
keepNewline?: boolean;
noExtraLine?: boolean;
maxlength?: number;
multiline?: boolean;
nocomment?: boolean;
numbering?: boolean;
output?: string;
placeholder?: string;
redshift?: boolean;
spaces?: number;
separator?: string;
formatType?: boolean;
tabs?: boolean;
keywordCase?: number;
typeCase?: number;
version?: boolean;
wrapLimit?: number;
wrapAfter?: number;
noRcfile?: boolean;
extraFunction?: string;
extraKeyword?: string;
noSpaceFunction?: boolean;
}

export function format(
code: string,
options: PgFormatOptions = {}
): Promise<string> {
return new Promise((resolve, reject) => {
const args = buildArguments(options);
args.push("-");

const pgFormatBinaryPath = resolvePath(
__dirname,
"pg-formatter/pg_format"
);
const pgFormatProcess = execFile(
"perl",
[pgFormatBinaryPath, ...args],
(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
}
);

pgFormatProcess.stdin?.write(code);
pgFormatProcess.stdin?.end();
});
}

function buildArguments(options: PgFormatOptions): string[] {
const args = [];

for (const [key, value] of Object.entries(options)) {
if (value !== undefined) {
const kebabCaseKey = camelCaseToKebabCase(key);
const option = `--${kebabCaseKey}`;

if (typeof value === "boolean") {
args.push(option);
} else {
args.push(option, value.toString());
}
}
}

return args;
}
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function camelCaseToKebabCase(str: string): string {
return str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2019",
"module": "CommonJS",
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 47c7715

Please sign in to comment.