Skip to content

Commit

Permalink
Adds readme file to init output + logs
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldelcore committed Apr 19, 2022
1 parent 598ec9e commit 380ed84
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .changeset/poor-eyes-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@codeshift/cli': minor
'@codeshift/initializer': minor
'@codeshift/validator': minor
---

Codeshift projects can now be initialized a config file only via the `init --config-only` command.

Initialized projects now contain a README with helpful getting started information.

The init command now outputs getting started tips + commands
34 changes: 33 additions & 1 deletion packages/cli/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,37 @@ export default async function init(
if (transform) initTransform(packageName, transform, 'version', targetPath);
if (preset) initTransform(packageName, preset, 'preset', targetPath);

console.log(chalk.green(`🚚 New codemod package created at: ${targetPath}`));
if (targetPath !== '.') {
console.log(
chalk.green(`🚚 New codemod package created at: ${targetPath}`),
);

console.log(`
Inside that directory, you can run the following commands:
${chalk.blueBright('npm run dev')}
Starts the Codeshift cli
${chalk.blueBright('npm run test')}
Launches the test runner in watch mode.
${chalk.blueBright('npm run validate')}
Checks the validity of your \`codeshift.config.js\` file
${chalk.blueBright('npm run build')}
Builds the app for production to the \`dist\` folder.
Get started by running:
${chalk.blueBright(`cd ${packageName}`)}
${chalk.blueBright(`npm install`)}
${chalk.blueBright(`npm start test`)}
`);
}

if (targetPath === '.') {
console.log(
chalk.green(`🚚 New codeshift.config.js created at: ${targetPath}`),
);
}
}
1 change: 1 addition & 0 deletions packages/initializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/initializer",
"dependencies": {
"@codeshift/cli": "*",
"@codeshift/test-utils": "*",
"@codeshift/utils": "*",
"fs-extra": "^9.1.0",
Expand Down
27 changes: 18 additions & 9 deletions packages/initializer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import fs from 'fs-extra';
import path from 'path';
import semver from 'semver';
import * as recast from 'recast';
import { version as cliVersion } from '@codeshift/cli/package.json';
import { version as utilVersion } from '@codeshift/utils/package.json';
import { version as testUtilVersion } from '@codeshift/test-utils/package.json';

const TEMPLATE_PATH = path.join(__dirname, '..', 'template');

export function getPackageJson(packageName: string, version = '0.0.0') {
return JSON.stringify(
{
Expand All @@ -13,13 +16,16 @@ export function getPackageJson(packageName: string, version = '0.0.0') {
license: 'MIT',
main: 'dist/codeshift.config.js',
scripts: {
dev: 'codeshift',
build: 'tsc --build',
test: 'jest',
test: 'jest --watch',
validate: 'codeshift validate .',
},
dependencies: {
'@codeshift/utils': `^${utilVersion}`,
},
devDependencies: {
'@codeshift/cli': `^${cliVersion}`,
'@codeshift/test-utils': `^${testUtilVersion}`,
'@types/node': '^16.11.0',
'@types/jest': '^26.0.15',
Expand Down Expand Up @@ -134,13 +140,9 @@ export function initDirectory(
targetPath = './',
isReduced = false,
) {
fs.copySync(
path.join(__dirname, '..', 'template', isReduced ? 'src' : ''),
targetPath,
{
filter: src => !src.includes('src/codemod'),
},
);
fs.copySync(path.join(TEMPLATE_PATH, isReduced ? 'src' : ''), targetPath, {
filter: src => !src.includes('src/codemod'),
});

if (!isReduced) {
fs.writeFileSync(
Expand All @@ -149,6 +151,13 @@ export function initDirectory(
);

fs.writeFileSync(path.join(targetPath, '.npmignore'), getNpmIgnore());

const readmeFilePath = path.join(targetPath, 'README.md');
const readmeFile = fs
.readFileSync(readmeFilePath, 'utf8')
.replace('<% packageName %>', packageName);

fs.writeFileSync(readmeFilePath, readmeFile);
}

initConfig(packageName, targetPath);
Expand Down Expand Up @@ -178,7 +187,7 @@ export function initTransform(
);

fs.copySync(
path.join(__dirname, '..', 'template', 'src', 'codemod'),
path.join(TEMPLATE_PATH, 'src', 'codemod'),
codemodTemplateDestinationPath,
);
fs.renameSync(codemodTemplateDestinationPath, transformPath);
Expand Down
39 changes: 39 additions & 0 deletions packages/initializer/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# <% packageName %>

This project was bootstrapped with [CodeshiftCommunity 🚚](https://www.codeshiftcommunity.com/). Please see the [external packages guide](https://www.codeshiftcommunity.com/docs/external-packages) for more information on how to work with this repo.

![CodeshiftCommunity logo](https://www.codeshiftcommunity.com/img/logo.svg)

## Scripts

### `npm run dev`

Runs the codeshift CLI useful for testing transform files as if they have been published

**example:** `npm run dev -t src/10.0.0/transform.ts`

See the [cli reference](https://www.codeshiftcommunity.com/docs/cli) for more information.

### `npm run test`

Launches the test runner in interactive watch mode.

See the [testing guide](https://www.codeshiftcommunity.com/docs/testing) for more information.

### `npm run validate`

Checks the validity of your `codeshift.config.js` file.

See the [configuration options](https://www.codeshiftcommunity.com/docs/configuration) for more information.

### `npm run build`

Builds the app for production to the `dist` folder.

## Publishing

This package can be published to npm via the normal commands `npm version` and `npm publish`

## Build tooling

Feel free to replace the preinstalled build tooling & dependencies to suit your needs. The only requirement is that there is a valid `codeshift.config.js` in your project root, `/src` or `/codemods` directories.
2 changes: 1 addition & 1 deletion website/docs/external-packages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ The file structure of your new codeshift package will look like this:

```
react-cool-library/
codeshift.config.js // main entrypoint containing configuration and references to your transforms
package.json
tsconfig.json
jest.config.js
src/
codeshift.config.js // main entrypoint containing configuration and references to your transforms
10.0.0/ // semver version
transform.ts // main logic (should contain a transformer)
transform.spec.ts // main tests
Expand Down

0 comments on commit 380ed84

Please sign in to comment.