Skip to content

Commit ea10219

Browse files
committed
🔨 Refactors init logic + adds an init command to the cli"
1 parent 7dc1e9d commit ea10219

File tree

7 files changed

+65
-17
lines changed

7 files changed

+65
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"types:check": "tsc --noEmit --skipLibCheck",
2323
"monorepo:check": "manypkg check",
2424
"monorepo:fix": "manypkg fix && preconstruct fix",
25-
"init:codemods": "ts-node packages/initializer/src/index.ts",
25+
"init:codemods": "ts-node scripts/initialize.ts",
2626
"start:codemods": "node packages/cli/bin/codeshift-cli.js",
2727
"validate:codemods": "ts-node packages/validator/src/index.ts ./community",
2828
"release:codemods": "ts-node packages/publisher/src/index.ts ./community ./.tmp",

packages/cli/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chalk from 'chalk';
22
import main from './main';
33
import list from './list';
4+
import init from './init';
45
import {
56
ValidationError,
67
NoTransformsExistError,
@@ -65,6 +66,21 @@ program
6566
.description('list available codemods for provided packages')
6667
.action(packageNames => list(packageNames));
6768

69+
program
70+
.command('init [path]')
71+
.description('create a new codemod package')
72+
// FIXME: Commander seems to have issues parsing the paths and arguments
73+
.option('--package-name <name>', 'Name of the package')
74+
.option('--version <version>', 'Target version')
75+
.action((path, options) => init(options.packageName, options.version, path))
76+
.addHelpText(
77+
'after',
78+
`
79+
Examples:
80+
$ npx @codeshift/cli init --package-name foobar --version 10.0.0 ~/Desktop
81+
`,
82+
);
83+
6884
program.exitOverride();
6985

7086
program.parseAsync(process.argv).catch(e => {

packages/cli/src/init.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { initDirectory } from '@codeshift/initializer';
2+
3+
export default async function init(
4+
packageName: string,
5+
version: string,
6+
targetPath: string = '.',
7+
) {
8+
initDirectory(packageName, version, targetPath);
9+
10+
console.log(
11+
`🚚 New codemod package created at: ${targetPath}/${packageName}`,
12+
);
13+
}

packages/initializer/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "@codeshift/initializer",
3-
"private": true,
43
"version": "0.1.0",
54
"main": "dist/codeshift-initializer.cjs.js",
65
"types": "dist/codeshift-initializer.cjs.d.ts",

packages/initializer/src/index.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ import fs from 'fs-extra';
22
import semver from 'semver';
33
import * as recast from 'recast';
44

5-
function main(packageName: string, version: string) {
6-
if (!packageName) throw new Error('Package name was not provided');
7-
if (!version) throw new Error('Version was not provided');
8-
5+
export function initDirectory(
6+
packageName: string,
7+
version: string,
8+
targetPath: string = './',
9+
) {
910
if (!semver.valid(version)) {
1011
throw new Error(
1112
`Provided version ${version} is not a valid semver version`,
1213
);
1314
}
1415

15-
const communityDirectoryPath = `${__dirname}/../../../community`;
16-
const safePackageName = packageName.replace('/', '__');
17-
const codemodBasePath = `${communityDirectoryPath}/${safePackageName}`;
18-
const codemodPath = `${codemodBasePath}/${version}`;
19-
const configPath = `${codemodBasePath}/codeshift.config.js`;
16+
const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
17+
const codemodPath = `${basePath}/${version}`;
18+
const configPath = `${basePath}/codeshift.config.js`;
2019
const motionsPath = `${codemodPath}/motions`;
2120

2221
fs.mkdirSync(codemodPath, { recursive: true });
@@ -88,10 +87,4 @@ function main(packageName: string, version: string) {
8887
recast.prettyPrint(ast, { quote: 'single', trailingComma: true }).code,
8988
);
9089
}
91-
92-
console.log(
93-
`🚚 New codemod package created at: community/${safePackageName}/${version}`,
94-
);
9590
}
96-
97-
main(process.argv[2], process.argv[3]);

scripts/initialize.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { initDirectory } from '@codeshift/initializer';
2+
3+
export function main(packageName: string, version: string) {
4+
const path = `${__dirname}/../community`;
5+
6+
if (!packageName) throw new Error('Package name was not provided');
7+
if (!version) throw new Error('Version was not provided');
8+
9+
initDirectory(packageName, version, path);
10+
11+
console.log(
12+
`🚚 New codemod package created at: community/${packageName}/${version}`,
13+
);
14+
}
15+
16+
main(process.argv[2], process.argv[3]);

website/docs/api/codeshift-cli.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,14 @@ Print a list of available codemods for a single package
117117
Print a list of available codemods for multiple packages
118118

119119
- `@codeshift/cli list mylib, @material-ui/button`
120+
121+
### init
122+
123+
Generates a new codemod at your desired path
124+
125+
**example:**
126+
127+
Create a new codemod package called foobar with a transform for version 10
128+
on the Desktop
129+
130+
- `@codeshift/cli init --packageName="foobar" --version"10.0.0" ~/Desktop`

0 commit comments

Comments
 (0)