feat(instance-logger-file): initial commit

This commit is contained in:
Ben Allfree 2024-06-30 07:02:00 -07:00
parent 9550c386d9
commit a1fcc9e217
9 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'@pockethost/plugin-instance-logger-file': minor
---
initial commit

View File

@ -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.

View File

@ -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"
}
}

View File

@ -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.
---

View File

@ -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 }),
})

View File

@ -0,0 +1,3 @@
import { plugin } from './plugin'
export default plugin

View File

@ -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

View File

@ -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 }))
}

View File

@ -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"]
}