@baseplate-dev/sync
Version:
Library for syncing Baseplate descriptions
32 lines • 1.09 kB
JavaScript
import { handleFileNotFoundError } from '@baseplate-dev/utils/node';
import fs from 'node:fs/promises';
/**
* Reads a template file source as a string
*
* @param source The source of the template file
* @returns The contents of the template file
*/
export async function readTemplateFileSource(source) {
return readTemplateFileSourceBuffer(source).then((buffer) => buffer.toString());
}
/**
* Reads a template file source as a buffer
*
* @param source The source of the template file
* @returns The contents of the template file as a buffer
*/
export async function readTemplateFileSourceBuffer(source) {
if ('path' in source) {
const fileContents = await fs
.readFile(source.path)
.catch(handleFileNotFoundError);
if (!fileContents) {
throw new Error(`Could not find template file in project: ${source.path}`);
}
return fileContents;
}
return typeof source.contents === 'string'
? Buffer.from(source.contents)
: source.contents;
}
//# sourceMappingURL=read-template-file-source.js.map