Skip to content

Commit 02b0a19

Browse files
Merge pull request #85 from CodeshiftCommunity/init-command
🔨 Refactors init command to allow staged inits
2 parents 4503e07 + 728624f commit 02b0a19

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

.changeset/sharp-forks-tap.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@codeshift/cli': patch
3+
'@codeshift/initializer': patch
4+
---
5+
6+
Init command can now be called without the transform or preset flag and output an empty directory

packages/cli/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ program
8282
'after',
8383
`
8484
Examples:
85+
# Initializes an empty codeshift package at the ~/Desktop directory
86+
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop
87+
8588
# Initializes a new codeshift package with a transform for 10.0.0
8689
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop
8790

packages/cli/src/init.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import path from 'path';
2-
import { initDirectory } from '@codeshift/initializer';
2+
import { initDirectory, initTransform } from '@codeshift/initializer';
33

44
export default async function init(
55
packageName: string,
66
transform?: string,
77
preset?: string,
88
targetPath: string = '.',
99
) {
10-
if (transform) {
11-
initDirectory(packageName, transform, 'version', targetPath);
12-
}
10+
initDirectory(packageName, targetPath);
1311

14-
if (preset) {
15-
initDirectory(packageName, preset, 'preset', targetPath);
16-
}
12+
if (transform) initTransform(packageName, transform, 'version', targetPath);
13+
if (preset) initTransform(packageName, preset, 'preset', targetPath);
1714

1815
console.log(
1916
`🚚 New codemod package created at: ${path.join(targetPath, packageName)}`,

packages/initializer/src/index.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs-extra';
2+
import path from 'path';
23
import semver from 'semver';
34
import * as recast from 'recast';
45
import { version as utilVersion } from '@codeshift/utils/package.json';
@@ -98,25 +99,47 @@ function updateConfig(
9899

99100
export function initDirectory(
100101
packageName: string,
101-
transform: string,
102-
type: 'version' | 'preset',
103102
targetPath: string = './',
104103
isReduced: boolean = false,
105104
) {
106-
if (type === 'version' && !semver.valid(transform)) {
105+
106+
const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
107+
const configPath = `${basePath}${
108+
!isReduced ? '/src' : ''
109+
}/codeshift.config.js`;
110+
111+
fs.copySync(`${__dirname}/../template${isReduced ? '/src' : ''}`, basePath, {
112+
filter: (src) => !src.includes('src/codemod')
113+
});
114+
115+
if (!isReduced) {
116+
fs.writeFileSync(path.join(basePath, 'package.json'), getPackageJson(packageName));
117+
}
118+
119+
if (!fs.existsSync(configPath)) {
120+
fs.writeFileSync(configPath, getConfig(packageName));
121+
}
122+
}
123+
124+
export function initTransform(packageName: string,
125+
id: string,
126+
type: 'version' | 'preset',
127+
targetPath: string = './',
128+
isReduced: boolean = false) {
129+
if (type === 'version' && !semver.valid(id)) {
107130
throw new Error(
108-
`Provided version ${transform} is not a valid semver version`,
131+
`Provided version ${id} is not a valid semver version`,
109132
);
110133
}
111134

112135
const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
113-
const transformPath = `${basePath}${!isReduced ? '/src' : ''}/${transform}`;
136+
const transformPath = `${basePath}${!isReduced ? '/src' : ''}/${id}`;
114137
const configPath = `${basePath}${
115138
!isReduced ? '/src' : ''
116139
}/codeshift.config.js`;
117140

118141
if (fs.existsSync(transformPath)) {
119-
throw new Error(`Codemod for ${type} "${transform}" already exists`);
142+
throw new Error(`Codemod for ${type} "${id}" already exists`);
120143
}
121144

122145
fs.copySync(`${__dirname}/../template${isReduced ? '/src' : ''}`, basePath);
@@ -125,24 +148,21 @@ export function initDirectory(
125148
transformPath,
126149
);
127150

151+
const testFilePath = path.join(transformPath, 'transform.spec.ts');
128152
const testFile = fs
129-
.readFileSync(`${transformPath}/transform.spec.ts`, 'utf8')
153+
.readFileSync(testFilePath, 'utf8')
130154
.replace('<% packageName %>', packageName)
131155
.replace('<% seperator %>', type === 'version' ? '@' : '#')
132-
.replace('<% transform %>', transform || '');
156+
.replace('<% transform %>', id || '');
133157

134-
fs.writeFileSync(`${transformPath}/transform.spec.ts`, testFile);
158+
fs.writeFileSync(testFilePath, testFile);
135159

136160
if (!isReduced) {
137-
fs.writeFileSync(`${basePath}/package.json`, getPackageJson(packageName));
161+
fs.writeFileSync(path.join(basePath, 'package.json'), getPackageJson(packageName));
138162
}
139163

140-
if (!fs.existsSync(configPath)) {
141-
fs.writeFileSync(configPath, getConfig(packageName, transform));
142-
} else {
143-
fs.writeFileSync(
144-
configPath,
145-
updateConfig(configPath, packageName, transform || '', type),
146-
);
147-
}
164+
fs.writeFileSync(
165+
configPath,
166+
updateConfig(configPath, packageName, id || '', type),
167+
);
148168
}

scripts/initialize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { initDirectory } from '@codeshift/initializer';
1+
import { initDirectory, initTransform } from '@codeshift/initializer';
22

33
const targetPath = `${__dirname}/../community`;
44

@@ -7,7 +7,8 @@ export function main(packageName: string, transform?: string) {
77
if (!transform) throw new Error('Version was not provided');
88

99
if (transform) {
10-
initDirectory(packageName, transform, 'version', targetPath);
10+
initDirectory(packageName, targetPath);
11+
initTransform(packageName, transform, 'version', targetPath);
1112
}
1213

1314
console.log(

0 commit comments

Comments
 (0)