@pikku/cli 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/bin/pikku-all.ts +10 -3
  3. package/cli.schema.json +4 -0
  4. package/dist/bin/pikku-all.js +7 -3
  5. package/dist/src/pikku-cli-config.d.ts +3 -0
  6. package/dist/src/pikku-cli-config.js +15 -4
  7. package/dist/src/serialize-import-map.js +62 -1
  8. package/dist/src/serialize-pikku-types.js +2 -2
  9. package/dist/src/utils.js +24 -2
  10. package/dist/src/wirings/functions/pikku-command-function-types.js +2 -2
  11. package/dist/src/wirings/functions/pikku-command-functions.d.ts +0 -4
  12. package/dist/src/wirings/functions/pikku-command-functions.js +10 -31
  13. package/dist/src/wirings/functions/pikku-command-services.js +1 -1
  14. package/dist/src/wirings/functions/pikku-function-types.js +2 -2
  15. package/dist/src/wirings/functions/serialize-function-imports.d.ts +6 -0
  16. package/dist/src/wirings/functions/serialize-function-imports.js +59 -0
  17. package/dist/src/wirings/rpc/pikku-command-rpc-client.js +2 -2
  18. package/dist/src/wirings/rpc/pikku-command-rpc-map.d.ts +2 -1
  19. package/dist/src/wirings/rpc/pikku-command-rpc-map.js +9 -3
  20. package/dist/src/wirings/rpc/pikku-command-rpc.js +7 -2
  21. package/dist/src/wirings/rpc/serialize-typed-rpc-map.d.ts +1 -2
  22. package/dist/src/wirings/rpc/serialize-typed-rpc-map.js +2 -1
  23. package/dist/tsconfig.tsbuildinfo +1 -1
  24. package/package.json +3 -3
  25. package/src/pikku-cli-config.ts +35 -5
  26. package/src/serialize-import-map.ts +67 -2
  27. package/src/serialize-pikku-types.ts +2 -2
  28. package/src/utils.ts +31 -2
  29. package/src/wirings/functions/pikku-command-function-types.ts +6 -2
  30. package/src/wirings/functions/pikku-command-functions.ts +28 -64
  31. package/src/wirings/functions/pikku-command-services.ts +1 -1
  32. package/src/wirings/functions/pikku-function-types.ts +6 -2
  33. package/src/wirings/functions/serialize-function-imports.ts +97 -0
  34. package/src/wirings/rpc/pikku-command-rpc-client.ts +8 -4
  35. package/src/wirings/rpc/pikku-command-rpc-map.ts +27 -4
  36. package/src/wirings/rpc/pikku-command-rpc.ts +16 -6
  37. package/src/wirings/rpc/serialize-typed-rpc-map.ts +4 -5
  38. package/dist/src/wirings/functions/pikku-functions.d.ts +0 -6
  39. package/dist/src/wirings/functions/pikku-functions.js +0 -35
  40. package/dist/src/wirings/rpc/pikku-rpc.d.ts +0 -2
  41. package/dist/src/wirings/rpc/pikku-rpc.js +0 -6
  42. package/src/wirings/functions/pikku-functions.ts +0 -84
  43. package/src/wirings/rpc/pikku-rpc.ts +0 -22
