Skip to content

Commit 380ed84

Browse files
committed
Adds readme file to init output + logs
1 parent 598ec9e commit 380ed84

File tree

6 files changed

+103
-11
lines changed

6 files changed

+103
-11
lines changed

.changeset/poor-eyes-chew.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@codeshift/cli': minor
3+
'@codeshift/initializer': minor
4+
'@codeshift/validator': minor
5+
---
6+
7+
Codeshift projects can now be initialized a config file only via the `init --config-only` command.
8+
9+
Initialized projects now contain a README with helpful getting started information.
10+
11+
The init command now outputs getting started tips + commands

packages/cli/src/init.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,37 @@ export default async function init(
2424
if (transform) initTransform(packageName, transform, 'version', targetPath);
2525
if (preset) initTransform(packageName, preset, 'preset', targetPath);
2626

27-
console.log(chalk.green(`🚚 New codemod package created at: ${targetPath}`));
27+
if (targetPath !== '.') {
28+
console.log(
29+
chalk.green(`🚚 New codemod package created at: ${targetPath}`),
30+
);
31+
32+
console.log(`
33+
Inside that directory, you can run the following commands:
34+
35+
${chalk.blueBright('npm run dev')}
36+
Starts the Codeshift cli
37+
38+
${chalk.blueBright('npm run test')}
39+
Launches the test runner in watch mode.
40+
41+
${chalk.blueBright('npm run validate')}
42+
Checks the validity of your \`codeshift.config.js\` file
43+
44+
${chalk.blueBright('npm run build')}
45+
Builds the app for production to the \`dist\` folder.
46+
47+
Get started by running:
48+
49+
${chalk.blueBright(`cd ${packageName}`)}
50+
${chalk.blueBright(`npm install`)}
51+
${chalk.blueBright(`npm start test`)}
52+
`);
53+
}
54+
55+
if (targetPath === '.') {
56+
console.log(
57+
chalk.green(`🚚 New codeshift.config.js created at: ${targetPath}`),
58+
);
59+
}
2860
}

packages/initializer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/packages/initializer",
88
"dependencies": {
9+
"@codeshift/cli": "*",
910
"@codeshift/test-utils": "*",
1011
"@codeshift/utils": "*",
1112
"fs-extra": "^9.1.0",

packages/initializer/src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import fs from 'fs-extra';
22
import path from 'path';
33
import semver from 'semver';
44
import * as recast from 'recast';
5+
import { version as cliVersion } from '@codeshift/cli/package.json';
56
import { version as utilVersion } from '@codeshift/utils/package.json';
67
import { version as testUtilVersion } from '@codeshift/test-utils/package.json';
78

9+
const TEMPLATE_PATH = path.join(__dirname, '..', 'template');
10+
811
export function getPackageJson(packageName: string, version = '0.0.0') {
912
return JSON.stringify(
1013
{
@@ -13,13 +16,16 @@ export function getPackageJson(packageName: string, version = '0.0.0') {
1316
license: 'MIT',
1417
main: 'dist/codeshift.config.js',
1518
scripts: {
19+
dev: 'codeshift',
1620
build: 'tsc --build',
17-
test: 'jest',
21+
test: 'jest --watch',
22+
validate: 'codeshift validate .',
1823
},
1924
dependencies: {
2025
'@codeshift/utils': `^${utilVersion}`,
2126
},
2227
devDependencies: {
28+
'@codeshift/cli': `^${cliVersion}`,
2329
'@codeshift/test-utils': `^${testUtilVersion}`,
2430
'@types/node': '^16.11.0',
2531
'@types/jest': '^26.0.15',
@@ -134,13 +140,9 @@ export function initDirectory(
134140
targetPath = './',
135141
isReduced = false,
136142
) {
137-
fs.copySync(
138-
path.join(__dirname, '..', 'template', isReduced ? 'src' : ''),
139-
targetPath,
140-
{
141-
filter: src => !src.includes('src/codemod'),
142-
},
143-
);
143+
fs.copySync(path.join(TEMPLATE_PATH, isReduced ? 'src' : ''), targetPath, {
144+
filter: src => !src.includes('src/codemod'),
145+
});
144146

145147
if (!isReduced) {
146148
fs.writeFileSync(
@@ -149,6 +151,13 @@ export function initDirectory(
149151
);
150152

151153
fs.writeFileSync(path.join(targetPath, '.npmignore'), getNpmIgnore());
154+
155+
const readmeFilePath = path.join(targetPath, 'README.md');
156+
const readmeFile = fs
157+
.readFileSync(readmeFilePath, 'utf8')
158+
.replace('<% packageName %>', packageName);
159+
160+
fs.writeFileSync(readmeFilePath, readmeFile);
152161
}
153162

154163
initConfig(packageName, targetPath);
@@ -178,7 +187,7 @@ export function initTransform(
178187
);
179188

180189
fs.copySync(
181-
path.join(__dirname, '..', 'template', 'src', 'codemod'),
190+
path.join(TEMPLATE_PATH, 'src', 'codemod'),
182191
codemodTemplateDestinationPath,
183192
);
184193
fs.renameSync(codemodTemplateDestinationPath, transformPath);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# <% packageName %>
2+
3+
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.
4+
5+
![CodeshiftCommunity logo](https://www.codeshiftcommunity.com/img/logo.svg)
6+
7+
## Scripts
8+
9+
### `npm run dev`
10+
11+
Runs the codeshift CLI useful for testing transform files as if they have been published
12+
13+
**example:** `npm run dev -t src/10.0.0/transform.ts`
14+
15+
See the [cli reference](https://www.codeshiftcommunity.com/docs/cli) for more information.
16+
17+
### `npm run test`
18+
19+
Launches the test runner in interactive watch mode.
20+
21+
See the [testing guide](https://www.codeshiftcommunity.com/docs/testing) for more information.
22+
23+
### `npm run validate`
24+
25+
Checks the validity of your `codeshift.config.js` file.
26+
27+
See the [configuration options](https://www.codeshiftcommunity.com/docs/configuration) for more information.
28+
29+
### `npm run build`
30+
31+
Builds the app for production to the `dist` folder.
32+
33+
## Publishing
34+
35+
This package can be published to npm via the normal commands `npm version` and `npm publish`
36+
37+
## Build tooling
38+
39+
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.

website/docs/external-packages.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ The file structure of your new codeshift package will look like this:
3535

3636
```
3737
react-cool-library/
38+
codeshift.config.js // main entrypoint containing configuration and references to your transforms
3839
package.json
3940
tsconfig.json
4041
jest.config.js
4142
src/
42-
codeshift.config.js // main entrypoint containing configuration and references to your transforms
4343
10.0.0/ // semver version
4444
transform.ts // main logic (should contain a transformer)
4545
transform.spec.ts // main tests

0 commit comments

Comments
 (0)