code-jinni

2.1.1 • Public • Published

code-jinni

codecov Build Status NPM Downloads Known Vulnerabilities Greenkeeper badge

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;
    });
});

Readme

Keywords

Package Sidebar

Install

npm i code-jinni

Weekly Downloads

3

Version

2.1.1

License

MIT

Unpacked Size

12.9 kB

Total Files

15

Last publish

Collaborators

  • dimichgh