@pikku/cli 0.9.2 → 0.9.4

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 (48) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/bin/pikku-all.ts +10 -3
  3. package/bin/pikku-rpc.ts +28 -0
  4. package/bin/pikku.ts +4 -0
  5. package/cli.schema.json +4 -0
  6. package/dist/bin/pikku-all.js +7 -3
  7. package/dist/bin/pikku-rpc.d.ts +4 -0
  8. package/dist/bin/pikku-rpc.js +19 -0
  9. package/dist/bin/pikku.js +4 -0
  10. package/dist/src/pikku-cli-config.d.ts +3 -0
  11. package/dist/src/pikku-cli-config.js +15 -4
  12. package/dist/src/serialize-import-map.js +62 -1
  13. package/dist/src/serialize-pikku-types.js +77 -5
  14. package/dist/src/utils.js +24 -2
  15. package/dist/src/wirings/functions/pikku-command-function-types.js +2 -2
  16. package/dist/src/wirings/functions/pikku-command-functions.d.ts +0 -4
  17. package/dist/src/wirings/functions/pikku-command-functions.js +10 -31
  18. package/dist/src/wirings/functions/pikku-command-services.js +1 -1
  19. package/dist/src/wirings/functions/pikku-function-types.js +2 -2
  20. package/dist/src/wirings/functions/serialize-function-imports.d.ts +6 -0
  21. package/dist/src/wirings/functions/serialize-function-imports.js +59 -0
  22. package/dist/src/wirings/rpc/pikku-command-rpc-client.js +2 -2
  23. package/dist/src/wirings/rpc/pikku-command-rpc-map.d.ts +2 -1
  24. package/dist/src/wirings/rpc/pikku-command-rpc-map.js +9 -3
  25. package/dist/src/wirings/rpc/pikku-command-rpc.js +7 -2
  26. package/dist/src/wirings/rpc/serialize-typed-rpc-map.d.ts +1 -2
  27. package/dist/src/wirings/rpc/serialize-typed-rpc-map.js +2 -1
  28. package/dist/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +3 -3
  30. package/src/pikku-cli-config.ts +35 -5
  31. package/src/serialize-import-map.ts +67 -2
  32. package/src/serialize-pikku-types.ts +77 -5
  33. package/src/utils.ts +31 -2
  34. package/src/wirings/functions/pikku-command-function-types.ts +6 -2
  35. package/src/wirings/functions/pikku-command-functions.ts +28 -64
  36. package/src/wirings/functions/pikku-command-services.ts +1 -1
  37. package/src/wirings/functions/pikku-function-types.ts +6 -2
  38. package/src/wirings/functions/serialize-function-imports.ts +97 -0
  39. package/src/wirings/rpc/pikku-command-rpc-client.ts +8 -4
  40. package/src/wirings/rpc/pikku-command-rpc-map.ts +27 -4
  41. package/src/wirings/rpc/pikku-command-rpc.ts +16 -6
  42. package/src/wirings/rpc/serialize-typed-rpc-map.ts +4 -5
  43. package/dist/src/wirings/functions/pikku-functions.d.ts +0 -6
  44. package/dist/src/wirings/functions/pikku-functions.js +0 -35
  45. package/dist/src/wirings/rpc/pikku-rpc.d.ts +0 -2
  46. package/dist/src/wirings/rpc/pikku-rpc.js +0 -6
  47. package/src/wirings/functions/pikku-functions.ts +0 -84
  48. package/src/wirings/rpc/pikku-rpc.ts +0 -22
@@ -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
- };
@@ -1,2 +0,0 @@
1
- import { PikkuCommand } from '../../types.js';
2
- export declare const pikkuRPC: PikkuCommand;
@@ -1,6 +0,0 @@
1
- import { logCommandInfoAndTime, writeFileInDir } from '../../utils.js';
2
- export const pikkuRPC = async (logger, { rpcWiringMetaFile }, { rpc }) => {
3
- return await logCommandInfoAndTime(logger, 'Finding RPCs tasks', 'Found RPCs', [false], async () => {
4
- await writeFileInDir(logger, rpcWiringMetaFile, `import { pikkuState } from '@pikku/core'\npikkuState('rpc', 'meta', ${JSON.stringify(rpc.meta, null, 2)})`);
5
- });
6
- };
@@ -1,84 +0,0 @@
1
- import {
2
- getFileImportRelativePath,
3
- logCommandInfoAndTime,
4
- writeFileInDir,
5
- } from '../../utils.js'
6
- 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
- }
56
-
57
- export const pikkuFunctions: PikkuCommand = async (
58
- logger,
59
- { functionsMetaFile, functionsFile, packageMappings },
60
- { functions }
61
- ) => {
62
- return await logCommandInfoAndTime(
63
- logger,
64
- 'Serializing Pikku functions',
65
- 'Serialized Pikku functions',
66
- [false],
67
- async () => {
68
- await writeFileInDir(
69
- logger,
70
- functionsFile,
71
- serializeFunctionImports(
72
- functionsFile,
73
- functions.files,
74
- packageMappings
75
- )
76
- )
77
- await writeFileInDir(
78
- logger,
79
- functionsMetaFile,
80
- `import { pikkuState } from '@pikku/core'\npikkuState('function', 'meta', ${JSON.stringify(functions.meta, null, 2)})`
81
- )
82
- }
83
- )
84
- }
@@ -1,22 +0,0 @@
1
- import { logCommandInfoAndTime, writeFileInDir } from '../../utils.js'
2
- import { PikkuCommand } from '../../types.js'
3
-
4
- export const pikkuRPC: PikkuCommand = async (
5
- logger,
6
- { rpcWiringMetaFile },
7
- { rpc }
8
- ) => {
9
- return await logCommandInfoAndTime(
10
- logger,
11
- 'Finding RPCs tasks',
12
- 'Found RPCs',
13
- [false],
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
- )
20
- }
21
- )
22
- }