@baseplate-dev/sync
Version:
Library for syncing Baseplate descriptions
27 lines • 1.17 kB
JavaScript
import fs from 'node:fs/promises';
import path from 'node:path';
import { createBuilderActionCreator } from '#src/output/builder-action.js';
import { normalizePathToOutputPath } from '#src/utils/canonical-path.js';
function applyReplacements(contents, replacements) {
let result = contents;
for (const [key, value] of Object.entries(replacements)) {
result = result.replaceAll(new RegExp(key, 'g'), value);
}
return result;
}
export const copyFileAction = createBuilderActionCreator((options) => async (builder) => {
const { destination, source, skipFormatting, shouldNeverOverwrite, replacements, } = options;
const templatePath = path.join(builder.generatorInfo.baseDirectory, 'templates', source);
const fileContents = await fs.readFile(templatePath, 'utf8');
const replacedFileContents = applyReplacements(fileContents, replacements ?? {});
builder.writeFile({
id: normalizePathToOutputPath(destination),
destination,
contents: replacedFileContents,
options: {
skipFormatting,
shouldNeverOverwrite,
},
});
});
//# sourceMappingURL=copy-file-action.js.map