diff --git a/.changeset/sharp-forks-tap.md b/.changeset/sharp-forks-tap.md new file mode 100644 index 000000000..b62963ac2 --- /dev/null +++ b/.changeset/sharp-forks-tap.md @@ -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 diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 08fc7ea69..755487a16 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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 diff --git a/packages/cli/src/init.ts b/packages/cli/src/init.ts index 05759c091..5e2efef1f 100644 --- a/packages/cli/src/init.ts +++ b/packages/cli/src/init.ts @@ -1,5 +1,5 @@ import path from 'path'; -import { initDirectory } from '@codeshift/initializer'; +import { initDirectory, initTransform } from '@codeshift/initializer'; export default async function init( packageName: string, @@ -7,13 +7,10 @@ export default async function init( 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)}`, diff --git a/packages/initializer/src/index.ts b/packages/initializer/src/index.ts index c8694af38..feb140d8a 100644 --- a/packages/initializer/src/index.ts +++ b/packages/initializer/src/index.ts @@ -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'; @@ -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); @@ -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), + ); } diff --git a/scripts/initialize.ts b/scripts/initialize.ts index c48dd2418..f2939813b 100644 --- a/scripts/initialize.ts +++ b/scripts/initialize.ts @@ -1,4 +1,4 @@ -import { initDirectory } from '@codeshift/initializer'; +import { initDirectory, initTransform } from '@codeshift/initializer'; const targetPath = `${__dirname}/../community`; @@ -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(