chore: $util refactor

This commit is contained in:
Ben Allfree 2022-12-31 09:16:54 -08:00
parent 0d4c2e90d7
commit c6ba24af87
18 changed files with 45 additions and 36 deletions

View File

@ -1,6 +1,8 @@
import { PlatformId, VersionId } from '../releases'
import { BaseFields, RecordId, Seconds, Subdomain, UserId } from './types'
export type VersionId = string
export type PlatformId = string
export enum InstanceStatus {
Unknown = '',
Idle = 'idle',

View File

@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"build": "echo 'Build complete' `date`",
"dev": "DEBUG=1 tsx watch src/server.ts",
"dev": "tsx watch src/server.ts",
"start": "tsx src/server.ts",
"pm2": "pm2 del daemon ; pm2 start \"yarn start\" --name=daemon",
"migrate": "tsx src/migrate/migrate.ts"

View File

@ -1,7 +1,6 @@
import { pocketbase } from '$services'
import { InstanceStatus, logger } from '@pockethost/common'
import { InstanceStatus, logger, safeCatch } from '@pockethost/common'
import { PH_BIN_CACHE, PUBLIC_PB_SUBDOMAIN } from '../constants'
import { safeCatch } from '../util/promiseHelper'
import { schema } from './schema'
import { withInstance } from './withInstance'
;(async () => {

View File

@ -1,6 +1,5 @@
import { logger } from '@pockethost/common'
import { logger, safeCatch } from '@pockethost/common'
import { exec } from 'child_process'
import { safeCatch } from '../util/promiseHelper'
export const pexec = safeCatch(`pexec`, (cmd: string) => {
const { dbg, error } = logger().create('pexec')

View File

@ -1,9 +1,5 @@
import {
createPbClient,
PocketbaseClientApi,
} from '$services/clientService/PbClient'
import { pocketbase } from '$services/PocketBaseService'
import { logger } from '@pockethost/common'
import { createPbClient, pocketbase, PocketbaseClientApi } from '$services'
import { logger, safeCatch } from '@pockethost/common'
import {
DAEMON_PB_PASSWORD,
DAEMON_PB_USERNAME,
@ -11,7 +7,6 @@ import {
PUBLIC_PB_PROTOCOL,
PUBLIC_PB_SUBDOMAIN,
} from '../constants'
import { safeCatch } from '../util/promiseHelper'
export const withInstance = safeCatch(
`withInstance`,

View File

@ -1,4 +1,5 @@
import { clientService, rpcService } from '$services'
import { backupInstance } from '$util'
import {
assertTruthy,
BackupFields,
@ -15,7 +16,6 @@ import {
RpcCommands,
} from '@pockethost/common'
import Bottleneck from 'bottleneck'
import { backupInstance } from '../util/backupInstance'
export const backupService = mkSingleton(async () => {
const { dbg } = logger().create('BackupService')

View File

@ -1,4 +1,5 @@
import { clientService, rpcService } from '$services'
import { mkInternalUrl, now } from '$util'
import {
assertTruthy,
CreateInstancePayload,
@ -10,6 +11,7 @@ import {
logger,
mkSingleton,
RpcCommands,
safeCatch,
} from '@pockethost/common'
import { forEachRight, map } from '@s-libs/micro-dash'
import Bottleneck from 'bottleneck'
@ -21,9 +23,6 @@ import {
PUBLIC_APP_DOMAIN,
PUBLIC_APP_PROTOCOL,
} from '../constants'
import { mkInternalUrl } from '../util/internal'
import { now } from '../util/now'
import { safeCatch } from '../util/promiseHelper'
import { pocketbase, PocketbaseProcess } from './PocketBaseService'
type InstanceApi = {

View File

@ -1,4 +1,11 @@
import { createTimerManager, logger } from '@pockethost/common'
import {
downloadAndExtract,
mkInternalAddress,
mkInternalUrl,
smartFetch,
tryFetch,
} from '$util'
import { createTimerManager, logger, safeCatch } from '@pockethost/common'
import { mkSingleton } from '@pockethost/common/src/mkSingleton'
import { keys } from '@s-libs/micro-dash'
import { spawn } from 'child_process'
@ -9,11 +16,6 @@ import { join } from 'path'
import { maxSatisfying, rsort } from 'semver'
import { AsyncReturnType } from 'type-fest'
import { DAEMON_PB_DATA_DIR } from '../constants'
import { downloadAndExtract } from '../util/downloadAndExtract'
import { mkInternalAddress, mkInternalUrl } from '../util/internal'
import { safeCatch } from '../util/promiseHelper'
import { smartFetch } from '../util/smartFetch'
import { tryFetch } from '../util/tryFetch'
export type PocketbaseCommand = 'serve' | 'upgrade'
export type SpawnConfig = {

View File

@ -7,8 +7,8 @@ import {
InstanceFields,
InstanceId,
logger,
safeCatch,
} from '@pockethost/common'
import { safeCatch } from '../../util/promiseHelper'
import { MixinContext } from './PbClient'
export type BackupApi = ReturnType<typeof createBackupMixin>

View File

@ -5,12 +5,12 @@ import {
InstanceId,
InstanceStatus,
logger,
safeCatch,
UserFields,
} from '@pockethost/common'
import { reduce } from '@s-libs/micro-dash'
import Bottleneck from 'bottleneck'
import { endOfMonth, startOfMonth } from 'date-fns'
import { safeCatch } from '../../util/promiseHelper'
import { MixinContext } from './PbClient'
export type InstanceApi = ReturnType<typeof createInstanceMixin>

View File

@ -3,8 +3,8 @@ import {
InvocationFields,
logger,
pocketNow,
safeCatch,
} from '@pockethost/common'
import { safeCatch } from '../../util/promiseHelper'
import { InstanceApi } from './InstanceMIxin'
import { MixinContext } from './PbClient'

View File

@ -1,4 +1,4 @@
import { logger } from '@pockethost/common'
import { logger, safeCatch } from '@pockethost/common'
import { Knex } from 'knex'
import {
Collection,
@ -7,7 +7,6 @@ import {
} from 'pocketbase'
import { DAEMON_PB_DATA_DIR, PUBLIC_PB_SUBDOMAIN } from '../../constants'
import { Collection_Serialized } from '../../migrate/schema'
import { safeCatch } from '../../util/promiseHelper'
import { createBackupMixin } from './BackupMixin'
import { createInstanceMixin } from './InstanceMIxin'
import { createInvocationMixin } from './InvocationMixin'

View File

@ -1,6 +1,10 @@
import { RpcFields, RpcStatus, RPC_COLLECTION } from '@pockethost/common'
import {
RpcFields,
RpcStatus,
RPC_COLLECTION,
safeCatch,
} from '@pockethost/common'
import { JsonObject } from 'type-fest'
import { safeCatch } from '../../util/promiseHelper'
import { MixinContext } from './PbClient'
export enum RecordSubscriptionActions {

View File

@ -1,4 +1,9 @@
import { BackupRecordId, InstanceId, logger } from '@pockethost/common'
import {
BackupRecordId,
InstanceId,
logger,
safeCatch,
} from '@pockethost/common'
import { statSync } from 'fs'
import { basename, resolve } from 'path'
import { chdir, cwd } from 'process'
@ -7,7 +12,6 @@ import tmp from 'tmp'
import { DAEMON_PB_DATA_DIR } from '../constants'
import { pexec } from '../migrate/pexec'
import { ensureDirExists } from './ensureDirExists'
import { safeCatch } from './promiseHelper'
export type BackupProgress = {
current: number

View File

@ -0,0 +1,8 @@
export * from './backupInstance'
export * from './downloadAndExtract'
export * from './ensureDirExists'
export * from './env'
export * from './internal'
export * from './now'
export * from './smartFetch'
export * from './tryFetch'

View File

@ -1 +0,0 @@
export { safeCatch } from '@pockethost/common'

View File

@ -1,5 +1,4 @@
import { logger } from '@pockethost/common'
import { safeCatch } from './promiseHelper'
import { logger, safeCatch } from '@pockethost/common'
export const tryFetch = safeCatch(`tryFetch`, (url: string) => {
const { dbg } = logger().create('tryFetch')

View File

@ -16,7 +16,7 @@
"noEmit": true,
"baseUrl": ".",
"paths": {
"$util/*": ["src/util/*"],
"$util": ["src/util/index"],
"$services": ["src/services/index"],
"$src/*": ["src/*"]
}