node.jsimportdependency-injectioncode-injectioninject

how to inject one line of code into all my files in my nodejs project?


I need to inject this line of code automatically every time I create a new file, is this possible?

import 'reflect-metadata'

My current problem is that every time I create a file and I need to import the 'type-graphql' module I end up having to put that line of code I mentioned, and it's very annoying ;(

I welcome suggestions to automate this in some way.


Solution

  • Assuming you cannot rely on your Integrated Development Environment (IDE) or code editor to insert your custom code when it opens/creates a file, I know of no way to accomplish your goal other than monkey-patching NodeJS' intrinsic module loading function (very much not recommended) or modifying the NodeJS source code to add your custom behavior to the AST at runtime (which fundamentally creates your own custom, non-standard version of NodeJS.)

    Many IDEs support file templates (used when creating a new file) or snippet macros you can trigger yourself to insert text.

    For example, you can add File Templates to VSCode which is a "Visual Studio code extenstion(sic) that allows to quickly create new files based on defined templates" written by Bruno Paz

    And the VSCode snippet feature can be configured to add custom text to any file with special handling for embedded variables. See Snippets in Visual Studio Code for more.

    Another strategy you might consider would be to write a script you could use to create a new file using a custom template. So, for instance, assume you keep your template files under ~/.templates and you name them like template.{EXT} where EXT is the file extension you'd like to use when creating new files with the same extension.

    You could create a NodeJS script, located in a directory accessible via your shell's PATH environment variable, named mkfile like:

    #!/usr/bin/env node
    
    // note cannot use `import` here as this is not a module.
    
    const path = require('path')
    const { readFileSync, writeFileSync } = require('fs')
    
    const targetFilename = process.argv.slice(2).shift()
    if (!targetFilename) {
      console.log('usage: mkfile FILENAME - where FILENAME is the name of the file you want to create.')
      exit(1)
    }
    const targetFileExt = path.extname(targetFilename)
    const homeDir = process.env.HOME
    const templateDir = `${homeDir}/.templates`
    const templateFilename = `${templateDir}/template${targetFileExt}`
    
    let content = ''
    try {
      content = readFileSync(templateFilename).toString()
    } catch (err) {
      // error purposefully ignored - no such template file found
    }
    writeFileSync(targetFilename, content)
    
    console.log(`wrote ${content.length} chars to ${targetFilename}`)
    

    After making mkfile executable (perhaps chmod +x mkfile which will work on MacOS or Linux), and assuming you have a template file named ~/.templates/template.js that contains your custom code, you can create a new file using your template called foo.js by invoking mkfile foo.js.