Skip to content

Repository files navigation

Altercode

Altercode is a powerful TypeScript-based CLI tool/library to assist you with large-scale code transformations that can be partially automated but still require human oversight and occasional intervention. Altercode is completely open source and free to use.

Features

  • Easy Configuration - Simple JSON configuration
  • TypeScript Support - First-class TypeScript support
  • Dry Run Mode - Test changes before applying them
  • Pattern Matching - Support for both literal strings and regex patterns
  • Detailed Reporting - Clear summary of all changes made
  • Granular Control - Include/exclude specific files and directories

Installation

Install the package as a development dependency with the below command:

npm i sly-altercode --save-dev

Usage

1. Create a configuration file

Create a config file named altercode.config.json at the root level of your repository:

{
"sourceDir": "./src/app",
"includeExtns": [".js", ".jsx", ".ts", ".tsx"],
"excludeExtns": [".test.ts", ".enum.ts", ".constants.ts"],
"excludeDirs": ["node_modules", "dist", "build"],
"mutations": [
{ "searchString": "page-wrapper", "replaceWith": "page-container" },
{ "searchString": "const\\s+([a-zA-Z]+)\\s*=\\s*\\(\\)\\s*=>\\s*{", "replaceWith": "function $1() {",
"useRegex": true
}
]
}

2. Add the command

Add the below command in the scripts of your package.json

"scripts": {
"assist": "altercode altercode.config.json"
}

3. Run Altercode

You're all set. Now run the assist command to process your code shift tasks:

npm run assist

For a dry run (no actual file changes):

npm run assist -- --dry-run

Configuration Options

OptionTypeDescription
sourceDirstringDirectory to search for files (required)
includeExtnsstring[]File extensions to include (if specified, only these will be processed)
excludeExtnsstring[]File extensions to exclude
excludeDirsstring[]Directories to exclude from processing
mutationsMutation[]Array of search and replace operations

Mutation Options

OptionTypeDescription
searchStringstringThe string or pattern to search for (required)
replaceWithstringThe replacement string (required)
useRegexbooleanWhether to treat searchString as a regex pattern (default: false)

Command Line Options

OptionDescription
--dry-run, -dRun without making actual changes to files

Examples

Basic String Replacement

{
"sourceDir": "./src",
"mutations": [
{ "searchString": "import React from 'react';", "replaceWith": "import * as React from 'react';" }
]
}

Using Regular Expressions

{
"sourceDir": "./src",
"mutations": [
{
"searchString": "const\\s+([a-zA-Z]+)\\s*=\\s*\\(\\)\\s*=>\\s*{",
"replaceWith": "function $1() {",
"useRegex": true
}
]
}

Working with Specific File Types

{
"sourceDir": "./src",
"includeExtns": [".tsx", ".jsx"],
"mutations": [
{ "searchString": "<div className=\"container\">", "replaceWith": "<Container>" },
{ "searchString": "</div>", "replaceWith": "</Container>" }
]
}

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Altercode is a node based CLI toolkit to assist you with code shift.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Sponsor this project

Used by

Contributors

Languages