Skip to content

Commit 96a1304

Browse files
committed
feat: add a command for generating boilerplate based on @strapi/generators
1 parent e46eeec commit 96a1304

12 files changed

Lines changed: 1487 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ Verifies the output of your plugin before publishing it
7777
```sh
7878
yarn run verify
7979
```
80+
81+
### `generate`
82+
83+
Starts an interactive CLI to generate boilerplate code for your plugin

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"watch": "pack-up watch"
5959
},
6060
"dependencies": {
61+
"@strapi/generators": "5.27.0",
6162
"@strapi/pack-up": "^5.0.1",
6263
"@types/prompts": "2.4.9",
6364
"boxen": "5.1.2",
@@ -67,6 +68,7 @@
6768
"execa": "^9.3.1",
6869
"get-latest-version": "5.1.0",
6970
"git-url-parse": "13.1.1",
71+
"inquirer": "^12.9.6",
7072
"nodemon": "^3.1.0",
7173
"ora": "5.4.1",
7274
"outdent": "0.8.0",

pnpm-lock.yaml

Lines changed: 1192 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { command as buildPluginCommand } from './plugin/build';
2+
import { command as generateCommand } from './plugin/generate';
23
import { command as initPluginCommand } from './plugin/init';
34
import { command as linkWatchPluginCommand } from './plugin/link-watch';
45
import { command as verifyPluginCommand } from './plugin/verify';
@@ -8,6 +9,7 @@ import type { StrapiCommand } from '../../types';
89

910
export const commands: StrapiCommand[] = [
1011
buildPluginCommand,
12+
generateCommand,
1113
initPluginCommand,
1214
linkWatchPluginCommand,
1315
watchPluginCommand,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { generate } from '@strapi/generators';
2+
import validateInput from '@strapi/generators/dist/plops/utils/validate-input';
3+
import inquirer from 'inquirer';
4+
5+
import { loadPkg, validatePkg } from '../../../utils/pkg';
6+
7+
import type { CLIContext } from '../../../../../types';
8+
9+
/**
10+
* api generator for Strapi plugins
11+
*/
12+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
13+
const pkg = await loadPkg({ cwd, logger });
14+
const validatedPkg = await validatePkg({ pkg });
15+
16+
const config = await inquirer.prompt([
17+
{
18+
type: 'input',
19+
name: 'id',
20+
message: 'API name',
21+
validate: (input) => validateInput(input),
22+
},
23+
]);
24+
25+
generate(
26+
'api',
27+
{
28+
id: config.id,
29+
isPluginApi: true,
30+
destination: 'root',
31+
plugin: validatedPkg.strapi.name,
32+
},
33+
{ dir: 'server' }
34+
);
35+
};
36+
37+
export default action;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { generate } from '@strapi/generators';
2+
import bootstrapApiPrompts from '@strapi/generators/dist/plops/prompts/bootstrap-api-prompts';
3+
import ctNamesPrompts from '@strapi/generators/dist/plops/prompts/ct-names-prompts';
4+
import getAttributesPrompts from '@strapi/generators/dist/plops/prompts/get-attributes-prompts';
5+
import kindPrompts from '@strapi/generators/dist/plops/prompts/kind-prompts';
6+
import inquirer from 'inquirer';
7+
8+
import { loadPkg, validatePkg } from '../../../utils/pkg';
9+
10+
import type { CLIContext } from '../../../../../types';
11+
12+
/**
13+
* content-type generator for Strapi plugins
14+
*/
15+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
16+
const pkg = await loadPkg({ cwd, logger });
17+
const validatedPkg = await validatePkg({ pkg });
18+
19+
const nameInfo = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts] as any);
20+
const attributes = await getAttributesPrompts(inquirer);
21+
const bootstrapInfo = await inquirer.prompt([...bootstrapApiPrompts] as any);
22+
23+
generate(
24+
'content-type',
25+
{
26+
kind: nameInfo.kind,
27+
singularName: nameInfo.singularName,
28+
id: nameInfo.id,
29+
pluralName: nameInfo.pluralName,
30+
displayName: nameInfo.displayName,
31+
destination: 'root',
32+
bootstrapApi: bootstrapInfo.bootstrapApi,
33+
attributes,
34+
plugin: validatedPkg.strapi.name,
35+
},
36+
{ dir: 'server' }
37+
);
38+
};
39+
40+
export default action;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { generate } from '@strapi/generators';
2+
import validateInput from '@strapi/generators/dist/plops/utils/validate-input';
3+
import inquirer from 'inquirer';
4+
5+
import { loadPkg, validatePkg } from '../../../utils/pkg';
6+
7+
import type { CLIContext } from '../../../../../types';
8+
9+
/**
10+
* controller generator for Strapi plugins
11+
*/
12+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
13+
const pkg = await loadPkg({ cwd, logger });
14+
const validatedPkg = await validatePkg({ pkg });
15+
16+
const config = await inquirer.prompt([
17+
{
18+
type: 'input',
19+
name: 'id',
20+
message: 'Controller name',
21+
validate: (input) => validateInput(input),
22+
},
23+
]);
24+
25+
generate(
26+
'controller',
27+
{
28+
id: config.id,
29+
destination: 'root',
30+
plugin: validatedPkg.strapi.name,
31+
},
32+
{ dir: 'server' }
33+
);
34+
};
35+
36+
export default action;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { generate } from '@strapi/generators';
2+
import validateInput from '@strapi/generators/dist/plops/utils/validate-input';
3+
import inquirer from 'inquirer';
4+
5+
import { loadPkg, validatePkg } from '../../../utils/pkg';
6+
7+
import type { CLIContext } from '../../../../../types';
8+
9+
/**
10+
* middleware generator for Strapi plugins
11+
*/
12+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
13+
const pkg = await loadPkg({ cwd, logger });
14+
const validatedPkg = await validatePkg({ pkg });
15+
16+
const config = await inquirer.prompt([
17+
{
18+
type: 'input',
19+
name: 'name',
20+
message: 'Middleware name',
21+
validate: (input) => validateInput(input),
22+
},
23+
]);
24+
25+
generate(
26+
'middleware',
27+
{
28+
name: config.name,
29+
destination: 'root',
30+
plugin: validatedPkg.strapi.name,
31+
},
32+
{ dir: 'server' }
33+
);
34+
};
35+
36+
export default action;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { generate } from '@strapi/generators';
2+
import validateInput from '@strapi/generators/dist/plops/utils/validate-input';
3+
import inquirer from 'inquirer';
4+
5+
import { loadPkg, validatePkg } from '../../../utils/pkg';
6+
7+
import type { CLIContext } from '../../../../../types';
8+
9+
/**
10+
* policy generator for Strapi plugins
11+
*/
12+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
13+
const pkg = await loadPkg({ cwd, logger });
14+
const validatedPkg = await validatePkg({ pkg });
15+
16+
const config = await inquirer.prompt([
17+
{
18+
type: 'input',
19+
name: 'id',
20+
message: 'Policy name',
21+
validate: (input) => validateInput(input),
22+
},
23+
]);
24+
25+
generate(
26+
'policy',
27+
{
28+
id: config.id,
29+
destination: 'root',
30+
plugin: validatedPkg.strapi.name,
31+
},
32+
{ dir: 'server' }
33+
);
34+
};
35+
36+
export default action;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { generate } from '@strapi/generators';
2+
import validateInput from '@strapi/generators/dist/plops/utils/validate-input';
3+
import inquirer from 'inquirer';
4+
5+
import { loadPkg, validatePkg } from '../../../utils/pkg';
6+
7+
import type { CLIContext } from '../../../../../types';
8+
9+
/**
10+
* service generator for Strapi plugins
11+
*/
12+
const action = async ({ ctx: { cwd, logger } }: { ctx: CLIContext }) => {
13+
const pkg = await loadPkg({ cwd, logger });
14+
const validatedPkg = await validatePkg({ pkg });
15+
16+
const config = await inquirer.prompt([
17+
{
18+
type: 'input',
19+
name: 'id',
20+
message: 'Service name',
21+
validate: (input) => validateInput(input),
22+
},
23+
]);
24+
25+
generate(
26+
'service',
27+
{
28+
id: config.id,
29+
destination: 'root',
30+
plugin: validatedPkg.strapi.name,
31+
},
32+
{ dir: 'server' }
33+
);
34+
};
35+
36+
export default action;

0 commit comments

Comments
 (0)