fix: align script push metadata warning with generated locks (#8690)

This commit is contained in:
Kain
2026-04-03 13:41:31 +02:00
committed by GitHub
parent 5b7fa63bf1
commit 6656b46f10
2 changed files with 74 additions and 2 deletions

View File

@@ -24,12 +24,13 @@ import {
import { Workspace } from "../workspace/workspace.ts";
import {
checkifMetadataUptodate,
generateScriptHash,
generateScriptMetadataInternal,
getRawWorkspaceDependencies,
parseMetadataFile,
readLockfile,
} from "../../utils/metadata.ts";
import { generateHash, validateRequiredArgs } from "../../utils/utils.ts";
import { validateRequiredArgs } from "../../utils/utils.ts";
import {
WorkspaceDependenciesLanguage,
ScriptLanguage,
@@ -105,6 +106,16 @@ export function isFlowInlineScriptPath(filePath: string): boolean {
}
type PushOptions = GlobalOptions & { message?: string };
export async function computePushMetadataHash(
filePath: string,
content: string
): Promise<string> {
const remotePath = removeExtensionToPath(filePath).replaceAll(SEP, "/");
const metadataWithType = await parseMetadataFile(remotePath, undefined);
const metadataContent = await readFile(metadataWithType.path, "utf-8");
return await generateScriptHash({}, content, metadataContent);
}
async function push(opts: PushOptions, filePath: string) {
opts = await mergeConfigWithConfigFile(opts);
const workspace = await resolveWorkspace(opts);
@@ -130,7 +141,7 @@ async function push(opts: PushOptions, filePath: string) {
try {
const content = await readFile(filePath, "utf-8");
const remotePath = removeExtensionToPath(filePath).replaceAll(SEP, "/");
const contentHash = await generateHash(content + remotePath);
const contentHash = await computePushMetadataHash(filePath, content);
const conf = await readLockfile();
const hasLockEntry = conf.locks && (conf.locks[remotePath] !== undefined || conf.locks[`${remotePath}.ts`] !== undefined);
if (!hasLockEntry) {

View File

@@ -0,0 +1,61 @@
import { describe, expect, test } from "bun:test";
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { stringify as yamlStringify } from "yaml";
import { computePushMetadataHash } from "../src/commands/script/script.ts";
import { generateScriptHash, checkifMetadataUptodate } from "../src/utils/metadata.ts";
async function withTempDir(fn: (tempDir: string) => Promise<void>): Promise<void> {
const tempDir = await mkdtemp(path.join(os.tmpdir(), "wmill_push_meta_"));
const originalCwd = process.cwd();
try {
process.chdir(tempDir);
await fn(tempDir);
} finally {
process.chdir(originalCwd);
await rm(tempDir, { recursive: true, force: true });
}
}
describe("script push metadata hash", () => {
test("matches generate-metadata lock hash for a script with inline metadata", async () => {
await withTempDir(async (tempDir) => {
const scriptPath = path.join(tempDir, "f/test/example.ts");
const metadataPath = path.join(tempDir, "f/test/example.script.yaml");
await mkdir(path.dirname(scriptPath), { recursive: true });
await writeFile(
scriptPath,
'export async function main() { return "metadata"; }\n'
);
const metadataContent = yamlStringify({
summary: "test",
description: "",
lock: "",
kind: "script",
schema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {},
required: [],
},
});
await writeFile(metadataPath, metadataContent);
const scriptContent = await readFile(scriptPath, "utf-8");
const expectedHash = await generateScriptHash({}, scriptContent, metadataContent);
const pushHash = await computePushMetadataHash("f/test/example.ts", scriptContent);
expect(pushHash).toEqual(expectedHash);
expect(
await checkifMetadataUptodate("f/test/example", pushHash, {
version: "v2",
locks: { "f/test/example": pushHash },
})
).toBeTrue();
});
});
});