The module allows to create a simple way to construct dom of code snippets that can be rendered at the end into javascript code.
One way to use it is to create templates for code generation with dynamic elements that can be related to each other. This would allow, for example, by changing base for some modules, it will modify imports in the module that imports the affected modules.
yarn add code-jinni
const { createModule } = require('code-generator');
const mod = createModule('foo');
console.log(mod.getPath()); // foo
const { createModule, Location } = require('code-generator');
const root = new Location('some/path/to/root');
const foo = createModule(root.relative('foo'));
console.log(mod.getPath()); // some/path/to/root/foo
const { createModule, Location } = require('code-generator');
const root = new Location('some/path/to/root');
const foo = createModule(root.relative('foo'));
const bar = createModule(root.relative('other/bar'));
foo.import('barvar', bar);
console.log(foo.toString()); // >> const barvar = require('./other/bar');
const { createModule, Location, Code, Var } = require('code-generator');
const root = new Location('some/path/to/root');
const foo = createModule(root.relative('foo'));
const bar = createModule(root.relative('other/bar'));
foo.import('barvar', bar);
foo.add(new Code('describe(__filename, () => {'))
.add(`it('should do some test', () => {`)
.add(new Var('foo', 10))
.add(new Var('bar', false))
.add('})')
.add(new Code('})'))
console.log(foo.toString());
// >> const barvar = require('./other/bar');describe(__filename, () => {it('should do some test', () => {const foo = 10;const bar = ;})})
console.log(Code.pretty(foo));
output:
const barvar = require("./other/bar");
describe(__filename, () => {
it("should do some test", () => {
const foo = 10;
const bar = false;
});
});