-
Notifications
You must be signed in to change notification settings - Fork 37
Create Plugin: Support new add cmd #2233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunker
wants to merge
5
commits into
main
Choose a base branch
from
create-plugin/add-cmd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/create-plugin/src/codemods/additions/additions.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import defaultAdditions from './additions.js'; | ||
|
|
||
| describe('additions json', () => { | ||
| // as addition scripts are imported dynamically when add is run we assert the path is valid | ||
| defaultAdditions.forEach((addition) => { | ||
| it(`should have a valid addition script path for ${addition.name}`, () => { | ||
| expect(async () => { | ||
| await import(addition.scriptPath); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
packages/create-plugin/src/codemods/additions/additions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Codemod } from '../types.js'; | ||
| import { resolveScriptPath } from '../utils.js'; | ||
|
|
||
| export default [ | ||
| { | ||
| name: 'example-addition', | ||
| description: 'Example addition demonstrating Valibot schema with type inference', | ||
| scriptPath: resolveScriptPath(import.meta.url, './scripts/example-addition.js'), | ||
sunker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ] satisfies Codemod[]; | ||
92 changes: 92 additions & 0 deletions
92
packages/create-plugin/src/codemods/additions/scripts/example-addition.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { Context } from '../../context.js'; | ||
| import migrate from './example-addition.js'; | ||
|
|
||
| describe('example-addition', () => { | ||
| it('should add example script to package.json', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} })); | ||
|
|
||
| const result = migrate(context, { featureName: 'testFeature', enabled: true, frameworks: ['react'] }); | ||
|
|
||
| const packageJson = JSON.parse(result.getFile('package.json') || '{}'); | ||
| expect(packageJson.scripts['example-script']).toBe('echo "Running testFeature"'); | ||
| }); | ||
|
|
||
| it('should add dev dependency', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} })); | ||
|
|
||
| const result = migrate(context, { featureName: 'myFeature', enabled: false, frameworks: ['react'] }); | ||
|
|
||
| const packageJson = JSON.parse(result.getFile('package.json') || '{}'); | ||
| expect(packageJson.devDependencies['example-dev-dep']).toBe('^1.0.0'); | ||
| }); | ||
|
|
||
| it('should create feature TypeScript file with options', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} })); | ||
|
|
||
| const result = migrate(context, { | ||
| featureName: 'myFeature', | ||
| enabled: false, | ||
| port: 4000, | ||
| frameworks: ['react', 'vue'], | ||
| }); | ||
|
|
||
| expect(result.doesFileExist('src/features/myFeature.ts')).toBe(true); | ||
| const featureCode = result.getFile('src/features/myFeature.ts'); | ||
| expect(featureCode).toContain('export const myFeature'); | ||
| expect(featureCode).toContain('enabled: false'); | ||
| expect(featureCode).toContain('port: 4000'); | ||
| expect(featureCode).toContain('frameworks: ["react","vue"]'); | ||
| expect(featureCode).toContain('myFeature initialized on port 4000'); | ||
| }); | ||
|
|
||
| it('should delete deprecated file if it exists', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} })); | ||
| context.addFile('src/deprecated.ts', 'export const old = true;'); | ||
|
|
||
| const result = migrate(context, { featureName: 'testFeature', enabled: true, frameworks: ['react'] }); | ||
|
|
||
| expect(result.doesFileExist('src/deprecated.ts')).toBe(false); | ||
| }); | ||
|
|
||
| it('should rename old-config.json if it exists', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile('package.json', JSON.stringify({ scripts: {}, dependencies: {}, devDependencies: {} })); | ||
| context.addFile('src/old-config.json', JSON.stringify({ old: true })); | ||
|
|
||
| const result = migrate(context, { featureName: 'testFeature', enabled: true, frameworks: ['react'] }); | ||
|
|
||
| expect(result.doesFileExist('src/old-config.json')).toBe(false); | ||
| expect(result.doesFileExist('src/new-config.json')).toBe(true); | ||
| const newConfig = JSON.parse(result.getFile('src/new-config.json') || '{}'); | ||
| expect(newConfig.old).toBe(true); | ||
| }); | ||
|
|
||
| it('should not add script if it already exists', () => { | ||
| const context = new Context('/virtual'); | ||
|
|
||
| context.addFile( | ||
| 'package.json', | ||
| JSON.stringify({ | ||
| scripts: { 'example-script': 'existing command' }, | ||
| dependencies: {}, | ||
| devDependencies: {}, | ||
| }) | ||
| ); | ||
|
|
||
| const result = migrate(context, { featureName: 'testFeature', enabled: true, frameworks: ['react'] }); | ||
|
|
||
| const packageJson = JSON.parse(result.getFile('package.json') || '{}'); | ||
| expect(packageJson.scripts['example-script']).toBe('existing command'); | ||
| }); | ||
| }); |
62 changes: 62 additions & 0 deletions
62
packages/create-plugin/src/codemods/additions/scripts/example-addition.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import * as v from 'valibot'; | ||
| import type { Context } from '../../context.js'; | ||
| import { addDependenciesToPackageJson } from '../../utils.js'; | ||
|
|
||
| /** | ||
| * Example addition demonstrating Valibot schema with type inference | ||
| * Schema defines validation rules, defaults and types are automatically inferred | ||
| */ | ||
| export const schema = v.object({ | ||
| featureName: v.pipe( | ||
| v.string(), | ||
| v.minLength(3, 'Feature name must be at least 3 characters'), | ||
| v.maxLength(50, 'Feature name must be at most 50 characters') | ||
| ), | ||
| enabled: v.optional(v.boolean(), true), | ||
| port: v.optional( | ||
| v.pipe(v.number(), v.minValue(1000, 'Port must be at least 1000'), v.maxValue(65535, 'Port must be at most 65535')) | ||
| ), | ||
| frameworks: v.optional(v.array(v.string()), ['react']), | ||
| }); | ||
|
|
||
| // Type is automatically inferred from the schema | ||
| type ExampleOptions = v.InferOutput<typeof schema>; | ||
|
|
||
| export default function exampleAddition(context: Context, options: ExampleOptions): Context { | ||
| // These options have been validated by the framework | ||
| const { featureName, enabled, port, frameworks } = options; | ||
|
|
||
| const rawPkgJson = context.getFile('./package.json') ?? '{}'; | ||
| const packageJson = JSON.parse(rawPkgJson); | ||
|
|
||
| if (packageJson.scripts && !packageJson.scripts['example-script']) { | ||
| packageJson.scripts['example-script'] = `echo "Running ${featureName}"`; | ||
| context.updateFile('./package.json', JSON.stringify(packageJson, null, 2)); | ||
| } | ||
|
|
||
| addDependenciesToPackageJson(context, {}, { 'example-dev-dep': '^1.0.0' }); | ||
|
|
||
| if (!context.doesFileExist(`./src/features/${featureName}.ts`)) { | ||
| const featureCode = `export const ${featureName} = { | ||
| name: '${featureName}', | ||
| enabled: ${enabled}, | ||
| port: ${port ?? 3000}, | ||
| frameworks: ${JSON.stringify(frameworks)}, | ||
| init() { | ||
| console.log('${featureName} initialized on port ${port ?? 3000}'); | ||
| }, | ||
| }; | ||
| `; | ||
| context.addFile(`./src/features/${featureName}.ts`, featureCode); | ||
| } | ||
|
|
||
| if (context.doesFileExist('./src/deprecated.ts')) { | ||
| context.deleteFile('./src/deprecated.ts'); | ||
| } | ||
|
|
||
| if (context.doesFileExist('./src/old-config.json')) { | ||
| context.renameFile('./src/old-config.json', './src/new-config.json'); | ||
| } | ||
|
|
||
| return context; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
packages/create-plugin/src/codemods/migrations/fixtures/migrations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| export default [ | ||
| { | ||
| name: 'migration-key1', | ||
| version: '5.0.0', | ||
| description: 'Update project to use new cache directory', | ||
| scriptPath: './5-0-0-cache-directory.js', | ||
| }, | ||
| { | ||
| name: 'migration-key2', | ||
| version: '5.4.0', | ||
| description: 'Update project to use new cache directory', | ||
| scriptPath: './5-4-0-cache-directory.js', | ||
| }, | ||
| { | ||
| name: 'migration-key3', | ||
| version: '6.0.0', | ||
| description: 'Update project to use new cache directory', | ||
| scriptPath: './5-4-0-cache-directory.js', | ||
| }, | ||
| ]; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scripts are now imported from the shared codemod runner, so paths must be resolved relative to this registry file's location to ensure correct resolution at runtime
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed during the call, let's move the directories around so we don't have to juggle the script path resolution.