{"id":12975,"library":"codemaker","title":"CodeMaker - Source Code Generation Utility","description":"`codemaker` is a concise utility designed for programmatic generation of source code. It facilitates the structured creation of text files by supporting features such as automatically indented code blocks, management of multiple output files across subdirectories, and the ability to selectively exclude files from the final output. The package is currently at version 1.128.0 and exhibits an active development and release cadence, primarily delivering bug fixes and minor feature enhancements as part of the larger `jsii` ecosystem. Its core differentiators include a fluent API for building hierarchical code structures and highly customizable block and indentation formatting. Additionally, it bundles widely used string casing utilities like `toCamelCase`, `toPascalCase`, and `toSnakeCase`, providing a complete toolkit for robust and predictable code generation tasks.","status":"active","version":"1.128.0","language":"javascript","source_language":"en","source_url":"https://github.com/aws/jsii","tags":["javascript","typescript"],"install":[{"cmd":"npm install codemaker","lang":"bash","label":"npm"},{"cmd":"yarn add codemaker","lang":"bash","label":"yarn"},{"cmd":"pnpm add codemaker","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES Modules. While Node.js's CommonJS `require` might work with transpilation or specific configurations, native ESM `import` is the recommended and best-supported approach.","wrong":"const { CodeMaker } = require('codemaker')","symbol":"CodeMaker","correct":"import { CodeMaker } from 'codemaker'"},{"note":"The `CodeMaker` class is a named export, not a default export. Always use curly braces for importing it.","wrong":"import CodeMaker from 'codemaker'; // Incorrect default import","symbol":"CodeMaker (default instance)","correct":"const maker = new CodeMaker();"}],"quickstart":{"code":"import { CodeMaker } from 'codemaker';\nimport * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs/promises';\n\nasync function generateCode() {\n  const maker = new CodeMaker();\n  const outputPath = path.join(os.tmpdir(), 'codemaker-example');\n  await fs.mkdir(outputPath, { recursive: true });\n\n  maker.openFile('myfile.js');\n  maker.line('console.log(\"Hello,\");');\n  maker.openBlock('function greet(name) {');\n  maker.line('  console.log(`Hello, ${name}!`);');\n  maker.closeBlock('}');\n  maker.line('greet(\"World\");');\n  maker.closeFile('myfile.js');\n\n  const yourfileRelativePath = 'sub/dir/yourfile.ts';\n  maker.openFile(yourfileRelativePath);\n  maker.indentation = 4; // Set indentation to 4 spaces\n  maker.openBlockFormatter = s => `/* ${s} */ {`;\n  maker.closeBlockFormatter = s => `} /* ${s} */`;\n\n  maker.line('export interface MyInterface {');\n  maker.indent('  ');\n  maker.line('id: string;');\n  maker.line('name: string;');\n  maker.unindent();\n  maker.line('}');\n\n  maker.openBlock('export class MyClass {');\n  maker.line('  constructor(private data: MyInterface) {}');\n  maker.line('');\n  maker.openBlock('  public greet(): void {');\n  maker.line('    console.log(`Hello from ${this.data.name}!`);');\n  maker.closeBlock('  }');\n  maker.closeBlock('}');\n  maker.closeFile(yourfileRelativePath);\n\n  // Exclude a file before saving\n  maker.openFile('temp/excluded.txt');\n  maker.line('This file will not be saved.');\n  maker.closeFile('temp/excluded.txt');\n  maker.exclude('temp/excluded.txt');\n\n  // Save all generated files\n  const files = await maker.save(outputPath);\n  console.log(`Generated files at ${outputPath}:`);\n  files.forEach(file => console.log(`- ${path.relative(outputPath, file)}`));\n\n  // Clean up example directory (optional)\n  // await fs.rm(outputPath, { recursive: true, force: true });\n}\n\ngenerateCode().catch(console.error);","lang":"typescript","description":"This quickstart demonstrates how to initialize `CodeMaker`, open and write to multiple files (including nested directories), use customizable block formatting, control indentation, and exclude files before saving the generated code to a specified output path."},"warnings":[{"fix":"Always test generated output with custom formatters. Consider using the built-in `openBlock()` and `closeBlock()` for standard brace-based blocks before customizing.","message":"Custom block formatters (`openBlockFormatter`, `closeBlockFormatter`, or overriding `openBlock`, `closeBlock` methods) can drastically change the output. Ensure careful testing when implementing custom logic to avoid unintended formatting or syntax errors in the generated code.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure that every call to `maker.openFile(fileName)` is paired with a corresponding `maker.closeFile(fileName)` when you are done writing to that file. The `closeFile` argument is optional but recommended for clarity.","message":"Failing to call `closeFile()` for an opened file before calling `save()` can result in incomplete or unsaved content for that specific file. `CodeMaker` operates on a per-file buffer that needs to be finalized.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Structure your code generation logic such that all calls to `maker.exclude()` are made before the final `await maker.save()` call.","message":"The `exclude(filePath)` method must be called *before* `maker.save()` is invoked for the exclusion to take effect. If `exclude()` is called after `save()`, the file will still be written to disk.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you have correctly initialized `CodeMaker` with `const maker = new CodeMaker();` before using its methods.","cause":"Attempting to call a `CodeMaker` method like `line()` or `openBlock()` on an uninitialized or incorrectly initialized `CodeMaker` instance.","error":"TypeError: Cannot read properties of undefined (reading 'line')"},{"fix":"Review your indentation settings and custom formatter functions. Use `maker.indentation = 2` (or your preferred value) for basic spacing. Debug custom formatters by logging their input/output.","cause":"Misconfiguration of `maker.indentation`, `maker.openBlockFormatter`, `maker.closeBlockFormatter`, or custom overrides of `openBlock`/`closeBlock` methods.","error":"Generated code has incorrect indentation or unexpected block formatting."},{"fix":"Ensure the target directory for `maker.save()` is prepared. If using `fs.mkdir` directly, add `{ recursive: true }`. `CodeMaker.save` itself should handle overwriting files by default, but directory conflicts can arise if not careful.","cause":"The `maker.save()` method might attempt to create directories that already exist if not handled correctly by the underlying filesystem operations, or if the target directory already contains a file with the same name as a generated file and the overwrite logic is not explicit.","error":"Error: EEXIST: file already exists, mkdir '...' (during `maker.save()` in existing directory)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}