-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update formatter command line client (#20)
- Loading branch information
1 parent
8accf61
commit 7da7ddd
Showing
14 changed files
with
2,943 additions
and
4,422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
], | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.