@husien/code_injector

1.0.2 • Public • Published

@husien/code_injector

@husien/code_injector is a simple npm package that allows you to inject code into JavaScript files based on specified tags within the file. With this package, you can easily insert code snippets or make modifications to existing JavaScript files without manually editing them.
The reason for me making this package was that I couldn't find any easy injection methods. I hope this can serve as one stop shop for injection. Also thinking about adding Mocha templates, Jest Templates so that we can write test's on runtime and also execute them.

Installation

You can install @husien/code_injector using npm:

npm install @husien/code_injector
const { inject } = require("@husien/code_injector");

inject(path, codeToInject, tagToInjectAt);

Tag to inject can be a comment inside a file above which the code will be injected
For example if tagToInjectAt is //tag
and this is executed

inject(
  fielpath,
  ` describe("multiply", () => {
    it("should correctly multiply two numbers", () => {
      const result = MathUtils.multiply(2, 3);
      assert.equal(result, 6);
    });
    it("should return 0 if any operand is 0", () => {
      const result = MathUtils.multiply(2, 0);
      assert.equal(result, 0);
    });
  });`,
  "//tag"
);

then the result will be

//Before
// Existing code

//tag

// Remaining code
//After
// Existing code
describe("multiply", () => {
  it("should correctly multiply two numbers", () => {
    const result = MathUtils.multiply(2, 3);
    assert.equal(result, 6);
  });
  it("should return 0 if any operand is 0", () => {
    const result = MathUtils.multiply(2, 0);
    assert.equal(result, 0);
  });
});
//tag

// Remaining code

The file then can be executed on runtime beacause the cache of the file is also cleared simultaneously on injection

Package Sidebar

Install

npm i @husien/code_injector

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

3.79 kB

Total Files

4

Last publish

Collaborators

  • husien