Skip to content

Commit

Permalink
Merge branch 'plugin-vite' into templates
Browse files Browse the repository at this point in the history
# Conflicts:
#	example/kopflos.config.ts
#	packages/plugin-express/package.json
#	packages/vite/lib/config.ts
  • Loading branch information
tpluscode committed Oct 22, 2024
2 parents 8283580 + fb65010 commit 5173bb2
Show file tree
Hide file tree
Showing 49 changed files with 883 additions and 56 deletions.
12 changes: 6 additions & 6 deletions .changeset/odd-hounds-float.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ accessible as `HandlerArgs#subjectVariables` and included when resolving `code:E
[
a kl:Handler ;
kl:method "GET" ;
] ;
code:implementedBy
[
a code:EcmaScriptModule ;
code:link <node:@kopflos-cms/serve-file#default> ;
code:arguments ( "pages/${type}.html"^^code:EcmaScriptTemplateLiteral ) ;
code:implementedBy
[
a code:EcmaScriptModule ;
code:link <node:@kopflos-cms/serve-file#default> ;
code:arguments ( "pages/${type}.html"^^code:EcmaScriptTemplateLiteral ) ;
] ;
] ;
.
```
5 changes: 5 additions & 0 deletions .changeset/thin-penguins-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kopflos-cms/core": patch
---

Added `./env.js` to package exports
4 changes: 2 additions & 2 deletions example/kopflos.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export default <KopflosConfig> {
'@kopflos-cms/plugin-deploy-resources': {
paths: ['resources', 'resources.dev'],
},
'@kopflos-cms/plugin-express': {
'@kopflos-cms/express/middleware': {
before: [
'cors',
['compression', { level: 9 }],
url.fileURLToPath(new URL('.', import.meta.url) + 'lib/static.js'),
url.fileURLToPath(new URL('lib/static.js', import.meta.url)),
],
},
'@kopflos-cms/vite': {
Expand Down
41 changes: 34 additions & 7 deletions package-lock.json

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

33 changes: 29 additions & 4 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import { variable } from './lib/options.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 @@ -16,8 +25,18 @@ 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 = 'production', config, port = 1429, host = '0.0.0.0', trustProxy, variable }) => {
const loadedConfig = await loadConfig(config)
.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,
Expand Down Expand Up @@ -46,10 +65,16 @@ program.command('serve')
})
})

interface BuildArgs {
config?: string
}

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

await instance.loadPlugins()

Expand Down
9 changes: 7 additions & 2 deletions packages/cli/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import type { KopflosConfig } from '@kopflos-cms/core'

const explorer = cosmiconfig('kopflos')

export async function loadConfig(path: string): Promise<KopflosConfig> {
interface LoadConfig {
root?: string
path: string | undefined
}

export async function loadConfig({ path, root }: LoadConfig): Promise<KopflosConfig> {
let ccResult: CosmiconfigResult
if (path) {
ccResult = await explorer.load(path)
} else {
ccResult = await explorer.search(path)
ccResult = await explorer.search(root)
}

if (!ccResult) {
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@
"bin": {
"kopflos": "./bin/kopflos.sh"
},
"files": [
"bin",
"lib/*.js",
"lib/*.d.ts",
"*.js",
"*.d.ts"
],
"scripts": {
"test": "mocha",
"build": "tsc",
"prepack": "npm run build"
},
"author": "Zazuko GmbH",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/zazuko/kopflos.git",
"directory": "packages/cli"
},
"bugs": {
"url": "https://github.com/zazuko/kopflos/issues"
},
"homepage": "https://github.com/zazuko/kopflos",
"dependencies": {
"@kopflos-cms/core": "^0.3.0-beta.8",
"@kopflos-cms/express": "^0.0.1-beta.4",
Expand All @@ -14,5 +37,16 @@
"cosmiconfig": "^9.0.0",
"express": "^4.21.0",
"ulog": "^2.0.0-beta.19"
},
"devDependencies": {
"chai": "^5.1.1"
},
"mocha": {
"extension": [
"ts"
],
"spec": "test/**/*.test.ts",
"loader": "ts-node/esm",
"require": "../../mocha-setup.js"
}
}
3 changes: 3 additions & 0 deletions packages/cli/test/fixtures/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"baseIri": "https://example.com/"
}
5 changes: 5 additions & 0 deletions packages/cli/test/kopflos.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { KopflosConfig } from '@kopflos-cms/core'

