@@ -21,112 +21,28 @@ Let's create plugin which auto-completes text in strings
2121``` bash
2222mkdir -p af-plugin-chatgpt
2323cd af-plugin-chatgpt
24- npm init -y
25- touch index.ts
26- npm i typescript @types/node -D
27- ```
28-
29- Edit ` package.json ` :
30-
31- ``` json title='./af-plugin-chatgpt/package.json'
32- {
33- ...
34- //diff-remove
35- "main" : " index.js" ,
36- //diff-add
37- "main" : " dist/index.js" ,
38- //diff-add
39- "types" : " dist/index.d.ts" ,
40- //diff-add
41- "type" : " module" ,
42- "scripts" : {
43- //diff-remove
44- "test" : " echo \" Error: no test specified\" && exit 1" ,
45- //diff-add
46- "build" : " tsc && rsync -av --exclude 'node_modules' custom dist/ && npm version patch"
47- },
48- }
49- ```
50-
51-
52- Install AdminForth for types and classes imports:
53-
54- ``` bash
55- npm i adminforth --save
56- ```
57-
58- Now create plugin boilerplate in ` index.ts ` :
59-
60- ``` ts title='./af-plugin-chatgpt/index.ts'
61-
62- import { AdminForthPlugin } from " adminforth" ;
63- import type { IAdminForth , IHttpServer , AdminForthResourcePages , AdminForthResourceColumn , AdminForthDataTypes , AdminForthResource } from " adminforth" ;
64- import type { PluginOptions } from ' ./types.js' ;
65-
66-
67- export default class ChatGptPlugin extends AdminForthPlugin {
68- options: PluginOptions ;
69-
70- constructor (options : PluginOptions ) {
71- super (options , import .meta .url );
72- this .options = options ;
73- }
74-
75- async modifyResourceConfig(adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
76- super .modifyResourceConfig (adminforth , resourceConfig );
77-
78- // simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
79- }
80-
81- validateConfigAfterDiscover(adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
82- // optional method where you can safely check field types after database discovery was performed
83- }
84-
85- instanceUniqueRepresentation(pluginOptions : any ) : string {
86- // optional method to return unique string representation of plugin instance.
87- // Needed if plugin can have multiple instances on one resource
88- return ` single ` ;
89- }
90-
91- setupEndpoints(server : IHttpServer ) {
92- server .endpoint ({
93- method: ' POST' ,
94- path: ` /plugin/${this .pluginInstanceId }/example ` ,
95- handler : async ({ body }) => {
96- const { name } = body ;
97- return { hey: ` Hello ${name } ` };
98- }
99- });
100- }
101-
102- }
24+ npx adminforth create-plugin
10325```
10426
105- Create ` types.ts ` file :
27+ CLI options :
10628
107- ``` ts title='./af-plugin-chatgpt/types.ts'
108-
109- export interface PluginOptions {
110-
111- }
112- ```
29+ * ** ` --plugin-name ` ** - name for your plugin.
11330
31+ This command will:
32+ 1 . Set up the TypeScript configuration
33+ 2 . Create initial plugin files
34+ 3 . Install required dependencies
11435
115- Create ` ./af-plugin-chatgpt/tsconfig.json ` file :
36+ The CLI will create the following files and directories :
11637
117- ``` json title='./af-plugin-chatgpt/tsconfig.json'
118- {
119- "compilerOptions" : {
120- "target" : " es2016" , /* Set the JavaScript language version for emitted JavaScript and include*/
121- "module" : " node16" , /* Specify what module code is generated. */
122- "outDir" : " ./dist" , /* Specify an output folder for all emitted files. */
123- "esModuleInterop" : true , /* Emit additional JavaScript to ease support for importing CommonJS modules. */
124- "forceConsistentCasingInFileNames" : true , /* Ensure that casing is correct in imports. */
125- "strict" : false , /* Enable all strict type-checking options. */
126- "skipLibCheck" : true , /* Skip type checking all .d.ts files. */
127- },
128- "exclude" : [" node_modules" , " dist" , " custom" ], /* Exclude files from compilation. */
129- }
38+ ``` text
39+ af-plugin-chatgpt/
40+ ├── custom
41+ │ └── tsconfig.json # TypeScript configuration for custom components
42+ ├── index.ts # Main plugin file with boilerplate code
43+ ├── package.json # Plugin package configuration
44+ ├── tsconfig.json # TypeScript configuration
45+ └── types.ts # TypeScript types for your plugin
13046
13147```
13248
@@ -204,37 +120,6 @@ export interface PluginOptions {
204120}
205121```
206122
207- Now we have to create custom Vue component which will be used in plugin. To do it create custom folder:
208-
209- ``` bash
210- mkdir -p af-plugin-chatgpt/custom
211- ```
212-
213- Also create ` tsconfig.ts ` file so your IDE will be able to resolve adminforth spa imports:
214-
215- ``` json title='./af-plugin-chatgpt/custom/tsconfig.json'
216- {
217- "compilerOptions" : {
218- "baseUrl" : " ." , // This should point to your project root
219- "paths" : {
220- "@/*" : [
221- // "node_modules/adminforth/dist/spa/src/*"
222- " ../../../spa/src/*"
223- ],
224- "*" : [
225- // "node_modules/adminforth/dist/spa/node_modules/*"
226- " ../../../spa/node_modules/*"
227- ],
228- "@@/*" : [
229- // "node_modules/adminforth/dist/spa/src/*"
230- " ."
231- ]
232- }
233- }
234- }
235- ```
236-
237-
238123We will use ` vue-suggestion-input ` package in our frontend component.
239124To install package into frontend component, first of all we have to initialize npm package in custom folder:
240125
@@ -535,7 +420,7 @@ Finally, since we want to support multiple installations on one resource (e.g. o
535420` ` `
536421
537422
538- Ro compile plugin run:
423+ To compile plugin run:
539424
540425` ` ` bash
541426npm run build
0 commit comments