sandlot 0.1.3 → 0.2.0

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 (115) hide show
  1. package/dist/browser/bundler.d.ts +68 -0
  2. package/dist/browser/bundler.d.ts.map +1 -0
  3. package/dist/browser/executor.d.ts +46 -0
  4. package/dist/browser/executor.d.ts.map +1 -0
  5. package/dist/browser/index.d.ts +9 -0
  6. package/dist/browser/index.d.ts.map +1 -0
  7. package/dist/browser/index.js +2692 -0
  8. package/dist/browser/preset.d.ts +63 -0
  9. package/dist/browser/preset.d.ts.map +1 -0
  10. package/dist/commands/index.d.ts +20 -11
  11. package/dist/commands/index.d.ts.map +1 -1
  12. package/dist/commands/types.d.ts +31 -132
  13. package/dist/commands/types.d.ts.map +1 -1
  14. package/dist/core/bundler-utils.d.ts +142 -0
  15. package/dist/core/bundler-utils.d.ts.map +1 -0
  16. package/dist/core/esm-types-resolver.d.ts +125 -0
  17. package/dist/core/esm-types-resolver.d.ts.map +1 -0
  18. package/dist/core/executor.d.ts +35 -0
  19. package/dist/core/executor.d.ts.map +1 -0
  20. package/dist/{fs.d.ts → core/fs.d.ts} +27 -29
  21. package/dist/core/fs.d.ts.map +1 -0
  22. package/dist/core/sandbox.d.ts +30 -0
  23. package/dist/core/sandbox.d.ts.map +1 -0
  24. package/dist/core/sandlot.d.ts +30 -0
  25. package/dist/core/sandlot.d.ts.map +1 -0
  26. package/dist/core/shared-module-registry.d.ts +46 -0
  27. package/dist/core/shared-module-registry.d.ts.map +1 -0
  28. package/dist/core/typechecker.d.ts +60 -0
  29. package/dist/core/typechecker.d.ts.map +1 -0
  30. package/dist/index.d.ts +11 -16
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1405 -2049
  33. package/dist/node/bundler.d.ts +48 -0
  34. package/dist/node/bundler.d.ts.map +1 -0
  35. package/dist/node/executor.d.ts +48 -0
  36. package/dist/node/executor.d.ts.map +1 -0
  37. package/dist/node/index.d.ts +9 -0
  38. package/dist/node/index.d.ts.map +1 -0
  39. package/dist/node/index.js +2646 -0
  40. package/dist/node/preset.d.ts +62 -0
  41. package/dist/node/preset.d.ts.map +1 -0
  42. package/dist/types.d.ts +525 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/package.json +27 -8
  45. package/src/browser/bundler.ts +294 -0
  46. package/src/browser/executor.ts +71 -0
  47. package/src/browser/index.ts +57 -0
  48. package/src/browser/preset.ts +179 -0
  49. package/src/commands/index.ts +526 -43
  50. package/src/commands/types.ts +82 -146
  51. package/src/core/bundler-utils.ts +630 -0
  52. package/src/core/esm-types-resolver.ts +432 -0
  53. package/src/core/executor.ts +161 -0
  54. package/src/{fs.ts → core/fs.ts} +59 -37
  55. package/src/core/sandbox.ts +621 -0
  56. package/src/core/sandlot.ts +77 -0
  57. package/src/core/shared-module-registry.ts +138 -0
  58. package/src/core/typechecker.ts +607 -0
  59. package/src/index.ts +104 -139
  60. package/src/node/bundler.ts +194 -0
  61. package/src/node/executor.ts +87 -0
  62. package/src/node/index.ts +39 -0
  63. package/src/node/preset.ts +178 -0
  64. package/src/types.ts +668 -0
  65. package/README.md +0 -243
  66. package/dist/build-emitter.d.ts +0 -47
  67. package/dist/build-emitter.d.ts.map +0 -1
  68. package/dist/builder.d.ts +0 -370
  69. package/dist/builder.d.ts.map +0 -1
  70. package/dist/bundler.d.ts +0 -148
  71. package/dist/bundler.d.ts.map +0 -1
  72. package/dist/commands/compile.d.ts +0 -13
  73. package/dist/commands/compile.d.ts.map +0 -1
  74. package/dist/commands/packages.d.ts +0 -17
  75. package/dist/commands/packages.d.ts.map +0 -1
  76. package/dist/commands/run.d.ts +0 -40
  77. package/dist/commands/run.d.ts.map +0 -1
  78. package/dist/commands.d.ts +0 -179
  79. package/dist/commands.d.ts.map +0 -1
  80. package/dist/fs.d.ts.map +0 -1
  81. package/dist/internal.d.ts +0 -79
  82. package/dist/internal.d.ts.map +0 -1
  83. package/dist/internal.js +0 -1976
  84. package/dist/loader.d.ts +0 -164
  85. package/dist/loader.d.ts.map +0 -1
  86. package/dist/packages.d.ts +0 -199
  87. package/dist/packages.d.ts.map +0 -1
  88. package/dist/runner.d.ts +0 -314
  89. package/dist/runner.d.ts.map +0 -1
  90. package/dist/sandbox-manager.d.ts +0 -261
  91. package/dist/sandbox-manager.d.ts.map +0 -1
  92. package/dist/sandbox.d.ts +0 -267
  93. package/dist/sandbox.d.ts.map +0 -1
  94. package/dist/shared-modules.d.ts +0 -148
  95. package/dist/shared-modules.d.ts.map +0 -1
  96. package/dist/shared-resources.d.ts +0 -102
  97. package/dist/shared-resources.d.ts.map +0 -1
  98. package/dist/ts-libs.d.ts +0 -98
  99. package/dist/ts-libs.d.ts.map +0 -1
  100. package/dist/typechecker.d.ts +0 -127
  101. package/dist/typechecker.d.ts.map +0 -1
  102. package/src/build-emitter.ts +0 -64
  103. package/src/builder.ts +0 -498
  104. package/src/bundler.ts +0 -542
  105. package/src/commands/compile.ts +0 -236
  106. package/src/commands/packages.ts +0 -154
  107. package/src/commands/run.ts +0 -245
  108. package/src/internal.ts +0 -119
  109. package/src/loader.ts +0 -229
  110. package/src/packages.ts +0 -936
  111. package/src/sandbox.ts +0 -396
  112. package/src/shared-modules.ts +0 -280
  113. package/src/shared-resources.ts +0 -166
  114. package/src/ts-libs.ts +0 -320
  115. package/src/typechecker.ts +0 -635
