Skip to content

Commit

Permalink
feat: add function to create files using templates
Browse files Browse the repository at this point in the history
  • Loading branch information
enxg committed Sep 12, 2021
1 parent ffe5a69 commit e95db50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/functions/CreateFileFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { readFile, writeFile } from 'fs/promises';

export function CreateFileFromTemplate(template: string, target: string, params?: Record<string, string>, custom = false) {
return new Promise(async (resolve, reject) => {
const location = custom ? '' : `${__dirname}/../templates/`;

let output = await readFile(`${location}${template}`, 'utf8');
if (!output) return reject(new Error("Can't read file."));
if (params) {
for (const param of Object.entries(params)) {
output = output.replaceAll(`{{${param[0]}}}`, param[1]);
}
}

await writeFile(target, output).catch(reject);

return resolve(true);
});
}
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './CommandExists';
export * from './CreateFileFromTemplate';

0 comments on commit e95db50

Please sign in to comment.