210c66b5dd
## Refactor app-dev state management and build utilities - Store only `manifest` in `AppDevState` instead of full `ManifestBuildResult`; add `sourcePath` to `FileStatus` - Pass `sourcePaths` directly to watchers instead of `ManifestBuildResult` - Only reset `fileUploadStatus` for functions/components when their source paths change - Add pure `updateManifestChecksum` utility that returns a new manifest without side effects - Extract `processEsbuildResult` to deduplicate build result processing between watchers - Rename `serverlessFunctions` → `functions` in SDK code (API unchanged) - Extract `writeManifestToOutput` to shared `manifest-writer.ts`
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import crypto from 'crypto';
|
|
import type * as esbuild from 'esbuild';
|
|
import * as fs from 'fs-extra';
|
|
import path from 'path';
|
|
import { type OnFileBuiltCallback } from './restartable-watcher.interface';
|
|
|
|
export type ProcessEsbuildResultParams = {
|
|
result: esbuild.BuildResult;
|
|
outputDir: string;
|
|
builtDir: string;
|
|
lastChecksums: Map<string, string>;
|
|
onFileBuilt?: OnFileBuiltCallback;
|
|
onSuccess: (relativePath: string) => void;
|
|
};
|
|
|
|
export type ProcessEsbuildResultOutput = {
|
|
hasChanges: boolean;
|
|
};
|
|
|
|
export const processEsbuildResult = async ({
|
|
result,
|
|
outputDir,
|
|
builtDir,
|
|
lastChecksums,
|
|
onFileBuilt,
|
|
onSuccess,
|
|
}: ProcessEsbuildResultParams): Promise<ProcessEsbuildResultOutput> => {
|
|
const outputFiles = Object.keys(result.metafile?.outputs ?? {})
|
|
.filter((file) => file.endsWith('.mjs'));
|
|
|
|
let hasChanges = false;
|
|
|
|
for (const outputFile of outputFiles) {
|
|
const absoluteOutputFile = path.resolve(outputFile);
|
|
const relativePath = path.relative(outputDir, absoluteOutputFile);
|
|
const builtPath = `${builtDir}/${relativePath}`;
|
|
|
|
const content = await fs.readFile(absoluteOutputFile);
|
|
const checksum = crypto.createHash('md5').update(content).digest('hex');
|
|
|
|
const lastChecksum = lastChecksums.get(builtPath);
|
|
if (lastChecksum === checksum) {
|
|
continue;
|
|
}
|
|
|
|
hasChanges = true;
|
|
lastChecksums.set(builtPath, checksum);
|
|
onSuccess(relativePath);
|
|
|
|
if (onFileBuilt) {
|
|
onFileBuilt(builtPath, checksum);
|
|
}
|
|
}
|
|
|
|
return { hasChanges };
|
|
};
|