Skip to content

Commit

Permalink
feat: kopflos deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 13, 2024
1 parent 5f83025 commit 916a5f8
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 108 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-ants-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kopflos": patch
---

Added `kopflos deploy` command
5 changes: 5 additions & 0 deletions .changeset/orange-years-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/plugin-deploy-resources": patch
---

Export function to deploy programmatically
2 changes: 1 addition & 1 deletion example/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
oxigraph:
image: ghcr.io/oxigraph/oxigraph:0.4.1
image: ghcr.io/oxigraph/oxigraph:0.4.2
user: root
command: serve --location /data --bind 0.0.0.0:7878
ports:
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"build": "kopflos build",
"start": "kopflos serve --mode development --trust-proxy",
"deploy": "kopflos deploy",
"prestart:prod": "npm run build",
"start:prod": "kopflos serve --trust-proxy"
},
Expand Down
60 changes: 30 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 8 additions & 70 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import 'ulog'
import express from 'express'
import { program } from 'commander'
import kopflos from '@kopflos-cms/express'
import log from '@kopflos-cms/logger'
import { loadPlugins } from '@kopflos-cms/core/plugins.js' // eslint-disable-line import/no-unresolved
import { loadConfig } from './lib/config.js'
import { variable } from './lib/options.js'
import deploy from './lib/command/deploy.js'
import build from './lib/command/build.js'
import serve from './lib/command/serve.js'

program.name('kopflos')

interface ServeArgs {
mode?: 'development' | 'production' | unknown
config?: string
port?: number
host?: string
trustProxy?: boolean
variable: Record<string, string>
}

program.command('serve')
.description('Start the server')
.option('-m, --mode <mode>', 'Mode to run in (default: "production")')
Expand All @@ -26,65 +15,14 @@ program.command('serve')
.option('-h, --host <host>', 'Host to bind to (default: "0.0.0.0")')
.addOption(variable)
.option('--trust-proxy [proxy]', 'Trust the X-Forwarded-Host header')
.action(async ({ mode: _mode = 'production', config, port = 1429, host = '0.0.0.0', trustProxy, variable }: ServeArgs) => {
let mode: 'development' | 'production'
if (_mode !== 'development' && _mode !== 'production') {
log.warn('Invalid mode, defaulting to "production"')
mode = 'production'
} else {
mode = _mode
}

const loadedConfig = await loadConfig({
path: config,
})

const finalOptions = {
port,
host,
mode,
...loadedConfig,
variables: {
...loadedConfig.variables,
...variable,
},
}

const app = express()

if (trustProxy) {
app.set('trust proxy', trustProxy)
}

const { instance, middleware } = await kopflos(finalOptions)
app.use(middleware)

await instance.start()

app.listen(port, host, () => {
log.info(`Server running on ${port}. API URL: ${finalOptions.baseIri}`)
})
})

interface BuildArgs {
config?: string
}
.action(serve)

program.command('build')
.option('-c, --config <config>', 'Path to config file')
.action(async ({ config: configPath }: BuildArgs) => {
const config = await loadConfig({
path: configPath,
})
const plugins = await loadPlugins(config.plugins)
.action(build)

log.info('Running build actions...')
const buildActions = plugins.map(plugin => plugin.build?.())
if (buildActions.length === 0) {
return log.warn('No plugins with build actions found')
} else {
await Promise.all(buildActions)
}
})
program.command('deploy')
.option('-c, --config <config>', 'Path to config file')
.action(deploy)

program.parse(process.argv)
22 changes: 22 additions & 0 deletions packages/cli/lib/command/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import log from '@kopflos-cms/logger'
import { loadPlugins } from '@kopflos-cms/core/plugins.js' // eslint-disable-line import/no-unresolved
import { loadConfig } from '../config.js'

interface BuildArgs {
config?: string
}

export default async function (args: BuildArgs) {
const config = await loadConfig({
path: args.config,
})
const plugins = await loadPlugins(config.plugins)

log.info('Running build actions...')
const buildActions = plugins.map(plugin => plugin.build?.())
if (buildActions.length === 0) {
return log.warn('No plugins with build actions found')
} else {
await Promise.all(buildActions)
}
}
28 changes: 28 additions & 0 deletions packages/cli/lib/command/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import log from '@kopflos-cms/logger'
import { deploy } from '@kopflos-cms/plugin-deploy-resources'
import { createEnv } from '@kopflos-cms/core/env.js' // eslint-disable-line import/no-unresolved
import { loadConfig } from '../config.js'

interface DeployArgs {
config?: string
}

export default async function (args: DeployArgs) {
const config = await loadConfig({
path: args.config,
})

const autoDeployPluginConfig = config.plugins?.['@kopflos-cms/plugin-deploy-resources']

if (!autoDeployPluginConfig) {
log.error("'@kopflos-cms/plugin-deploy-resources' not found in plugin configuration")
return process.exit(1)
}

if (!autoDeployPluginConfig.paths?.length) {
log.warn('No resource paths specified. Nothing to deploy')
return process.exit(1)
}

return deploy(autoDeployPluginConfig.paths, createEnv(config))
}
Loading

0 comments on commit 916a5f8

Please sign in to comment.