Skip to content

Instantly share code, notes, and snippets.

@Luna-Klatzer
Last active June 27, 2022 19:12
Show Gist options
  • Save Luna-Klatzer/4661ec5eaf6cdaa55e5614cfc7f90d4d to your computer and use it in GitHub Desktop.
Save Luna-Klatzer/4661ec5eaf6cdaa55e5614cfc7f90d4d to your computer and use it in GitHub Desktop.
Simple script for buidling dot.js template files (`.jst`) and generating the js template functions.
import * as dot from 'dot';
import * as fs from "fs";
import * as rimraf from 'rimraf';
const source: string = `${__dirname }/templates`;
const destination: string = `${__dirname}/dot-build`;
// Generate destination files from source template files
(async () => {
// If the destination folder exists, remove it to ensure it does not contain old files
if (fs.existsSync(destination)){
rimraf.sync(destination, {});
}
// Create new empty destination directory
await fs.promises.mkdir(destination);
dot.process({
destination: destination,
path: source,
});
let templates = {};
for (const file of fs.readdirSync(destination)) {
if (file.endsWith(".js")) {
const fileName = file.split('.')[0];
templates[fileName] = await import(`${destination}/${file}`);
// Log file
console.log(`Loaded template '${fileName}'`);
}
}
return;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment