diff --git a/packages/typegen/src/write/index.ts b/packages/typegen/src/write/index.ts index badbfead..9bc5dac8 100644 --- a/packages/typegen/src/write/index.ts +++ b/packages/typegen/src/write/index.ts @@ -6,12 +6,14 @@ import {addTags} from '../query/tag' import * as assert from 'assert' import * as fs from 'fs' import * as path from 'path' +import {prettifyOne} from './prettify' export type WriteFile = (filepath: string, content: string) => Promise export const defaultWriteFile: WriteFile = async (filepath, content) => { await fs.promises.mkdir(path.dirname(filepath), {recursive: true}) - await fs.promises.writeFile(filepath, content) + const pretty = prettifyOne({filepath, content}) + await fs.promises.writeFile(filepath, pretty) } /** diff --git a/packages/typegen/src/write/inline.ts b/packages/typegen/src/write/inline.ts index 3d60f3e4..2a5aedd7 100644 --- a/packages/typegen/src/write/inline.ts +++ b/packages/typegen/src/write/inline.ts @@ -1,7 +1,7 @@ import * as lodash from 'lodash' import {TaggedQuery} from '../types' import {relativeUnixPath} from '../util' -import {prettifyOne, tsPrettify} from './prettify' +import {tsPrettify} from './prettify' import type * as ts from 'typescript' import * as path from 'path' import {queryInterfaces} from './typescript' @@ -45,7 +45,7 @@ export function getFileWriter({getQueriesModulePath = defaultGetQueriesModule, w }) } else { let content = queryInterfaces(group) - await writeFile(destPath, prettifyOne({filepath: destPath, content})) + await writeFile(destPath, content) const importPath = relativeUnixPath(destPath, path.dirname(file)) const importStatement = `import * as queries from './${importPath.replace(/\.(js|ts|tsx)$/, '')}'` @@ -67,7 +67,7 @@ export function getFileWriter({getQueriesModulePath = defaultGetQueriesModule, w const newSource = applyEdits(originalSource, edits) - await writeFile(file, prettifyOne({filepath: file, content: newSource})) + await writeFile(file, newSource) function visit(node: ts.Node) { if (ts.isModuleDeclaration(node) && node.name.getText() === 'queries') { diff --git a/packages/typegen/src/write/sql.ts b/packages/typegen/src/write/sql.ts index b1a4f4f7..3e6021c7 100644 --- a/packages/typegen/src/write/sql.ts +++ b/packages/typegen/src/write/sql.ts @@ -2,7 +2,7 @@ import {dedent, relativeUnixPath, typeName, truncateQuery} from '../util' import * as path from 'path' import {getterExpression, jsdocComment, queryInterfaces, quotePropKey} from './typescript' import {TaggedQuery} from '../types' -import {prettifyOne, tsPrettify} from './prettify' +import {tsPrettify} from './prettify' import {WriteFile} from '.' export interface WriteSQLFileOptions { @@ -19,7 +19,7 @@ export const getSQLHelperWriter = const destPath = getModulePath(query.file) const newContent = getSQLHelperContent(query, destPath) - await writeFile(destPath, prettifyOne({content: newContent, filepath: destPath})) + await writeFile(destPath, newContent) } export function getSQLHelperContent(query: TaggedQuery, destPath: string) { diff --git a/packages/typegen/test/ugly.test.ts b/packages/typegen/test/ugly.test.ts index d6666f47..268edd73 100644 --- a/packages/typegen/test/ugly.test.ts +++ b/packages/typegen/test/ugly.test.ts @@ -1,6 +1,9 @@ import * as fsSyncer from 'fs-syncer' import * as typegen from '../src' +import {defaultWriteTypes} from '../src/write' import {getHelper} from './helper' +import * as fs from 'fs' +import * as path from 'path' export const {typegenOptions, logger, poolHelper: helper} = getHelper({__filename}) @@ -119,3 +122,62 @@ test('prettier can fail', async () => { " `) }) + +test('no prettier warning for custom writeTypes', async () => { + const syncer = fsSyncer.jestFixture({ + targetState: { + 'index.ts': ` + import {sql} from 'slonik'; + + export default sql\`select id, n from test_table\`; + `, + }, + }) + + syncer.sync() + + mockFormat.mockImplementationOnce(() => { + throw Object.assign(new Error('prettier not found'), {code: 'MODULE_NOT_FOUND'}) + }) + const mockWarn = jest.spyOn(console, 'warn').mockReset() + + const options = typegenOptions(syncer.baseDir) + await typegen.generate({ + ...options, + writeTypes: queries => { + return defaultWriteTypes({ + writeFile: async (filepath, content) => { + await fs.promises.mkdir(path.dirname(filepath), {recursive: true}) + // dummy lint + const linted = content.replace(/;/g, '') + await fs.promises.writeFile(filepath, linted) + }, + })(queries) + }, + }) + + expect(mockWarn).not.toHaveBeenCalled() + + expect(syncer.yaml()).toMatchInlineSnapshot(` + "--- + index.ts: |- + import {sql} from 'slonik' + + export default sql\`select id, n from test_table\` + + export declare namespace queries { + // Generated by @slonik/typegen + + /** - query: \`select id, n from test_table\` */ + export interface TestTable { + + /** column: \`ugly_test.test_table.id\`, not null: \`true\`, regtype: \`integer\` */ + id: number + + /** column: \`ugly_test.test_table.n\`, regtype: \`integer\` */ + n: (number) | null + } + } + " + `) +})