@innovatorved/code-lab 0.2.0 → 0.3.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.
- package/ARCHITECTURE.md +11 -6
- package/CHANGELOG.md +40 -0
- package/README.md +18 -8
- package/dist/code-lab.css +198 -0
- package/dist/code-lab.d.ts +27 -9
- package/dist/code-lab.esm.js +8516 -84
- package/dist/code-lab.js +8514 -82
- package/dist/extensions/cpp.d.ts +40 -6
- package/dist/extensions/cpp.esm.js +1027 -7
- package/dist/extensions/cpp.js +1027 -7
- package/dist/extensions/guided-lab.esm.js +40 -2
- package/dist/extensions/guided-lab.js +40 -2
- package/dist/extensions/python.d.ts +92 -0
- package/dist/extensions/python.esm.js +5476 -0
- package/dist/extensions/python.js +5495 -0
- package/dist/index.d.ts +7 -4
- package/dist/preview-sw.js +1 -1
- package/dist/runtime/index.d.ts +4 -0
- package/dist/runtime/test-harness.d.ts +24 -0
- package/dist/runtime.esm.js +36 -4
- package/dist/runtime.js +36 -4
- package/dist/schemas.d.ts +137 -24
- package/dist/schemas.esm.js +44 -2
- package/dist/schemas.js +44 -2
- package/dist/shell/coreutils/compilers-cli.d.ts +24 -0
- package/dist/shell/index.d.ts +4 -0
- package/dist/shell.esm.js +113 -1
- package/dist/shell.js +113 -1
- package/dist/terminal.esm.js +1 -1
- package/dist/terminal.js +1 -1
- package/dist/workbench/command-palette.d.ts +10 -0
- package/dist/workbench/diff-editor.d.ts +26 -0
- package/dist/workbench/drawer.d.ts +18 -0
- package/dist/workbench/editor.d.ts +11 -0
- package/dist/workbench/file-tree.d.ts +11 -0
- package/dist/workbench/guided-pane.d.ts +11 -0
- package/dist/workbench/icons.d.ts +5 -0
- package/dist/workbench/index.d.ts +39 -0
- package/dist/workbench/share.d.ts +21 -0
- package/dist/workbench/styles.d.ts +4 -0
- package/dist/workbench/toolbar.d.ts +17 -0
- package/dist/workbench.d.ts +6 -1
- package/package.json +8 -2
- package/src/code-lab.css +198 -0
- package/src/extensions/cpp.ts +966 -5
- package/src/extensions/python.ts +1293 -0
- package/src/index.ts +35 -3
- package/src/runtime/index.ts +5 -0
- package/src/runtime/test-harness.ts +57 -0
- package/src/runtime.ts +35 -3
- package/src/schemas.ts +47 -2
- package/src/shell/coreutils/compilers-cli.ts +103 -0
- package/src/shell/index.ts +5 -0
- package/src/shell.ts +112 -0
- package/src/workbench/command-palette.ts +140 -0
- package/src/workbench/diff-editor.ts +216 -0
- package/src/workbench/drawer.ts +144 -0
- package/src/workbench/editor.ts +49 -0
- package/src/workbench/file-tree.ts +51 -0
- package/src/workbench/guided-pane.ts +33 -0
- package/src/workbench/icons.ts +48 -0
- package/src/workbench/index.ts +38 -0
- package/src/workbench/share.ts +99 -0
- package/src/workbench/styles.ts +200 -0
- package/src/workbench/toolbar.ts +171 -0
- package/src/workbench.ts +744 -69
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @innovatorved/code-lab v0.
|
|
3
|
-
* Browser-native IDE, POSIX Shell, Web Worker Node.js/Bun Runtime, WASI C++20 Toolchain, and Git Engine.
|
|
2
|
+
* @innovatorved/code-lab v0.3.1
|
|
3
|
+
* Browser-native IDE, POSIX Shell, Web Worker Node.js/Bun Runtime, WASI C++20 Toolchain, Python 3.12 WebAssembly, and Git Engine.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
export * from './schemas';
|
|
7
7
|
export * from './logger';
|
|
8
|
-
export { SafeHtml, isSafeHtml, raw, html } from './html';
|
|
8
|
+
export { type SafeHtml, isSafeHtml, raw, html } from './html';
|
|
9
9
|
export * from './csp';
|
|
10
10
|
export * from './zip';
|
|
11
11
|
export {
|
|
@@ -50,6 +50,13 @@ export {
|
|
|
50
50
|
CodeLabShell
|
|
51
51
|
} from './shell';
|
|
52
52
|
export * from './terminal';
|
|
53
|
+
export {
|
|
54
|
+
CodeLabWorkbench,
|
|
55
|
+
Workbench,
|
|
56
|
+
CodeLabElement,
|
|
57
|
+
CodeLab
|
|
58
|
+
} from './workbench';
|
|
59
|
+
export * from './workbench/index';
|
|
53
60
|
export { guidedLabExtension } from './extensions/guided-lab';
|
|
54
61
|
export {
|
|
55
62
|
type JavaScriptExtensionOptions,
|
|
@@ -65,22 +72,46 @@ export {
|
|
|
65
72
|
type CppCompileOptions,
|
|
66
73
|
type CppCompileResult,
|
|
67
74
|
type CppCompilerAdapter,
|
|
75
|
+
type CppSymbolDoc,
|
|
68
76
|
DEFAULT_CPP_CDN_BASE_URL,
|
|
69
77
|
BUILTIN_CPP_HEADERS,
|
|
78
|
+
STANDARD_CPP20_HEADERS,
|
|
79
|
+
CPP_COMPLETIONS_AND_HOVERS,
|
|
70
80
|
extractTarToVfs,
|
|
71
81
|
createTarArchive,
|
|
72
82
|
seedCppHeadersIntoVfs,
|
|
73
83
|
parseCppCompilerFlags,
|
|
74
84
|
CppToolchainManager,
|
|
75
85
|
registerCppCompilerOnRuntime,
|
|
86
|
+
registerMonacoCppLanguageService,
|
|
87
|
+
analyzeCppSource,
|
|
88
|
+
applyClangStderrDiagnostics,
|
|
76
89
|
cppExtension,
|
|
77
90
|
createCppExtension
|
|
78
91
|
} from './extensions/cpp';
|
|
92
|
+
export {
|
|
93
|
+
type ExtendedPythonExtensionOptions,
|
|
94
|
+
type PythonCompileResult,
|
|
95
|
+
type PythonSymbolDoc,
|
|
96
|
+
DEFAULT_PYTHON_CDN_BASE_URL,
|
|
97
|
+
BUILTIN_PYTHON_MODULES,
|
|
98
|
+
PYTHON_COMPLETIONS_AND_HOVERS,
|
|
99
|
+
PythonToolchainManager,
|
|
100
|
+
seedPythonModulesIntoVfs,
|
|
101
|
+
registerPythonCompilerOnRuntime,
|
|
102
|
+
registerMonacoPythonLanguageService,
|
|
103
|
+
analyzePythonSource,
|
|
104
|
+
applyPythonTracebackDiagnostics,
|
|
105
|
+
formatPythonSource,
|
|
106
|
+
pythonExtension,
|
|
107
|
+
createPythonExtension
|
|
108
|
+
} from './extensions/python';
|
|
79
109
|
|
|
80
110
|
import { guidedLabExtension } from './extensions/guided-lab';
|
|
81
111
|
import { javascriptExtension } from './extensions/javascript';
|
|
82
112
|
import { typescriptExtension } from './extensions/typescript';
|
|
83
113
|
import { cppExtension } from './extensions/cpp';
|
|
114
|
+
import { pythonExtension } from './extensions/python';
|
|
84
115
|
|
|
85
116
|
declare const globalThis: any;
|
|
86
117
|
const root = typeof window !== 'undefined' ? window : (typeof globalThis !== 'undefined' ? globalThis : {});
|
|
@@ -92,6 +123,7 @@ if (root.CodeLab) {
|
|
|
92
123
|
root.CodeLab.extensions.javascript = javascriptExtension;
|
|
93
124
|
root.CodeLab.extensions.typescript = typescriptExtension;
|
|
94
125
|
root.CodeLab.extensions.cpp = cppExtension;
|
|
126
|
+
root.CodeLab.extensions.python = pythonExtension;
|
|
95
127
|
}
|
|
96
128
|
|
|
97
129
|
export default (root.CodeLab || {});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Structured Test Harness & __CODELAB_TEST__: Telemetry Parser
|
|
3
|
+
* Normalizes test events from node:test, <codelab/test.hpp>, and codelab_test.py.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface TestResultItem {
|
|
7
|
+
index: number;
|
|
8
|
+
name: string;
|
|
9
|
+
status: 'pass' | 'fail';
|
|
10
|
+
passed: boolean;
|
|
11
|
+
durationMs: number;
|
|
12
|
+
error: {
|
|
13
|
+
name?: string;
|
|
14
|
+
message?: string;
|
|
15
|
+
stack?: string;
|
|
16
|
+
actual?: string;
|
|
17
|
+
expected?: string;
|
|
18
|
+
} | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class CodeLabTestParser {
|
|
22
|
+
private count = 0;
|
|
23
|
+
private onResult: ((item: TestResultItem) => void) | null;
|
|
24
|
+
|
|
25
|
+
constructor(onResult?: (item: TestResultItem) => void) {
|
|
26
|
+
this.onResult = onResult || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public parseLine(line: string): TestResultItem | null {
|
|
30
|
+
const trimmed = String(line || '').trim();
|
|
31
|
+
const prefix = '__CODELAB_TEST__:';
|
|
32
|
+
const idx = trimmed.indexOf(prefix);
|
|
33
|
+
if (idx === -1) return null;
|
|
34
|
+
|
|
35
|
+
const payload = trimmed.slice(idx + prefix.length).trim();
|
|
36
|
+
try {
|
|
37
|
+
const data = JSON.parse(payload);
|
|
38
|
+
this.count++;
|
|
39
|
+
const item: TestResultItem = {
|
|
40
|
+
index: typeof data.index === 'number' ? data.index : this.count,
|
|
41
|
+
name: String(data.name || `Test #${this.count}`),
|
|
42
|
+
status: data.passed ? 'pass' : 'fail',
|
|
43
|
+
passed: Boolean(data.passed),
|
|
44
|
+
durationMs: typeof data.durationMs === 'number' ? data.durationMs : 0,
|
|
45
|
+
error: data.error || null
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
if (this.onResult) {
|
|
49
|
+
this.onResult(item);
|
|
50
|
+
}
|
|
51
|
+
return item;
|
|
52
|
+
} catch (err) {
|
|
53
|
+
void err;
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/runtime.ts
CHANGED
|
@@ -8606,6 +8606,7 @@ function __initVirtualRuntime(rootScope) {
|
|
|
8606
8606
|
if (vfs.existsSync(alt)) entryAbs = alt;
|
|
8607
8607
|
}
|
|
8608
8608
|
const isCppEntry = /\.(cpp|cc|cxx)$/i.test(entryAbs);
|
|
8609
|
+
const isPyEntry = /\.py$/i.test(entryAbs);
|
|
8609
8610
|
const isWasmEntry = /\.wasm$/i.test(entryAbs) || Boolean(options.wasmBinary);
|
|
8610
8611
|
if (isWasmEntry) {
|
|
8611
8612
|
const wasmBytes = options.wasmBinary || vfs.readFileSync(entryAbs);
|
|
@@ -8655,6 +8656,36 @@ function __initVirtualRuntime(rootScope) {
|
|
|
8655
8656
|
exitCode = compileRes.exitCode;
|
|
8656
8657
|
hasExited = true;
|
|
8657
8658
|
}
|
|
8659
|
+
} else if (isPyEntry) {
|
|
8660
|
+
const pyCompiler =
|
|
8661
|
+
options.pythonCompiler ||
|
|
8662
|
+
(typeof globalThis !== 'undefined' && globalThis.__codelabPythonCompiler) ||
|
|
8663
|
+
(rootScope && rootScope.__codelabPythonCompiler);
|
|
8664
|
+
if (typeof pyCompiler !== 'function') {
|
|
8665
|
+
throw new Error(
|
|
8666
|
+
'Python WebAssembly toolchain is not registered. Enable pythonExtension from "@innovatorved/code-lab/extensions/python" to run Python files.'
|
|
8667
|
+
);
|
|
8668
|
+
}
|
|
8669
|
+
const pyRes = await pyCompiler({
|
|
8670
|
+
entry: entryAbs,
|
|
8671
|
+
vfs,
|
|
8672
|
+
files: typeof vfs.toSnapshot === 'function' ? vfs.toSnapshot('/workspace') : (files || {}),
|
|
8673
|
+
args: argv || [entryAbs],
|
|
8674
|
+
env: Object.assign({}, sandboxProcess.env, options.env || {}),
|
|
8675
|
+
stdin: options.stdin || '',
|
|
8676
|
+
testFilter,
|
|
8677
|
+
onStdout: emitStdout,
|
|
8678
|
+
onStderr: emitStderr
|
|
8679
|
+
});
|
|
8680
|
+
if (pyRes) {
|
|
8681
|
+
exitCode = typeof pyRes.exitCode === 'number' ? pyRes.exitCode : (pyRes.ok ? 0 : 1);
|
|
8682
|
+
if (pyRes.testResults && Array.isArray(pyRes.testResults)) {
|
|
8683
|
+
for (const tr of pyRes.testResults) {
|
|
8684
|
+
testResults.push(tr);
|
|
8685
|
+
}
|
|
8686
|
+
}
|
|
8687
|
+
}
|
|
8688
|
+
hasExited = true;
|
|
8658
8689
|
} else {
|
|
8659
8690
|
// Check if the file in VFS starts with WASM magic bytes (\0asm)
|
|
8660
8691
|
let rawBuf = null;
|
|
@@ -9521,12 +9552,13 @@ _hostRuntime.cancelWorkerExecution = function cancelWorkerExecution(reason = 'Ex
|
|
|
9521
9552
|
};
|
|
9522
9553
|
|
|
9523
9554
|
_hostRuntime.executeInWorker = async function executeInWorker(options = {}) {
|
|
9524
|
-
const
|
|
9555
|
+
const isNonJs =
|
|
9525
9556
|
Boolean(options.wasmBinary) ||
|
|
9526
9557
|
Boolean(options.cppCompiler) ||
|
|
9527
|
-
(
|
|
9558
|
+
Boolean(options.pythonCompiler) ||
|
|
9559
|
+
(typeof options.entry === 'string' && /\.(cpp|cc|cxx|wasm|py)$/i.test(options.entry));
|
|
9528
9560
|
if (
|
|
9529
|
-
|
|
9561
|
+
isNonJs ||
|
|
9530
9562
|
typeof Worker === 'undefined' ||
|
|
9531
9563
|
typeof Blob === 'undefined' ||
|
|
9532
9564
|
typeof URL === 'undefined' ||
|
package/src/schemas.ts
CHANGED
|
@@ -22,8 +22,9 @@ export type GuidedStep = z.infer<typeof GuidedStepSchema>;
|
|
|
22
22
|
|
|
23
23
|
// ── 2. Language Bundle ──────────────────────────────────────────────────────
|
|
24
24
|
|
|
25
|
-
export const LanguageIdSchema = z.enum(['js', 'ts', 'cpp']);
|
|
25
|
+
export const LanguageIdSchema = z.enum(['js', 'ts', 'cpp', 'py']);
|
|
26
26
|
export type LanguageId = z.infer<typeof LanguageIdSchema>;
|
|
27
|
+
export const LanguageEnum = LanguageIdSchema;
|
|
27
28
|
|
|
28
29
|
export const LanguageBundleSchema = z.object({
|
|
29
30
|
language: LanguageIdSchema.default('js'),
|
|
@@ -52,6 +53,12 @@ export const GuidedLabExtensionOptionsSchema = z.object({
|
|
|
52
53
|
cppStarterFiles: z.record(z.string()).optional(),
|
|
53
54
|
cppSolutionFiles: z.record(z.string()).optional(),
|
|
54
55
|
cppFiles: z.record(z.string()).optional(),
|
|
56
|
+
pyStarterFiles: z.record(z.string()).optional(),
|
|
57
|
+
pySolutionFiles: z.record(z.string()).optional(),
|
|
58
|
+
pyFiles: z.record(z.string()).optional(),
|
|
59
|
+
pyEntry: z.string().optional(),
|
|
60
|
+
pyTestEntry: z.string().optional(),
|
|
61
|
+
pyTestCode: z.string().optional(),
|
|
55
62
|
summary: z.string().optional(),
|
|
56
63
|
onStepComplete: z.function().args(GuidedStepSchema, z.number()).returns(z.void()).optional(),
|
|
57
64
|
onLabComplete: z.function().args(z.string()).returns(z.void()).optional()
|
|
@@ -60,7 +67,7 @@ export const GuidedLabExtensionOptionsSchema = z.object({
|
|
|
60
67
|
export type GuidedLabExtensionOptions = z.infer<typeof GuidedLabExtensionOptionsSchema>;
|
|
61
68
|
|
|
62
69
|
export const CppToolchainProgressSchema = z.object({
|
|
63
|
-
stage: z.enum(['downloading', 'caching', 'extracting', 'compiling', 'linking', 'ready']),
|
|
70
|
+
stage: z.enum(['downloading', 'caching', 'extracting', 'compiling', 'linking', 'ready', 'error']),
|
|
64
71
|
loadedBytes: z.number().optional(),
|
|
65
72
|
totalBytes: z.number().optional(),
|
|
66
73
|
percent: z.number().optional(),
|
|
@@ -92,6 +99,36 @@ export const CppExtensionOptionsSchema = z.object({
|
|
|
92
99
|
|
|
93
100
|
export type CppExtensionOptions = z.infer<typeof CppExtensionOptionsSchema>;
|
|
94
101
|
|
|
102
|
+
export const PythonToolchainProgressSchema = z.object({
|
|
103
|
+
stage: z.enum(['downloading', 'caching', 'extracting', 'initializing', 'ready']),
|
|
104
|
+
loadedBytes: z.number().optional(),
|
|
105
|
+
totalBytes: z.number().optional(),
|
|
106
|
+
percent: z.number().optional(),
|
|
107
|
+
message: z.string().optional()
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export type PythonToolchainProgress = z.infer<typeof PythonToolchainProgressSchema>;
|
|
111
|
+
|
|
112
|
+
export const PythonExtensionOptionsSchema = z.object({
|
|
113
|
+
cdnBaseUrl: z.string().optional(),
|
|
114
|
+
indexUrl: z.string().optional(),
|
|
115
|
+
pyodideIndexUrl: z.string().optional(),
|
|
116
|
+
cacheInIndexedDB: z.boolean().default(true),
|
|
117
|
+
preload: z.boolean().default(false),
|
|
118
|
+
activate: z.boolean().optional(),
|
|
119
|
+
entry: z.string().default('main.py'),
|
|
120
|
+
testEntry: z.string().default('test.py'),
|
|
121
|
+
packages: z.array(z.string()).optional(),
|
|
122
|
+
preloadPackages: z.array(z.string()).optional(),
|
|
123
|
+
compilerMemoryLimitMb: z.number().optional(),
|
|
124
|
+
files: z.record(z.string()).optional(),
|
|
125
|
+
starterFiles: z.record(z.string()).optional(),
|
|
126
|
+
solutionFiles: z.record(z.string()).optional(),
|
|
127
|
+
onProgress: z.function().args(z.any()).returns(z.void()).optional()
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
export type PythonExtensionOptions = z.infer<typeof PythonExtensionOptionsSchema>;
|
|
131
|
+
|
|
95
132
|
// ── 4. GitHub Clone Options & Progress ──────────────────────────────────────
|
|
96
133
|
|
|
97
134
|
export const CloneProgressPhaseSchema = z.enum([
|
|
@@ -210,6 +247,14 @@ export const WorkbenchOptionsSchema = z.object({
|
|
|
210
247
|
cppFiles: z.record(z.string()).optional(),
|
|
211
248
|
cppStarterFiles: z.record(z.string()).optional(),
|
|
212
249
|
cppSolutionFiles: z.record(z.string()).optional(),
|
|
250
|
+
pyFiles: z.record(z.string()).optional(),
|
|
251
|
+
pyStarterFiles: z.record(z.string()).optional(),
|
|
252
|
+
pySolutionFiles: z.record(z.string()).optional(),
|
|
253
|
+
pyEntry: z.string().optional().default('main.py'),
|
|
254
|
+
pyTestEntry: z.string().optional().default('test.py'),
|
|
255
|
+
pyTestCode: z.string().optional(),
|
|
256
|
+
showWebPreview: z.boolean().optional(),
|
|
257
|
+
stdin: z.string().optional(),
|
|
213
258
|
guided: z.any().optional(),
|
|
214
259
|
mode: z.enum(['guided', 'sandbox']).optional(),
|
|
215
260
|
language: LanguageIdSchema.optional(),
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Shell Compiler Coreutils
|
|
3
|
+
* Provides `node`, `tsx`, `tsc`, `g++`, `clang++`, `python`, `python3`, `pytest`, `pip`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface ShellExecutionContext {
|
|
7
|
+
vfs: any;
|
|
8
|
+
cwd: string;
|
|
9
|
+
env: Record<string, string>;
|
|
10
|
+
resolvePath: (p: string) => string;
|
|
11
|
+
onStdout?: (text: string) => void;
|
|
12
|
+
onStderr?: (text: string) => void;
|
|
13
|
+
shell?: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ShellExecResult {
|
|
17
|
+
code: number;
|
|
18
|
+
stdout: string;
|
|
19
|
+
stderr: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const COMPILER_COREUTILS = {
|
|
23
|
+
async python3(args: string[], ctx: ShellExecutionContext): Promise<ShellExecResult> {
|
|
24
|
+
if (args.length === 0) {
|
|
25
|
+
return { code: 0, stdout: 'Python 3.12.8 (CPython WebAssembly via Pyodide)\n', stderr: '' };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (args[0] === '--version' || args[0] === '-V') {
|
|
29
|
+
return { code: 0, stdout: 'Python 3.12.8 (CPython WebAssembly via Pyodide)\n', stderr: '' };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let codeToRun: string | undefined;
|
|
33
|
+
let entryFile: string | undefined;
|
|
34
|
+
|
|
35
|
+
if (args[0] === '-c' && args.length > 1) {
|
|
36
|
+
codeToRun = args[1];
|
|
37
|
+
} else if (!args[0].startsWith('-')) {
|
|
38
|
+
entryFile = ctx.resolvePath(args[0]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const pyCompiler =
|
|
42
|
+
(ctx.shell && ctx.shell.options && ctx.shell.options.pythonCompiler) ||
|
|
43
|
+
(typeof globalThis !== 'undefined' && (globalThis as any).__codelabPythonCompiler) ||
|
|
44
|
+
(typeof window !== 'undefined' && (window as any).__codelabPythonCompiler);
|
|
45
|
+
|
|
46
|
+
if (typeof pyCompiler !== 'function') {
|
|
47
|
+
return {
|
|
48
|
+
code: 1,
|
|
49
|
+
stdout: '',
|
|
50
|
+
stderr: 'python3: Python WebAssembly toolchain is not loaded. Enable pythonExtension from "@innovatorved/code-lab/extensions/python".\n'
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let stdoutAcc = '';
|
|
55
|
+
let stderrAcc = '';
|
|
56
|
+
|
|
57
|
+
const res = await pyCompiler({
|
|
58
|
+
entry: entryFile,
|
|
59
|
+
source: codeToRun,
|
|
60
|
+
vfs: ctx.vfs,
|
|
61
|
+
onStdout: (t: string) => {
|
|
62
|
+
stdoutAcc += t;
|
|
63
|
+
if (ctx.onStdout) ctx.onStdout(t);
|
|
64
|
+
},
|
|
65
|
+
onStderr: (t: string) => {
|
|
66
|
+
stderrAcc += t;
|
|
67
|
+
if (ctx.onStderr) ctx.onStderr(t);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
code: res.ok ? 0 : (res.exitCode || 1),
|
|
73
|
+
stdout: stdoutAcc,
|
|
74
|
+
stderr: stderrAcc
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
async python(args: string[], ctx: ShellExecutionContext): Promise<ShellExecResult> {
|
|
79
|
+
return COMPILER_COREUTILS.python3(args, ctx);
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
async pytest(args: string[], ctx: ShellExecutionContext): Promise<ShellExecResult> {
|
|
83
|
+
const testFiles = args.filter((a) => !a.startsWith('-'));
|
|
84
|
+
const target = testFiles[0] || 'test.py';
|
|
85
|
+
return COMPILER_COREUTILS.python3([target], ctx);
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async pip(args: string[], ctx: ShellExecutionContext): Promise<ShellExecResult> {
|
|
89
|
+
void ctx;
|
|
90
|
+
if (args.length === 0 || args[0] === '--version' || args[0] === '-V') {
|
|
91
|
+
return { code: 0, stdout: 'pip 24.0 from /usr/lib/python/pip (python 3.12)\n', stderr: '' };
|
|
92
|
+
}
|
|
93
|
+
if (args[0] === 'install') {
|
|
94
|
+
const pkgs = args.slice(1).filter((p) => !p.startsWith('-'));
|
|
95
|
+
return {
|
|
96
|
+
code: 0,
|
|
97
|
+
stdout: `Successfully installed ${pkgs.join(', ') || 'packages'}\n`,
|
|
98
|
+
stderr: ''
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return { code: 0, stdout: 'pip 24.0 (Pyodide micropip)\n', stderr: '' };
|
|
102
|
+
}
|
|
103
|
+
};
|
package/src/shell.ts
CHANGED
|
@@ -5937,6 +5937,7 @@
|
|
|
5937
5937
|
stdout += ' git [status diff log] Built-in version control\n';
|
|
5938
5938
|
stdout += ' node, bun, npm, pnpm, npx, bunx CLI runtime and package shims\n';
|
|
5939
5939
|
stdout += ' g++, clang++, c++ C++20 WebAssembly compiler (via cppExtension)\n';
|
|
5940
|
+
stdout += ' python, python3, pytest Python 3.12 WebAssembly runtime (via pythonExtension)\n';
|
|
5940
5941
|
stdout += ' serve [dir] [--port] Built-in preview server (transpiles TS/JSX for the browser)\n';
|
|
5941
5942
|
stdout += ' ports, curl Port table inspection & HTTP requests\n';
|
|
5942
5943
|
stdout += ' hey, ab, wrk Concurrent HTTP load generator & latency percentiles\n';
|
|
@@ -6174,6 +6175,117 @@
|
|
|
6174
6175
|
return COREUTILS['g++'](args, ctx);
|
|
6175
6176
|
},
|
|
6176
6177
|
|
|
6178
|
+
async python3(args, ctx) {
|
|
6179
|
+
if (args.length === 0) {
|
|
6180
|
+
return { code: 0, stdout: 'Python 3.12.8 (CPython WebAssembly via Pyodide)\n', stderr: '' };
|
|
6181
|
+
}
|
|
6182
|
+
|
|
6183
|
+
if (args[0] === '--version' || args[0] === '-V') {
|
|
6184
|
+
return { code: 0, stdout: 'Python 3.12.8 (CPython WebAssembly via Pyodide)\n', stderr: '' };
|
|
6185
|
+
}
|
|
6186
|
+
|
|
6187
|
+
let codeToRun = undefined;
|
|
6188
|
+
let entryFile = undefined;
|
|
6189
|
+
|
|
6190
|
+
if (args[0] === '-c' && args.length > 1) {
|
|
6191
|
+
codeToRun = args[1];
|
|
6192
|
+
} else if (!args[0].startsWith('-')) {
|
|
6193
|
+
entryFile = ctx.resolvePath(args[0]);
|
|
6194
|
+
}
|
|
6195
|
+
|
|
6196
|
+
const pyCompiler =
|
|
6197
|
+
(ctx.shell && ctx.shell.options && ctx.shell.options.pythonCompiler) ||
|
|
6198
|
+
(typeof globalThis !== 'undefined' && globalThis.__codelabPythonCompiler) ||
|
|
6199
|
+
(typeof window !== 'undefined' && window.__codelabPythonCompiler);
|
|
6200
|
+
|
|
6201
|
+
if (typeof pyCompiler !== 'function') {
|
|
6202
|
+
return {
|
|
6203
|
+
code: 1,
|
|
6204
|
+
stdout: '',
|
|
6205
|
+
stderr:
|
|
6206
|
+
'python3: Python WebAssembly toolchain is not enabled. Enable pythonExtension from "@innovatorved/code-lab/extensions/python".\n'
|
|
6207
|
+
};
|
|
6208
|
+
}
|
|
6209
|
+
|
|
6210
|
+
let outText = '';
|
|
6211
|
+
let errText = '';
|
|
6212
|
+
const res = await pyCompiler({
|
|
6213
|
+
entry: entryFile,
|
|
6214
|
+
source: codeToRun,
|
|
6215
|
+
vfs: ctx.vfs,
|
|
6216
|
+
onStdout: (t) => {
|
|
6217
|
+
outText += t;
|
|
6218
|
+
if (ctx.onStdout) ctx.onStdout(t);
|
|
6219
|
+
},
|
|
6220
|
+
onStderr: (t) => {
|
|
6221
|
+
errText += t;
|
|
6222
|
+
if (ctx.onStderr) ctx.onStderr(t);
|
|
6223
|
+
}
|
|
6224
|
+
});
|
|
6225
|
+
|
|
6226
|
+
return {
|
|
6227
|
+
code: res && res.ok ? 0 : ((res && res.exitCode) || 1),
|
|
6228
|
+
stdout: outText,
|
|
6229
|
+
stderr: errText
|
|
6230
|
+
};
|
|
6231
|
+
},
|
|
6232
|
+
|
|
6233
|
+
async python(args, ctx) {
|
|
6234
|
+
return COREUTILS['python3'](args, ctx);
|
|
6235
|
+
},
|
|
6236
|
+
|
|
6237
|
+
async pytest(args, ctx) {
|
|
6238
|
+
if (args.length > 0 && (args[0] === '--version' || args[0] === '-V')) {
|
|
6239
|
+
return { code: 0, stdout: 'pytest 8.3.4 (Python 3.12 WebAssembly)\n', stderr: '' };
|
|
6240
|
+
}
|
|
6241
|
+
const testFiles = args.filter((a) => !a.startsWith('-'));
|
|
6242
|
+
let target = testFiles[0];
|
|
6243
|
+
if (!target && ctx.vfs) {
|
|
6244
|
+
try {
|
|
6245
|
+
const allFiles = Object.keys(typeof ctx.vfs.toSnapshot === 'function' ? ctx.vfs.toSnapshot('/workspace') : (ctx.vfs.files || {}));
|
|
6246
|
+
target = allFiles.find((f) => /(?:^|\/)(?:test_.*|.*_test|test)\.py$/i.test(f));
|
|
6247
|
+
} catch (err) {
|
|
6248
|
+
void err;
|
|
6249
|
+
}
|
|
6250
|
+
}
|
|
6251
|
+
target = target || 'test.py';
|
|
6252
|
+
return COREUTILS['python3']([target], ctx);
|
|
6253
|
+
},
|
|
6254
|
+
|
|
6255
|
+
async pip(args, ctx) {
|
|
6256
|
+
if (args.length === 0 || args[0] === '--version' || args[0] === '-V') {
|
|
6257
|
+
return { code: 0, stdout: 'pip 24.0 from /usr/lib/python/pip (python 3.12)\n', stderr: '' };
|
|
6258
|
+
}
|
|
6259
|
+
if (args[0] === 'install') {
|
|
6260
|
+
const pkgs = args.slice(1).filter((p) => !p.startsWith('-'));
|
|
6261
|
+
const toolchain =
|
|
6262
|
+
(ctx.shell && ctx.shell.options && ctx.shell.options.pythonToolchain) ||
|
|
6263
|
+
(typeof globalThis !== 'undefined' && globalThis.__codelabPythonToolchain) ||
|
|
6264
|
+
(typeof window !== 'undefined' && window.__codelabPythonToolchain);
|
|
6265
|
+
if (toolchain && typeof toolchain.installPackages === 'function') {
|
|
6266
|
+
const res = await toolchain.installPackages(pkgs);
|
|
6267
|
+
if (res.ok) {
|
|
6268
|
+
return {
|
|
6269
|
+
code: 0,
|
|
6270
|
+
stdout: `Successfully installed ${res.installed.join(', ') || 'packages'}\n`,
|
|
6271
|
+
stderr: ''
|
|
6272
|
+
};
|
|
6273
|
+
}
|
|
6274
|
+
return {
|
|
6275
|
+
code: 1,
|
|
6276
|
+
stdout: '',
|
|
6277
|
+
stderr: `ERROR: Could not find a version that satisfies the requirement ${res.error || pkgs.join(', ')}\n`
|
|
6278
|
+
};
|
|
6279
|
+
}
|
|
6280
|
+
return {
|
|
6281
|
+
code: 0,
|
|
6282
|
+
stdout: `Successfully installed ${pkgs.join(', ') || 'packages'}\n`,
|
|
6283
|
+
stderr: ''
|
|
6284
|
+
};
|
|
6285
|
+
}
|
|
6286
|
+
return { code: 0, stdout: 'pip 24.0 (Pyodide micropip)\n', stderr: '' };
|
|
6287
|
+
},
|
|
6288
|
+
|
|
6177
6289
|
async bun(args, ctx) {
|
|
6178
6290
|
if (args.length === 0) {
|
|
6179
6291
|
return { code: 0, stdout: 'Bun v1.1.20\n', stderr: '' };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Fuzzy-Searchable Command Palette (Cmd+Shift+P / Cmd+K)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { CL_ICONS } from './icons';
|
|
6
|
+
|
|
7
|
+
export interface CommandPaletteItem {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
shortcut?: string;
|
|
11
|
+
action: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function getStandardWorkbenchCommands(workbench: any): CommandPaletteItem[] {
|
|
15
|
+
const isGuided = Boolean(workbench.hasGuidedLab && workbench.mode === 'guided');
|
|
16
|
+
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
id: 'run-code',
|
|
20
|
+
label: isGuided ? 'CodeLab: Verify Current Guided Step' : 'CodeLab: Run Active Code / Demo',
|
|
21
|
+
shortcut: '⌘↵',
|
|
22
|
+
action: () => {
|
|
23
|
+
if (isGuided) workbench.verifyCurrentStep();
|
|
24
|
+
else workbench.runCode();
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: 'run-demo',
|
|
29
|
+
label: 'CodeLab: Run Demo Script',
|
|
30
|
+
shortcut: '⇧⌘↵',
|
|
31
|
+
action: () => workbench.runDemo()
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'run-tests',
|
|
35
|
+
label: 'CodeLab: Run Unit Tests',
|
|
36
|
+
shortcut: '⌘↵',
|
|
37
|
+
action: () => workbench.runTests()
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: 'format-document',
|
|
41
|
+
label: 'Format: Format Active Document',
|
|
42
|
+
shortcut: '⇧⌥F',
|
|
43
|
+
action: () => workbench.formatActiveDocument()
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 'share-snapshot',
|
|
47
|
+
label: 'Share: Copy URL Snapshot to Clipboard (#codelab=...)',
|
|
48
|
+
action: () => workbench.shareSnapshot()
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: 'compare-solution',
|
|
52
|
+
label: 'Diff: Compare Active File with Reference Solution',
|
|
53
|
+
action: () => workbench.showDiffModal(workbench.activeFile || workbench._pickDefaultFile(), 'solution')
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: 'compare-starter',
|
|
57
|
+
label: 'Diff: Compare Active File with Starter Template',
|
|
58
|
+
action: () => workbench.showDiffModal(workbench.activeFile || workbench._pickDefaultFile(), 'starter')
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'open-stdin',
|
|
62
|
+
label: 'View: Open Stdin / Custom Input Panel',
|
|
63
|
+
action: () => {
|
|
64
|
+
workbench.drawerTab = 'stdin';
|
|
65
|
+
if (typeof workbench._renderDrawerBody === 'function') workbench._renderDrawerBody();
|
|
66
|
+
const drawer = workbench.container?.querySelector('#cl-drawer');
|
|
67
|
+
if (drawer) drawer.classList.remove('is-minimized');
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: 'open-problems',
|
|
72
|
+
label: 'View: Show Problems & Diagnostics',
|
|
73
|
+
action: () => {
|
|
74
|
+
workbench.drawerTab = 'problems';
|
|
75
|
+
if (typeof workbench._renderDrawerBody === 'function') workbench._renderDrawerBody();
|
|
76
|
+
const drawer = workbench.container?.querySelector('#cl-drawer');
|
|
77
|
+
if (drawer) drawer.classList.remove('is-minimized');
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'new-file',
|
|
82
|
+
label: 'File: New File...',
|
|
83
|
+
action: () => workbench.startInlineCreate('file', '')
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: 'new-folder',
|
|
87
|
+
label: 'File: New Folder...',
|
|
88
|
+
action: () => workbench.startInlineCreate('folder', '')
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: 'rename-active',
|
|
92
|
+
label: 'File: Rename Active File...',
|
|
93
|
+
shortcut: 'F2',
|
|
94
|
+
action: () => { if (workbench.activeFile) workbench.startInlineRename('file', workbench.activeFile); }
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
id: 'delete-active',
|
|
98
|
+
label: 'File: Delete Active File',
|
|
99
|
+
action: () => { if (workbench.activeFile) workbench.deleteFile(workbench.activeFile); }
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: 'export-workspace-zip',
|
|
103
|
+
label: 'Workspace: Export Project as ZIP Archive',
|
|
104
|
+
action: () => workbench.exportProjectZip({ download: true })
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: 'export-workspace-json',
|
|
108
|
+
label: 'Workspace: Export Workspace as JSON',
|
|
109
|
+
action: () => workbench.exportWorkspace({ download: true })
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
id: 'switch-lang',
|
|
113
|
+
label: 'Language: Cycle Active Language (JS / TS / C++ / PY)',
|
|
114
|
+
action: () => {
|
|
115
|
+
const order = ['js'];
|
|
116
|
+
if (workbench.enabledLanguages?.has('ts')) order.push('ts');
|
|
117
|
+
if (workbench.enabledLanguages?.has('cpp')) order.push('cpp');
|
|
118
|
+
if (workbench.enabledLanguages?.has('py')) order.push('py');
|
|
119
|
+
const next = order[(order.indexOf(workbench.lang) + 1) % order.length] || 'js';
|
|
120
|
+
workbench.setLanguage(next);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'toggle-fullscreen',
|
|
125
|
+
label: 'View: Toggle Fullscreen Workbench',
|
|
126
|
+
action: () => workbench.toggleFullscreen()
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: 'search-workspace',
|
|
130
|
+
label: 'Search: Find & Replace Across Files...',
|
|
131
|
+
shortcut: '⇧⌘F',
|
|
132
|
+
action: () => workbench.showSearchModal()
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: 'open-scm',
|
|
136
|
+
label: 'Git: Source Control (Status, Stage & Commit)...',
|
|
137
|
+
action: () => workbench.showScmModal()
|
|
138
|
+
}
|
|
139
|
+
];
|
|
140
|
+
}
|