feat(pockethost): Added plugin template command and updated docs

This commit is contained in:
Ben Allfree
2024-06-30 12:11:52 -07:00
parent a967c050a1
commit 3c7e9bfe2d
19 changed files with 96 additions and 26 deletions

View File

@@ -0,0 +1,5 @@
---
'pockethost': minor
---
Added plugin template command and updated docs

View File

@@ -10,6 +10,7 @@
"czvf",
"daos",
"Darkmode",
"dasherize",
"Deallocator",
"devcert",
"Dockerized",
@@ -44,6 +45,7 @@
"pbgo",
"PBOUNCE",
"pexec",
"plopfile",
"plugified",
"pocketbase",
"pockethost",

View File

@@ -36,11 +36,13 @@
"get-port": "^6.1.2",
"http-proxy": "^1.18.1",
"immer": "^10.1.1",
"inflection": "^3.0.0",
"json-stringify-safe": "^5.0.1",
"listr2": "^8.2.3",
"minimist": "^1.2.8",
"nanoid": "^5.0.2",
"node-fetch": "^3.3.2",
"node-plop": "^0.32.0",
"pocketbase": "^0.21.3",
"rimraf": "^5.0.5",
"semver": "^7.6.2",

View File

@@ -11,6 +11,8 @@
- [Extending the PocketHost CLI](#extending-the-pockethost-cli)
- [Reference](#reference)
- [Core Actions](#core-actions)
- [AfterPluginsLoaded (since 1.6.0)](#afterpluginsloaded-since-160)
- [KillInstance (since 1.6.0)](#killinstance-since-160)
- [Core Filters](#core-filters)
- [ServerSlugs (since 1.4.0)](#serverslugs-since-140)
- [InstanceConfig (since 1.5.0)](#instanceconfig-since-150)
@@ -63,6 +65,14 @@ Use PocketHost to generate a Typescript plugin starter, and follow that code.
pockethost plugin create <my-plugin-name>
```
Plugin template features:
- Listen to PocketHost events
- Install a PocketBase JS Hooks file when instances launch
- Use a local JSON database to store local settings and config
- Add custom commands to the PocketHost CLI
- Attach to the `serve` PocketHost event
## Plugin Events Lifecycle
PocketHost executes many actions and filters that your plugin can access to add, extend, and enhance various features.

View File

@@ -1,10 +1,19 @@
import { Command } from 'commander'
import { readFileSync } from 'fs'
import { dasherize, underscore } from 'inflection'
import nodePlop from 'node-plop'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { join } from 'path'
import { cwd } from 'process'
import { error, info } from '../..'
import { version } from '../../../../package.json'
import { PH_PLUGINS, PH_PROJECT_DIR } from '../../../constants'
import { appendConfig, filterConfig } from '../../../core'
import { getCompatibleVersions, getPackageJson, removePackage } from './util'
const __dirname = dirname(fileURLToPath(import.meta.url))
export const PluginCommand = () => {
const cmd = new Command(`plugin`)
.description(`Manage PocketHost plugins`)
@@ -77,5 +86,28 @@ export const PluginCommand = () => {
}
}),
)
.addCommand(
new Command(`create`)
.description(`Create a new plugin`)
.alias(`new`)
.alias(`n`)
.alias(`make`)
.alias(`generate`)
.argument(`<name>`, `Plugin name (dash case)`)
.action(async (name) => {
const dashCase = dasherize(underscore(name))
const plopfilePath = join(__dirname, 'plopfile.mjs')
const plopInstance = await nodePlop(plopfilePath, {
destBasePath: cwd(),
force: false,
})
const generator = plopInstance.getGenerator('plugin')
const results = await generator.runActions({
name: dashCase,
version,
})
}),
)
return cmd
}

View File

@@ -0,0 +1,38 @@
import { execSync } from 'child_process'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
/** @typedef {import('plop').NodePlopAPI} Plop */
export default function (/** @type {Plop} */ plop) {
plop.setGenerator('plugin', {
description: 'Generate a new plugin',
prompts: [
{
type: 'input',
name: 'name',
message: 'Plugin Name (dash case)',
},
{
type: 'input',
name: 'version',
message: 'PocketHost base version',
},
],
actions: (data) => {
return [
{
type: 'addMany',
destination: 'plugin-{{dashCase name}}',
base: join(__dirname, 'plugin-template'),
templateFiles: join(__dirname, `plugin-template/**/*`),
},
async () => {
console.log(execSync(`pnpm i`).toString())
},
]
},
})
}

View File

@@ -185,29 +185,4 @@ export default function (/** @type {Plop} */ plop) {
]
},
})
plop.setGenerator('plugin', {
description: 'Generate a new plugin',
prompts: [
{
type: 'input',
name: 'name',
message: 'Plugin Name (dash case)',
},
],
actions: (data) => {
data.version = version
return [
{
type: 'addMany',
destination: 'packages/plugin-{{dashCase name}}',
base: 'plop-templates/plugin-template',
templateFiles: `plop-templates/plugin-template/**/*`,
},
async () => {
console.log(execSync(`pnpm i`).toString())
},
]
},
})
}

8
pnpm-lock.yaml generated
View File

@@ -163,7 +163,7 @@ importers:
specifier: ^3.0.0
version: 3.0.0
pockethost:
specifier: workspace:^1.5.0
specifier: workspace:^1.6.0
version: link:../pockethost
devDependencies:
'@types/node':
@@ -352,6 +352,9 @@ importers:
immer:
specifier: ^10.1.1
version: 10.1.1
inflection:
specifier: ^3.0.0
version: 3.0.0
json-stringify-safe:
specifier: ^5.0.1
version: 5.0.1
@@ -367,6 +370,9 @@ importers:
node-fetch:
specifier: ^3.3.2
version: 3.3.2
node-plop:
specifier: ^0.32.0
version: 0.32.0
pocketbase:
specifier: ^0.21.3
version: 0.21.3