diff --git a/.changeset/1719756120812.md b/.changeset/1719756120812.md new file mode 100644 index 00000000..eae7353a --- /dev/null +++ b/.changeset/1719756120812.md @@ -0,0 +1,5 @@ +--- +'@pockethost/plugin-instance-logger-file': minor +--- + +initial commit \ No newline at end of file diff --git a/packages/plugin-instance-logger-file/LICENSE.md b/packages/plugin-instance-logger-file/LICENSE.md new file mode 100644 index 00000000..6f385987 --- /dev/null +++ b/packages/plugin-instance-logger-file/LICENSE.md @@ -0,0 +1,16 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software +and associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/packages/plugin-instance-logger-file/package.json b/packages/plugin-instance-logger-file/package.json new file mode 100644 index 00000000..5a4cc119 --- /dev/null +++ b/packages/plugin-instance-logger-file/package.json @@ -0,0 +1,34 @@ +{ + "name": "@pockethost/plugin-instance-logger-file", + "version": "0.0.1", + "repository": { + "type": "git", + "url": "http://github.com/pockethost/pockethost/packages/plugin-instance-logger-file" + }, + "description": "A pockethost plugin.", + "main": "src/index.ts", + "module": "src/index.ts", + "types": "src/index.ts", + "type": "module", + "scripts": { + "check-types": "tsc --noEmit " + }, + "keywords": [ + "pockethost" + ], + "author": { + "name": "Ben Allfree", + "url": "https://github.com/benallfree" + }, + "license": "MIT", + "peerDependencies": { + "pockethost": "workspace:^1.6.0" + }, + "devDependencies": { + "@types/node": "^20.8.10", + "typescript": "^5.4.5" + }, + "dependencies": { + "inflection": "^3.0.0" + } +} diff --git a/packages/plugin-instance-logger-file/readme.md b/packages/plugin-instance-logger-file/readme.md new file mode 100644 index 00000000..64075bf0 --- /dev/null +++ b/packages/plugin-instance-logger-file/readme.md @@ -0,0 +1,33 @@ +# plugin-instance-logger-file + +A plugin for [pockethost](https://www.npmjs.com/package/pockethost). + +## Quickstart + +```bash +npx pockethost plugin install @pockethost/plugin-instance-logger-file + +npx pockethost instance-logger-file --help +``` + +## Variables + +The following variables will be used if they are found in the shell environment. PocketHost will also load them from an `.env` file if found at load time. + +| Name | Default | Discussion | +| ------------------------------- | -------------------------------------- | ------------------------------------------------------------- | +| PH\_INSTANCE_LOGGER_FILE\_HOME | `.pockethost/plugin-instance-logger-file` | The home directory for any data storage needs of this plugin. | + +## Actions + +## Filters + +## Support + +PocketHost has a thriving [Discord community](https://discord.gg/nVTxCMEcGT). + +--- + +### Sponsored by https://pockethost.io. Instantly host your PocketBase projects. + +--- diff --git a/packages/plugin-instance-logger-file/src/constants.ts b/packages/plugin-instance-logger-file/src/constants.ts new file mode 100644 index 00000000..0613cacd --- /dev/null +++ b/packages/plugin-instance-logger-file/src/constants.ts @@ -0,0 +1,22 @@ +import { underscore } from 'inflection' +import { dirname, join } from 'path' +import { PH_HOME, Settings, mkPath } from 'pockethost/core' +import { fileURLToPath } from 'url' + +export const PLUGIN_NAME = `plugin-instance-logger-file` + +export const HOME_DIR = + process.env.PH_INSTANCE_LOGGER_FILE_HOME || join(PH_HOME(),PLUGIN_NAME) + +export const PLUGIN_NAME_CONSTANT_CASE = underscore(PLUGIN_NAME, true) + +export const PLUGIN_DATA = (...paths: string[]) => join(HOME_DIR, ...paths) + +const __dirname = dirname(fileURLToPath(import.meta.url)) +export const PROJECT_DIR = (...paths: string[]) => + join(__dirname, '..', ...paths) + +export const settings = Settings({ + PH_INSTANCE_LOGGER_FILE_HOME: mkPath(HOME_DIR, { create: true }), +}) + diff --git a/packages/plugin-instance-logger-file/src/index.ts b/packages/plugin-instance-logger-file/src/index.ts new file mode 100644 index 00000000..9e1018b3 --- /dev/null +++ b/packages/plugin-instance-logger-file/src/index.ts @@ -0,0 +1,3 @@ +import { plugin } from './plugin' + +export default plugin diff --git a/packages/plugin-instance-logger-file/src/log.ts b/packages/plugin-instance-logger-file/src/log.ts new file mode 100644 index 00000000..0865d2e8 --- /dev/null +++ b/packages/plugin-instance-logger-file/src/log.ts @@ -0,0 +1,5 @@ +import { LoggerService } from 'pockethost' +import { PLUGIN_NAME } from './constants' + +const logger = LoggerService().create(PLUGIN_NAME) +export const { dbg, info, error } = logger diff --git a/packages/plugin-instance-logger-file/src/plugin.ts b/packages/plugin-instance-logger-file/src/plugin.ts new file mode 100644 index 00000000..242c9ddd --- /dev/null +++ b/packages/plugin-instance-logger-file/src/plugin.ts @@ -0,0 +1,37 @@ +import { appendFileSync, existsSync, mkdirSync } from 'fs' +import { + PocketHostPlugin, + onAfterPluginsLoadedAction, + onInstanceLogAction, + onSettingsFilter, +} from 'pockethost' +import { INSTANCE_DATA_DIR } from 'pockethost/core' +import { PLUGIN_NAME, settings } from './constants' +import { dbg } from './log' + +export const plugin: PocketHostPlugin = async ({}) => { + dbg(`initializing ${PLUGIN_NAME}`) + + onAfterPluginsLoadedAction(async () => {}) + + onInstanceLogAction(async ({ instance, type, data }) => { + const { id } = instance + const logDirectory = INSTANCE_DATA_DIR(id, `logs`) + if (!existsSync(logDirectory)) { + dbg(`Creating ${logDirectory}`) + mkdirSync(logDirectory, { recursive: true }) + } + + const logFile = INSTANCE_DATA_DIR(id, `logs`, `exec.log`) + + const logLine = JSON.stringify({ + stream: type, + time: +new Date(), + message: data, + }) + appendFileSync(logFile, logLine + '\n') + dbg(`Logged: ${logLine}`) + }) + + onSettingsFilter(async (allSettings) => ({ ...allSettings, ...settings })) +} diff --git a/packages/plugin-instance-logger-file/tsconfig.json b/packages/plugin-instance-logger-file/tsconfig.json new file mode 100644 index 00000000..d0035bbf --- /dev/null +++ b/packages/plugin-instance-logger-file/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "lib": ["ES2021.String"], + "allowJs": true, + "checkJs": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "node", + "noUncheckedIndexedAccess": true, + "strictNullChecks": true, + "noEmit": true + }, + "include": ["**/*.ts"] +}