Skip to content

Commit

Permalink
Update formatter command line client (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriblancke authored May 27, 2020
1 parent 8accf61 commit 7da7ddd
Show file tree
Hide file tree
Showing 14 changed files with 2,943 additions and 4,422 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:8.9.1
- image: circleci/node:10

jobs:
test:
Expand All @@ -20,7 +20,7 @@ jobs:
- run: npm install
- run:
name: Run tests
command: npm run test:prod
command: npm run test:ci

- save_cache:
paths:
Expand Down
7,029 changes: 2,763 additions & 4,266 deletions package-lock.json

Large diffs are not rendered by default.

88 changes: 47 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "dbt-formatter",
"version": "1.2.0",
"version": "1.3.0",
"description": "DBT jinja/sql formatter",
"keywords": [
"dbt",
"formatter",
"jinja",
"sql"
],
"main": "dist/dbt-formatter.umd.js",
"module": "dist/dbt-formatter.es5.js",
"typings": "dist/types/dbt-formatter.d.ts",
"main": "dist/dbt-formatter.cjs.js",
"module": "dist/dbt-formatter.esm.js",
"browser": "dist/dbt-formatter.umd.js",
"files": [
"dist"
],
Expand All @@ -25,18 +25,19 @@
},
"homepage": "https://github.com/henriblancke/dbt-formatter#readme",
"engines": {
"node": ">=6.0.0"
"node": ">=10.0.0"
},
"bin": "dist/lib/dbt-command.js",
"bin": "dist/cli/formatter.js",
"scripts": {
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"prebuild": "rimraf dist bin",
"build": "npm run prebuild && tsc --module commonjs && rollup -c rollup.config.ts",
"package": "npm run prebuild && npm run build && pkg . --out-path bin",
"test": "jest --coverage",
"prebuild": "rimraf dist bin",
"build": "npm run prebuild && rollup -c rollup.config.ts",
"prepublishOnly": "npm run build",
"package": "npm run prebuild && npm run build && pkg dist/cli/* --out-path bin --debug",
"test:watch": "jest --coverage --watch",
"test:prod": "npm run lint && npm run test -- --no-cache",
"tslint-check": "tslint-config-prettier-check ./tslint.json"
"test:ci": "npm run lint && npm run test -- --no-cache",
"tslint-check": "tslint-config-prettier-check ./tslint.json",
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'"
},
"lint-staged": {
"{src,test}/**/*.ts": [
Expand All @@ -55,16 +56,23 @@
"tsx",
"js"
],
"moduleNameMapper": {
"@types": "<rootDir>/src/types",
"@core/(.*)": "<rootDir>/src/core/$1",
"@utils/(.*)": "<rootDir>/src/utils/$1",
"@constants/(.*)": "<rootDir>/src/constants/$1",
"@constants": "<rootDir>/src/constants"
},
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageThreshold": {
"global": {
"branches": 25,
"functions": 50,
"lines": 50,
"statements": 50
"branches": 15,
"functions": 25,
"lines": 25,
"statements": 25
}
},
"collectCoverageFrom": [
Expand All @@ -78,35 +86,33 @@
"printWidth": 120
},
"devDependencies": {
"@types/jest": "^23.3.14",
"@types/lodash": "^4.14.149",
"@types/node": "^10.17.16",
"jest": "^24.9.0",
"jest-config": "^24.9.0",
"@babel/types": "^7.10.0",
"@rollup/plugin-commonjs": "^12.0.0",
"@rollup/plugin-json": "^4.0.3",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-typescript": "^3.1.1",
"@types/glob": "^7.1.1",
"@types/jest": "^25.2.3",
"@types/lodash": "^4.14.153",
"@types/node": "^14.0.5",
"jest": "^26.0.1",
"jest-config": "^26.0.1",
"lodash.camelcase": "^4.3.0",
"prettier": "^1.19.1",
"rimraf": "^2.7.1",
"rollup": "^0.67.0",
"rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-typescript2": "^0.21.2",
"ts-jest": "^23.10.2",
"ts-node": "^7.0.1",
"tslint": "^5.20.1",
"pkg": "^4.4.8",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"rollup": "^2.10.9",
"ts-jest": "^26.0.0",
"ts-node": "^8.10.1",
"tslint": "^6.1.2",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^8.0.1",
"typedoc": "^0.16.10",
"typescript": "^3.8.2"
"tslint-config-standard": "^9.0.0",
"typedoc": "^0.17.7",
"typescript": "^3.9.3"
},
"dependencies": {
"@types/glob": "^7.1.1",
"commander": "^4.1.1",
"commander": "^5.1.0",
"glob": "^7.1.6",
"lodash": "^4.17.15",
"nvm": "0.0.4",
"pkg": "^4.4.8",
"save": "^2.4.0"
"lodash": "^4.17.15"
}
}
76 changes: 42 additions & 34 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import sourceMaps from 'rollup-plugin-sourcemaps';
import camelCase from 'lodash.camelcase';
import typescript from 'rollup-plugin-typescript2';
import json from 'rollup-plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';

const pkg = require('./package.json');

const libraryName = 'dbt-formatter';
const cmdName = 'dbt-command';
const libName = 'dbt-formatter';

