What is codemaker?
The codemaker npm package is a utility for generating code programmatically. It provides a set of tools to create and manipulate code structures in a structured and maintainable way.
What are codemaker's main functionalities?
Code Generation
This feature allows you to generate code files programmatically. In this example, a new JavaScript file named 'example.js' is created with a single line of code that logs 'Hello, World!' to the console.
const { CodeMaker } = require('codemaker');
const code = new CodeMaker();
code.openFile('example.js');
code.line('console.log("Hello, World!");');
code.closeFile('example.js');
code.save('./output');
Code Structuring
This feature helps in structuring the code with proper indentation. In this example, a function named 'greet' is created with an indented console log statement inside it.
const { CodeMaker } = require('codemaker');
const code = new CodeMaker();
code.openFile('example.js');
code.indent(() => {
code.line('function greet() {');
code.indent(() => {
code.line('console.log("Hello, World!");');
});
code.line('}');
});
code.closeFile('example.js');
code.save('./output');
Code Formatting
This feature ensures that the generated code is properly formatted. In this example, multiple lines of code are added to 'example.js' to declare variables and log their sum.
const { CodeMaker } = require('codemaker');
const code = new CodeMaker();
code.openFile('example.js');
code.line('const x = 10;');
code.line('const y = 20;');
code.line('const sum = x + y;');
code.line('console.log(sum);');
code.closeFile('example.js');
code.save('./output');
Other packages similar to codemaker
escodegen
Escodegen is a package for generating JavaScript code from an abstract syntax tree (AST). It is more focused on transforming and generating code from ASTs, whereas codemaker provides a more straightforward API for generating code directly.
prettier
Prettier is an opinionated code formatter that supports many languages. While it is primarily used for formatting existing code, it can be used in conjunction with code generation tools like codemaker to ensure the generated code is well-formatted.
babel-generator
Babel Generator is part of the Babel toolchain and is used to generate code from Babel's AST. It is similar to escodegen but is part of the larger Babel ecosystem, which includes tools for parsing, transforming, and generating JavaScript code.
codemaker
Makes code. Well.. just a simple text writer with support for:
- Blocks (indentation/open/close)
- Supports multiple files (with subdirectories)
- Exclusion of files
import { CodeMaker } from 'codemaker'
let maker = new CodeMaker();
maker.openFile('myfile.js');
maker.line('first line');
maker.openBlock('open');
maker.line('second line');
maker.closeBlock();
maker.indent('generic open [');
maker.line('boom');
maker.line('bam');
maker.unindent(']');
maker.closeFile('myfile.js');
let yourfileRelativePath = './relative/subdirs/are/also/supported/yourfile.js';
maker.openFile(yourfileRelativePath);
maker.line('this is your file speaking');
maker.indentation = 10;
maker.openBlockFormatter = s => `(--- ${s} ---`;
maker.closeBlockFormatter = s => `--- ${s} ---)`;
maker.openBlock('block1');
maker.line('block1.line1');
maker.line('block1.line2');
maker.openBlock('block2');
maker.line('block2.line1');
maker.closeBlock('block2 (close)');
maker.line('block1.line3');
maker.closeBlock('block1 (close)');
maker.closeFile(yourfileRelativePath);
maker.openFile('rel/excluded.txt');
maker.line('this file will not be emitted in save()');
maker.closeFile('rel/excluded.txt');
maker.exclude('rel/excluded.txt');
maker.openBlock = function(s) {
this.line(s);
this.open('{');
};
maker.openFile('custom-blocks.cpp');
maker.openBlock('Block1()');
maker.line('L1');
maker.openBlock('Block2()');
maker.line('L2');
maker.closeBlock();
maker.closeBlock();
maker.closeFile('custom-blocks.cpp');
let files = await maker.save('/tmp/source-files');
- /tmp/source-files/myfile.js:
first line
open {
second line
}
generic open [
boom
bam
]
- /tmp/source-files/relative/subdirs/are/also/supported/yourfile.js:
this is your file speaking
(--- block1 ---
block1.line1
block1.line2
(--- block2 ---
block2.line1
--- block2 (close) ---)
block1.line3
--- block1 (close) ---)
- /tmp/source-files/custom-blocks.cpp:
Block1()
{
L1
Block2()
{
L2
}
}
Neat.
Also bundles a couple of case utils from sindresorhus:
maker.toCamelCase(s, ...)
maker.toPascalCase(s, ...)
maker.toSnakCase(s, sep = '_')