-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff-component-contract.tool.ts
More file actions
111 lines (95 loc) · 3.09 KB
/
Copy pathdiff-component-contract.tool.ts
File metadata and controls
111 lines (95 loc) · 3.09 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
createHandler,
BaseHandlerOptions,
} from '../../shared/utils/handler-helpers.js';
import {
resolveCrossPlatformPath,
normalizePathsInObject,
} from '../../shared/utils/cross-platform-path.js';
import {
OUTPUT_SUBDIRS,
resolveDefaultSaveLocation,
} from '../../shared/constants.js';
import { diffComponentContractSchema } from './models/schema.js';
import type { DomPathDictionary } from '../shared/models/types.js';
import { loadContract } from '../shared/utils/contract-file-ops.js';
import {
consolidateAndPruneRemoveOperationsWithDeduplication,
groupChangesByDomainAndType,
generateDiffSummary,
} from './utils/diff-utils.js';
import { writeFile, mkdir } from 'node:fs/promises';
import { dirname } from 'node:path';
import diff from 'microdiff';
interface DiffComponentContractOptions extends BaseHandlerOptions {
saveLocation?: string;
contractBeforePath: string;
contractAfterPath: string;
dsComponentName?: string;
}
export const diffComponentContractHandler = createHandler<
DiffComponentContractOptions,
{
fileUrl: string;
domPathStats?: DomPathDictionary['stats'];
}
>(
diffComponentContractSchema.name,
async (params, { cwd, workspaceRoot }) => {
const {
saveLocation,
contractBeforePath,
contractAfterPath,
dsComponentName = '',
} = params;
const effectiveBeforePath = resolveCrossPlatformPath(
cwd,
contractBeforePath,
);
const effectiveAfterPath = resolveCrossPlatformPath(cwd, contractAfterPath);
const defaultSaveLocation = resolveDefaultSaveLocation(
saveLocation,
OUTPUT_SUBDIRS.CONTRACT_DIFFS,
contractBeforePath,
'-diff.json',
'-contract.json',
);
const contractBefore = await loadContract(effectiveBeforePath);
const contractAfter = await loadContract(effectiveAfterPath);
const rawDiffResult = diff(contractBefore, contractAfter);
const { processedResult, domPathDict } =
consolidateAndPruneRemoveOperationsWithDeduplication(rawDiffResult);
const groupedChanges = groupChangesByDomainAndType(processedResult);
const diffData = {
before: effectiveBeforePath,
after: effectiveAfterPath,
dsComponentName,
timestamp: new Date().toISOString(),
domPathDictionary: domPathDict.paths,
changes: groupedChanges,
summary: generateDiffSummary(processedResult, groupedChanges),
};
const normalizedDiffData = normalizePathsInObject(diffData, workspaceRoot);
const effectiveSaveLocation = resolveCrossPlatformPath(
cwd,
defaultSaveLocation,
);
await mkdir(dirname(effectiveSaveLocation), { recursive: true });
const diffFilePath = effectiveSaveLocation;
const formattedJson = JSON.stringify(normalizedDiffData, null, 2);
await writeFile(diffFilePath, formattedJson, 'utf-8');
return {
fileUrl: `file://${diffFilePath}`,
domPathStats: domPathDict.stats,
};
},
(result) => {
return [result.fileUrl];
},
);
export const diffComponentContractTools = [
{
schema: diffComponentContractSchema,
handler: diffComponentContractHandler,
},
];