diff --git a/.changeset/1719774712814.md b/.changeset/1719774712814.md new file mode 100644 index 00000000..0ccf0ed6 --- /dev/null +++ b/.changeset/1719774712814.md @@ -0,0 +1,5 @@ +--- +'pockethost': minor +--- + +Added plugin template command and updated docs \ No newline at end of file diff --git a/cspell.json b/cspell.json index ee33d27f..11d043d0 100644 --- a/cspell.json +++ b/cspell.json @@ -10,6 +10,7 @@ "czvf", "daos", "Darkmode", + "dasherize", "Deallocator", "devcert", "Dockerized", @@ -44,6 +45,7 @@ "pbgo", "PBOUNCE", "pexec", + "plopfile", "plugified", "pocketbase", "pockethost", diff --git a/packages/pockethost/package.json b/packages/pockethost/package.json index 33b9a52e..1373592f 100644 --- a/packages/pockethost/package.json +++ b/packages/pockethost/package.json @@ -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", diff --git a/packages/pockethost/plugin-guide.md b/packages/pockethost/plugin-guide.md index 9a8c0849..f201c859 100644 --- a/packages/pockethost/plugin-guide.md +++ b/packages/pockethost/plugin-guide.md @@ -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 ``` +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. diff --git a/packages/pockethost/src/cli/commands/PluginCommand/index.ts b/packages/pockethost/src/cli/commands/PluginCommand/index.ts index ccef764e..7b8de1a4 100644 --- a/packages/pockethost/src/cli/commands/PluginCommand/index.ts +++ b/packages/pockethost/src/cli/commands/PluginCommand/index.ts @@ -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(``, `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 } diff --git a/packages/pockethost/src/cli/commands/PluginCommand/plopfile.mjs b/packages/pockethost/src/cli/commands/PluginCommand/plopfile.mjs new file mode 100644 index 00000000..4c819c14 --- /dev/null +++ b/packages/pockethost/src/cli/commands/PluginCommand/plopfile.mjs @@ -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()) + }, + ] + }, + }) +} diff --git a/plop-templates/plugin-template/.npmignore b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/.npmignore similarity index 100% rename from plop-templates/plugin-template/.npmignore rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/.npmignore diff --git a/plop-templates/plugin-template/LICENSE.md b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/LICENSE.md similarity index 100% rename from plop-templates/plugin-template/LICENSE.md rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/LICENSE.md diff --git a/plop-templates/plugin-template/package.json b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/package.json similarity index 100% rename from plop-templates/plugin-template/package.json rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/package.json diff --git a/plop-templates/plugin-template/readme.md b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/readme.md similarity index 100% rename from plop-templates/plugin-template/readme.md rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/readme.md diff --git a/plop-templates/plugin-template/src/constants.ts b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/constants.ts similarity index 100% rename from plop-templates/plugin-template/src/constants.ts rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/constants.ts diff --git a/plop-templates/plugin-template/src/db.ts b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/db.ts similarity index 100% rename from plop-templates/plugin-template/src/db.ts rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/db.ts diff --git a/plop-templates/plugin-template/src/index.ts b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/index.ts similarity index 100% rename from plop-templates/plugin-template/src/index.ts rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/index.ts diff --git a/plop-templates/plugin-template/src/instance-app/hooks/{{ dashCase name }}-example.pb.js b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/instance-app/hooks/{{ dashCase name }}-example.pb.js similarity index 100% rename from plop-templates/plugin-template/src/instance-app/hooks/{{ dashCase name }}-example.pb.js rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/instance-app/hooks/{{ dashCase name }}-example.pb.js diff --git a/plop-templates/plugin-template/src/log.ts b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/log.ts similarity index 100% rename from plop-templates/plugin-template/src/log.ts rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/log.ts diff --git a/plop-templates/plugin-template/src/plugin.ts b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/plugin.ts similarity index 100% rename from plop-templates/plugin-template/src/plugin.ts rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/src/plugin.ts diff --git a/plop-templates/plugin-template/tsconfig.json b/packages/pockethost/src/cli/commands/PluginCommand/plugin-template/tsconfig.json similarity index 100% rename from plop-templates/plugin-template/tsconfig.json rename to packages/pockethost/src/cli/commands/PluginCommand/plugin-template/tsconfig.json diff --git a/plopfile.mjs b/plopfile.mjs index dfa02c72..16870c8c 100644 --- a/plopfile.mjs +++ b/plopfile.mjs @@ -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()) - }, - ] - }, - }) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 908fe4eb..9981d559 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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