code-jinni
v2.1.1
Published
Generic javascript code generator that uses actual code snippets and mantains relationship ebtween artifacts to simplify DOM construction and eventual code generation
Downloads
7
Readme
code-jinni
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.
Install
yarn add code-jinni
Usage
External module
const { createModule } = require('code-generator');
const mod = createModule('foo');
console.log(mod.getPath()); // foo
Relative module
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
Adding import to the module
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');
Adding body to the module
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 = ;})})
Pretty print
console.log(Code.pretty(foo));
output:
const barvar = require("./other/bar");
describe(__filename, () => {
it("should do some test", () => {
const foo = 10;
const bar = false;
});
});