export default <KopflosConfig> {
baseIri: 'https://example.com/',
}
35 changes: 35 additions & 0 deletions packages/cli/test/lib/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import url from 'node:url'
import { expect } from 'chai'
import { loadConfig } from '../../lib/config.js'

describe('kopflos/lib/config.js', function () {
this.timeout(10000)

describe('loadConfig', () => {
it('should discover the config file', async () => {
const config = await loadConfig({
path: undefined,
root: url.fileURLToPath(new URL('..', import.meta.url)),
})

expect(config).to.be.deep.equal({
baseIri: 'https://example.com/',
})
})

it('should load config from path', async () => {
// given
const configPath = url.fileURLToPath(new URL('../fixtures/config.json', import.meta.url))

// when
const config = await loadConfig({
path: configPath,
})

// given
expect(config).to.be.deep.equal({
baseIri: 'https://example.com/',
})
})
})
})
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { KopflosResponse, KopflosPlugin, ResultEnvelope } from './lib/Kopflos.js'
export type { KopflosResponse, KopflosPlugin, PluginConfig, ResultEnvelope } from './lib/Kopflos.js'
export type { Kopflos, KopflosConfig, Body, Query } from './lib/Kopflos.js'
export { default } from './lib/Kopflos.js'
export { loadHandlers as defaultHandlerLookup } from './lib/handler.js'
Expand Down
6 changes: 5 additions & 1 deletion packages/core/lib/Kopflos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ interface Clients {

type Endpoint = string | EndpointOptions | Clients | Client

export interface PluginConfig {
[plugin: string]: unknown
}

export interface KopflosConfig {
mode?: 'development' | 'production'
baseIri: string
sparql: Record<string, Endpoint> & { default: Endpoint }
codeBase?: string
apiGraphs?: Array<NamedNode | string>
plugins?: Record<string, unknown>
plugins?: PluginConfig
variables?: Record<string, unknown>
}

Expand Down
11 changes: 7 additions & 4 deletions packages/core/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const loadHandlers: HandlerLookup = ({ resourceShape, ...rest }: Resource
vars.set(k, v)
}
}
const createHandler = createCreateHandler(env, vars)
const createHandler = createHandlerFactory(env, vars)

const impl = handler.out(env.ns.code.implementedBy)
if (impl.isList()) {
Expand All @@ -80,18 +80,21 @@ export const loadHandlers: HandlerLookup = ({ resourceShape, ...rest }: Resource
return []
}

function createCreateHandler(env: KopflosEnvironment, variables: Map<string, unknown>) {
function createHandlerFactory(env: KopflosEnvironment, variables: Map<string, unknown>) {
return (impl: AnyPointer): Promise<Handler> | undefined => {
const factory = env.load<HandlerFactory>(impl)
if (!factory) {
return
}

let promise: Promise<HandlerFactory>
if (typeof factory === 'function') {
return Promise.resolve(factory?.())
promise = Promise.resolve(factory)
} else {
promise = factory
}

return factory.then(async factory => {
return promise.then(async factory => {
if (isGraphPointer(impl) && isGraphPointer(impl.out(env.ns.code.arguments))) {
const args = await loadArguments(impl, {
variables,
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"exports": {
".": "./index.js",
"./env.js": "./lib/env/index.js",
"./resourceLoaders.js": "./resourceLoaders.js"
},
"files": [
Expand Down
Loading

0 comments on commit 5173bb2

Please sign in to comment.