Skip to content

Commit

Permalink
feat: aeria cli automatic codegen for aeria-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
minenwerfer committed Mar 4, 2024
1 parent d8e11dd commit a15a3a4
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 33 deletions.
46 changes: 38 additions & 8 deletions packages/aeria-build/src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import chokidar from 'chokidar'
import { spawn } from 'child_process'
import path from 'path'
import { spawn, fork } from 'child_process'
import { systemFunctions } from '@aeriajs/builtins'
import { compile } from './compile.js'
import { log } from './log.js'

const compileAndSpawn = async () => {
export const compileAndSpawn = async () => {
const result = await compile()

try {
const { getConfig } = await import('aeria-sdk/utils')
const { writeMirrorFiles } = await import('aeria-sdk/mirror')

const mirror = await systemFunctions.describe({
router: true,
noMemoize: true,
})

const config = await getConfig()
writeMirrorFiles(mirror, config)

} catch( err: any ) {
if( err.code !== 'MODULE_NOT_FOUND' ) {
throw err
}
}

if( result.success ) {
const api = spawn('node', [
'-r',
Expand All @@ -14,6 +34,7 @@ const compileAndSpawn = async () => {
'.env',
'dist/index.js',
])

api.stdout.on('data', (data) => {
process.stdout.write(data)
})
Expand All @@ -23,25 +44,34 @@ const compileAndSpawn = async () => {

return api
}

return null
}

export const watch = async () => {
let runningApi = await compileAndSpawn()
let runningApi = fork(path.join(__dirname, 'watchWorker.js'))
const srcWatcher = chokidar.watch([
'./src',
'./package.json',
'./tsconfig.json',
'./.env',
])

srcWatcher.on('change', async (path) => {
if( runningApi ) {
runningApi.kill()
srcWatcher.on('change', async (filePath) => {
runningApi.kill()

if( runningApi.exitCode === null ) {
await new Promise<void>((resolve) => {
runningApi.on('exit', () => {
resolve()
})
})
}

log('info', `change detected in file: ${path}`)
log('info', `change detected in file: ${filePath}`)
log('info', 'compiling...')
runningApi = await compileAndSpawn()

runningApi = fork(path.join(__dirname, 'watchWorker.js'))
})
}

19 changes: 19 additions & 0 deletions packages/aeria-build/src/watchWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { compileAndSpawn } from './watch.js'

const result = compileAndSpawn()

process.on('SIGTERM', async () => {
const proc = await result
if( !proc ) {
process.exit(1)
}

if( proc.exitCode !== null ) {
process.exit(1)
}

proc.kill()
proc.on('exit', () => {
process.exit(0)
})
})
6 changes: 6 additions & 0 deletions packages/aeria-build/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
],
"@aeriajs/builtins": [
"../builtins/dist"
],
"aeria-sdk": [
"../aeria-sdk/dist"
],
"aeria-sdk/*": [
"../aeria-sdk/dist/*"
]
}
},
Expand Down
13 changes: 13 additions & 0 deletions packages/aeria-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./types": {
"types": "./types.d.ts"
},
"./storage": {
"import": "./dist/storage.mjs",
"require": "./dist/storage.js",
Expand All @@ -43,6 +46,16 @@
"import": "./dist/topLevel.mjs",
"require": "./dist/topLevel.js",
"types": "./dist/topLevel.d.ts"
},
"./utils": {
"import": "./dist/utils.mjs",
"require": "./dist/utils.js",
"types": "./dist/utils.d.ts"
},
"./mirror": {
"import": "./dist/mirror.mjs",
"require": "./dist/mirror.js",
"types": "./dist/mirror.d.ts"
}
},
"peerDependencies": {
Expand Down
13 changes: 4 additions & 9 deletions packages/aeria-sdk/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import path from 'path'
import { mirror } from './mirror.js'
import { mirrorRemotely } from './mirror.js'
import { getConfig } from './utils.js'

const main = async () => {
const { aeriaSdk } = require(path.join(process.cwd(), 'package.json'))
if( typeof aeriaSdk !== 'object' || !aeriaSdk ) {
console.log('aeriaSdk is absent in package.json')
return
}

mirror(aeriaSdk)
const config = await getConfig()
mirrorRemotely(config)
}

main()
2 changes: 2 additions & 0 deletions packages/aeria-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from '@aeriajs/common'
export * from './topLevel.js'
export * from './runtime.js'
export * from './storage.js'
export * from './mirror.js'
export * from './utils.js'
18 changes: 12 additions & 6 deletions packages/aeria-sdk/src/mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,25 @@ export const aeria = Aeria(config)
export const storage = getStorage(config)
\n`

export const mirror = async (config: InstanceConfig) => {
const api = topLevel(config)
export const writeMirrorFiles = async (mirror: any, config: InstanceConfig) => {
const runtimeBase = path.dirname(require.resolve('aeria-sdk'))

const mirror = deserialize(await api.describe.POST({
router: true,
}))

await mkdir(runtimeBase, {
recursive: true,
})

await writeFile(path.join(process.cwd(), 'aeria-sdk.d.ts'), mirrorDts(mirror))
await writeFile(path.join(runtimeBase, 'runtime.js'), runtimeCjs(config))
await writeFile(path.join(runtimeBase, 'runtime.mjs'), runtimeEsm(config))
}

export const mirrorRemotely = async (config: InstanceConfig) => {
const api = topLevel(config)

const mirror = deserialize(await api.describe.POST({
router: true,
}))

return writeMirrorFiles(mirror, config)
}

10 changes: 10 additions & 0 deletions packages/aeria-sdk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { InstanceConfig } from './types.js'
import path from 'path'

export const apiUrl = (config: InstanceConfig) => {
if( typeof config.apiUrl === 'string' ) {
Expand All @@ -10,3 +11,12 @@ export const apiUrl = (config: InstanceConfig) => {
: config.apiUrl.development
}

export const getConfig = async () => {
const { aeriaSdk } = await import(path.join(process.cwd(), 'package.json'))
if( typeof aeriaSdk !== 'object' || !aeriaSdk ) {
throw new Error('aeriaSdk is absent in package.json')
}

return aeriaSdk

}
17 changes: 11 additions & 6 deletions packages/builtins/src/functions/describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ type Payload = {
roles?: boolean
revalidate?: boolean
router?: boolean
noMemoize?: boolean
}

export const describe = async (context: Context): Promise<any> => {
export const describe = async (contextOrPayload: Context | Payload) => {
const result = {} as {
descriptions: typeof descriptions
roles?: string[]
Expand All @@ -24,7 +25,9 @@ export const describe = async (context: Context): Promise<any> => {
router?: any
}

const props: Payload = context.request.payload
const props = 'request' in contextOrPayload
? contextOrPayload.request.payload
: contextOrPayload

if( props.revalidate ) {
const authEither = await authenticate({
Expand All @@ -43,7 +46,9 @@ export const describe = async (context: Context): Promise<any> => {
result.auth = JSON.parse(JSON.stringify(auth))
}

const collections = await getCollections()
const collections = await getCollections({
memoize: !props.noMemoize
})

const retrievedCollections = props.collections?.length
? Object.fromEntries(Object.entries(collections).filter(([key]) => props.collections!.includes(key)))
Expand Down Expand Up @@ -72,11 +77,11 @@ export const describe = async (context: Context): Promise<any> => {
result.router = router.routesMeta
}

if( props.noSerialize ) {
if( props.noSerialize || !('response' in contextOrPayload) ) {
return result
}

context.response.setHeader('content-type', 'application/bson')
return context.response.end(serialize(result))
contextOrPayload.response.setHeader('content-type', 'application/bson')
return contextOrPayload.response.end(serialize(result))
}

12 changes: 8 additions & 4 deletions packages/entrypoint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import fs from 'fs/promises'
let collectionsMemo: Awaited<ReturnType<typeof internalGetCollections>> | undefined
const collectionMemo: Record<string, Collection | undefined> = {}

export const getEntrypoint = async () => {
export const getEntrypointPath = async () => {
const { main, aeriaMain } = JSON.parse(await fs.readFile(path.join(process.cwd(), 'package.json'), {
encoding: 'utf8',
}))

return dynamicImport(path.join(process.cwd(), aeriaMain || main))
return path.join(process.cwd(), aeriaMain || main)
}

export const getEntrypoint = async () => {
return dynamicImport(await getEntrypointPath())
}

const internalGetCollections = async (): Promise<Record<string, Collection | (()=> Collection)>> => {
Expand All @@ -24,8 +28,8 @@ const internalGetCollections = async (): Promise<Record<string, Collection | (()
return Object.assign({}, collections)
}

export const getCollections = async () => {
if( collectionsMemo ) {
export const getCollections = async ({ memoize } = { memoize: true }) => {
if( memoize && collectionsMemo ) {
return Object.assign({}, collectionsMemo)
}

Expand Down

0 comments on commit a15a3a4

Please sign in to comment.