sandlot 0.1.4 → 0.2.1

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 +2690 -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 +37 -130
  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 +1398 -2010
  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 +2644 -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 +528 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/package.json +16 -6
  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 +498 -37
  50. package/src/commands/types.ts +117 -145
  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 +624 -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 +609 -0
  59. package/src/index.ts +106 -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 +672 -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 -152
  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 -1942
  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 -85
  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 -575
  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 -398
  112. package/src/shared-modules.ts +0 -280
  113. package/src/shared-resources.ts +0 -166
  114. package/src/ts-libs.ts +0 -218
  115. package/src/typechecker.ts +0 -635
@@ -1,172 +1,144 @@
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
+ BuildFailureDetails,
14
+ TypecheckResult,
15
+ SandboxBuildOptions,
16
+ SandboxTypecheckOptions,
17
+ RunOptions,
18
+ RunResult,
19
+ BundleError,
20
+ Diagnostic,
21
+ } from "../types";
8
22
 
9
23
  /**
10
- * The result of a successful build, including the bundle and loaded module.
24
+ * Reference to sandbox methods that commands can call.
25
+ *
26
+ * Commands receive this interface rather than raw dependencies,
27
+ * ensuring they use the same logic as direct API calls.
11
28
  */
12
- export interface BuildOutput {
13
- /**
14
- * The compiled bundle (code, metadata, etc.)
15
- */
16
- bundle: BundleResult;
17
-
18
- /**
19
- * The loaded module exports.
20
- * If validation was provided, this is the validated module.
21
- */
22
- module: Record<string, unknown>;
23
- }
29
+ export interface SandboxRef {
30
+ /** The virtual filesystem */
31
+ readonly fs: Filesystem;
24
32
 
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>;
33
+ /** Install a package */
34
+ install(packageSpec: string): Promise<InstallResult>;
30
35
 
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[];
36
+ /** Uninstall a package */
37
+ uninstall(packageName: string): Promise<UninstallResult>;
38
+
39
+ /** Build the project */
40
+ build(options?: SandboxBuildOptions): Promise<BuildResult>;
41
+
42
+ /** Type check the project */
43
+ typecheck(options?: SandboxTypecheckOptions): Promise<TypecheckResult>;
44
+
45
+ /** Run code (build + execute) */
46
+ run(options?: RunOptions): Promise<RunResult>;
80
47
  }
81
48
 
82
49
  /**
83
- * Runtime context passed to the `main()` function when code is executed.
84
- * This provides sandboxed code with access to sandbox capabilities.
50
+ * Format a file size in bytes to a human-readable string
85
51
  */
86
- export interface RunContext {
87
- /**
88
- * The virtual filesystem - read/write files within the sandbox
89
- */
90
- fs: IFileSystem;
91
-
92
- /**
93
- * Environment variables (configurable per-sandbox)
94
- */
95
- env: Record<string, string>;
96
-
97
- /**
98
- * Command-line arguments passed to `run`
99
- */
100
- args: string[];
101
-
102
- /**
103
- * Explicit logging function (alternative to console.log)
104
- */
105
- log: (...args: unknown[]) => void;
106
-
107
- /**
108
- * Explicit error logging function (alternative to console.error)
109
- */
110
- error: (...args: unknown[]) => void;
52
+ export function formatSize(bytes: number): string {
53
+ if (bytes < 1024) return `${bytes} B`;
54
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)} KB`;
55
+ return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
111
56
  }
112
57
 
113
58
  /**
114
- * Options for configuring the `run` command behavior
59
+ * Format diagnostics for shell output
115
60
  */
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;
61
+ export function formatDiagnostics(diagnostics: Diagnostic[]): string {
62
+ if (diagnostics.length === 0) return "";
63
+
64
+ return diagnostics
65
+ .map((d) => {
66
+ const severity = d.severity.toUpperCase();
67
+ if (d.file) {
68
+ const loc = `${d.file}${d.line ? `:${d.line}` : ""}${d.column ? `:${d.column}` : ""}`;
69
+ return `${severity}: ${loc}: ${d.message}`;
70
+ }
71
+ return `${severity}: ${d.message}`;
72
+ })
73
+ .join("\n");
132
74
  }
133
75
 
134
76
  /**
135
- * Result of running code via the `run` command
77
+ * Format bundle errors for shell output
136
78
  */
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;
79
+ export function formatBundleErrors(errors: BundleError[]): string {
80
+ if (errors.length === 0) return "";
81
+
82
+ return errors
83
+ .map((e) => {
84
+ let output = "";
85
+
86
+ // Location header
87
+ if (e.location) {
88
+ const loc = `${e.location.file}:${e.location.line}${e.location.column ? `:${e.location.column}` : ""}`;
89
+ output += `ERROR: ${loc}: ${e.text}`;
90
+
91
+ // Show source line if available
92
+ if (e.location.lineText) {
93
+ output += `\n ${e.location.line} | ${e.location.lineText}`;
94
+ // Add caret pointing to column
95
+ if (e.location.column) {
96
+ const padding = " ".repeat(
97
+ String(e.location.line).length + 3 + e.location.column - 1
98
+ );
99
+ output += `\n${padding}^`;
100
+ }
101
+ }
102
+ } else {
103
+ output += `ERROR: ${e.text}`;
104
+ }
105
+
106
+ return output;
107
+ })
108
+ .join("\n\n");
152
109
  }
153
110
 
154
111
  /**
155
- * Format esbuild messages (warnings/errors) for display
112
+ * Format a build failure for shell output.
113
+ * Used by both build and run commands for consistent error formatting.
114
+ *
115
+ * @param failure - The build failure details
116
+ * @param prefix - Optional prefix for the error message (default: "Build failed")
156
117
  */
157
- export function formatEsbuildMessages(
158
- messages: { text: string; location?: { file?: string; line?: number; column?: number } | null }[]
118
+ export function formatBuildFailure(
119
+ failure: BuildFailureDetails,
120
+ prefix = "Build failed"
159
121
  ): 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;
122
+ switch (failure.phase) {
123
+ case "entry":
124
+ return `${prefix}: ${failure.message}\n`;
125
+
126
+ case "typecheck":
127
+ if (failure.diagnostics && failure.diagnostics.length > 0) {
128
+ const errors = failure.diagnostics.filter((d) => d.severity === "error");
129
+ if (errors.length > 0) {
130
+ return `${prefix}: Type check errors\n\n${formatDiagnostics(errors)}\n`;
131
+ }
168
132
  }
169
- return msg.text;
170
- })
171
- .join("\n");
133
+ return `${prefix}: Type check errors\n`;
134
+
135
+ case "bundle":
136
+ if (failure.bundleErrors && failure.bundleErrors.length > 0) {
137
+ return `${prefix}: Bundle errors\n\n${formatBundleErrors(failure.bundleErrors)}\n`;
138
+ }
139
+ return `${prefix}: Bundle error\n`;
140
+
141
+ default:
142
+ return `${prefix}: Unknown error\n`;
143
+ }
172
144
  }