-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathref-advancement.ts
More file actions
68 lines (63 loc) · 2.07 KB
/
ref-advancement.ts
File metadata and controls
68 lines (63 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { readFile } from 'node:fs/promises';
import type { ContractIR } from '@prisma-next/migration-tools/refs';
import { writeRefPaired } from '@prisma-next/migration-tools/refs';
import { ifDefined } from '@prisma-next/utils/defined';
export interface RefAdvancementFields {
readonly advancedRef: { readonly name: string; readonly hash: string } | null;
readonly plannedAdvanceRef: { readonly name: string; readonly hash: string } | null;
}
export function computeRefAdvancementName(options: {
readonly advanceRef?: string;
readonly db?: string;
}): string | null {
if (options.advanceRef !== undefined) {
return options.advanceRef;
}
if (options.db === undefined) {
return 'db';
}
return null;
}
export async function readContractIR(
contractJson: Record<string, unknown>,
contractJsonPath: string,
): Promise<ContractIR> {
const contractDtsPath = contractJsonPath.replace(/\.json$/i, '.d.ts');
const contractDts = await readFile(contractDtsPath, 'utf-8');
return { contract: contractJson, contractDts };
}
export async function executeRefAdvancement(
refsDir: string,
name: string,
hash: string,
contractIR: ContractIR,
): Promise<{ name: string; hash: string }> {
await writeRefPaired(refsDir, name, { hash, invariants: [] }, contractIR);
return { name, hash };
}
export async function buildRefAdvancementFields(options: {
readonly advanceRef?: string;
readonly db?: string;
readonly refsDir: string;
readonly contractIR: ContractIR;
readonly mode: 'plan' | 'apply';
readonly hash: string;
}): Promise<RefAdvancementFields> {
const name = computeRefAdvancementName({
...ifDefined('advanceRef', options.advanceRef),
...ifDefined('db', options.db),
});
if (name === null) {
return { advancedRef: null, plannedAdvanceRef: null };
}
if (options.mode === 'plan') {
return { advancedRef: null, plannedAdvanceRef: { name, hash: options.hash } };
}
const advancedRef = await executeRefAdvancement(
options.refsDir,
name,
options.hash,
options.contractIR,
);
return { advancedRef, plannedAdvanceRef: null };
}