-
Notifications
You must be signed in to change notification settings - Fork 8
feat: generate cli #98
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
boazpoolman
wants to merge
9
commits into
strapi:main
Choose a base branch
from
boazpoolman:feature/generate-cli
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 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a5569df
feat: add validation for the 'strapi' object in the package json
boazpoolman 74841c6
feat: add a command for generating boilerplate based on @strapi/gener…
boazpoolman 0669919
chore: downgrade inquirer to align with the version used in @strapi/g…
boazpoolman 608d305
fix: set display name for usage in the plop templates
boazpoolman ff76f94
fix: feedback
boazpoolman f1d4572
feat: allow passing the generator type as a cli arg
boazpoolman de587e8
Merge branch 'main' into feature/generate-cli
boazpoolman d1bc60a
chore: update @strapi/generators
boazpoolman 23cc74c
Merge branch 'feature/generate-cli' of github.com:boazpoolman/sdk-plu…
boazpoolman 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
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,5 @@ | ||
| --- | ||
| '@strapi/sdk-plugin': minor | ||
| --- | ||
|
|
||
| feat: add a command for generating boilerplate based on @strapi/generators |
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,5 @@ | ||
| --- | ||
| '@strapi/sdk-plugin': patch | ||
| --- | ||
|
|
||
| feat: add validation for the 'strapi' object in the package json |
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
Large diffs are not rendered by default.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import validateInput from '@strapi/generators/dist/plops/utils/validate-input'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * api generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const config = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'id', | ||
| message: 'API name', | ||
| validate: (input) => validateInput(input), | ||
| }, | ||
| ]); | ||
|
|
||
| generate( | ||
| 'api', | ||
| { | ||
| id: config.id, | ||
| isPluginApi: true, | ||
| destination: 'root', | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; |
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,40 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import bootstrapApiPrompts from '@strapi/generators/dist/plops/prompts/bootstrap-api-prompts'; | ||
| import ctNamesPrompts from '@strapi/generators/dist/plops/prompts/ct-names-prompts'; | ||
| import getAttributesPrompts from '@strapi/generators/dist/plops/prompts/get-attributes-prompts'; | ||
| import kindPrompts from '@strapi/generators/dist/plops/prompts/kind-prompts'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * content-type generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const nameInfo = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts] as any); | ||
| const attributes = await getAttributesPrompts(inquirer); | ||
| const bootstrapInfo = await inquirer.prompt([...bootstrapApiPrompts] as any); | ||
|
|
||
| generate( | ||
| 'content-type', | ||
| { | ||
| kind: nameInfo.kind, | ||
| singularName: nameInfo.singularName, | ||
| id: nameInfo.displayName, | ||
innerdvations marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pluralName: nameInfo.pluralName, | ||
| displayName: nameInfo.displayName, | ||
| destination: 'root', | ||
| bootstrapApi: bootstrapInfo.bootstrapApi, | ||
| attributes, | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; | ||
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,36 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import validateInput from '@strapi/generators/dist/plops/utils/validate-input'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * controller generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const config = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'id', | ||
| message: 'Controller name', | ||
| validate: (input) => validateInput(input), | ||
| }, | ||
| ]); | ||
|
|
||
| generate( | ||
| 'controller', | ||
| { | ||
| id: config.id, | ||
| destination: 'root', | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; |
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,36 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import validateInput from '@strapi/generators/dist/plops/utils/validate-input'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * middleware generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const config = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'name', | ||
| message: 'Middleware name', | ||
| validate: (input) => validateInput(input), | ||
| }, | ||
| ]); | ||
|
|
||
| generate( | ||
| 'middleware', | ||
| { | ||
| name: config.name, | ||
| destination: 'root', | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; |
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,36 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import validateInput from '@strapi/generators/dist/plops/utils/validate-input'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * policy generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const config = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'id', | ||
| message: 'Policy name', | ||
| validate: (input) => validateInput(input), | ||
| }, | ||
| ]); | ||
|
|
||
| generate( | ||
| 'policy', | ||
| { | ||
| id: config.id, | ||
| destination: 'root', | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; |
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,36 @@ | ||
| import { generate } from '@strapi/generators'; | ||
| import validateInput from '@strapi/generators/dist/plops/utils/validate-input'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import { loadPkg, validatePkg } from '../../../utils/pkg'; | ||
|
|
||
| import type { CLIContext } from '../../../../../types'; | ||
|
|
||
| /** | ||
| * service generator for Strapi plugins | ||
| */ | ||
| const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => { | ||
| const pkg = await loadPkg({ cwd, logger }); | ||
| const validatedPkg = await validatePkg({ pkg }); | ||
|
|
||
| const config = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'id', | ||
| message: 'Service name', | ||
| validate: (input) => validateInput(input), | ||
| }, | ||
| ]); | ||
|
|
||
| generate( | ||
| 'service', | ||
| { | ||
| id: config.id, | ||
| destination: 'root', | ||
| plugin: validatedPkg.strapi.name, | ||
| }, | ||
| { dir: 'server' } | ||
| ); | ||
| }; | ||
|
|
||
| export default action; |
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,65 @@ | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| import apiAction from './actions/api'; | ||
| import ctAction from './actions/content-type'; | ||
| import controllerAction from './actions/controller'; | ||
| import middlewareAction from './actions/middleware'; | ||
| import policyAction from './actions/policy'; | ||
| import serviceAction from './actions/service'; | ||
|
|
||
| import type { StrapiCommand } from '../../../../types'; | ||
|
|
||
| /** | ||
| * `$ strapi-plugin generate` | ||
| */ | ||
| const command: StrapiCommand = ({ command: commanderCommand, ctx }) => { | ||
| commanderCommand | ||
| .command('generate') | ||
| .description('Generate some boilerplate code for a Strapi plugin') | ||
| .option('-d, --debug', 'Enable debugging mode with verbose logs', false) | ||
| .option('--silent', "Don't log anything", false) | ||
| .action(async () => { | ||
innerdvations marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const options = [ | ||
| { name: 'api', value: 'api' }, | ||
| { name: 'controller', value: 'controller' }, | ||
| { name: 'content-type', value: 'content-type' }, | ||
| { name: 'policy', value: 'policy' }, | ||
| { name: 'middleware', value: 'middleware' }, | ||
| { name: 'service', value: 'service' }, | ||
| ]; | ||
|
|
||
| const { generator } = await inquirer.prompt([ | ||
| { | ||
| type: 'list', | ||
| name: 'generator', | ||
| message: 'Strapi Generators', | ||
| choices: options, | ||
| }, | ||
| ]); | ||
|
|
||
| switch (generator) { | ||
| case 'api': | ||
| apiAction({ ctx }); | ||
| break; | ||
| case 'controller': | ||
| controllerAction({ ctx }); | ||
| break; | ||
| case 'content-type': | ||
| ctAction({ ctx }); | ||
| break; | ||
| case 'policy': | ||
| policyAction({ ctx }); | ||
| break; | ||
| case 'middleware': | ||
| middlewareAction({ ctx }); | ||
| break; | ||
| case 'service': | ||
| serviceAction({ ctx }); | ||
| break; | ||
| default: | ||
| ctx.logger.error('Unknown generator type'); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| export default command; | ||
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 @@ | ||
| export { default as command } from './command'; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.