CodeWriter is a fluent Node library that can be used to generate code blocks. It is typically used by code generation tools to simplify the task of building the generated code.
CodeWriter is similar to concepts like StringBuilder
's or StringWriter
's in other languages. Instead of using string concatenation to build the generated code, CodeWriter adds support for code-specific concepts like indentation and language-specific comments.
npm install --save-dev codewriter
const CodeWriter = require('codewriter');
const options = { indentSize: 4 };
const writer = new CodeWriter(options);
// Use CodeWriter methods to build the code
const code = writer.toCode();
CodeWriter ships with a Typescript definition file, which provides strong-typing and compile-time support.
import { CodeWriter, CodeWriterOptions } from 'codewriter';
const options: CodeWriterOptions = { indentSize: 4 };
const writer = new CodeWriter(options);
// Use CodeWriter methods to build the code
const code: string = writer.toCode();
CodeWriter allows some options to be specified in the constructor to customize how the code is generated.
Sample options for generating JavaScript code
Through the OptionsLibrary
class, CodeWriter also provides predefined options for certain common languages and language families.
Generate empty C# console program
You can view the source code comments directly from GitHub.
If you are using Typescript, you can install the package locally to get code completion support from supported IDE's and editors.
# Clone repo from GitHub
git clone https://github.com/JeevanJames/codewriter.git .
# Install dependencies
npm install
# Build code
npm run build
# Continuously watch for code changes and build
npm run watch
# Lint code
npm run lint
# The codebase has a test-harness directory that contains an index.ts file.
# You can update this file to quickly test code against the framework.
# To run the test-harness, run the following command:
npm run test-harness