@greenmind/cli 0.0.0-stage → 0.9.40

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.
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@greenmind/cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.9.40",
4
+ "description": "GreenMind's command line, gm: sign in once, manage environments and keys, and check, push, upload and run extensions",
5
+ "license": "UNLICENSED",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/GreenMind-Software/gm-core.git",
9
+ "directory": "packages/ext-cli"
10
+ },
11
+ "bin": {
12
+ "greenmind": "./src/bin.ts",
13
+ "gm": "./src/bin.ts"
14
+ },
15
+ "main": "./src/index.ts",
16
+ "types": "./src/index.ts",
17
+ "files": [
18
+ "src"
19
+ ],
20
+ "engines": {
21
+ "bun": ">=1.4.2"
22
+ },
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "provenance": false
26
+ },
27
+ "dependencies": {
28
+ "@commander-js/extra-typings": "~15.0.0",
29
+ "@greenmind/ext-function-host": "0.9.40",
30
+ "@greenmind/ext-schema": "0.9.40",
31
+ "@greenmind/ext-sdk": "0.9.40",
32
+ "commander": "~15.0.0"
33
+ },
34
+ "peerDependencies": {
35
+ "@types/bun": ">=1.4.2"
36
+ },
37
+ "peerDependenciesMeta": {
38
+ "@types/bun": {
39
+ "optional": true
40
+ }
41
+ }
42
+ }
package/src/bin.ts ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bun
2
+ import { gmCommand } from './command'
3
+
4
+ /**
5
+ * `gm` on its own, without the rest of this repository's command line: what a studio runs, and
6
+ * what our games' `push` scripts run, so a deploy needs no database address in its environment.
7
+ * The published package installs this file as both `greenmind` and `gm`.
8
+ */
9
+ gmCommand.parseAsync(process.argv).then(
10
+ () => process.exit(process.exitCode ?? 0),
11
+ (error) => {
12
+ console.error(error instanceof Error ? error.message : error)
13
+ process.exit(1)
14
+ },
15
+ )
package/src/bundle.ts ADDED
@@ -0,0 +1,100 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
3
+ import { tmpdir } from 'node:os'
4
+ import { extname, join } from 'node:path'
5
+ import { type CompiledDefinition, compileProject, type DefinitionBase } from '@greenmind/ext-sdk'
6
+ import type { BunPlugin } from 'bun'
7
+ import { filesUnder, LoadError, loadModule } from './load'
8
+ import { FUNCTION_BUNDLE_MAX_BYTES } from './protocol'
9
+
10
+ /**
11
+ * Building a function bundle: one ESM file, `index.mjs`, for Node 24, holding every function the
12
+ * given modules define (exported or used by an exported flow), the handler module over them as
13
+ * `handle`, and the Lambda adapter over that same handle as `handler`. Its content hash is the
14
+ * SHA-256 of its bytes, which is what a version names and a release moves.
15
+ */
16
+
17
+ const CODE = new Set(['.ts', '.mts', '.js', '.mjs'])
18
+
19
+ /** The runtime every bundle is built for. */
20
+ export const RUNTIME = 'nodejs24.x'
21
+
22
+ export interface Bundle {
23
+ readonly bytes: Uint8Array
24
+ readonly contentHash: string
25
+ /** The functions it holds, compiled. */
26
+ readonly definitions: readonly CompiledDefinition[]
27
+ }
28
+
29
+ export function sha256Hex(bytes: Uint8Array): string {
30
+ return createHash('sha256').update(bytes).digest('hex')
31
+ }
32
+
33
+ function hostEntry(): string {
34
+ return require.resolve('@greenmind/ext-function-host')
35
+ }
36
+
37
+ /**
38
+ * `plugins` are Bun build plugins for code that needs one to compile (a macro, say): a project's own
39
+ * build step passes them; the bundle is the same for the same plugins and sources.
40
+ */
41
+ export async function buildBundle(
42
+ paths: readonly string[],
43
+ plugins: readonly BunPlugin[] = [],
44
+ ): Promise<Bundle> {
45
+ const files = paths.flatMap(filesUnder).filter((file) => CODE.has(extname(file)))
46
+ if (files.length === 0) {
47
+ throw new LoadError('A bundle is built from .ts or .js modules; none was given.')
48
+ }
49
+ const loaded: DefinitionBase[] = []
50
+ for (const file of files) loaded.push(...(await loadModule(file)))
51
+ const definitions = compileProject(loaded).filter((each) => each.kind === 'function')
52
+ if (definitions.length === 0) {
53
+ throw new LoadError('The modules define no function, so there is nothing to bundle.')
54
+ }
55
+
56
+ // The builder writes the entry's path into the output, so the same modules always build in the
57
+ // same place: otherwise one build's bytes, and so its hash, would differ from the next one's.
58
+ const dir = join(
59
+ tmpdir(),
60
+ `ext-bundle-${sha256Hex(new TextEncoder().encode(files.join('\n'))).slice(0, 16)}`,
61
+ )
62
+ mkdirSync(dir, { recursive: true })
63
+ try {
64
+ const entry = join(dir, 'index.ts')
65
+ const imports = files.map((file, i) => `import * as m${i} from ${JSON.stringify(file)}`)
66
+ writeFileSync(
67
+ entry,
68
+ [
69
+ `import { createHandler, functionsOf, lambdaHandler } from ${JSON.stringify(hostEntry())}`,
70
+ ...imports,
71
+ `export const handle = createHandler(functionsOf(${files.map((_, i) => `m${i}`).join(', ')}))`,
72
+ 'export const handler = lambdaHandler(handle)',
73
+ '',
74
+ ].join('\n'),
75
+ )
76
+ const built = await Bun.build({
77
+ entrypoints: [entry],
78
+ target: 'node',
79
+ format: 'esm',
80
+ splitting: false,
81
+ sourcemap: 'none',
82
+ plugins: [...plugins],
83
+ })
84
+ if (!built.success || built.outputs.length !== 1) {
85
+ const why = built.logs.map((log) => String(log)).join('; ')
86
+ throw new LoadError(`The bundle did not build: ${why || 'no single output'}.`)
87
+ }
88
+ const [output] = built.outputs
89
+ if (!output) throw new LoadError('The bundle did not build: no output.')
90
+ const bytes = new Uint8Array(await output.arrayBuffer())
91
+ if (bytes.length > FUNCTION_BUNDLE_MAX_BYTES) {
92
+ throw new LoadError(
93
+ `The bundle is ${bytes.length} bytes; the most an upload takes is ${FUNCTION_BUNDLE_MAX_BYTES}.`,
94
+ )
95
+ }
96
+ return { bytes, contentHash: sha256Hex(bytes), definitions }
97
+ } finally {
98
+ rmSync(dir, { recursive: true, force: true })
99
+ }
100
+ }