Skip to content

Commit

Permalink
refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gurza committed Sep 20, 2023
1 parent 6665027 commit 7ee575e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 60 deletions.
32 changes: 1 addition & 31 deletions src/metadata/compileView.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PathLike } from 'fs'
import fs from 'fs/promises'
import path from 'path'
import { TEMPLATES, VIEWS } from './constants'
import { Template } from './getTemplates'

const IMPORT_REGEX = /--\s*#import\s+(\S+\.sql)/g

Expand Down Expand Up @@ -69,34 +69,4 @@ export const prepareQuery = (str: string) => {
return cleaned
}

export const getTemplates = async (templatesPath: PathLike) => {
const templates = new Map<string, Template>()

try {
const dir = await fs.opendir(templatesPath, { recursive: true })
for await (const dirent of dir)
if (dirent.isFile()) {
const filePath = dirent.path
if (path.extname(filePath) !== '.sql') continue
const content = await fs.readFile(filePath, 'utf-8')
templates.set(path.relative(templatesPath.toString(), filePath), {
filePath,
content
})
}
} catch (err) {
// FIXME: catch different error types
console.warn(err)
}

return templates
}

type Template = {
filePath: string
content: string
root?: string
lastProccessedBy?: string
}

export default compileView
3 changes: 2 additions & 1 deletion src/metadata/extractMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { PathReporter } from 'io-ts/lib/PathReporter'
import path from 'path'
import yaml from 'yaml'
import { EntityConfig, Schema } from '.'
import compileView, { getTemplates } from './compileView'
import compileView from './compileView'
import { CONFIGS, QUERY, TEMPLATES, VIEWS } from './constants'
import getTemplates from './getTemplates'

async function processMetadata(directory: string): Promise<Schema> {
const schema: Schema = new Map()
Expand Down
35 changes: 35 additions & 0 deletions src/metadata/getTemplates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { PathLike } from 'fs'
import fs from 'fs/promises'
import path from 'path'

const getTemplates = async (templatesPath: PathLike) => {
const templates = new Map<string, Template>()

try {
const dir = await fs.opendir(templatesPath, { recursive: true })
for await (const dirent of dir)
if (dirent.isFile()) {
const filePath = dirent.path
if (path.extname(filePath) !== '.sql') continue
const content = await fs.readFile(filePath, 'utf-8')
templates.set(path.relative(templatesPath.toString(), filePath), {
filePath,
content
})
}
} catch (err) {
// FIXME: catch different error types
console.warn(err)
}

return templates
}

export type Template = {
filePath: string
content: string
root?: string
lastProccessedBy?: string
}

export default getTemplates
29 changes: 1 addition & 28 deletions tests/metadata/compileView.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
import fs from 'fs/promises'
import { getTemplates, prepareQuery } from '../../src/metadata/compileView'

describe('getTemplates', () => {
beforeAll(() => {
jest.mock('fs')
})

beforeEach(() => {
jest.clearAllMocks()
})

it('returns an empty map if the path does not exist', async () => {
fs.stat = jest.fn().mockRejectedValueOnce(new Error('Path not accessible'))

const result = await getTemplates('/tmp/metadata/templates')
expect(result.size).toBe(0)
})

it('returns an empty map if the path is not a directory', async () => {
fs.stat = jest
.fn()
.mockResolvedValueOnce({ isDirectory: () => false } as any)

const result = await getTemplates('/tmp/metadata/templates')
expect(result.size).toBe(0)
})
})
import { prepareQuery } from '../../src/metadata/compileView'

describe('prepareQuery', () => {
it('appends a semicolon if not present', () => {
Expand Down
28 changes: 28 additions & 0 deletions tests/metadata/getTemplates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'fs/promises'
import getTemplates from '../../src/metadata/getTemplates'

describe('getTemplates', () => {
beforeAll(() => {
jest.mock('fs')
})

beforeEach(() => {
jest.clearAllMocks()
})

it('returns an empty map if the path does not exist', async () => {
fs.stat = jest.fn().mockRejectedValueOnce(new Error('Path not accessible'))

const result = await getTemplates('/tmp/metadata/templates')
expect(result.size).toBe(0)
})

it('returns an empty map if the path is not a directory', async () => {
fs.stat = jest
.fn()
.mockResolvedValueOnce({ isDirectory: () => false } as any)

const result = await getTemplates('/tmp/metadata/templates')
expect(result.size).toBe(0)
})
})

0 comments on commit 7ee575e

Please sign in to comment.