mirror of
https://github.com/pockethost/pockethost.git
synced 2025-09-16 05:30:17 +00:00
fix(pockethost): refactor .env config and fix plugin appending
This commit is contained in:
parent
c32b845dee
commit
7931204f19
5
.changeset/red-news-retire.md
Normal file
5
.changeset/red-news-retire.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'pockethost': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed a bug where adding plugins was not saved correctly
|
@ -1,20 +1,5 @@
|
|||||||
import { forEach, keys, omit } from '@s-libs/micro-dash'
|
|
||||||
import { Command } from 'commander'
|
import { Command } from 'commander'
|
||||||
import { parse } from 'dotenv'
|
import { listConfig, setConfig, unsetConfig } from '../../../core'
|
||||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
|
||||||
import { info } from '../..'
|
|
||||||
import { PH_HOME, isSettingKey, settings } from '../../../constants'
|
|
||||||
|
|
||||||
const envFile = () => {
|
|
||||||
const envFile = PH_HOME(`.env`)
|
|
||||||
if (!existsSync(envFile)) {
|
|
||||||
writeFileSync(envFile, '')
|
|
||||||
}
|
|
||||||
return envFile
|
|
||||||
}
|
|
||||||
|
|
||||||
const _parse = () =>
|
|
||||||
parse(readFileSync(envFile(), { encoding: 'utf8' }).toString())
|
|
||||||
|
|
||||||
export const ConfigCommand = () => {
|
export const ConfigCommand = () => {
|
||||||
const cmd = new Command(`config`)
|
const cmd = new Command(`config`)
|
||||||
@ -24,76 +9,20 @@ export const ConfigCommand = () => {
|
|||||||
.argument(`<name>`, `Config name`)
|
.argument(`<name>`, `Config name`)
|
||||||
.argument(`<value>`, `Config value`)
|
.argument(`<value>`, `Config value`)
|
||||||
.description(`Set a config value`)
|
.description(`Set a config value`)
|
||||||
.action((name, value) => {
|
.action(setConfig),
|
||||||
if (!isSettingKey(name))
|
|
||||||
throw new Error(`Invalid setting name ${name}`)
|
|
||||||
|
|
||||||
if (value === '=') throw new Error(`Invalid value ${value}`)
|
|
||||||
|
|
||||||
const values = _parse()
|
|
||||||
values[name] = value
|
|
||||||
writeFileSync(
|
|
||||||
envFile(),
|
|
||||||
Object.entries(values)
|
|
||||||
.map(([k, v]) => `${k}=${v}`)
|
|
||||||
.join('\n'),
|
|
||||||
)
|
|
||||||
info(`Set ${name}=${value}`)
|
|
||||||
info(`Written to ${envFile()}`)
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
.addCommand(
|
.addCommand(
|
||||||
new Command(`unset`)
|
new Command(`unset`)
|
||||||
.argument(`<name>`, `Config name`)
|
.argument(`<name>`, `Config name`)
|
||||||
.description(`Unset a config value`)
|
.description(`Unset a config value`)
|
||||||
.action((name) => {
|
.action(unsetConfig),
|
||||||
if (!isSettingKey(name))
|
|
||||||
throw new Error(`Invalid setting name ${name}`)
|
|
||||||
|
|
||||||
const values = _parse()
|
|
||||||
delete values[name]
|
|
||||||
writeFileSync(
|
|
||||||
envFile(),
|
|
||||||
Object.entries(values)
|
|
||||||
.map(([k, v]) => `${k}=${v}`)
|
|
||||||
.join('\n'),
|
|
||||||
)
|
|
||||||
info(`Unset ${name}`)
|
|
||||||
info(`Written to ${envFile()}`)
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
.addCommand(
|
.addCommand(
|
||||||
new Command(`list`)
|
new Command(`list`)
|
||||||
.description(`List all config values`)
|
.description(`List all config values`)
|
||||||
.alias(`ls`)
|
.alias(`ls`)
|
||||||
.action(() => {
|
.action(listConfig),
|
||||||
const values = _parse()
|
|
||||||
|
|
||||||
if (keys(values).length > 0) {
|
|
||||||
info()
|
|
||||||
info(`Config values from ${envFile()}:`)
|
|
||||||
forEach(values, (v, k) => {
|
|
||||||
info(`\t${k}=${v}`)
|
|
||||||
})
|
|
||||||
info()
|
|
||||||
} else {
|
|
||||||
info(`No config values found in ${envFile()}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaults = omit(settings, keys(values) as any)
|
|
||||||
if (keys(defaults).length > 0) {
|
|
||||||
info(`Default values:`)
|
|
||||||
forEach(settings, (v, k) => {
|
|
||||||
if (k in values) return
|
|
||||||
info(`\t${k}=${v}`)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
info(
|
|
||||||
`No default values because all values are defined in ${envFile()}`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,10 @@
|
|||||||
import { uniq } from '@s-libs/micro-dash'
|
|
||||||
import { Command } from 'commander'
|
import { Command } from 'commander'
|
||||||
import { parse } from 'dotenv'
|
import { readFileSync } from 'fs'
|
||||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
|
||||||
import { error, info } from '../..'
|
import { error, info } from '../..'
|
||||||
import { PH_HOME, PH_PLUGINS, PH_PROJECT_DIR } from '../../../constants'
|
import { PH_PLUGINS, PH_PROJECT_DIR } from '../../../constants'
|
||||||
|
import { appendConfig, filterConfig } from '../../../core'
|
||||||
import { getCompatibleVersions, getPackageJson, removePackage } from './util'
|
import { getCompatibleVersions, getPackageJson, removePackage } from './util'
|
||||||
|
|
||||||
const envFile = () => {
|
|
||||||
const envFile = PH_HOME(`.env`)
|
|
||||||
if (!existsSync(envFile)) {
|
|
||||||
writeFileSync(envFile, '')
|
|
||||||
}
|
|
||||||
return envFile
|
|
||||||
}
|
|
||||||
|
|
||||||
const _parse = () =>
|
|
||||||
parse(readFileSync(envFile(), { encoding: 'utf8' }).toString())
|
|
||||||
|
|
||||||
export const PluginCommand = () => {
|
export const PluginCommand = () => {
|
||||||
const cmd = new Command(`plugin`)
|
const cmd = new Command(`plugin`)
|
||||||
.description(`Manage PocketHost plugins`)
|
.description(`Manage PocketHost plugins`)
|
||||||
@ -33,22 +21,7 @@ export const PluginCommand = () => {
|
|||||||
const { version } = pkg
|
const { version } = pkg
|
||||||
await getCompatibleVersions(`pockethost`, version, [name])
|
await getCompatibleVersions(`pockethost`, version, [name])
|
||||||
|
|
||||||
const values = _parse()
|
appendConfig(`PH_PLUGINS`, name)
|
||||||
values[`PH_PLUGINS`] = uniq([
|
|
||||||
...(values[`PH_PLUGINS`]
|
|
||||||
?.split(/,/)
|
|
||||||
.map((v) => v.trim())
|
|
||||||
.filter((v) => !!v) || []),
|
|
||||||
name,
|
|
||||||
]).join(`,`)
|
|
||||||
writeFileSync(
|
|
||||||
envFile(),
|
|
||||||
Object.entries(values)
|
|
||||||
.map(([k, v]) => `${k}=${v}`)
|
|
||||||
.join('\n'),
|
|
||||||
)
|
|
||||||
info(`Installed ${name}`)
|
|
||||||
info(`Written to ${envFile()}`)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error(`${e}`)
|
error(`${e}`)
|
||||||
}
|
}
|
||||||
@ -81,21 +54,7 @@ export const PluginCommand = () => {
|
|||||||
.action(async (name) => {
|
.action(async (name) => {
|
||||||
try {
|
try {
|
||||||
await removePackage(name)
|
await removePackage(name)
|
||||||
const values = _parse()
|
filterConfig(`PH_PLUGINS`, name)
|
||||||
values[`PH_PLUGINS`] = uniq([
|
|
||||||
...(values[`PH_PLUGINS`]
|
|
||||||
?.split(/,/)
|
|
||||||
.map((v) => v.trim())
|
|
||||||
.filter((v) => v !== name) || []),
|
|
||||||
]).join(`,`)
|
|
||||||
writeFileSync(
|
|
||||||
envFile(),
|
|
||||||
Object.entries(values)
|
|
||||||
.map(([k, v]) => `${k}=${v}`)
|
|
||||||
.join('\n'),
|
|
||||||
)
|
|
||||||
info(`Removed ${name}`)
|
|
||||||
info(`Written to ${envFile()}`)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error(`${e}`)
|
error(`${e}`)
|
||||||
}
|
}
|
||||||
|
108
packages/pockethost/src/core/config.ts
Normal file
108
packages/pockethost/src/core/config.ts
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
import { forEach, keys, omit, uniq } from '@s-libs/micro-dash'
|
||||||
|
import { parse } from 'dotenv'
|
||||||
|
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||||
|
import { info } from '../cli'
|
||||||
|
import { PH_HOME, settings } from '../constants'
|
||||||
|
|
||||||
|
const envFile = () => {
|
||||||
|
const envFile = PH_HOME(`.env`)
|
||||||
|
if (!existsSync(envFile)) {
|
||||||
|
writeFileSync(envFile, '')
|
||||||
|
}
|
||||||
|
return envFile
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setConfig = (name: string, value: string) => {
|
||||||
|
if (value === '=') throw new Error(`Invalid value ${value}`)
|
||||||
|
|
||||||
|
const values = _parse()
|
||||||
|
values[name] = value
|
||||||
|
writeFileSync(
|
||||||
|
envFile(),
|
||||||
|
Object.entries(values)
|
||||||
|
.map(([k, v]) => `${k}=${v}`)
|
||||||
|
.join('\n'),
|
||||||
|
)
|
||||||
|
process.env[name] = value
|
||||||
|
info(`Set ${name}=${value}`)
|
||||||
|
info(`Written to ${envFile()}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const unsetConfig = (name: string) => {
|
||||||
|
const values = _parse()
|
||||||
|
delete values[name]
|
||||||
|
writeFileSync(
|
||||||
|
envFile(),
|
||||||
|
Object.entries(values)
|
||||||
|
.map(([k, v]) => `${k}=${v}`)
|
||||||
|
.join('\n'),
|
||||||
|
)
|
||||||
|
info(`Unset ${name}`)
|
||||||
|
info(`Written to ${envFile()}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const listConfig = () => {
|
||||||
|
const values = _parse()
|
||||||
|
|
||||||
|
if (keys(values).length > 0) {
|
||||||
|
info()
|
||||||
|
info(`Config values from ${envFile()}:`)
|
||||||
|
forEach(values, (v, k) => {
|
||||||
|
info(`\t${k}=${v}`)
|
||||||
|
})
|
||||||
|
info()
|
||||||
|
} else {
|
||||||
|
info(`No config values found in ${envFile()}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaults = omit(settings, keys(values) as any)
|
||||||
|
if (keys(defaults).length > 0) {
|
||||||
|
info(`Default values:`)
|
||||||
|
forEach(settings, (v, k) => {
|
||||||
|
if (k in values) return
|
||||||
|
info(`\t${k}=${v}`)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
info(`No default values because all values are defined in ${envFile()}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const appendConfig = (name: string, value: string) => {
|
||||||
|
const values = _parse()
|
||||||
|
values[name] = uniq([
|
||||||
|
...(values[name]
|
||||||
|
?.split(/,/)
|
||||||
|
.map((v) => v.trim())
|
||||||
|
.filter((v) => !!v) || []),
|
||||||
|
value,
|
||||||
|
]).join(`,`)
|
||||||
|
writeFileSync(
|
||||||
|
envFile(),
|
||||||
|
Object.entries(values)
|
||||||
|
.map(([k, v]) => `${k}=${v}`)
|
||||||
|
.join('\n'),
|
||||||
|
)
|
||||||
|
info(`Added ${value} to ${name}`)
|
||||||
|
info(`Written to ${envFile()}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const filterConfig = (name: string, value: string) => {
|
||||||
|
const values = _parse()
|
||||||
|
values[name] = uniq([
|
||||||
|
...(values[name]
|
||||||
|
?.split(/,/)
|
||||||
|
.map((v) => v.trim())
|
||||||
|
.filter((v) => v !== value) || []),
|
||||||
|
]).join(`,`)
|
||||||
|
writeFileSync(
|
||||||
|
envFile(),
|
||||||
|
Object.entries(values)
|
||||||
|
.map(([k, v]) => `${k}=${v}`)
|
||||||
|
.join('\n'),
|
||||||
|
)
|
||||||
|
info(`Filtered ${value} from ${name}`)
|
||||||
|
info(`Written to ${envFile()}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const _parse = () =>
|
||||||
|
parse(readFileSync(envFile(), { encoding: 'utf8' }).toString())
|
@ -1,6 +1,7 @@
|
|||||||
export * from '../constants'
|
export * from '../constants'
|
||||||
export * from './Settings'
|
export * from './Settings'
|
||||||
export * from './asyncExecutionGuard'
|
export * from './asyncExecutionGuard'
|
||||||
|
export * from './config'
|
||||||
export * from './exit'
|
export * from './exit'
|
||||||
export * from './internal'
|
export * from './internal'
|
||||||
export * from './smartFetch'
|
export * from './smartFetch'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user