
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
js-templatizer
Advanced tools
Transform any file into new file with a template of the original file. The new file will also include a function that enables you to generate additional new files from the template.
Transform any file into new file with a template of the original file.
The new file will include a function that enables you to generate additional new files from the template.
You can also turn any text in the original file you're templatizing into variables, which will become parameters in the function that's generated.
npm i js-templatizer --save-dev
Let's say we have a file called userModel.js we'd like to templatize so we can quickly reuse the code we've already written.
In the same directory as userModel.js, we have the following in templatizer.js:
import { templatize } from 'js-templatizer';
templatize({
fileToTemplatize: 'userModel.js',
convertToVariables: { user: 'param1' },
nameOfNewFile: 'modelGenerator',
});
Note that the fileToTemplatize property would need to be a relative path to userModel.js if templatizer.js and userModel.js were not in the same directory.
For example, if userModel.js was one directory level up, the first argument to the function call in the example above would be '../userModel.js'.
Running node templatizer.js would create a new file called modelGenerator.js containing the following:
import fs from 'fs/promises';
export const generateNewFileText = (param1) => {
return `import mongoose from 'mongoose';\n` + `\n` + `const UserSchema = new mongoose.Schema({\n` + ` email: {\n` + ` type: String,\n` + ` required: true,\n` + ` },\n` + ` password: {\n` + ` type: String,\n` + ` required: true,\n` + ` },\n` + ` name: {\n` + ` type: String,\n` + ` },\n` + ` age: {\n` + ` type: Number,\n` + ` },\n` + `});\n` + `\n` + `const User = mongoose.model('${param1}', UserSchema);\n` + `export default User;\n`;
};
export const generateNewFile = async (param1) => {
const fileText = generateNewFileText(param1);
try {
const promise = await fs.writeFile('newFile.js', fileText);
return promise;
} catch (error) {
console.error(error);
}
};
// generateNewFile('param1');
At the bottom of the new file you'll notice the generateNewFile function call that is commented out. Uncomment it and pass in any parameters you'd like to include. Then, in this example, you'd call node modelGenerator.js to generate a new file from your template.
The templatize method accepts an object with the following properties:
The file to templatize. If this file is in the same directory as the file that's calling the templatize method, then you can just write the file name (including the file extension). If they're in different directories, you'll need to write a relative path to the file to templatize.
This object's keys will be the text in the file you're templatizing you'd like to turn into variables. The corresponding values will become the names of the variables in the new template. These variables will also become parameters in the function that's generated.
Allows you to name the new file the templatize method will generate. If this property is not included, the new file will be named newTemplate(randomNumber).js.
If you get an error using the import-export syntax for ES modules in the examples above, add the following to your package.json file:
{
"type": "module"
}
FAQs
Transform any file into new file with a template of the original file. The new file will also include a function that enables you to generate additional new files from the template.
We found that js-templatizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.