export default {
input: `src/${libraryName}.ts`,
output: [
{ file: pkg.main, name: camelCase(libraryName), format: 'umd', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true }
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [
'lodash'
],
watch: {
include: 'src/**'
export default [
{
input: `src/${libName}.ts`,
output: {
name: camelCase(libName),
file: pkg.browser,
format: 'umd',
},
plugins: [
resolve(), // so Rollup can find `ms`
commonjs(), // so Rollup can convert `ms` to an ES module
typescript(),
],
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),

// Resolve source maps to the original source
sourceMaps()
]
};
{
input: `src/${libName}.ts`,
external: ['fs'],
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' },
],
plugins: [
resolve(), // so Rollup can find `ms`
commonjs(), // so Rollup can convert `ms` to an ES module
typescript(),
],
},
{
input: `src/${cmdName}.ts`,
external: ['fs'],
output: [{ file: pkg.bin, format: 'cjs' }],
plugins: [
resolve(), // so Rollup can find `ms`
commonjs(), // so Rollup can convert `ms` to an ES module
typescript(),
],
},
];
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Preset, SqlPresets } from '../types';
import { Preset, SqlPresets } from '@types';

export const formatters: string[] = ['default'];
export const DbtConfig: Preset = {
Expand Down
4 changes: 2 additions & 2 deletions src/core/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Indentation from './indentation';
import InlineBlock from './inline-block';
import { DbtConfig } from '../constants';
import { Options, Token, Node } from '../types';
import { DbtConfig } from '@constants';
import { Options, Token, Node } from '@types';
import * as normalize from '../utils/normalize';
import tokenTypes from '../constants/token-types';
import { LinkedList } from '../utils/data-structures';
Expand Down
4 changes: 2 additions & 2 deletions src/core/inline-block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Token, Node } from '../types';
import tokenTypes from '../constants/token-types';
import { Token, Node } from '@types';
import tokenTypes from '@constants/token-types';

const INLINE_MAX_LENGTH = 50;

Expand Down
8 changes: 4 additions & 4 deletions src/core/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DbtConfig } from '../constants';
import { DbtConfig } from '@constants';
import { isEmpty, escapeRegExp } from 'lodash';
import tokenTypes from '../constants/token-types';
import { LinkedList } from '../utils/data-structures';
import { Config, Token, RegexDefinition } from '../types';
import tokenTypes from '@constants/token-types';
import { LinkedList } from '@utils/data-structures';
import { Config, Token, RegexDefinition } from '@types';

export default class Tokenizer {
private static escapeParen = (paren: string): string => {
Expand Down
44 changes: 20 additions & 24 deletions src/core/writer.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import {writeFileSync, readFileSync} from 'fs';
import { writeFileSync, readFileSync } from 'fs';


export interface Writer{
write(buffer: string) : void
export interface Writer {
write(buffer: string): void;
}

export class FileWriter implements Writer{
private file_path: string;
constructor(file_path: string){
this.file_path = file_path;
}
write(buffer: string): void {
// lets read the file first, and output to stdout if we actually modify it
let original = readFileSync(this.file_path)
if(original.compare(Buffer.from(buffer))){
process.stdout.write(`${this.file_path}\n`);
}
writeFileSync(this.file_path, buffer)
export class FileWriter implements Writer {
private fp: string;
constructor(fp: string) {
this.fp = fp;
}
write(buffer: string): void {
// lets read the file first, and output to stdout if we actually modify it
let original = readFileSync(this.fp);
if (original.compare(Buffer.from(buffer))) {
process.stdout.write(`${this.fp}\n`);
}

writeFileSync(this.fp, buffer);
}
}
export class StdOutWriter implements Writer {
write(buffer: string): void {
process.stdout.write(buffer);
}
}
export class StdOutWriter implements Writer{

write(buffer: string): void {
process.stdout.write(buffer)
}

}
45 changes: 22 additions & 23 deletions src/dbt-command.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { readFileSync, fstat, writeFileSync } from 'fs';
import * as glob from 'glob';
import { readFileSync } from 'fs';
import * as program from 'commander';

import formatter from './dbt-formatter';
import * as glob from 'glob';
import { Writer, StdOutWriter, FileWriter } from './core/writer';
import { writer } from 'repl';
import { Writer, StdOutWriter, FileWriter } from '@core/writer';

program
.option('-f, --file <file>', 'sql file to format')
.option('-d, --directory <directory>', 'The directory to recursively search.')
.option('-i', '--indent <spaces>', 'number of spaces for indent. Defaults to 4.')
.option('--upper', 'upercase reserved sql words')
.option('--replace', 'overwrites the linted file(s).')
.option('--camelcase', 'allow column names to be camel cased')
.parse(process.argv);

const writer_factory = (file:string): Writer => {
return program.replace ? new FileWriter(file) : new StdOutWriter()
}
const factory = (file: string): Writer => {
return program.replace ? new FileWriter(file) : new StdOutWriter();
};

const do_work = (file: string) => {

let query = readFileSync(file);
let formatted = formatter(query.toString(), {
sql: 'default',
const worker = (file: string) => {
const query = readFileSync(file);
const formatted = formatter(query.toString(), {
sql: 'default',
indent: program.indent || 4,
upper: program.upper,
allowCamelcase: false,
allowCamelcase: program.camelcase,
});
let writer = writer_factory(file)
writer.write(formatted)

}

if(program.file){
do_work(program.file);
}else if (program.directory){
let all_files = glob.sync(`${program.directory}/**/*.sql`)
all_files.forEach(do_work)
}
const writer = factory(file);
writer.write(formatted);
};

if (program.file) {
worker(program.file);
} else if (program.directory) {
const allFiles = glob.sync(`${program.directory}/**/*.sql`);
allFiles.forEach(worker);
}
8 changes: 4 additions & 4 deletions src/dbt-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Tokenizer from './core/tokenizer';
import Formatter from './core/formatter';
import { Config, Options } from './types';
import { presets, formatters } from './constants';
import Tokenizer from '@core/tokenizer';
import Formatter from '@core/formatter';
import { Config, Options } from '@types';
import { presets, formatters } from '@constants';

const getConfiguration = (opt: Options): Config => {
const identifier = opt.sql;
Expand Down
File renamed without changes.
Loading

0 comments on commit 7da7ddd

Please sign in to comment.