Files
pgbackweb/scripts/sqlc-prebuild.mjs
Luis Eduardo Jeréz Girón 09b0ff0283 Translate sqlc-prebuild script
2024-10-14 01:16:50 -06:00

40 lines
1.2 KiB
JavaScript

#!/usr/bin/env node
import path from 'path'
import fse from 'fs-extra'
import { fileURLToPath } from 'url'
import { glob } from 'glob'
const sourceGlobs = [
'./internal/service/**/*.sql'
]
const outputFilePath = './internal/database/dbgen/queries.gen.sql'
// Obtains the project's root directory
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const rootDir = path.join(__dirname, '..')
const absoluteOutputFilePath = path.join(rootDir, outputFilePath)
try {
// Find all files that match the source globs
const files = []
for (const sourceGlob of sourceGlobs) {
const foundFiles = await glob(path.join(rootDir, sourceGlob))
files.push(...foundFiles)
}
// Read each file and concatenate them into a single string
let outFileContent = `-- This file is auto-generated by ${__filename}. DO NOT EDIT.\n\n`
for (const file of files) {
const content = await fse.readFile(file, 'utf-8')
outFileContent += `-- file: ${file}\n\n${content}\n\n`
}
await fse.ensureDir(path.dirname(absoluteOutputFilePath))
await fse.outputFile(absoluteOutputFilePath, outFileContent)
} catch (error) {
console.error('Error creating SQLC prebuild:', error)
}