Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sharp-forks-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@codeshift/cli': patch
'@codeshift/initializer': patch
---

Init command can now be called without the transform or preset flag and output an empty directory
3 changes: 3 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ program
'after',
`
Examples:
# Initializes an empty codeshift package at the ~/Desktop directory
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop

# Initializes a new codeshift package with a transform for 10.0.0
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop

Expand Down
11 changes: 4 additions & 7 deletions packages/cli/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import path from 'path';
import { initDirectory } from '@codeshift/initializer';
import { initDirectory, initTransform } from '@codeshift/initializer';

export default async function init(
packageName: string,
transform?: string,
preset?: string,
targetPath: string = '.',
) {
if (transform) {
initDirectory(packageName, transform, 'version', targetPath);
}
initDirectory(packageName, targetPath);

if (preset) {
initDirectory(packageName, preset, 'preset', targetPath);
}
if (transform) initTransform(packageName, transform, 'version', targetPath);
if (preset) initTransform(packageName, preset, 'preset', targetPath);

console.log(
`🚚 New codemod package created at: ${path.join(targetPath, packageName)}`,
Expand Down
56 changes: 38 additions & 18 deletions packages/initializer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import semver from 'semver';
import * as recast from 'recast';
import { version as utilVersion } from '@codeshift/utils/package.json';
Expand Down Expand Up @@ -98,25 +99,47 @@ function updateConfig(

export function initDirectory(
packageName: string,
transform: string,
type: 'version' | 'preset',
targetPath: string = './',
isReduced: boolean = false,
) {
if (type === 'version' && !semver.valid(transform)) {

const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
const configPath = `${basePath}${
!isReduced ? '/src' : ''
}/codeshift.config.js`;

fs.copySync(`${__dirname}/../template${isReduced ? '/src' : ''}`, basePath, {
filter: (src) => !src.includes('src/codemod')
});

if (!isReduced) {
fs.writeFileSync(path.join(basePath, 'package.json'), getPackageJson(packageName));
}

if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, getConfig(packageName));
}
}

export function initTransform(packageName: string,
id: string,
type: 'version' | 'preset',
targetPath: string = './',
isReduced: boolean = false) {
if (type === 'version' && !semver.valid(id)) {
throw new Error(
`Provided version ${transform} is not a valid semver version`,
`Provided version ${id} is not a valid semver version`,
);
}

const basePath = `${targetPath}/${packageName.replace('/', '__')}`;
const transformPath = `${basePath}${!isReduced ? '/src' : ''}/${transform}`;
const transformPath = `${basePath}${!isReduced ? '/src' : ''}/${id}`;
const configPath = `${basePath}${
!isReduced ? '/src' : ''
}/codeshift.config.js`;

if (fs.existsSync(transformPath)) {
throw new Error(`Codemod for ${type} "${transform}" already exists`);
throw new Error(`Codemod for ${type} "${id}" already exists`);
}

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

const testFilePath = path.join(transformPath, 'transform.spec.ts');
const testFile = fs
.readFileSync(`${transformPath}/transform.spec.ts`, 'utf8')
.readFileSync(testFilePath, 'utf8')
.replace('<% packageName %>', packageName)
.replace('<% seperator %>', type === 'version' ? '@' : '#')
.replace('<% transform %>', transform || '');
.replace('<% transform %>', id || '');

fs.writeFileSync(`${transformPath}/transform.spec.ts`, testFile);
fs.writeFileSync(testFilePath, testFile);

if (!isReduced) {
fs.writeFileSync(`${basePath}/package.json`, getPackageJson(packageName));
fs.writeFileSync(path.join(basePath, 'package.json'), getPackageJson(packageName));
}

if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, getConfig(packageName, transform));
} else {
fs.writeFileSync(
configPath,
updateConfig(configPath, packageName, transform || '', type),
);
}
fs.writeFileSync(
configPath,
updateConfig(configPath, packageName, id || '', type),
);
}
5 changes: 3 additions & 2 deletions scripts/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initDirectory } from '@codeshift/initializer';
import { initDirectory, initTransform } from '@codeshift/initializer';

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

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

if (transform) {
initDirectory(packageName, transform, 'version', targetPath);
initDirectory(packageName, targetPath);
initTransform(packageName, transform, 'version', targetPath);
}

console.log(
Expand Down