@@ -1,172 +1,108 @@
1
1
  /**
2
- * Types and utilities for sandbox bash commands.
2
+ * Types and utilities for v2 sandbox bash commands.
3
+ *
4
+ * Commands wrap the sandbox's direct methods to provide a shell interface.
5
+ * This ensures consistency between `sandbox.build()` and `sandbox.exec('sandlot build')`.
3
6
  */
4
7
 
5
- import type { IFileSystem } from "just-bash/browser";
6
- import type { BundleResult } from "../bundler";
7
- import type { TypesCache } from "../packages";
8
+ import type {
9
+ Filesystem,
10
+ InstallResult,
11
+ UninstallResult,
12
+ BuildResult,
13
+ TypecheckResult,
14
+ SandboxBuildOptions,
15
+ SandboxTypecheckOptions,
16
+ RunOptions,
17
+ RunResult,
18
+ BundleError,
19
+ Diagnostic,
20
+ } from "../types";
8
21
 
9
22
  /**
10
- * The result of a successful build, including the bundle and loaded module.
23
+ * Reference to sandbox methods that commands can call.
24
+ *
25
+ * Commands receive this interface rather than raw dependencies,
26
+ * ensuring they use the same logic as direct API calls.
11
27
  */
12
- export interface BuildOutput {
13
- /**
14
- * The compiled bundle (code, metadata, etc.)
15
- */
16
- bundle: BundleResult;
28
+ export interface SandboxRef {
29
+ /** The virtual filesystem */
30
+ readonly fs: Filesystem;
17
31
 
18
- /**
19
- * The loaded module exports.
20
- * If validation was provided, this is the validated module.
21
- */
22
- module: Record<string, unknown>;
23
- }
24
-
25
- /**
26
- * Validation function type for module validation.
27
- * Takes the raw module exports and returns validated exports (or throws).
28
- */
29
- export type ValidateFn = (module: Record<string, unknown>) => Record<string, unknown>;
30
-
31
- /**
32
- * Dependencies required by command factories
33
- */
34
- export interface CommandDeps {
35
- /**
36
- * The virtual filesystem to operate on
37
- */
38
- fs: IFileSystem;
39
-
40
- /**
41
- * Pre-loaded TypeScript lib files for type checking
42
- */
43
- libFiles: Map<string, string>;
44
-
45
- /**
46
- * Path to tsconfig.json in the virtual filesystem
47
- */
48
- tsconfigPath: string;
49
-
50
- /**
51
- * Callback invoked when a build succeeds (after loading and validation).
52
- */
53
- onBuild?: (result: BuildOutput) => void | Promise<void>;
54
-
55
- /**
56
- * Getter for the current validation function.
57
- * Called during build to check if validation should be performed.
58
- */
59
- getValidation?: () => ValidateFn | null;
60
-
61
- /**
62
- * Cache for package type definitions.
63
- * When provided, avoids redundant network fetches for packages
64
- * that have already been installed in other sandboxes.
65
- */
66
- typesCache?: TypesCache;
67
-
68
- /**
69
- * Options for the `run` command
70
- */
71
- runOptions?: RunOptions;
72
-
73
- /**
74
- * Module IDs that should be resolved from the host's SharedModuleRegistry
75
- * instead of esm.sh CDN. The host must have registered these modules.
76
- *
77
- * Example: ['react', 'react-dom/client']
78
- */
79
- sharedModules?: string[];
80
- }
81
-
82
- /**
83
- * Runtime context passed to the `main()` function when code is executed.
84
- * This provides sandboxed code with access to sandbox capabilities.
85
- */
86
- export interface RunContext {
87
- /**
88
- * The virtual filesystem - read/write files within the sandbox
89
- */
90
- fs: IFileSystem;
32
+ /** Install a package */
33
+ install(packageSpec: string): Promise<InstallResult>;
91
34
 
92
- /**
93
- * Environment variables (configurable per-sandbox)
94
- */
95
- env: Record<string, string>;
35
+ /** Uninstall a package */
36
+ uninstall(packageName: string): Promise<UninstallResult>;
96
37
 
97
- /**
98
- * Command-line arguments passed to `run`
99
- */
100
- args: string[];
38
+ /** Build the project */
39
+ build(options?: SandboxBuildOptions): Promise<BuildResult>;
101
40
 
102
- /**
103
- * Explicit logging function (alternative to console.log)
104
- */
105
- log: (...args: unknown[]) => void;
41
+ /** Type check the project */
42
+ typecheck(options?: SandboxTypecheckOptions): Promise<TypecheckResult>;
106
43
 
107
- /**
108
- * Explicit error logging function (alternative to console.error)
109
- */
110
- error: (...args: unknown[]) => void;
44
+ /** Run code (build + execute) */
45
+ run(options?: RunOptions): Promise<RunResult>;
111
46
  }
112
47
 
113
48
  /**
114
- * Options for configuring the `run` command behavior
49
+ * Format a file size in bytes to a human-readable string
115
50
  */
116
- export interface RunOptions {
117
- /**
118
- * Environment variables available via ctx.env
119
- */
120
- env?: Record<string, string>;
121
-
122
- /**
123
- * Maximum execution time in milliseconds (default: 30000 = 30s)
124
- * Set to 0 to disable timeout.
125
- */
126
- timeout?: number;
127
-
128
- /**
129
- * Whether to skip type checking before running (default: false)
130
- */
131
- skipTypecheck?: boolean;
51
+ export function formatSize(bytes: number): string {
52
+ if (bytes < 1024) return `${bytes} B`;
53
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
54
+ return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
132
55
  }
133
56
 
134
57
  /**
135
- * Result of running code via the `run` command
58
+ * Format diagnostics for shell output
136
59
  */
137
- export interface RunResult {
138
- /**
139
- * Captured console output (log, warn, error)
140
- */
141
- logs: string[];
142
-
143
- /**
144
- * Return value from main() if present
145
- */
146
- returnValue?: unknown;
147
-
148
- /**
149
- * Execution time in milliseconds
150
- */
151
- executionTimeMs: number;
60
+ export function formatDiagnostics(diagnostics: Diagnostic[]): string {
61
+ if (diagnostics.length === 0) return "";
62
+
63
+ return diagnostics
64
+ .map((d) => {
65
+ const severity = d.severity.toUpperCase();
66
+ if (d.file) {
67
+ const loc = `${d.file}${d.line ? `:${d.line}` : ""}${d.column ? `:${d.column}` : ""}`;
68
+ return `${severity}: ${loc}: ${d.message}`;
69
+ }
70
+ return `${severity}: ${d.message}`;
71
+ })
72
+ .join("\n");
152
73
  }
153
74
 
154
75
  /**
155
- * Format esbuild messages (warnings/errors) for display
76
+ * Format bundle errors for shell output
156
77
  */
157
- export function formatEsbuildMessages(
158
- messages: { text: string; location?: { file?: string; line?: number; column?: number } | null }[]
159
- ): string {
160
- if (messages.length === 0) return "";
161
-
162
- return messages
163
- .map((msg) => {
164
- if (msg.location) {
165
- const { file, line, column } = msg.location;
166
- const loc = file ? `${file}${line ? `:${line}` : ""}${column ? `:${column}` : ""}` : "";
167
- return loc ? `${loc}: ${msg.text}` : msg.text;
78
+ export function formatBundleErrors(errors: BundleError[]): string {
79
+ if (errors.length === 0) return "";
80
+
81
+ return errors
82
+ .map((e) => {
83
+ let output = "";
84
+
85
+ // Location header
86
+ if (e.location) {
87
+ const loc = `${e.location.file}:${e.location.line}${e.location.column ? `:${e.location.column}` : ""}`;
88
+ output += `ERROR: ${loc}: ${e.text}`;
89
+
90
+ // Show source line if available
91
+ if (e.location.lineText) {
92
+ output += `\n ${e.location.line} | ${e.location.lineText}`;
93
+ // Add caret pointing to column
94
+ if (e.location.column) {
95
+ const padding = " ".repeat(
96
+ String(e.location.line).length + 3 + e.location.column - 1
97
+ );
98
+ output += `\n${padding}^`;
99
+ }
100
+ }
101
+ } else {
102
+ output += `ERROR: ${e.text}`;
168
103
  }
169
- return msg.text;
104
+
105
+ return output;
170
106
  })
171
- .join("\n");
107
+ .join("\n\n");
172
108
  }