Skip to content

Commit 81717c0

Browse files
committed
feat: add template
0 parents  commit 81717c0

File tree

16 files changed

+388
-0
lines changed

16 files changed

+388
-0
lines changed

CONTRIBUTING.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Contributing
2+
3+
Thanks for helping users adopt the latest features with your codemods!
4+
5+
### Before You Open a PR
6+
7+
- **Issue**: Check for an existing issue, or open one first.
8+
- **Safety**: Codemods must be safe, predictable, and idempotent (running twice should not change code again). Avoid mixing patterns with different safety levels.
9+
- **Naming**: In `codemod.yaml`, the codemod name must start with `@<scope>`, where `<scope>` is this repo's GitHub org.
10+
- **Tests**: Add multiple fixtures (positive and negative).
11+
- **Docs**: Update the README for your codemod.
12+
13+
### Development
14+
15+
- Scaffold a new codemod:
16+
```bash
17+
npx codemod@latest init
18+
```
19+
- Test your codemod locally;
20+
```bash
21+
cd /path/to/sample/project
22+
npx codemod workflow run -w /path/to/my-codemod/workflow.yaml
23+
```
24+
### Project Layout
25+
26+
- Place all codemods in the `codemods/` directory.
27+
28+
### Checks
29+
30+
- Lint/format: npm run check (Biome)
31+
- Types: npm run typecheck
32+
33+
### Pull Requests
34+
35+
- Describe the codemod and its migration use case.
36+
- Follow Conventional Commits:
37+
38+
| Type | Usage |
39+
|----------|--------------------------------------|
40+
| feat | New codemod or capability |
41+
| fix | Bugfix in a transform or test |
42+
| docs | Documentation-only changes |
43+
| refactor | Non-feature, non-bugfix code changes |
44+
| test | Add or update fixtures/tests |
45+
| chore | Tooling, CI, formatting, repo hygiene|
46+
47+
### License
48+
49+
By contributing, you agree that your work will be licensed under the MIT License.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [YEAR] [ORGANIZATION]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
> **REMOVE THIS SECTION ONCE THE REPO IS SET UP.**
2+
>
3+
> Framework/SDK maintainers: This template includes setup guides, utilities, and a GitHub Action to help you and your community build and publish codemods with ease.
4+
>
5+
> ## One-time setup
6+
> NOTE: You need repo creation privileges in your org and permission to install GitHub apps on repos. If you don’t, you can request access from your org admin during setup.
7+
> 1. Use this template and create a codemods repo in your org.
8+
> 2. Sign up at [Codemod](https://app.codemod.com) with your GitHub account.
9+
> 3. Install the Codemod GitHub app:
10+
> 1. Click your profile photo (top left) and select "Add organization"
11+
> 2. Pick GitHub, choose your org and the new codemods repo. This installs the Codemod GitHub App and reserves a **scope** with your org name.
12+
> - Benefit: Only members of your org can publish codemods with a name that starts wihh your scope.
13+
> - **Important**: In `codemod.yaml`, the name must start with your scope, otherwise it won’t appear when users filter for your scope in the registry.
14+
> 4. In Codemod, switch from personal account to org account and generate a [Codemod API key](https://app.codemod.com/api-keys).
15+
> 5. In your GitHub repo: **Settings → Secrets & variables → actions**
16+
> 1. Create a repository secret.
17+
> - Name: `CODEMOD_API_KEY`
18+
> - Value: the key from step 1.
19+
>
20+
> ✅ Done! After a codemod PR is merged, you can trigger the GitHub Action to auto-publish it to the [Codemod Registry](https://app.codemod.com/registry) under your org scope. See [Node.js codemods](https://codemod.link/nodejs-official) for an example.
21+
>
22+
> To build a codemod, clone your new repo locally and use [Codemod MCP](https://docs.codemod.com/more-resources/codemod-mcp) in your IDE to replace the boilerplate with a codemod generated using AI. You can also run `npx codemod@latest init` for the initial scaffolding, or use [Codemod Studio](https://codemod.studio) for its live codemod runner and AST viewer.
23+
24+
---
25+
Official <FRAMEWORK_OR_SDK_OR_ORG> codemods to help users adopt new features and handle breaking changes with ease.
26+
27+
Community contributions are welcome and appreciated! Check open issues for codemods to build, or open a new one if something’s missing. See the [contribution guide](./CONTRIBUTING.md) for details.
28+
29+
## Running codemods
30+
> [!CAUTION]
31+
> Codemods modify code! Run them only on Git-tracked files, and commit or stash changes first.
32+
33+
### From the registry
34+
Recommended for the best UX. This downloads the package from the [Registry](https://app.codemod.com/registry).
35+
36+
```bash
37+
npx codemod@latest <codemod-name>
38+
```
39+
40+
For example:
41+
42+
```bash
43+
npx codemod@latest @nodejs/tmpDir-to-tmpdir
44+
```
45+
46+
### From source
47+
48+
```bash
49+
npx codemod workflow run -w /path/to/folder/containing/workflow.yaml
50+
```
51+
52+
> [!NOTE]
53+
> By default, codemods run in the current folder. Add `-t /target/path` to change it.
54+
55+
See the [Codemod docs](https://go.codemod.com/cli-docs) for all CLI commands and options.
56+
57+
## License
58+
59+
MIT

biome.jsonc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.2.2/schema.json",
3+
"files": {
4+
"includes": ["."],
5+
"experimentalScannerIgnores": ["codemods/**/tests/**/fixtures/**"]
6+
},
7+
"formatter": {
8+
"enabled": true,
9+
"lineWidth": 100
10+
},
11+
"linter": {
12+
"enabled": true
13+
},
14+
"javascript": {
15+
"formatter": {
16+
"quoteStyle": "double"
17+
}
18+
}
19+
}

codemods/.gitkeep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Dependencies
2+
node_modules/
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
7+
# Build artifacts
8+
target/
9+
dist/
10+
build/
11+
12+
# Temporary files
13+
*.tmp
14+
*.temp
15+
.cache/
16+
17+
# Environment files
18+
.env
19+
.env.local
20+
21+
# IDE files
22+
.vscode/
23+
.idea/
24+
*.swp
25+
*.swo
26+
27+
# OS files
28+
.DS_Store
29+
Thumbs.db
30+
31+
# Package bundles
32+
*.tar.gz
33+
*.tgz
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# jssg-codemod
2+
3+
Transform legacy code patterns
4+
5+
## Installation
6+
7+
```bash
8+
# Install from registry
9+
codemod run jssg-codemod
10+
11+
# Or run locally
12+
codemod run -w workflow.yaml
13+
```
14+
15+
## Usage
16+
17+
This codemod transforms typescript code by:
18+
19+
- Converting `var` declarations to `const`/`let`
20+
- Removing debug statements
21+
- Modernizing syntax patterns
22+
23+
## Development
24+
25+
```bash
26+
# Test the transformation
27+
npm test
28+
29+
# Validate the workflow
30+
codemod validate -w workflow.yaml
31+
32+
# Publish to registry
33+
codemod login
34+
codemod publish
35+
```
36+
37+
## License
38+
39+
MIT
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
schema_version: "1.0"
2+
3+
name: "jssg-codemod"
4+
version: "0.1.0"
5+
description: "Transform legacy code patterns"
6+
author: "Author <[email protected]>"
7+
license: "MIT"
8+
workflow: "workflow.yaml"
9+
category: "migration"
10+
11+
targets:
12+
languages: ["typescript"]
13+
14+
keywords: ["transformation", "migration"]
15+
16+
registry:
17+
access: "public"
18+
visibility: "public"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "jssg-codemod",
3+
"version": "0.1.0",
4+
"description": "Transform legacy code patterns",
5+
"type": "module",
6+
"devDependencies": {
7+
"@codemod.com/jssg-types": "^1.0.3",
8+
"typescript": "^5.8.3"
9+
},
10+
"scripts": {
11+
"test": "npx codemod@latest jssg test -l typescript ./scripts/codemod.ts",
12+
"check-types": "npx tsc --noEmit"
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { SgRoot } from "codemod:ast-grep";
2+
import type TS from "codemod:ast-grep/langs/typescript";
3+
4+
async function transform(root: SgRoot<TS>): Promise<string> {
5+
const rootNode = root.root();
6+
7+
const nodes = rootNode.findAll({
8+
rule: {
9+
pattern: "var $VAR = $VALUE",
10+
},
11+
});
12+
13+
const edits = nodes.map((node) => {
14+
const varName = node.getMatch("VAR")?.text();
15+
const value = node.getMatch("VALUE")?.text();
16+
return node.replace(`const ${varName} = ${value}`);
17+
});
18+
19+
const newSource = rootNode.commitEdits(edits);
20+
return newSource;
21+
}
22+
23+
export default transform;

0 commit comments

Comments
 (0)