Skip to content

Commit

Permalink
Merge e2088ef into 4c75541
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Jan 26, 2020
2 parents 4c75541 + e2088ef commit bc0d749
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
25 changes: 13 additions & 12 deletions packages/typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {QueryResultRowType, sql as slonikSql, TaggedTemplateLiteralInvocationTyp
import * as fs from 'fs'
import { basename, join } from 'path'
import { inspect } from 'util';
import {EOL} from 'os';

const keys = <T>(obj: T) => Object.keys(obj) as Array<keyof T>
const fromPairs = <K, V>(pairs: Array<[K, V]>) => pairs.reduce(
Expand Down Expand Up @@ -84,7 +85,7 @@ export interface TypeGenWithSqlGetter<KnownTypes> {

export const createCodegenDirectory = (directory: string) => {
fs.mkdirSync(directory, {recursive: true})
fs.writeFileSync(join(directory, 'index.ts'), 'export const knownTypes = {}\n', 'utf8')
fs.writeFileSync(join(directory, 'index.ts'), 'export const knownTypes = {}' + EOL, 'utf8')
}

export const resetCodegenDirectory = (directory: string) => {
Expand Down Expand Up @@ -167,14 +168,14 @@ export const setupSqlGetter = <KnownTypes>(config: TypeGenConfig<KnownTypes>): T
[
`${header}`,
`export const _pg_types = ${inspect(fromPairs(types.map(t => [t.typname, t.typname])))} as const`,
`export type _pg_types = typeof _pg_types\n`,
].join('\n\n'),
`export type _pg_types = typeof _pg_types${EOL}`,
].join(EOL + EOL),
)
}
return null
},
afterQueryExecution: async ({ originalQuery }, _query, result) => {
const trimmedSql = originalQuery.sql.replace(/^\n+/, '').trimRight()
const trimmedSql = originalQuery.sql.replace(/^\r?\n+/, '').trimRight()
const _identifiers = _map[mapKey(originalQuery)]
_identifiers && _identifiers.forEach(identifier => writeTypes(
identifier,
Expand Down Expand Up @@ -207,16 +208,16 @@ const codegen = {
...properties.map(p => [
blockComment(p.description),
`${p.name}: ${p.value}`
].filter(Boolean).map(s => ' ' + s).join('\n')),
].filter(Boolean).map(s => ' ' + s).join(EOL)),
`}`,
].filter(Boolean).join('\n')
].filter(Boolean).join(EOL)
}

const header = [
'/* eslint-disable */',
'// tslint:disable',
`// this file is generated by a tool; don't change it manually.`,
].join('\n')
].join(EOL)

const getFsTypeWriter = (generatedPath: string) =>
(typeName: string, properties: Property[], description: string) => {
Expand All @@ -225,7 +226,7 @@ const getFsTypeWriter = (generatedPath: string) =>
? fs.readFileSync(tsPath, 'utf8')
: ''
const metaDeclaration = `export const ${typeName}_meta_v0 = `
const lines = existingContent.split('\n').map(line => line.trim())
const lines = existingContent.split(EOL).map(line => line.trim())
const metaLine = lines.find(line => line.startsWith(metaDeclaration)) || '[]'
let _entries: Array<typeof newEntry> = JSON.parse(metaLine.replace(metaDeclaration, ''))

Expand All @@ -241,8 +242,8 @@ const getFsTypeWriter = (generatedPath: string) =>
`export interface ${typeName}_QueryTypeMap {`,
' ' + _entries
.map(e => `[${JSON.stringify(e.description)}]: ${codegen.writeInterfaceBody(e.properties)}`)
.join('\n')
.replace(/\n/g, '\n '),
.join(EOL)
.replace(/\r?\n/g, EOL + ' '),
`}`,
``,
`export type ${typeName}_UnionType = ${typeName}_QueryTypeMap[keyof ${typeName}_QueryTypeMap]`,
Expand All @@ -254,7 +255,7 @@ const getFsTypeWriter = (generatedPath: string) =>
``,
`${metaDeclaration}${JSON.stringify(_entries)}`,
``,
].join('\n')
].join(EOL)

void fs.writeFileSync(tsPath, contnt, 'utf8')

Expand All @@ -277,7 +278,7 @@ const getFsTypeWriter = (generatedPath: string) =>
...knownTypes.map(name => ` ${name},`),
`}`,
'',
].join('\n')
].join(EOL)
)
}

Expand Down
7 changes: 5 additions & 2 deletions packages/typegen/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.mock('fs', () => {
mockableFunctions.forEach(name => {
mockFunctions[name] = jest.fn()
mockedFsModule[name] = (...args: any[]) => {
const writer = args[0].includes('test/generated') ? mockFunctions[name] : realFs[name]
const writer = args[0].replace(/\\/g, '/').includes('test/generated') ? mockFunctions[name] : realFs[name]
return writer(...args)
}
})
Expand All @@ -23,7 +23,10 @@ expect.addSnapshotSerializer({
test: jest.isMockFunction,
print: v =>
JSON.stringify(v.mock.calls, null, 2)
.split(process.cwd())
.replace(/(\\r)?\\n/g, '__EOL__')
.replace(/\\+/g, '/') // fix Windows backslashes :'(
.replace(/__EOL__/g, '\\n')
.split(process.cwd().replace(/\\+/g, '/'))
.join('[cwd]'),
})

Expand Down

0 comments on commit bc0d749

Please sign in to comment.