@innovatorved/code-lab 0.1.7 → 0.1.8
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 +34 -16
- package/README.md +17 -7
- package/dist/code-lab.css +26 -0
- package/dist/code-lab.d.ts +271 -1
- package/dist/code-lab.esm.js +2687 -172
- package/dist/code-lab.js +2647 -170
- package/dist/csp.d.ts +86 -0
- package/dist/diagnostics.d.ts +43 -0
- package/dist/extensions/guided-lab.esm.js +63 -0
- package/dist/extensions/guided-lab.js +63 -0
- package/dist/html.d.ts +24 -0
- package/dist/index.d.ts +9 -1
- package/dist/logger.d.ts +60 -0
- package/dist/preview-sw.js +412 -46
- package/dist/runtime.esm.js +169 -31
- package/dist/runtime.js +169 -31
- package/dist/schemas.d.ts +357 -25
- package/dist/schemas.esm.js +70 -0
- package/dist/schemas.js +70 -0
- package/dist/scm.d.ts +77 -0
- package/dist/search.d.ts +75 -0
- package/dist/shell.esm.js +216 -36
- package/dist/shell.js +216 -36
- package/dist/storage.d.ts +57 -0
- package/dist/terminal.esm.js +122 -7
- package/dist/terminal.js +122 -7
- package/dist/zip.d.ts +42 -0
- package/package.json +3 -1
- package/src/code-lab.css +26 -0
- package/src/csp.ts +396 -0
- package/src/diagnostics.ts +116 -0
- package/src/html.ts +88 -0
- package/src/index.ts +39 -1
- package/src/logger.ts +139 -0
- package/src/preview-sw.js +84 -46
- package/src/runtime.ts +67 -30
- package/src/schemas.ts +91 -1
- package/src/scm.ts +268 -0
- package/src/search.ts +389 -0
- package/src/shell.ts +114 -35
- package/src/storage.ts +377 -0
- package/src/terminal.ts +20 -6
- package/src/workbench.ts +728 -95
- package/src/zip.ts +326 -0
package/src/schemas.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Zod Schemas and Inferred TypeScript Types for code-lab v0.1.
|
|
4
|
+
* Zod Schemas and Inferred TypeScript Types for code-lab v0.1.8
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
// ── 1. Guided Step ──────────────────────────────────────────────────────────
|
|
@@ -178,3 +178,93 @@ export const WorkbenchOptionsSchema = z.object({
|
|
|
178
178
|
});
|
|
179
179
|
|
|
180
180
|
export type WorkbenchOptions = z.infer<typeof WorkbenchOptionsSchema>;
|
|
181
|
+
|
|
182
|
+
// ── 8. Search Options & Match Results ────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
export const SearchOptionsSchema = z.object({
|
|
185
|
+
query: z.string().default(''),
|
|
186
|
+
caseSensitive: z.boolean().optional().default(false),
|
|
187
|
+
wholeWord: z.boolean().optional().default(false),
|
|
188
|
+
regex: z.boolean().optional().default(false),
|
|
189
|
+
isRegex: z.boolean().optional().default(false),
|
|
190
|
+
include: z.string().optional(),
|
|
191
|
+
includeGlob: z.string().optional(),
|
|
192
|
+
includePattern: z.union([z.string(), z.array(z.string())]).optional(),
|
|
193
|
+
exclude: z.string().optional(),
|
|
194
|
+
excludeGlob: z.string().optional(),
|
|
195
|
+
excludePattern: z.union([z.string(), z.array(z.string())]).optional(),
|
|
196
|
+
maxMatches: z.number().int().positive().optional(),
|
|
197
|
+
maxMatchesPerFile: z.number().int().positive().optional()
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
export type SearchOptions = z.infer<typeof SearchOptionsSchema>;
|
|
201
|
+
|
|
202
|
+
// ── 9. Diagnostic Item & Problems ───────────────────────────────────────────
|
|
203
|
+
|
|
204
|
+
export const DiagnosticSeveritySchema = z.enum(['error', 'warning', 'info', 'hint']);
|
|
205
|
+
export type DiagnosticSeverity = z.infer<typeof DiagnosticSeveritySchema>;
|
|
206
|
+
|
|
207
|
+
export const DiagnosticItemSchema = z.object({
|
|
208
|
+
id: z.string(),
|
|
209
|
+
file: z.string(),
|
|
210
|
+
line: z.number().int(),
|
|
211
|
+
col: z.number().int().optional(),
|
|
212
|
+
column: z.number().int().optional(),
|
|
213
|
+
endLine: z.number().int().optional(),
|
|
214
|
+
endCol: z.number().int().optional(),
|
|
215
|
+
endColumn: z.number().int().optional(),
|
|
216
|
+
message: z.string(),
|
|
217
|
+
severity: DiagnosticSeveritySchema,
|
|
218
|
+
source: z.string().optional(),
|
|
219
|
+
code: z.union([z.string(), z.number()]).optional(),
|
|
220
|
+
tags: z.array(z.string()).optional()
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
export type DiagnosticItem = z.infer<typeof DiagnosticItemSchema>;
|
|
224
|
+
|
|
225
|
+
// ── 10. Stored Project ──────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
export const StoredProjectSchema = z.object({
|
|
228
|
+
id: z.string(),
|
|
229
|
+
name: z.string(),
|
|
230
|
+
files: z.record(z.string()),
|
|
231
|
+
updatedAt: z.number(),
|
|
232
|
+
createdAt: z.number().optional(),
|
|
233
|
+
entry: z.string().optional(),
|
|
234
|
+
testEntry: z.string().optional(),
|
|
235
|
+
version: z.number().optional()
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
export type StoredProject = z.infer<typeof StoredProjectSchema>;
|
|
239
|
+
|
|
240
|
+
// ── 11. Git File Status & SCM Summary ───────────────────────────────────────
|
|
241
|
+
|
|
242
|
+
export const GitFileChangeTypeSchema = z.enum(['modified', 'added', 'deleted', 'untracked']);
|
|
243
|
+
export type GitFileChangeType = z.infer<typeof GitFileChangeTypeSchema>;
|
|
244
|
+
|
|
245
|
+
export const GitFileStatusSchema = z.object({
|
|
246
|
+
path: z.string(),
|
|
247
|
+
status: GitFileChangeTypeSchema,
|
|
248
|
+
staged: z.boolean(),
|
|
249
|
+
oldContent: z.string().optional(),
|
|
250
|
+
newContent: z.string().optional()
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
export type GitFileStatus = z.infer<typeof GitFileStatusSchema>;
|
|
254
|
+
|
|
255
|
+
export const GitStateSummarySchema = z.object({
|
|
256
|
+
branch: z.string().default('main'),
|
|
257
|
+
headCommit: z.string().optional(),
|
|
258
|
+
branches: z.array(z.string()).default(['main']),
|
|
259
|
+
staged: z.array(GitFileStatusSchema).default([]),
|
|
260
|
+
unstaged: z.array(GitFileStatusSchema).default([]),
|
|
261
|
+
untracked: z.array(GitFileStatusSchema).default([]),
|
|
262
|
+
modified: z.array(GitFileStatusSchema).optional(),
|
|
263
|
+
deleted: z.array(GitFileStatusSchema).optional(),
|
|
264
|
+
hasChanges: z.boolean().default(false),
|
|
265
|
+
isClean: z.boolean().default(true),
|
|
266
|
+
ahead: z.number().int().default(0),
|
|
267
|
+
behind: z.number().int().default(0)
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
export type GitStateSummary = z.infer<typeof GitStateSummarySchema>;
|
package/src/scm.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @innovatorved/code-lab — Interactive Git Source Control (SCM) Engine
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* Pure TypeScript in-browser Git status, staging, unstaging, and commit engine
|
|
5
|
+
* backed by workspace VFS state. Powers both the interactive Source Control UI
|
|
6
|
+
* and shell/CLI Git operations.
|
|
7
|
+
*
|
|
8
|
+
* Strict leaf module: fully typed, zero explicit any, zero empty catches.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export type GitFileChangeType = 'modified' | 'added' | 'deleted' | 'untracked';
|
|
12
|
+
|
|
13
|
+
export interface GitFileStatus {
|
|
14
|
+
path: string;
|
|
15
|
+
status: GitFileChangeType;
|
|
16
|
+
staged: boolean;
|
|
17
|
+
oldContent?: string;
|
|
18
|
+
newContent?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GitCommitRecord {
|
|
22
|
+
hash: string;
|
|
23
|
+
message: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
author?: string;
|
|
26
|
+
files: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GitState {
|
|
30
|
+
branch: string;
|
|
31
|
+
branches?: string[];
|
|
32
|
+
headCommit: string;
|
|
33
|
+
headFiles: Record<string, string>;
|
|
34
|
+
staged: Map<string, string | null>;
|
|
35
|
+
commits: GitCommitRecord[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface GitStatusResult {
|
|
39
|
+
branch: string;
|
|
40
|
+
headCommit: string;
|
|
41
|
+
untracked: GitFileStatus[];
|
|
42
|
+
modified: GitFileStatus[];
|
|
43
|
+
deleted: GitFileStatus[];
|
|
44
|
+
staged: GitFileStatus[];
|
|
45
|
+
unstaged: GitFileStatus[];
|
|
46
|
+
hasChanges: boolean;
|
|
47
|
+
isClean: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Computes a deterministic 7-character short hash for a commit payload.
|
|
52
|
+
*/
|
|
53
|
+
export function computeShortCommitHash(input: string): string {
|
|
54
|
+
let h1 = 0xdeadbeef ^ input.length;
|
|
55
|
+
let h2 = 0x41c6ce57 ^ input.length;
|
|
56
|
+
for (let i = 0; i < input.length; i++) {
|
|
57
|
+
const ch = input.charCodeAt(i);
|
|
58
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
59
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
60
|
+
}
|
|
61
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
62
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
63
|
+
const combined = 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
64
|
+
return combined.toString(16).padStart(7, '0').slice(0, 7);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initializes a fresh in-memory Git repository state from initial VFS files.
|
|
69
|
+
*/
|
|
70
|
+
export function createGitState(
|
|
71
|
+
initialFiles: Record<string, string> = {},
|
|
72
|
+
branch = 'main'
|
|
73
|
+
): GitState {
|
|
74
|
+
const headFiles: Record<string, string> = { ...initialFiles };
|
|
75
|
+
const initialHash = computeShortCommitHash(JSON.stringify(headFiles) + ':init');
|
|
76
|
+
return {
|
|
77
|
+
branch,
|
|
78
|
+
branches: [branch],
|
|
79
|
+
headCommit: initialHash,
|
|
80
|
+
headFiles,
|
|
81
|
+
staged: new Map<string, string | null>(),
|
|
82
|
+
commits: [
|
|
83
|
+
{
|
|
84
|
+
hash: initialHash,
|
|
85
|
+
message: 'Initial commit',
|
|
86
|
+
timestamp: Date.now(),
|
|
87
|
+
files: { ...headFiles }
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Computes working-tree and staging-area Git status against `gitState`.
|
|
95
|
+
*/
|
|
96
|
+
export function computeGitStatus(
|
|
97
|
+
vfsFiles: Record<string, string>,
|
|
98
|
+
_repoRoot = '/workspace',
|
|
99
|
+
gitState?: GitState
|
|
100
|
+
): GitStatusResult {
|
|
101
|
+
const state = gitState || createGitState({});
|
|
102
|
+
const headFiles = state.headFiles || {};
|
|
103
|
+
const currentStaged = state.staged instanceof Map ? state.staged : new Map<string, string | null>();
|
|
104
|
+
|
|
105
|
+
const untracked: GitFileStatus[] = [];
|
|
106
|
+
const modified: GitFileStatus[] = [];
|
|
107
|
+
const deleted: GitFileStatus[] = [];
|
|
108
|
+
const staged: GitFileStatus[] = [];
|
|
109
|
+
|
|
110
|
+
for (const [filePath, stagedContent] of currentStaged.entries()) {
|
|
111
|
+
const inHead = Object.prototype.hasOwnProperty.call(headFiles, filePath);
|
|
112
|
+
if (stagedContent === null) {
|
|
113
|
+
staged.push({
|
|
114
|
+
path: filePath,
|
|
115
|
+
status: 'deleted',
|
|
116
|
+
staged: true,
|
|
117
|
+
oldContent: headFiles[filePath]
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
staged.push({
|
|
121
|
+
path: filePath,
|
|
122
|
+
status: inHead ? 'modified' : 'added',
|
|
123
|
+
staged: true,
|
|
124
|
+
oldContent: inHead ? headFiles[filePath] : undefined,
|
|
125
|
+
newContent: stagedContent
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const [filePath, content] of Object.entries(vfsFiles || {})) {
|
|
131
|
+
if (currentStaged.has(filePath)) {
|
|
132
|
+
const stagedVal = currentStaged.get(filePath);
|
|
133
|
+
if (stagedVal !== content) {
|
|
134
|
+
modified.push({
|
|
135
|
+
path: filePath,
|
|
136
|
+
status: 'modified',
|
|
137
|
+
staged: false,
|
|
138
|
+
oldContent: stagedVal ?? undefined,
|
|
139
|
+
newContent: content
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
} else if (Object.prototype.hasOwnProperty.call(headFiles, filePath)) {
|
|
143
|
+
if (headFiles[filePath] !== content) {
|
|
144
|
+
modified.push({
|
|
145
|
+
path: filePath,
|
|
146
|
+
status: 'modified',
|
|
147
|
+
staged: false,
|
|
148
|
+
oldContent: headFiles[filePath],
|
|
149
|
+
newContent: content
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
untracked.push({
|
|
154
|
+
path: filePath,
|
|
155
|
+
status: 'untracked',
|
|
156
|
+
staged: false,
|
|
157
|
+
newContent: content
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const filePath of Object.keys(headFiles)) {
|
|
163
|
+
if (
|
|
164
|
+
!Object.prototype.hasOwnProperty.call(vfsFiles, filePath) &&
|
|
165
|
+
!currentStaged.has(filePath)
|
|
166
|
+
) {
|
|
167
|
+
deleted.push({
|
|
168
|
+
path: filePath,
|
|
169
|
+
status: 'deleted',
|
|
170
|
+
staged: false,
|
|
171
|
+
oldContent: headFiles[filePath]
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const unstaged: GitFileStatus[] = [...modified, ...deleted];
|
|
177
|
+
const isClean =
|
|
178
|
+
untracked.length === 0 &&
|
|
179
|
+
modified.length === 0 &&
|
|
180
|
+
deleted.length === 0 &&
|
|
181
|
+
staged.length === 0;
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
branch: state.branch || 'main',
|
|
185
|
+
headCommit: state.headCommit || '',
|
|
186
|
+
untracked,
|
|
187
|
+
modified,
|
|
188
|
+
deleted,
|
|
189
|
+
staged,
|
|
190
|
+
unstaged,
|
|
191
|
+
hasChanges: !isClean,
|
|
192
|
+
isClean
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Stages a file addition, modification, or deletion (`currentContent === null`).
|
|
198
|
+
*/
|
|
199
|
+
export function stageFile(
|
|
200
|
+
gitState: GitState,
|
|
201
|
+
relativePath: string,
|
|
202
|
+
currentContent: string | null
|
|
203
|
+
): void {
|
|
204
|
+
if (!gitState) return;
|
|
205
|
+
if (!(gitState.staged instanceof Map)) {
|
|
206
|
+
gitState.staged = new Map<string, string | null>();
|
|
207
|
+
}
|
|
208
|
+
gitState.staged.set(relativePath, currentContent === undefined ? null : currentContent);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Removes a file from the staging index.
|
|
213
|
+
*/
|
|
214
|
+
export function unstageFile(gitState: GitState, relativePath: string): void {
|
|
215
|
+
if (!gitState || !(gitState.staged instanceof Map)) return;
|
|
216
|
+
gitState.staged.delete(relativePath);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Commits all staged changes to `gitState`, advancing `headCommit` and `headFiles`,
|
|
221
|
+
* and clearing the staging area. Throws if message is empty or staging area is empty.
|
|
222
|
+
*/
|
|
223
|
+
export function commitChanges(gitState: GitState, message: string): string {
|
|
224
|
+
const trimmed = typeof message === 'string' ? message.trim() : '';
|
|
225
|
+
if (!trimmed) {
|
|
226
|
+
throw new Error('Commit message cannot be empty');
|
|
227
|
+
}
|
|
228
|
+
if (!gitState || !(gitState.staged instanceof Map) || gitState.staged.size === 0) {
|
|
229
|
+
throw new Error('Nothing to commit (staging area is empty)');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const newHeadFiles: Record<string, string> = { ...(gitState.headFiles || {}) };
|
|
233
|
+
for (const [filePath, content] of gitState.staged.entries()) {
|
|
234
|
+
if (content === null) {
|
|
235
|
+
delete newHeadFiles[filePath];
|
|
236
|
+
} else {
|
|
237
|
+
newHeadFiles[filePath] = content;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const timestamp = Date.now();
|
|
242
|
+
const hash = computeShortCommitHash(`${gitState.headCommit}:${trimmed}:${timestamp}`);
|
|
243
|
+
const commit: GitCommitRecord = {
|
|
244
|
+
hash,
|
|
245
|
+
message: trimmed,
|
|
246
|
+
timestamp,
|
|
247
|
+
files: newHeadFiles
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
if (!Array.isArray(gitState.commits)) {
|
|
251
|
+
gitState.commits = [];
|
|
252
|
+
}
|
|
253
|
+
gitState.commits.push(commit);
|
|
254
|
+
gitState.headCommit = hash;
|
|
255
|
+
gitState.headFiles = newHeadFiles;
|
|
256
|
+
gitState.staged.clear();
|
|
257
|
+
|
|
258
|
+
return hash;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export default {
|
|
262
|
+
createGitState,
|
|
263
|
+
computeGitStatus,
|
|
264
|
+
stageFile,
|
|
265
|
+
unstageFile,
|
|
266
|
+
commitChanges,
|
|
267
|
+
computeShortCommitHash
|
|
268
|
+
};
|