diff --git a/src/metadata/compileView.ts b/src/metadata/compileView.ts index e0fd81f..b055e40 100644 --- a/src/metadata/compileView.ts +++ b/src/metadata/compileView.ts @@ -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 @@ -69,34 +69,4 @@ export const prepareQuery = (str: string) => { return cleaned } -export const getTemplates = async (templatesPath: PathLike) => { - const templates = new Map() - - 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 diff --git a/src/metadata/extractMetadata.ts b/src/metadata/extractMetadata.ts index 8ff9b79..9401b94 100644 --- a/src/metadata/extractMetadata.ts +++ b/src/metadata/extractMetadata.ts @@ -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 { const schema: Schema = new Map() diff --git a/src/metadata/getTemplates.ts b/src/metadata/getTemplates.ts new file mode 100644 index 0000000..dfe853a --- /dev/null +++ b/src/metadata/getTemplates.ts @@ -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() + + 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 diff --git a/tests/metadata/compileView.test.ts b/tests/metadata/compileView.test.ts index f8680b5..bc4de37 100644 --- a/tests/metadata/compileView.test.ts +++ b/tests/metadata/compileView.test.ts @@ -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', () => { diff --git a/tests/metadata/getTemplates.test.ts b/tests/metadata/getTemplates.test.ts new file mode 100644 index 0000000..7c723c8 --- /dev/null +++ b/tests/metadata/getTemplates.test.ts @@ -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) + }) +})