@playcraft/build 0.1.86 → 0.1.88

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.
@@ -99,6 +99,37 @@ export async function scanSourceForChannels(context, channels) {
99
99
  }
100
100
  return results;
101
101
  }
102
+ /**
103
+ * Finds where each pattern first occurs in executable project source (comments and strings in
104
+ * JavaScript-like files are ignored, as in the compliance scan). Absent patterns are left out.
105
+ */
106
+ export async function findSourcePatterns(projectRoot, patterns, sourceGlobs) {
107
+ const found = new Map();
108
+ const ids = Object.keys(patterns);
109
+ for (const filePath of await collectSourceFiles(projectRoot, sourceGlobs)) {
110
+ if (found.size === ids.length)
111
+ break;
112
+ let content;
113
+ try {
114
+ content = await fs.readFile(filePath, 'utf-8');
115
+ }
116
+ catch {
117
+ continue;
118
+ }
119
+ for (const id of ids) {
120
+ if (found.has(id))
121
+ continue;
122
+ const match = findExecutableSourcePattern(filePath, content, patterns[id]);
123
+ if (match)
124
+ found.set(id, {
125
+ file: filePath,
126
+ line: lineOfIndex(content, match.index),
127
+ evidence: trimEvidence(match.evidence),
128
+ });
129
+ }
130
+ }
131
+ return found;
132
+ }
102
133
  async function scanSourceFilesForChannel(context, sourceFiles, includeCommonPatterns) {
103
134
  const results = [];
104
135
  const requiredPatterns = CHANNEL_SOURCE_PATTERNS[context.channel] ?? [];
@@ -333,7 +364,6 @@ function maskJavaScriptLikeSource(content) {
333
364
  maskRange(start, index);
334
365
  return index;
335
366
  };
336
- let scanCode;
337
367
  const scanTemplate = (start) => {
338
368
  maskAt(start);
339
369
  let index = start + 1;
@@ -361,7 +391,7 @@ function maskJavaScriptLikeSource(content) {
361
391
  }
362
392
  return index;
363
393
  };
364
- scanCode = (start, interpolation) => {
394
+ const scanCode = (start, interpolation) => {
365
395
  let index = start;
366
396
  let braceDepth = interpolation ? 1 : 0;
367
397
  while (index < content.length) {
@@ -1,7 +1,7 @@
1
1
  import type { BuildTool, EngineType, OutputFormat, Platform } from '../types.js';
2
- export type ValidationStage = 'prebuild-source' | 'base-output' | 'html-artifact' | 'delivery-package' | 'runtime-manual' | 'external-validator';
2
+ export type ValidationStage = 'prebuild-source' | 'base-output' | 'html-artifact' | 'delivery-package' | 'runtime-load' | 'runtime-manual' | 'external-validator';
3
3
  export type RuleSeverity = 'error' | 'warning' | 'info';
4
- export type RuleStatus = 'pass' | 'fail' | 'warning' | 'manual' | 'external' | 'not-applicable';
4
+ export type RuleStatus = 'pass' | 'fail' | 'warning' | 'manual' | 'external' | 'not-applicable' | 'inconclusive';
5
5
  export type RuleConfidence = 'high' | 'medium' | 'low';
6
6
  export type RuleOwner = 'playcraft-build' | 'playcraft-delivery' | 'devkit' | 'manual' | 'external';
7
7
  export type DeliveryMode = 'single-html' | 'zip' | 'html-backed-zip' | 'fragment-assets' | 'mixed';
@@ -110,3 +110,151 @@ export interface ValidationReport {
110
110
  manualChecklist: ManualChecklistItem[];
111
111
  externalChecklist: ExternalChecklistItem[];
112
112
  }
113
+ /** How a built artifact is stored on disk. */
114
+ export type ArtifactContainer = 'html' | 'zip' | 'directory';
115
+ /**
116
+ * How the playable is laid out inside its container: one self-contained HTML file,
117
+ * or an HTML entry with separate files (scripts, styles, assets).
118
+ */
119
+ export type ArtifactLayout = 'single-html' | 'multi-file';
120
+ export interface ArtifactFile {
121
+ /** Path inside the artifact, `/`-separated, without a leading slash. */
122
+ path: string;
123
+ /** Uncompressed size in bytes. */
124
+ size: number;
125
+ }
126
+ export interface PlayableArtifact {
127
+ /** Absolute path of the file or directory that was read. */
128
+ sourcePath: string;
129
+ container: ArtifactContainer;
130
+ layout: ArtifactLayout;
131
+ /** Entry HTML path inside the artifact. */
132
+ entry: string;
133
+ files: ArtifactFile[];
134
+ /** Bytes on disk: the HTML or ZIP file size, or the sum of the directory's files. */
135
+ totalSize: number;
136
+ /** `sha256:<hex>`; stable for the same content regardless of where it is stored. */
137
+ digest: string;
138
+ readText(filePath: string): Promise<string | null>;
139
+ readBuffer(filePath: string): Promise<Buffer | null>;
140
+ }
141
+ export type ArtifactReadErrorCode = 'artifact_not_found' | 'artifact_unsupported' | 'artifact_zip_invalid' | 'artifact_entry_missing' | 'artifact_entry_ambiguous' | 'artifact_entry_invalid';
142
+ /**
143
+ * Artifact QA stages. Each stage runs on its own: it depends only on the artifact and its own
144
+ * options, never on another stage's results, and produces its own report.
145
+ * - `base`: static checks of the artifact files.
146
+ * - `channel`: the channel's existing `html-artifact` / `delivery-package` rules.
147
+ * - `runtime-load`: opens the artifact in a headless browser.
148
+ * - `source-hints`: scans project source; advisory only.
149
+ */
150
+ export type QaStageId = 'base' | 'channel' | 'runtime-load' | 'source-hints';
151
+ export declare const QA_STAGES: readonly QaStageId[];
152
+ /**
153
+ * - `ran`: the stage ran; see its results.
154
+ * - `skipped`: the stage applies but could not run (e.g. no browser); the QA is incomplete.
155
+ * - `not-applicable`: the stage does not apply (e.g. channel rules for the `playcraft` profile).
156
+ */
157
+ export type QaStageStatus = 'ran' | 'skipped' | 'not-applicable';
158
+ export interface QaStageReport {
159
+ stage: QaStageId;
160
+ status: QaStageStatus;
161
+ /** Why the stage did not run. */
162
+ reason?: string;
163
+ /** Advisory stages never affect the overall summary or exit code. */
164
+ advisory: boolean;
165
+ durationMs: number;
166
+ summary: ValidationSummary;
167
+ results: ValidationResult[];
168
+ manualChecklist: ManualChecklistItem[];
169
+ externalChecklist: ExternalChecklistItem[];
170
+ /** Stage-specific facts, e.g. the size limit used by `base`. */
171
+ details?: Record<string, unknown>;
172
+ }
173
+ /**
174
+ * Report of `runArtifactQa`: one report per stage, and an overall summary of the non-advisory
175
+ * stages that ran. Kept apart from `ValidationReport` so the existing delivery validation output,
176
+ * which is checked against strict schemas, stays unchanged.
177
+ */
178
+ export interface ArtifactQaReport {
179
+ version: string;
180
+ rulesVersion: string;
181
+ profile: Platform;
182
+ artifact: {
183
+ sourcePath: string;
184
+ container: ArtifactContainer;
185
+ layout: ArtifactLayout;
186
+ entry: string;
187
+ digest: string;
188
+ totalSize: number;
189
+ fileCount: number;
190
+ };
191
+ summary: ValidationSummary;
192
+ /** A requested stage was skipped, so passing does not cover it. */
193
+ incomplete: boolean;
194
+ stages: QaStageReport[];
195
+ }
196
+ export interface ReadArtifactOptions {
197
+ /** Entry HTML path inside a ZIP or directory; defaults to `index.html`, then the only HTML file. */
198
+ entry?: string;
199
+ }
200
+ /**
201
+ * Browser access for runtime QA. `@playcraft/build` only defines this contract; hosts provide the
202
+ * implementation (the CLI uses playwright-core), so the build package does not depend on a browser.
203
+ */
204
+ export interface RuntimeViewport {
205
+ width: number;
206
+ height: number;
207
+ deviceScaleFactor: number;
208
+ isMobile: boolean;
209
+ hasTouch: boolean;
210
+ }
211
+ export interface RuntimeResource {
212
+ body: Buffer;
213
+ contentType: string;
214
+ }
215
+ export interface RuntimePageOptions {
216
+ /** Origin the artifact is served from, e.g. `http://artifact.playcraft-qa.test`. */
217
+ origin: string;
218
+ /** Entry path inside the artifact, served at `${origin}/${entryPath}`. */
219
+ entryPath: string;
220
+ viewport: RuntimeViewport;
221
+ userAgent: string;
222
+ /** Runs before any page script. */
223
+ initScript: string;
224
+ /** Content-Security-Policy header sent with HTML responses. */
225
+ contentSecurityPolicy: string;
226
+ /** Serves a path inside the artifact; null means the file does not exist (404). */
227
+ resolve(pathname: string): Promise<RuntimeResource | null>;
228
+ /** Navigation timeout in milliseconds. */
229
+ timeoutMs: number;
230
+ }
231
+ export interface RuntimePageEvents {
232
+ consoleErrors: string[];
233
+ pageErrors: string[];
234
+ /** Requests for artifact files that do not exist, or that failed to load. */
235
+ failedRequests: Array<{
236
+ url: string;
237
+ reason: string;
238
+ }>;
239
+ /** Requests to any host other than the artifact origin; they are blocked, never sent. */
240
+ blockedRequests: string[];
241
+ /** Top-level navigations away from the entry; they are blocked, never followed. */
242
+ navigationAttempts: string[];
243
+ }
244
+ export interface RuntimePage {
245
+ /** Whether the entry document loaded. */
246
+ loaded: boolean;
247
+ loadError?: string;
248
+ events(): RuntimePageEvents;
249
+ /** Evaluates a JavaScript expression in the page and returns its JSON-serializable result. */
250
+ evaluate<T>(expression: string): Promise<T>;
251
+ /** Dispatches a real pointer click at viewport coordinates. */
252
+ click(x: number, y: number): Promise<void>;
253
+ /** PNG screenshot of the viewport. */
254
+ screenshot(): Promise<Buffer>;
255
+ wait(ms: number): Promise<void>;
256
+ close(): Promise<void>;
257
+ }
258
+ export interface BrowserDriver {
259
+ open(options: RuntimePageOptions): Promise<RuntimePage>;
260
+ }
@@ -1 +1 @@
1
- export {};
1
+ export const QA_STAGES = ['base', 'channel', 'runtime-load', 'source-hints'];
@@ -1,5 +1,15 @@
1
1
  import type { Platform } from '../types.js';
2
- import type { ValidationContext, ValidationReport, ValidationStage } from './types.js';
2
+ import type { ExternalChecklistItem, ManualChecklistItem, QaStageId, QaStageReport, ValidationContext, ValidationReport, ValidationResult, ValidationStage, ValidationSummary } from './types.js';
3
3
  export declare const VALIDATION_REPORT_VERSION = "0.1.0";
4
4
  export declare function runPlayableValidation(context: ValidationContext, stages?: ValidationStage[]): Promise<ValidationReport>;
5
5
  export declare function runPlayableSourceValidationForChannels(context: Omit<ValidationContext, 'channel'>, channels: Platform[]): Promise<ValidationReport>;
6
+ export declare function summarize(results: ValidationResult[], manualChecklist: ManualChecklistItem[], externalChecklist: ExternalChecklistItem[]): ValidationSummary;
7
+ /** Builds the report of a QA stage that ran. */
8
+ export declare function ranStageReport(stage: QaStageId, startedAt: number, results: ValidationResult[], extra?: {
9
+ advisory?: boolean;
10
+ manualChecklist?: ManualChecklistItem[];
11
+ externalChecklist?: ExternalChecklistItem[];
12
+ details?: Record<string, unknown>;
13
+ }): QaStageReport;
14
+ /** Builds the report of a QA stage that did not run. */
15
+ export declare function idleStageReport(stage: QaStageId, status: 'skipped' | 'not-applicable', reason: string, advisory?: boolean): QaStageReport;
@@ -71,7 +71,7 @@ function buildExternalChecklist(rules, enabledStages) {
71
71
  owner: rule.owner,
72
72
  }));
73
73
  }
74
- function summarize(results, manualChecklist, externalChecklist) {
74
+ export function summarize(results, manualChecklist, externalChecklist) {
75
75
  const errorCount = results.filter((result) => result.status === 'fail').length;
76
76
  const warningCount = results.filter((result) => result.status === 'warning').length;
77
77
  const manualCount = manualChecklist.length;
@@ -94,3 +94,33 @@ function summarize(results, manualChecklist, externalChecklist) {
94
94
  externalCount,
95
95
  };
96
96
  }
97
+ /** Builds the report of a QA stage that ran. */
98
+ export function ranStageReport(stage, startedAt, results, extra = {}) {
99
+ const manualChecklist = extra.manualChecklist ?? [];
100
+ const externalChecklist = extra.externalChecklist ?? [];
101
+ return {
102
+ stage,
103
+ status: 'ran',
104
+ advisory: extra.advisory ?? false,
105
+ durationMs: Date.now() - startedAt,
106
+ summary: summarize(results, manualChecklist, externalChecklist),
107
+ results,
108
+ manualChecklist,
109
+ externalChecklist,
110
+ ...(extra.details ? { details: extra.details } : {}),
111
+ };
112
+ }
113
+ /** Builds the report of a QA stage that did not run. */
114
+ export function idleStageReport(stage, status, reason, advisory = false) {
115
+ return {
116
+ stage,
117
+ status,
118
+ reason,
119
+ advisory,
120
+ durationMs: 0,
121
+ summary: summarize([], [], []),
122
+ results: [],
123
+ manualChecklist: [],
124
+ externalChecklist: [],
125
+ };
126
+ }
@@ -11,6 +11,7 @@ export declare class SimpleZipReader {
11
11
  constructor(buffer: Buffer);
12
12
  listEntries(): ZipEntryInfo[];
13
13
  readEntryText(entryPath: string): string | null;
14
+ readEntryBuffer(entryPath: string): Buffer | null;
14
15
  private readCentralDirectory;
15
16
  private readEntryData;
16
17
  private findEndOfCentralDirectory;
@@ -4,14 +4,22 @@ export class SimpleZipReader {
4
4
  this.buffer = buffer;
5
5
  }
6
6
  listEntries() {
7
- return this.readCentralDirectory().map(({ localHeaderOffset: _offset, ...entry }) => entry);
7
+ return this.readCentralDirectory().map((entry) => ({
8
+ path: entry.path,
9
+ compressedSize: entry.compressedSize,
10
+ uncompressedSize: entry.uncompressedSize,
11
+ compressionMethod: entry.compressionMethod,
12
+ isDirectory: entry.isDirectory,
13
+ }));
8
14
  }
9
15
  readEntryText(entryPath) {
16
+ return this.readEntryBuffer(entryPath)?.toString('utf-8') ?? null;
17
+ }
18
+ readEntryBuffer(entryPath) {
10
19
  const entry = this.readCentralDirectory().find((item) => item.path === entryPath);
11
20
  if (!entry || entry.isDirectory)
12
21
  return null;
13
- const data = this.readEntryData(entry);
14
- return data.toString('utf-8');
22
+ return this.readEntryData(entry);
15
23
  }
16
24
  readCentralDirectory() {
17
25
  if (this.entriesCache)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playcraft/build",
3
- "version": "0.1.86",
3
+ "version": "0.1.88",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -30,6 +30,7 @@
30
30
  "README.md"
31
31
  ],
32
32
  "scripts": {
33
+ "lint": "pnpm -w run lint:eslint packages/build",
33
34
  "dev": "tsc -w",
34
35
  "build": "tsc",
35
36
  "release": "node ../../scripts/release-npm.mjs --packages=build --skip-install --skip-tests --skip-git-push"