@@ -15,6 +15,7 @@ export interface PikkuCLICoreOutputFiles {
15
15
  // Function definitions
16
16
  functionsFile: string
17
17
  functionsMetaFile: string
18
+ functionsMetaMinFile: string
18
19
 
19
20
  // HTTP
20
21
  httpWiringsFile: string
@@ -26,7 +27,11 @@ export interface PikkuCLICoreOutputFiles {
26
27
  channelsWiringMetaFile: string
27
28
  channelsMapDeclarationFile: string
28
29
 
29
- // RPC
30
+ // RPC Internal
31
+ rpcInternalWiringMetaFile: string
32
+ rpcInternalMapDeclarationFile: string
33
+
34
+ // RPC Exposed
30
35
  rpcWiringMetaFile: string
31
36
  rpcMapDeclarationFile: string
32
37
 
@@ -166,7 +171,8 @@ const _getPikkuCLIConfig = async (
166
171
  const functionDir = join(result.outDir, 'function')
167
172
  const httpDir = join(result.outDir, 'http')
168
173
  const channelDir = join(result.outDir, 'channel')
169
- const rpcDir = join(result.outDir, 'rpc')
174
+ const internalRPCDirectory = join(result.outDir, 'rpc-internal')
175
+ const externalRPCDirectory = join(result.outDir, 'rpc')
170
176
  const schedulerDir = join(result.outDir, 'scheduler')
171
177
  const queueDir = join(result.outDir, 'queue')
172
178
  const mcpDir = join(result.outDir, 'mcp')
@@ -187,6 +193,12 @@ const _getPikkuCLIConfig = async (
187
193
  'pikku-functions-meta.gen.ts'
188
194
  )
189
195
  }
196
+ if (!result.functionsMetaMinFile) {
197
+ result.functionsMetaMinFile = join(
198
+ functionDir,
199
+ 'pikku-functions-meta.min.gen.ts'
200
+ )
201
+ }
190
202
  if (!result.typesDeclarationFile) {
191
203
  result.typesDeclarationFile = join(result.outDir, 'pikku-types.gen.ts')
192
204
  }
@@ -225,13 +237,31 @@ const _getPikkuCLIConfig = async (
225
237
  )
226
238
  }
227
239
 
228
- // RPC
240
+ // Internal
241
+ if (!result.rpcInternalWiringMetaFile) {
242
+ result.rpcInternalWiringMetaFile = join(
243
+ internalRPCDirectory,
244
+ 'pikku-rpc-wirings-meta.internal.gen.ts'
245
+ )
246
+ }
247
+
248
+ if (!result.rpcInternalMapDeclarationFile) {
249
+ result.rpcInternalMapDeclarationFile = join(
250
+ internalRPCDirectory,
251
+ 'pikku-rpc-wirings-map.internal.gen.d.ts'
252
+ )
253
+ }
254
+
255
+ // External
229
256
  if (!result.rpcWiringMetaFile) {
230
- result.rpcWiringMetaFile = join(rpcDir, 'pikku-rpc-wirings-meta.gen.ts')
257
+ result.rpcWiringMetaFile = join(
258
+ externalRPCDirectory,
259
+ 'pikku-rpc-wirings-meta.gen.ts'
260
+ )
231
261
  }
232
262
  if (!result.rpcMapDeclarationFile) {
233
263
  result.rpcMapDeclarationFile = join(
234
- rpcDir,
264
+ externalRPCDirectory,
235
265
  'pikku-rpc-wirings-map.gen.d.ts'
236
266
  )
237
267
  }
@@ -9,8 +9,73 @@ export const serializeImportMap = (
9
9
  ) => {
10
10
  const paths = new Map<string, string[]>()
11
11
  Array.from(requiredTypes).forEach((requiredType) => {
12
- const { originalName, uniqueName, path } =
13
- typesMap.getTypeMeta(requiredType)
12
+ let originalName, uniqueName, path
13
+
14
+ try {
15
+ const typeMeta = typesMap.getTypeMeta(requiredType)
16
+ originalName = typeMeta.originalName
17
+ uniqueName = typeMeta.uniqueName
18
+ path = typeMeta.path
19
+ } catch (e) {
20
+ // Handle missing types by trying to find a suitable import path
21
+ // Look through all existing types in the map to find a path that might contain this type
22
+ let foundPath: string | null = null
23
+
24
+ // Get all unique paths from the typesMap
25
+ const allPaths = new Set<string>()
26
+ typesMap.customTypes.forEach(({ type, references }) => {
27
+ references.forEach((ref) => {
28
+ try {
29
+ const refMeta = typesMap.getTypeMeta(ref)
30
+ if (refMeta.path) {
31
+ allPaths.add(refMeta.path)
32
+ }
33
+ } catch {
34
+ // Continue
35
+ }
36
+ })
37
+ })
38
+
39
+ // Also check direct types in the map
40
+ try {
41
+ const mapEntries = (typesMap as any).map?.entries?.() || []
42
+ for (const [_, typeMeta] of mapEntries) {
43
+ if (typeMeta.path) {
44
+ allPaths.add(typeMeta.path)
45
+ }
46
+ }
47
+ } catch {
48
+ // Continue
49
+ }
50
+
51
+ // For PascalCase types, prefer paths that look like type definition files
52
+ if (/^[A-Z]/.test(requiredType)) {
53
+ for (const candidatePath of allPaths) {
54
+ if (
55
+ candidatePath.includes('types') ||
56
+ candidatePath.includes('.d.')
57
+ ) {
58
+ foundPath = candidatePath
59
+ break
60
+ }
61
+ }
62
+
63
+ // If no types file found, use the first available path
64
+ if (!foundPath && allPaths.size > 0) {
65
+ foundPath = Array.from(allPaths)[0] || null
66
+ }
67
+ }
68
+
69
+ if (foundPath) {
70
+ originalName = requiredType
71
+ uniqueName = requiredType
72
+ path = foundPath
73
+ } else {
74
+ // No suitable path found, skip
75
+ return
76
+ }
77
+ }
78
+
14
79
  if (!path) {
15
80
  // This is a custom type that exists in file, so we don't need to import it
16
81
  return
@@ -58,7 +58,7 @@ type PikkuFunctionSessionless<
58
58
  Out = never,
59
59
  ChannelData = null, // null means optional channel
60
60
  MCPData = null, // null means optional MCP
61
- RequiredServices extends Services = Services &
61
+ RequiredServices extends Services = Omit<Services, 'rpc'> &
62
62
  { rpc: TypedPikkuRPC } & (
63
63
  [ChannelData] extends [null]
64
64
  ? { channel?: PikkuChannel<unknown, Out> } // Optional channel
@@ -84,7 +84,7 @@ type PikkuFunction<
84
84
  Out = never,
85
85
  ChannelData = null, // null means optional channel
86
86
  MCPData = null, // null means optional MCP
87
- RequiredServices extends Services = Services &
87
+ RequiredServices extends Services = Omit<Services, 'rpc'> &
88
88
  { rpc: TypedPikkuRPC } & (
89
89
  [ChannelData] extends [null]
90
90
  ? { channel?: PikkuChannel<unknown, Out> } // Optional channel
package/src/utils.ts CHANGED
@@ -371,9 +371,38 @@ export function generateCustomTypes(
371
371
  ${Array.from(typesMap.customTypes.entries())
372
372
  .map(([name, { type, references }]) => {
373
373
  references.forEach((name) => {
374
- const originalName = typesMap.getTypeMeta(name).originalName
375
- requiredTypes.add(originalName)
374
+ requiredTypes.add(name)
376
375
  })
376
+
377
+ // Extract type names from the type string that might not be in references
378
+ const typeString = type
379
+ // Use regex to extract potential type names (PascalCase identifiers)
380
+ const typeNameRegex = /\b[A-Z][a-zA-Z0-9]*\b/g
381
+ const potentialTypes = typeString.match(typeNameRegex) || []
382
+
383
+ potentialTypes.forEach((typeName) => {
384
+ // Skip string literals and common keywords
385
+ if (
386
+ typeString.includes(`"${typeName}"`) ||
387
+ ['Pick', 'Omit', 'Partial', 'Required', 'Record', 'Readonly'].includes(
388
+ typeName
389
+ )
390
+ ) {
391
+ return
392
+ }
393
+
394
+ // Try to find this type in the typesMap and add it if found
395
+ try {
396
+ const typeMeta = typesMap.getTypeMeta(typeName)
397
+ if (typeMeta.path) {
398
+ requiredTypes.add(typeName)
399
+ }
400
+ } catch (e) {
401
+ // Type not found in map, but add it anyway for fallback resolution
402
+ requiredTypes.add(typeName)
403
+ }
404
+ })
405
+
377
406
  return `export type ${name} = ${type}`
378
407
  })
379
408
  .join('\n')}`
@@ -9,7 +9,11 @@ import { PikkuCommand } from '../../types.js'
9
9
 
10
10
  export const pikkuFunctionTypes: PikkuCommand = async (
11
11
  logger,
12
- { typesDeclarationFile: typesFile, packageMappings, rpcMapDeclarationFile },
12
+ {
13
+ typesDeclarationFile: typesFile,
14
+ packageMappings,
15
+ rpcInternalMapDeclarationFile,
16
+ },
13
17
  visitState,
14
18
  options = {}
15
19
  ) => {
@@ -39,7 +43,7 @@ export const pikkuFunctionTypes: PikkuCommand = async (
39
43
  `import type { ${singletonServicesType.type} } from '${getFileImportRelativePath(typesFile, singletonServicesType.typePath, packageMappings)}'`,
40
44
  singletonServicesType.type,
41
45
  `import type { ${sessionServicesType.type} } from '${getFileImportRelativePath(typesFile, sessionServicesType.typePath, packageMappings)}'`,
42
- `import type { TypedPikkuRPC } from '${getFileImportRelativePath(typesFile, rpcMapDeclarationFile, packageMappings)}'`
46
+ `import type { TypedPikkuRPC } from '${getFileImportRelativePath(typesFile, rpcInternalMapDeclarationFile, packageMappings)}'`
43
47
  )
44
48
 
45
49
  await writeFileInDir(logger, typesFile, content)
@@ -1,63 +1,14 @@
1
- import {
2
- getFileImportRelativePath,
3
- logCommandInfoAndTime,
4
- writeFileInDir,
5
- } from '../../utils.js'
1
+ import { logCommandInfoAndTime, writeFileInDir } from '../../utils.js'
6
2
  import { PikkuCommand } from '../../types.js'
7
-
8
- export const serializeFunctionImports = (
9
- outputPath: string,
10
- functionsMap: Map<string, { path: string; exportedName: string }>,
11
- packageMappings: Record<string, string> = {}
12
- ) => {
13
- const serializedImports: string[] = [
14
- `/* Import and register RPCs */`,
15
- `import { addFunction } from '@pikku/core'`,
16
- ]
17
-
18
- const serializedRegistrations: string[] = []
19
-
20
- // Sort by function name for consistent output
21
- const sortedEntries = Array.from(functionsMap.entries()).sort((a, b) =>
22
- a[0].localeCompare(b[0])
23
- )
24
-
25
- for (const [name, { path, exportedName }] of sortedEntries) {
26
- const filePath = getFileImportRelativePath(
27
- outputPath,
28
- path,
29
- packageMappings
30
- )
31
-
32
- // For directly exported functions, we can just import and register them
33
- if (name === exportedName) {
34
- serializedImports.push(`import { ${exportedName} } from '${filePath}'`)
35
- serializedRegistrations.push(
36
- `addFunction('${name}', { func: ${exportedName} })`
37
- )
38
- }
39
- // For renamed functions, we need to import and alias them
40
- else {
41
- serializedImports.push(
42
- `import { ${exportedName} as ${name} } from '${filePath}'`
43
- )
44
- serializedRegistrations.push(`addFunction('${name}', ${name})`)
45
- }
46
- }
47
-
48
- // Add a blank line between imports and registrations
49
- if (serializedImports.length > 0 && serializedRegistrations.length > 0) {
50
- serializedImports.push('')
51
- }
52
-
53
- // Combine the imports and registrations
54
- return [...serializedImports, ...serializedRegistrations].join('\n')
55
- }
3
+ import {
4
+ generateRuntimeMeta,
5
+ serializeFunctionImports,
6
+ } from './serialize-function-imports.js'
56
7
 
57
8
  export const pikkuFunctions: PikkuCommand = async (
58
9
  logger,
59
- { functionsMetaFile, functionsFile, packageMappings },
60
- { functions }
10
+ { functionsMetaFile, functionsMetaMinFile, functionsFile, packageMappings },
11
+ { functions, rpc }
61
12
  ) => {
62
13
  return await logCommandInfoAndTime(
63
14
  logger,
@@ -65,20 +16,33 @@ export const pikkuFunctions: PikkuCommand = async (
65
16
  'Serialized Pikku functions',
66
17
  [false],
67
18
  async () => {
19
+ // Generate full metadata
68
20
  await writeFileInDir(
69
21
  logger,
70
- functionsFile,
71
- serializeFunctionImports(
72
- functionsFile,
73
- functions.files,
74
- packageMappings
75
- )
22
+ functionsMetaFile,
23
+ `import { pikkuState } from '@pikku/core'\npikkuState('function', 'meta', ${JSON.stringify(functions.meta, null, 2)})`
76
24
  )
25
+
26
+ // Generate minimal metadata (runtime)
27
+ const runtimeMeta = generateRuntimeMeta(functions.meta)
77
28
  await writeFileInDir(
78
29
  logger,
79
- functionsMetaFile,
80
- `import { pikkuState } from '@pikku/core'\npikkuState('function', 'meta', ${JSON.stringify(functions.meta, null, 2)})`
30
+ functionsMetaMinFile,
31
+ `import { pikkuState } from '@pikku/core'\npikkuState('function', 'meta', ${JSON.stringify(runtimeMeta, null, 2)})`
81
32
  )
33
+
34
+ if (rpc.exposedFiles.size > 0 || rpc.internalFiles.size > 0) {
35
+ await writeFileInDir(
36
+ logger,
37
+ functionsFile,
38
+ serializeFunctionImports(
39
+ functionsFile,
40
+ rpc.internalFiles,
41
+ functions.meta,
42
+ packageMappings
43
+ )
44
+ )
45
+ }
82
46
  }
83
47
  )
84
48
  }
@@ -95,7 +95,7 @@ export const pikkuServices: PikkuCommand = async (
95
95
  logger,
96
96
  'Generating Pikku services map',
97
97
  'Generated Pikku services map',
98
- [visitState.functions.files.size === 0],
98
+ [false],
99
99
  async () => {
100
100
  const { sessionServicesType, singletonServicesType } =
101
101
  await getPikkuFilesAndMethods(
@@ -9,7 +9,11 @@ import { PikkuCommand } from '../../types.js'
9
9
 
10
10
  export const pikkuFunctionTypes: PikkuCommand = async (
11
11
  logger,
12
- { typesDeclarationFile: typesFile, packageMappings, rpcMapDeclarationFile },
12
+ {
13
+ typesDeclarationFile: typesFile,
14
+ packageMappings,
15
+ rpcInternalMapDeclarationFile,
16
+ },
13
17
  visitState,
14
18
  options = {}
15
19
  ) => {
@@ -39,7 +43,7 @@ export const pikkuFunctionTypes: PikkuCommand = async (
39
43
  `import type { ${singletonServicesType.type} } from '${getFileImportRelativePath(typesFile, singletonServicesType.typePath, packageMappings)}'`,
40
44
  singletonServicesType.type,
41
45
  `import type { ${sessionServicesType.type} } from '${getFileImportRelativePath(typesFile, sessionServicesType.typePath, packageMappings)}'`,
42
- `import type { TypedPikkuRPC } from '${getFileImportRelativePath(typesFile, rpcMapDeclarationFile, packageMappings)}'`
46
+ `import type { TypedPikkuRPC } from '${getFileImportRelativePath(typesFile, rpcInternalMapDeclarationFile, packageMappings)}'`
43
47
  )
44
48
  await writeFileInDir(logger, typesFile, content)
45
49
  }
@@ -0,0 +1,97 @@
1
+ import { getFileImportRelativePath } from '../../utils.js'
2
+ import { FunctionsMeta, FunctionsRuntimeMeta } from '@pikku/core'
3
+
4
+ export const serializeFunctionImports = (
5
+ outputPath: string,
6
+ functionsMap: Map<string, { path: string; exportedName: string }>,
7
+ functionsMeta: FunctionsMeta,
8
+ packageMappings: Record<string, string> = {}
9
+ ) => {
10
+ const serializedImports: string[] = [
11
+ `/* Import and register functions used by RPCs */`,
12
+ `import { addFunction } from '@pikku/core'`,
13
+ ]
14
+
15
+ const serializedRegistrations: string[] = []
16
+
17
+ // Sort by function name for consistent output
18
+ const sortedEntries = Array.from(functionsMap.entries()).sort((a, b) =>
19
+ a[0].localeCompare(b[0])
20
+ )
21
+
22
+ for (const [name, { path, exportedName }] of sortedEntries) {
23
+ const filePath = getFileImportRelativePath(
24
+ outputPath,
25
+ path,
26
+ packageMappings
27
+ )
28
+
29
+ // Find the function metadata to check if it's a direct function
30
+ // The functionsMeta is keyed by the function name (exported name)
31
+ const funcMeta = functionsMeta[name]
32
+ const isDirectFunction = funcMeta?.isDirectFunction ?? false
33
+
34
+ // For directly exported functions, we can just import and register them
35
+ if (name === exportedName) {
36
+ serializedImports.push(`import { ${exportedName} } from '${filePath}'`)
37
+
38
+ if (isDirectFunction) {
39
+ // Direct function: pikkuFunc(fn) - needs to be wrapped
40
+ serializedRegistrations.push(
41
+ `addFunction('${name}', { func: ${exportedName} })`
42
+ )
43
+ } else {
44
+ // Object format: pikkuFunc({ func: fn }) - can be used directly
45
+ serializedRegistrations.push(
46
+ `addFunction('${name}', ${exportedName} as any) // TODO`
47
+ )
48
+ }
49
+ }
50
+ // For renamed functions, we need to import and alias them
51
+ else {
52
+ serializedImports.push(
53
+ `import { ${exportedName} as ${name} } from '${filePath}'`
54
+ )
55
+
56
+ if (isDirectFunction) {
57
+ // Direct function: pikkuFunc(fn) - needs to be wrapped
58
+ serializedRegistrations.push(
59
+ `addFunction('${name}', { func: ${name} })`
60
+ )
61
+ } else {
62
+ // Object format: pikkuFunc({ func: fn }) - can be used directly
63
+ serializedRegistrations.push(
64
+ `addFunction('${name}', ${name} as any) // TODO`
65
+ )
66
+ }
67
+ }
68
+ }
69
+
70
+ // Add a blank line between imports and registrations
71
+ if (serializedImports.length > 0 && serializedRegistrations.length > 0) {
72
+ serializedImports.push('')
73
+ }
74
+
75
+ // Combine the imports and registrations
76
+ return [...serializedImports, ...serializedRegistrations].join('\n')
77
+ }
78
+
79
+ export const generateRuntimeMeta = (
80
+ functions: FunctionsMeta
81
+ ): FunctionsRuntimeMeta => {
82
+ const runtimeMeta: FunctionsRuntimeMeta = {}
83
+
84
+ for (const [
85
+ key,
86
+ { pikkuFuncName, inputSchemaName, outputSchemaName, expose },
87
+ ] of Object.entries(functions)) {
88
+ runtimeMeta[key] = {
89
+ pikkuFuncName,
90
+ inputSchemaName,
91
+ outputSchemaName,
92
+ expose,
93
+ }
94
+ }
95
+
96
+ return runtimeMeta
97
+ }
@@ -8,12 +8,17 @@ import { PikkuCommandWithoutState } from '../../types.js'
8
8
 
9
9
  export const pikkuRPCClient: PikkuCommandWithoutState = async (
10
10
  logger,
11
- { rpcWiringsFile, rpcMapDeclarationFile, packageMappings }
11
+ {
12
+ rpcWiringsFile,
13
+ rpcMapDeclarationFile,
14
+ rpcInternalMapDeclarationFile,
15
+ packageMappings,
16
+ }
12
17
  ) => {
13
18
  return await logCommandInfoAndTime(
14
19
  logger,
15
- 'Generating RPC wrapper',
16
- 'Generated RPC wrapper',
20
+ 'Generating RPC wrappers',
21
+ 'Generated RPC wrappers',
17
22
  [
18
23
  rpcWiringsFile === undefined || rpcWiringsFile === null,
19
24
  "rpcWiringsFile isn't set in the pikku config",
@@ -28,7 +33,6 @@ export const pikkuRPCClient: PikkuCommandWithoutState = async (
28
33
  rpcMapDeclarationFile,
29
34
  packageMappings
30
35
  )
31
-
32
36
  const content = [serializeRPCWrapper(rpcMapDeclarationPath)]
33
37
  await writeFileInDir(logger, rpcWiringsFile, content.join('\n'))
34
38
  }
@@ -2,15 +2,38 @@ import { logCommandInfoAndTime, writeFileInDir } from '../../utils.js'
2
2
  import { serializeTypedRPCMap } from './serialize-typed-rpc-map.js'
3
3
  import { PikkuCommand } from '../../types.js'
4
4
 
5
- export const pikkuRPCMap: PikkuCommand = async (
5
+ export const pikkuRPCInternalMap: PikkuCommand = async (
6
+ logger,
7
+ { rpcInternalMapDeclarationFile, packageMappings },
8
+ { functions, rpc }
9
+ ) => {
10
+ return await logCommandInfoAndTime(
11
+ logger,
12
+ 'Creating RPC internal map',
13
+ 'Created RPC internal map',
14
+ [false],
15
+ async () => {
16
+ const content = serializeTypedRPCMap(
17
+ rpcInternalMapDeclarationFile,
18
+ packageMappings,
19
+ functions.typesMap,
20
+ functions.meta,
21
+ rpc.internalMeta
22
+ )
23
+ await writeFileInDir(logger, rpcInternalMapDeclarationFile, content)
24
+ }
25
+ )
26
+ }
27
+
28
+ export const pikkuRPCExposedMap: PikkuCommand = async (
6
29
  logger,
7
30
  { rpcMapDeclarationFile, packageMappings },
8
31
  { functions, rpc }
9
32
  ) => {
10
33
  return await logCommandInfoAndTime(
11
34
  logger,
12
- 'Creating RPC map',
13
- 'Created RPC map',
35
+ 'Creating RPC external map',
36
+ 'Created RPC external map',
14
37
  [false],
15
38
  async () => {
16
39
  const content = serializeTypedRPCMap(
@@ -18,7 +41,7 @@ export const pikkuRPCMap: PikkuCommand = async (
18
41
  packageMappings,
19
42
  functions.typesMap,
20
43
  functions.meta,
21
- rpc.meta
44
+ rpc.exposedMeta
22
45
  )
23
46
  await writeFileInDir(logger, rpcMapDeclarationFile, content)
24
47
  }
@@ -3,7 +3,7 @@ import { PikkuCommand } from '../../types.js'
3
3
 
4
4
  export const pikkuRPC: PikkuCommand = async (
5
5
  logger,
6
- { rpcWiringMetaFile },
6
+ { rpcInternalWiringMetaFile, rpcWiringMetaFile },
7
7
  { rpc }
8
8
  ) => {
9
9
  return await logCommandInfoAndTime(
@@ -12,11 +12,21 @@ export const pikkuRPC: PikkuCommand = async (
12
12
  'Found RPCs',
13
13
  [false],
14
14
  async () => {
15
- await writeFileInDir(
16
- logger,
17
- rpcWiringMetaFile,
18
- `import { pikkuState } from '@pikku/core'\npikkuState('rpc', 'meta', ${JSON.stringify(rpc.meta, null, 2)})`
19
- )
15
+ if (rpc.internalFiles.size > 0) {
16
+ await writeFileInDir(
17
+ logger,
18
+ rpcInternalWiringMetaFile,
19
+ `import { pikkuState } from '@pikku/core'\npikkuState('rpc', 'meta', ${JSON.stringify(rpc.internalMeta, null, 2)})`
20
+ )
21
+ }
22
+
23
+ if (rpc.exposedFiles.size > 0) {
24
+ await writeFileInDir(
25
+ logger,
26
+ rpcWiringMetaFile,
27
+ `import { pikkuState } from '@pikku/core'\npikkuState('rpc', 'meta', ${JSON.stringify(rpc.exposedFiles, null, 2)})`
28
+ )
29
+ }
20
30
  }
21
31
  )
22
32
  }
@@ -1,5 +1,3 @@
1
- import type { RPCMeta } from '@pikku/core/rpc'
2
-
3
1
  import { serializeImportMap } from '../../serialize-import-map.js'
4
2
  import { TypesMap } from '@pikku/inspector'
5
3
  import { FunctionsMeta } from '@pikku/core'
@@ -10,7 +8,7 @@ export const serializeTypedRPCMap = (
10
8
  packageMappings: Record<string, string>,
11
9
  typesMap: TypesMap,
12
10
  functionsMeta: FunctionsMeta,
13
- rpcMeta: Record<string, RPCMeta>
11
+ rpcMeta: Record<string, string>
14
12
  ) => {
15
13
  const requiredTypes = new Set<string>()
16
14
  const serializedCustomTypes = generateCustomTypes(typesMap, requiredTypes)
@@ -54,12 +52,13 @@ export type TypedPikkuRPC = {
54
52
  depth: number;
55
53
  global: boolean;
56
54
  invoke: RPCInvoke;
55
+ invokeExposed: (name: string, data: any) => Promise<any>
57
56
  }
58
57
  `
59
58
  }
60
59
 
61
60
  function generateRPCs(
62
- rpcMeta: Record<string, RPCMeta>,
61
+ rpcMeta: Record<string, string>,
63
62
  functionsMeta: FunctionsMeta,
64
63
  typesMap: TypesMap,
65
64
  requiredTypes: Set<string>
@@ -68,7 +67,7 @@ function generateRPCs(
68
67
  const rpcsObj: Record<string, { inputType: string; outputType: string }> = {}
69
68
 
70
69
  // Iterate through RPC metadata
71
- for (const [funcName, { pikkuFuncName }] of Object.entries(rpcMeta)) {
70
+ for (const [funcName, pikkuFuncName] of Object.entries(rpcMeta)) {
72
71
  const functionMeta = functionsMeta[pikkuFuncName]
73
72
  if (!functionMeta) {
74
73
  throw new Error(
@@ -1,6 +0,0 @@
1
- import { PikkuCommand } from '../../types.js';
2
- export declare const serializeFunctionImports: (outputPath: string, functionsMap: Map<string, {
3
- path: string;
4
- exportedName: string;
5
- }>, packageMappings?: Record<string, string>) => string;
6
- export declare const pikkuFunctions: PikkuCommand;
@@ -1,35 +0,0 @@
1
- import { getFileImportRelativePath, logCommandInfoAndTime, writeFileInDir, } from '../../utils.js';
2
- export const serializeFunctionImports = (outputPath, functionsMap, packageMappings = {}) => {
3
- const serializedImports = [
4
- `/* Import and register RPCs */`,
5
- `import { addFunction } from '@pikku/core'`,
6
- ];
7
- const serializedRegistrations = [];
8
- // Sort by function name for consistent output
9
- const sortedEntries = Array.from(functionsMap.entries()).sort((a, b) => a[0].localeCompare(b[0]));
10
- for (const [name, { path, exportedName }] of sortedEntries) {
11
- const filePath = getFileImportRelativePath(outputPath, path, packageMappings);
12
- // For directly exported functions, we can just import and register them
13
- if (name === exportedName) {
14
- serializedImports.push(`import { ${exportedName} } from '${filePath}'`);
15
- serializedRegistrations.push(`addFunction('${name}', { func: ${exportedName} })`);
16
- }
17
- // For renamed functions, we need to import and alias them
18
- else {
19
- serializedImports.push(`import { ${exportedName} as ${name} } from '${filePath}'`);
20
- serializedRegistrations.push(`addFunction('${name}', ${name})`);
21
- }
22
- }
23
- // Add a blank line between imports and registrations
24
- if (serializedImports.length > 0 && serializedRegistrations.length > 0) {
25
- serializedImports.push('');
26
- }
27
- // Combine the imports and registrations
28
- return [...serializedImports, ...serializedRegistrations].join('\n');
29
- };
30
- export const pikkuFunctions = async (logger, { functionsMetaFile, functionsFile, packageMappings }, { functions }) => {
31
- return await logCommandInfoAndTime(logger, 'Serializing Pikku functions', 'Serialized Pikku functions', [false], async () => {
32
- await writeFileInDir(logger, functionsFile, serializeFunctionImports(functionsFile, functions.files, packageMappings));
33
- await writeFileInDir(logger, functionsMetaFile, `import { pikkuState } from '@pikku/core'\npikkuState('function', 'meta', ${JSON.stringify(functions.meta, null, 2)})`);
34
- });
35
- };