@dataparade/cli 0.0.26 → 0.0.28
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.
- package/README.md +52 -12
- package/dist/package.json +7 -7
- package/dist/src/ai-enrichment/providers/platform-proxy-provider.js +0 -1
- package/dist/src/cli.js +37 -37
- package/dist/src/discoveries/contract/ocsf-discovery-record.d.ts +1 -1
- package/dist/src/discoveries/load-ocsf-discoveries.d.ts +0 -8
- package/dist/src/discoveries/load-ocsf-discoveries.js +0 -31
- package/dist/src/discoveries/write-ocsf-discoveries.d.ts +0 -8
- package/dist/src/discoveries/write-ocsf-discoveries.js +0 -38
- package/dist/src/evals/run-model-matrix.js +0 -1
- package/dist/src/export/export-ocsf.d.ts +18 -0
- package/dist/src/export/export-ocsf.js +104 -0
- package/dist/src/export/register-export-commands.d.ts +2 -0
- package/dist/src/export/register-export-commands.js +30 -0
- package/dist/src/facts/apply-answers.d.ts +29 -0
- package/dist/src/facts/apply-answers.js +202 -0
- package/dist/src/facts/build-facts.d.ts +24 -0
- package/dist/src/facts/build-facts.js +198 -0
- package/dist/src/facts/io.d.ts +17 -0
- package/dist/src/facts/io.js +67 -0
- package/dist/src/facts/layout.d.ts +29 -0
- package/dist/src/facts/layout.js +38 -0
- package/dist/src/facts/schema.d.ts +265 -0
- package/dist/src/facts/schema.js +122 -0
- package/dist/src/facts/slug.d.ts +7 -0
- package/dist/src/facts/slug.js +47 -0
- package/dist/src/kb/brief.d.ts +1 -2
- package/dist/src/kb/brief.js +2 -4
- package/dist/src/kb/interview-record.d.ts +18 -0
- package/dist/src/kb/interview-record.js +70 -0
- package/dist/src/kb/kb-store.d.ts +24 -18
- package/dist/src/kb/kb-store.js +89 -28
- package/dist/src/kb/land.d.ts +32 -12
- package/dist/src/kb/land.js +97 -90
- package/dist/src/kb/migrate.d.ts +23 -0
- package/dist/src/kb/migrate.js +91 -0
- package/dist/src/kb/project-diagram.js +4 -4
- package/dist/src/kb/register-kb-commands.js +71 -20
- package/dist/src/observability/scan-sentry.js +0 -1
- package/dist/src/output/scan-output-excludes.d.ts +1 -1
- package/dist/src/output/scan-output-excludes.js +14 -5
- package/dist/src/output/write-scan-knowledge.d.ts +33 -0
- package/dist/src/output/write-scan-knowledge.js +115 -0
- package/dist/src/platform-api/estimate-scan-footprint.d.ts +31 -0
- package/dist/src/platform-api/estimate-scan-footprint.js +75 -2
- package/dist/src/ui/scan-progress-renderer.d.ts +2 -0
- package/dist/src/ui/scan-progress-renderer.js +10 -0
- package/dist/src/ui/teaching-lines.js +18 -6
- package/dist/src/upload/collect-scan-sidecars.d.ts +2 -27
- package/dist/src/upload/collect-scan-sidecars.js +25 -121
- package/dist/src/upload/run-upload.js +0 -3
- package/dist/tests/cli/cli.spec.js +30 -13
- package/dist/tests/helpers/facts-fixture.d.ts +9 -0
- package/dist/tests/helpers/facts-fixture.js +72 -0
- package/dist/tests/unit/export/export-ocsf.spec.js +72 -0
- package/dist/tests/unit/facts/apply-answers.spec.js +128 -0
- package/dist/tests/unit/facts/build-facts.spec.d.ts +1 -0
- package/dist/tests/unit/facts/build-facts.spec.js +161 -0
- package/dist/tests/unit/facts/slug.spec.d.ts +1 -0
- package/dist/tests/unit/facts/slug.spec.js +30 -0
- package/dist/tests/unit/kb/kb-interview-loop.spec.js +83 -35
- package/dist/tests/unit/kb/migrate.spec.d.ts +1 -0
- package/dist/tests/unit/kb/migrate.spec.js +108 -0
- package/dist/tests/unit/output/graphify-cache-dir.spec.d.ts +1 -0
- package/dist/tests/unit/output/graphify-cache-dir.spec.js +20 -0
- package/dist/tests/unit/output/scan-output-excludes.spec.js +4 -2
- package/dist/tests/unit/platform-api/estimate-scan-footprint.spec.js +77 -0
- package/dist/tests/unit/platform-api/upload-client.spec.js +2 -2
- package/dist/tests/unit/ui/teaching-lines.spec.js +16 -0
- package/dist/tests/unit/upload/collect-scan-sidecars.spec.js +46 -46
- package/dist/tests/unit/upload/pack-scan-upload.spec.js +6 -6
- package/dist/vendor/scanner/dist/src/classifier/component-factory.js +6 -14
- package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.d.ts +1 -0
- package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.js +32 -1
- package/dist/vendor/scanner/dist/src/classifier/naming.d.ts +5 -0
- package/dist/vendor/scanner/dist/src/classifier/naming.js +8 -4
- package/dist/vendor/scanner/dist/src/core/pipeline/build-scan-personal-data-layers.js +26 -14
- package/dist/vendor/scanner/dist/src/core/pipeline/enrich-personal-data-layers.js +3 -1
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.d.ts +18 -0
- package/dist/vendor/scanner/dist/src/data-flow/intra-component-lineage.js +125 -66
- package/dist/vendor/scanner/dist/src/index.d.ts +2 -0
- package/dist/vendor/scanner/dist/src/index.js +7 -2
- package/dist/vendor/scanner/dist/src/ingest/file-system.d.ts +5 -0
- package/dist/vendor/scanner/dist/src/ingest/file-system.js +5 -0
- package/dist/vendor/scanner/dist/src/ingest/gitignore.d.ts +17 -0
- package/dist/vendor/scanner/dist/src/ingest/gitignore.js +47 -9
- package/dist/vendor/scanner/dist/src/ingest/schema-files.d.ts +10 -2
- package/dist/vendor/scanner/dist/src/ingest/schema-files.js +24 -7
- package/dist/vendor/scanner/dist/src/patterns/frontend-frameworks.d.ts +3 -0
- package/dist/vendor/scanner/dist/src/patterns/frontend-frameworks.js +45 -0
- package/dist/vendor/scanner/dist/src/patterns/scan-exclusions.js +7 -0
- package/dist/vendor/scanner/patterns/classifier/third-party.classifier.yaml +8 -0
- package/dist/vendor/scanner/patterns/third-party.patterns.yaml +9 -0
- package/package.json +7 -7
- package/dist/src/discoveries/project-collected-diagram.d.ts +0 -7
- package/dist/src/discoveries/project-collected-diagram.js +0 -182
- package/dist/src/discoveries/project-ocsf-to-discoveries-document.d.ts +0 -16
- package/dist/src/discoveries/project-ocsf-to-discoveries-document.js +0 -269
- package/dist/src/discoveries/scan-result-to-discovery-input.d.ts +0 -13
- package/dist/src/discoveries/scan-result-to-discovery-input.js +0 -60
- package/dist/src/output/write-knowledge-graph.d.ts +0 -20
- package/dist/src/output/write-knowledge-graph.js +0 -53
- package/dist/src/output/write-scan-discoveries.d.ts +0 -17
- package/dist/src/output/write-scan-discoveries.js +0 -61
- package/dist/tests/unit/discoveries/project-collected-diagram.spec.js +0 -97
- package/dist/tests/unit/discoveries/rescan-replaces-scan-records.spec.js +0 -89
- /package/dist/tests/unit/{discoveries/project-collected-diagram.spec.d.ts → export/export-ocsf.spec.d.ts} +0 -0
- /package/dist/tests/unit/{discoveries/rescan-replaces-scan-records.spec.d.ts → facts/apply-answers.spec.d.ts} +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.appliedStateToScanInput = appliedStateToScanInput;
|
|
7
|
+
exports.exportOcsf = exportOcsf;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const scan_discovery_to_ocsf_1 = require("../discoveries/scan-discovery-to-ocsf");
|
|
11
|
+
const write_ocsf_discoveries_1 = require("../discoveries/write-ocsf-discoveries");
|
|
12
|
+
const interview_record_1 = require("../kb/interview-record");
|
|
13
|
+
const kb_store_1 = require("../kb/kb-store");
|
|
14
|
+
const slot_shapes_1 = require("../kb/slot-shapes");
|
|
15
|
+
/** Used when an interview answer has no brief or skill SHA (it was not landed by `kb land`). */
|
|
16
|
+
const NO_SHA = "0".repeat(40);
|
|
17
|
+
/** The applied state in the shape the OCSF record builders read. */
|
|
18
|
+
function appliedStateToScanInput(applied) {
|
|
19
|
+
return {
|
|
20
|
+
components: applied.facts.components.map((c) => ({
|
|
21
|
+
id: c.id,
|
|
22
|
+
name: c.name,
|
|
23
|
+
type: c.type,
|
|
24
|
+
subType: c.subType,
|
|
25
|
+
confidence: c.confidence,
|
|
26
|
+
sourceLocations: c.sourceLocations,
|
|
27
|
+
})),
|
|
28
|
+
dataFlows: applied.facts.dataFlows.map((f) => ({
|
|
29
|
+
id: f.id,
|
|
30
|
+
sourceComponentId: f.sourceComponentId,
|
|
31
|
+
targetComponentId: f.targetComponentId,
|
|
32
|
+
type: f.type,
|
|
33
|
+
confidence: f.confidence,
|
|
34
|
+
...(f.targetScope !== undefined ? { targetScope: f.targetScope } : {}),
|
|
35
|
+
})),
|
|
36
|
+
occurrences: applied.dataItemFiles.flatMap((file) => file.occurrences.map((o) => ({
|
|
37
|
+
id: o.id,
|
|
38
|
+
filePath: o.filePath,
|
|
39
|
+
startLine: o.startLine,
|
|
40
|
+
endLine: o.endLine,
|
|
41
|
+
...(o.code !== undefined ? { code: o.code } : {}),
|
|
42
|
+
labels: o.labels,
|
|
43
|
+
}))),
|
|
44
|
+
dataItems: applied.dataItemFiles.map((file) => ({
|
|
45
|
+
id: file.id,
|
|
46
|
+
occurrenceIds: file.occurrences.map((o) => o.id),
|
|
47
|
+
labels: file.labels,
|
|
48
|
+
})),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Write the knowledge base as OCSF Architecture Discovery records: the scan's Facts with
|
|
53
|
+
* the answers applied, plus one interview record for each interview answer that is still
|
|
54
|
+
* current. This is the only producer of OCSF records; the folder it writes is an export,
|
|
55
|
+
* not storage.
|
|
56
|
+
*/
|
|
57
|
+
function exportOcsf(paths, outDir) {
|
|
58
|
+
const state = (0, kb_store_1.loadKbState)(paths);
|
|
59
|
+
const resolvedOut = path_1.default.resolve(outDir);
|
|
60
|
+
if (resolvedOut === paths.dir || resolvedOut === path_1.default.dirname(paths.dir)) {
|
|
61
|
+
throw new Error(`--out must be a separate folder, not ${resolvedOut}.`);
|
|
62
|
+
}
|
|
63
|
+
const scanRecords = (0, scan_discovery_to_ocsf_1.landScanDiscoveryToOcsfRecords)(appliedStateToScanInput(state.applied), {
|
|
64
|
+
assertedAt: state.facts.scan.scannedAt,
|
|
65
|
+
});
|
|
66
|
+
const interviewRecords = [];
|
|
67
|
+
const previous = new Map();
|
|
68
|
+
const appliedIds = new Set(state.applied.applied.map((a) => a.id));
|
|
69
|
+
const ordered = [...state.answers].sort((a, b) => Date.parse(a.at) - Date.parse(b.at));
|
|
70
|
+
for (const answer of ordered) {
|
|
71
|
+
if (answer.source !== "interview" || answer.action !== "set" || !answer.property)
|
|
72
|
+
continue;
|
|
73
|
+
if (!appliedIds.has(answer.id) || !(0, slot_shapes_1.isInterviewSlot)(answer.property))
|
|
74
|
+
continue;
|
|
75
|
+
const key = `${answer.factId}\u0000${answer.property}`;
|
|
76
|
+
const uid = (0, interview_record_1.interviewRecordUid)(answer.id);
|
|
77
|
+
interviewRecords.push((0, interview_record_1.buildInterviewOcsfRecord)({
|
|
78
|
+
uid,
|
|
79
|
+
answer,
|
|
80
|
+
slot: answer.property,
|
|
81
|
+
briefSha: answer.briefSha ?? NO_SHA,
|
|
82
|
+
skillSha: answer.skillSha ?? NO_SHA,
|
|
83
|
+
supersedes: previous.get(key),
|
|
84
|
+
}));
|
|
85
|
+
previous.set(key, uid);
|
|
86
|
+
}
|
|
87
|
+
// An earlier export is replaced; anything else in the folder is not ours to delete.
|
|
88
|
+
if (fs_1.default.existsSync(resolvedOut)) {
|
|
89
|
+
const entries = fs_1.default.readdirSync(resolvedOut);
|
|
90
|
+
const foreign = entries.filter((name) => !name.endsWith(".json"));
|
|
91
|
+
if (foreign.length > 0) {
|
|
92
|
+
throw new Error(`${resolvedOut} holds files that are not OCSF records (${foreign[0]}); pick an empty folder.`);
|
|
93
|
+
}
|
|
94
|
+
for (const name of entries)
|
|
95
|
+
fs_1.default.rmSync(path_1.default.join(resolvedOut, name));
|
|
96
|
+
}
|
|
97
|
+
(0, write_ocsf_discoveries_1.writeOcsfDiscoveryRecordsToDir)([...scanRecords, ...interviewRecords], resolvedOut);
|
|
98
|
+
return {
|
|
99
|
+
outDir: resolvedOut,
|
|
100
|
+
records: scanRecords.length + interviewRecords.length,
|
|
101
|
+
scanRecords: scanRecords.length,
|
|
102
|
+
interviewRecords: interviewRecords.length,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerExportCommands = registerExportCommands;
|
|
4
|
+
const kb_store_1 = require("../kb/kb-store");
|
|
5
|
+
const layout_1 = require("../facts/layout");
|
|
6
|
+
const export_ocsf_1 = require("./export-ocsf");
|
|
7
|
+
function registerExportCommands(program) {
|
|
8
|
+
const exportCommand = program
|
|
9
|
+
.command('export')
|
|
10
|
+
.description('Export the knowledge base to other formats');
|
|
11
|
+
exportCommand
|
|
12
|
+
.command('ocsf')
|
|
13
|
+
.description('Write OCSF Architecture Discovery records from the knowledge base (facts with answers applied)')
|
|
14
|
+
.option('--kb <dir>', 'Knowledge base directory (where scan wrote dataflow.json)', layout_1.DEFAULT_OUTPUT_DIRNAME)
|
|
15
|
+
.requiredOption('--out <dir>', 'Folder for the OCSF records; an earlier export there is replaced')
|
|
16
|
+
.action((options) => {
|
|
17
|
+
try {
|
|
18
|
+
const result = (0, export_ocsf_1.exportOcsf)((0, kb_store_1.resolveKbPaths)(options.kb), options.out);
|
|
19
|
+
console.log(JSON.stringify({ ok: true, ...result }, null, 2));
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.log(JSON.stringify({
|
|
23
|
+
ok: false,
|
|
24
|
+
code: error instanceof kb_store_1.KbError ? error.code : 'ERROR',
|
|
25
|
+
error: error instanceof Error ? error.message : String(error),
|
|
26
|
+
}, null, 2));
|
|
27
|
+
process.exitCode = 1;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Answer, DataItemFile, FactsFile } from "./schema";
|
|
2
|
+
/** The Fact id for facts about the whole system, such as its confirmed scope. */
|
|
3
|
+
export declare const SYSTEM_FACT_ID = "dp:a0/system";
|
|
4
|
+
export interface AppliedState {
|
|
5
|
+
/** facts.json with the answers applied. Overridden properties may be added to any Fact. */
|
|
6
|
+
facts: FactsFile;
|
|
7
|
+
/** The data item files with the answers applied, in the order of `facts.dataItems`. */
|
|
8
|
+
dataItemFiles: DataItemFile[];
|
|
9
|
+
/** Answers about the system itself (`dp:a0/system`), by property. */
|
|
10
|
+
system: Record<string, unknown>;
|
|
11
|
+
/** Answers that were used. */
|
|
12
|
+
applied: Answer[];
|
|
13
|
+
/** Answers about a Fact the latest scan does not have. They stay in answers.json. */
|
|
14
|
+
orphans: Answer[];
|
|
15
|
+
/** Answers not used: a `set` on a removed Fact, a `set` without a property, or on a protected property. */
|
|
16
|
+
ignored: Answer[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The current state of the knowledge base: the latest scan's Facts with people's answers
|
|
20
|
+
* applied on top. Pure: nothing is read or written, and the inputs are not changed.
|
|
21
|
+
*
|
|
22
|
+
* - `set` overrides one property of a Fact. The latest answer for a property wins.
|
|
23
|
+
* - `remove` drops the Fact. A removed data item also drops its occurrences and its
|
|
24
|
+
* entry in facts.json; a removed component drops its data flows and every handledBy
|
|
25
|
+
* reference to it; a removed group drops its occurrences. A removal is final: later
|
|
26
|
+
* `set` answers about a removed Fact are ignored.
|
|
27
|
+
* - An answer about a Fact the scan no longer has is returned in `orphans`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function applyAnswers(factsInput: FactsFile, dataItemFilesInput: readonly DataItemFile[], answers: readonly Answer[]): AppliedState;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SYSTEM_FACT_ID = void 0;
|
|
4
|
+
exports.applyAnswers = applyAnswers;
|
|
5
|
+
/** The Fact id for facts about the whole system, such as its confirmed scope. */
|
|
6
|
+
exports.SYSTEM_FACT_ID = "dp:a0/system";
|
|
7
|
+
/** Properties an answer may not change: they identify the Fact or link it to the code. */
|
|
8
|
+
const PROTECTED_PROPERTIES = new Set(["id", "file", "codeRefs", "groups", "occurrences", "schemaVersion"]);
|
|
9
|
+
function clone(value) {
|
|
10
|
+
return JSON.parse(JSON.stringify(value));
|
|
11
|
+
}
|
|
12
|
+
function sortByTime(answers) {
|
|
13
|
+
return answers
|
|
14
|
+
.map((answer, index) => ({ answer, index, time: Date.parse(answer.at) }))
|
|
15
|
+
.sort((a, b) => (Number.isNaN(a.time) || Number.isNaN(b.time) ? 0 : a.time - b.time) || a.index - b.index)
|
|
16
|
+
.map((entry) => entry.answer);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The current state of the knowledge base: the latest scan's Facts with people's answers
|
|
20
|
+
* applied on top. Pure: nothing is read or written, and the inputs are not changed.
|
|
21
|
+
*
|
|
22
|
+
* - `set` overrides one property of a Fact. The latest answer for a property wins.
|
|
23
|
+
* - `remove` drops the Fact. A removed data item also drops its occurrences and its
|
|
24
|
+
* entry in facts.json; a removed component drops its data flows and every handledBy
|
|
25
|
+
* reference to it; a removed group drops its occurrences. A removal is final: later
|
|
26
|
+
* `set` answers about a removed Fact are ignored.
|
|
27
|
+
* - An answer about a Fact the scan no longer has is returned in `orphans`.
|
|
28
|
+
*/
|
|
29
|
+
function applyAnswers(factsInput, dataItemFilesInput, answers) {
|
|
30
|
+
const facts = clone(factsInput);
|
|
31
|
+
const files = clone([...dataItemFilesInput]);
|
|
32
|
+
const system = {};
|
|
33
|
+
const applied = [];
|
|
34
|
+
const orphans = [];
|
|
35
|
+
const ignored = [];
|
|
36
|
+
const components = new Map(facts.components.map((c) => [c.id, c]));
|
|
37
|
+
const flows = new Map(facts.dataFlows.map((f) => [f.id, f]));
|
|
38
|
+
const items = new Map(facts.dataItems.map((i) => [i.id, i]));
|
|
39
|
+
const fileOfItem = new Map(files.map((f) => [f.id, f]));
|
|
40
|
+
const kindById = new Map();
|
|
41
|
+
const groupOwner = new Map();
|
|
42
|
+
const occurrenceOwner = new Map();
|
|
43
|
+
for (const id of components.keys())
|
|
44
|
+
kindById.set(id, "component");
|
|
45
|
+
for (const id of flows.keys())
|
|
46
|
+
kindById.set(id, "dataFlow");
|
|
47
|
+
for (const item of facts.dataItems) {
|
|
48
|
+
kindById.set(item.id, "dataItem");
|
|
49
|
+
for (const group of item.groups) {
|
|
50
|
+
kindById.set(group.id, "group");
|
|
51
|
+
groupOwner.set(group.id, item.id);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const file of files) {
|
|
55
|
+
for (const group of file.groups) {
|
|
56
|
+
kindById.set(group.id, "group");
|
|
57
|
+
groupOwner.set(group.id, file.id);
|
|
58
|
+
}
|
|
59
|
+
for (const occurrence of file.occurrences) {
|
|
60
|
+
kindById.set(occurrence.id, "occurrence");
|
|
61
|
+
occurrenceOwner.set(occurrence.id, file.id);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
kindById.set(exports.SYSTEM_FACT_ID, "system");
|
|
65
|
+
const removed = new Set();
|
|
66
|
+
const groupsIn = (itemId, id) => [items.get(itemId)?.groups, fileOfItem.get(itemId)?.groups]
|
|
67
|
+
.flatMap((list) => list ?? [])
|
|
68
|
+
.filter((group) => group.id === id);
|
|
69
|
+
const dropRelations = (ids) => {
|
|
70
|
+
facts.relations = facts.relations.filter((r) => !ids.has(r.source) && !ids.has(r.target));
|
|
71
|
+
};
|
|
72
|
+
const removeFact = (id, kind) => {
|
|
73
|
+
const gone = new Set([id]);
|
|
74
|
+
if (kind === "component") {
|
|
75
|
+
facts.components = facts.components.filter((c) => c.id !== id);
|
|
76
|
+
for (const flow of facts.dataFlows) {
|
|
77
|
+
if (flow.sourceComponentId === id || flow.targetComponentId === id)
|
|
78
|
+
gone.add(flow.id);
|
|
79
|
+
}
|
|
80
|
+
facts.dataFlows = facts.dataFlows.filter((f) => !gone.has(f.id));
|
|
81
|
+
const strip = (groups) => {
|
|
82
|
+
for (const group of groups)
|
|
83
|
+
group.handledBy = group.handledBy.filter((c) => c !== id);
|
|
84
|
+
};
|
|
85
|
+
for (const item of facts.dataItems)
|
|
86
|
+
strip(item.groups);
|
|
87
|
+
for (const file of files)
|
|
88
|
+
strip(file.groups);
|
|
89
|
+
}
|
|
90
|
+
else if (kind === "dataFlow") {
|
|
91
|
+
facts.dataFlows = facts.dataFlows.filter((f) => f.id !== id);
|
|
92
|
+
}
|
|
93
|
+
else if (kind === "dataItem") {
|
|
94
|
+
facts.dataItems = facts.dataItems.filter((i) => i.id !== id);
|
|
95
|
+
const index = files.findIndex((f) => f.id === id);
|
|
96
|
+
if (index !== -1) {
|
|
97
|
+
for (const group of files[index].groups)
|
|
98
|
+
gone.add(group.id);
|
|
99
|
+
for (const occurrence of files[index].occurrences)
|
|
100
|
+
gone.add(occurrence.id);
|
|
101
|
+
files.splice(index, 1);
|
|
102
|
+
}
|
|
103
|
+
for (const group of items.get(id)?.groups ?? [])
|
|
104
|
+
gone.add(group.id);
|
|
105
|
+
}
|
|
106
|
+
else if (kind === "group") {
|
|
107
|
+
const owner = groupOwner.get(id);
|
|
108
|
+
const file = owner ? fileOfItem.get(owner) : undefined;
|
|
109
|
+
const item = owner ? items.get(owner) : undefined;
|
|
110
|
+
if (file) {
|
|
111
|
+
for (const occurrence of file.occurrences)
|
|
112
|
+
if (occurrence.groupId === id)
|
|
113
|
+
gone.add(occurrence.id);
|
|
114
|
+
file.occurrences = file.occurrences.filter((o) => o.groupId !== id);
|
|
115
|
+
file.groups = file.groups.filter((g) => g.id !== id);
|
|
116
|
+
}
|
|
117
|
+
if (item)
|
|
118
|
+
item.groups = item.groups.filter((g) => g.id !== id);
|
|
119
|
+
}
|
|
120
|
+
else if (kind === "occurrence") {
|
|
121
|
+
const file = fileOfItem.get(occurrenceOwner.get(id) ?? "");
|
|
122
|
+
if (file)
|
|
123
|
+
file.occurrences = file.occurrences.filter((o) => o.id !== id);
|
|
124
|
+
}
|
|
125
|
+
for (const goneId of gone)
|
|
126
|
+
removed.add(goneId);
|
|
127
|
+
dropRelations(gone);
|
|
128
|
+
};
|
|
129
|
+
const setProperty = (id, kind, property, value) => {
|
|
130
|
+
if (kind === "system") {
|
|
131
|
+
system[property] = value;
|
|
132
|
+
}
|
|
133
|
+
else if (kind === "component") {
|
|
134
|
+
components.get(id)[property] = value;
|
|
135
|
+
}
|
|
136
|
+
else if (kind === "dataFlow") {
|
|
137
|
+
flows.get(id)[property] = value;
|
|
138
|
+
}
|
|
139
|
+
else if (kind === "dataItem") {
|
|
140
|
+
items.get(id)[property] = value;
|
|
141
|
+
const file = fileOfItem.get(id);
|
|
142
|
+
if (file)
|
|
143
|
+
file[property] = value;
|
|
144
|
+
}
|
|
145
|
+
else if (kind === "group") {
|
|
146
|
+
const owner = groupOwner.get(id) ?? "";
|
|
147
|
+
for (const group of groupsIn(owner, id))
|
|
148
|
+
group[property] = value;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const file = fileOfItem.get(occurrenceOwner.get(id) ?? "");
|
|
152
|
+
const occurrence = file?.occurrences.find((o) => o.id === id);
|
|
153
|
+
if (occurrence)
|
|
154
|
+
occurrence[property] = value;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
for (const answer of sortByTime(answers)) {
|
|
158
|
+
const kind = kindById.get(answer.factId);
|
|
159
|
+
if (!kind) {
|
|
160
|
+
orphans.push(answer);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (removed.has(answer.factId)) {
|
|
164
|
+
ignored.push(answer);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (answer.action === "remove") {
|
|
168
|
+
if (kind === "system") {
|
|
169
|
+
ignored.push(answer);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
removeFact(answer.factId, kind);
|
|
173
|
+
applied.push(answer);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (!answer.property || PROTECTED_PROPERTIES.has(answer.property)) {
|
|
177
|
+
ignored.push(answer);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
setProperty(answer.factId, kind, answer.property, answer.value);
|
|
181
|
+
applied.push(answer);
|
|
182
|
+
}
|
|
183
|
+
// Counts follow what is left.
|
|
184
|
+
for (const file of files) {
|
|
185
|
+
const counts = new Map();
|
|
186
|
+
for (const occurrence of file.occurrences) {
|
|
187
|
+
if (occurrence.groupId)
|
|
188
|
+
counts.set(occurrence.groupId, (counts.get(occurrence.groupId) ?? 0) + 1);
|
|
189
|
+
}
|
|
190
|
+
for (const group of file.groups)
|
|
191
|
+
group.occurrenceCount = counts.get(group.id) ?? 0;
|
|
192
|
+
const item = items.get(file.id);
|
|
193
|
+
if (item) {
|
|
194
|
+
item.occurrenceCount = file.occurrences.length;
|
|
195
|
+
for (const group of item.groups)
|
|
196
|
+
group.occurrenceCount = counts.get(group.id) ?? 0;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const order = new Map(facts.dataItems.map((item, index) => [item.id, index]));
|
|
200
|
+
files.sort((a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0));
|
|
201
|
+
return { facts, dataItemFiles: files, system, applied, orphans, ignored };
|
|
202
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DataParadeGraph } from "../../vendor/scanner/dist/src/index.js";
|
|
2
|
+
import type { OrchestratorScanResult } from "../core/pipeline/orchestrator-result";
|
|
3
|
+
import { type DataItemFile, type FactsFile } from "./schema";
|
|
4
|
+
export interface BuildFactsInput {
|
|
5
|
+
result: OrchestratorScanResult;
|
|
6
|
+
/** The knowledge graph of the scan; without it, Facts have no code refs and no relations. */
|
|
7
|
+
graph?: DataParadeGraph;
|
|
8
|
+
/** Directory that was scanned. Ids and paths in the output are relative to it. */
|
|
9
|
+
scanRoot: string;
|
|
10
|
+
projectName: string;
|
|
11
|
+
cliVersion: string;
|
|
12
|
+
scannerVersion: string;
|
|
13
|
+
scannedAt?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface BuiltFacts {
|
|
16
|
+
facts: FactsFile;
|
|
17
|
+
/** File content by the file's path relative to the output folder (`data-items/<slug>.json`). */
|
|
18
|
+
dataItemFiles: Map<string, DataItemFile>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Turn a scan into Facts. Every Fact appears once: components, flows and data items
|
|
22
|
+
* (with their groups) in facts.json, occurrences in their data item's file.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildFacts(input: BuildFactsInput): BuiltFacts;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildFacts = buildFacts;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const slug_1 = require("./slug");
|
|
9
|
+
const schema_1 = require("./schema");
|
|
10
|
+
const DP_PREFIX = "dp::";
|
|
11
|
+
/** Links that point at code, or that the data item files and groups already carry. */
|
|
12
|
+
const NOT_RELATIONS = new Set(["occurrence_of", "part_of", "declared_by", "occurs_in", "defined_in", "observed_in"]);
|
|
13
|
+
function stripDp(id) {
|
|
14
|
+
return id.startsWith(DP_PREFIX) ? id.slice(DP_PREFIX.length) : id;
|
|
15
|
+
}
|
|
16
|
+
/** Removes the scan root from any string, so absolute paths never reach the output. */
|
|
17
|
+
function makeScrubber(scanRoot) {
|
|
18
|
+
const root = path_1.default.resolve(scanRoot).split(path_1.default.sep).join("/");
|
|
19
|
+
const prefixes = [`${root}/`];
|
|
20
|
+
// A scan of a single file has the file's folder as its root.
|
|
21
|
+
return (value) => {
|
|
22
|
+
let out = value.split(path_1.default.sep).join("/");
|
|
23
|
+
for (const prefix of prefixes) {
|
|
24
|
+
while (out.includes(prefix))
|
|
25
|
+
out = out.replace(prefix, "");
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function toRef(link, scrub) {
|
|
31
|
+
const kind = link.target_kind;
|
|
32
|
+
const file = link.target_file;
|
|
33
|
+
const line = link.target_line;
|
|
34
|
+
return {
|
|
35
|
+
nodeId: scrub(link.target),
|
|
36
|
+
...(typeof kind === "string" ? { kind } : {}),
|
|
37
|
+
...(typeof file === "string" ? { filePath: scrub(file) } : {}),
|
|
38
|
+
...(typeof line === "number" ? { line } : {}),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function mostCommon(values) {
|
|
42
|
+
const counts = new Map();
|
|
43
|
+
for (const value of values)
|
|
44
|
+
if (value)
|
|
45
|
+
counts.set(value, (counts.get(value) ?? 0) + 1);
|
|
46
|
+
return [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))[0]?.[0];
|
|
47
|
+
}
|
|
48
|
+
function byId(list) {
|
|
49
|
+
return list.sort((a, b) => a.id.localeCompare(b.id));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Turn a scan into Facts. Every Fact appears once: components, flows and data items
|
|
53
|
+
* (with their groups) in facts.json, occurrences in their data item's file.
|
|
54
|
+
*/
|
|
55
|
+
function buildFacts(input) {
|
|
56
|
+
const { result, graph, scanRoot } = input;
|
|
57
|
+
const scrub = makeScrubber(scanRoot);
|
|
58
|
+
// Code refs and handled-by, from the knowledge graph links.
|
|
59
|
+
const refs = new Map();
|
|
60
|
+
const handledBy = new Map();
|
|
61
|
+
const declaredBy = new Map();
|
|
62
|
+
const relations = [];
|
|
63
|
+
const relationKeys = new Set();
|
|
64
|
+
for (const link of graph?.links ?? []) {
|
|
65
|
+
const source = scrub(stripDp(link.source));
|
|
66
|
+
if (link.relation === "occurs_in" || link.relation === "defined_in" || link.relation === "observed_in") {
|
|
67
|
+
const list = refs.get(source) ?? [];
|
|
68
|
+
list.push(toRef(link, scrub));
|
|
69
|
+
refs.set(source, list);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (link.relation === "declared_by") {
|
|
73
|
+
const kind = link.declaration_kind;
|
|
74
|
+
declaredBy.set(source, { id: scrub(stripDp(link.target)), ...(typeof kind === "string" ? { kind } : {}) });
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (NOT_RELATIONS.has(link.relation))
|
|
78
|
+
continue;
|
|
79
|
+
const target = scrub(stripDp(link.target));
|
|
80
|
+
if (link.relation === "handled_by") {
|
|
81
|
+
const set = handledBy.get(source) ?? new Set();
|
|
82
|
+
set.add(target);
|
|
83
|
+
handledBy.set(source, set);
|
|
84
|
+
}
|
|
85
|
+
const key = `${link.relation}\u0000${source}\u0000${target}`;
|
|
86
|
+
if (relationKeys.has(key))
|
|
87
|
+
continue;
|
|
88
|
+
relationKeys.add(key);
|
|
89
|
+
relations.push({
|
|
90
|
+
type: link.relation,
|
|
91
|
+
source,
|
|
92
|
+
target,
|
|
93
|
+
confidence: link.confidence,
|
|
94
|
+
basis: typeof link.basis === "string" ? link.basis : null,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const refsFor = (id) => refs.get(id) ?? [];
|
|
98
|
+
const locations = (list) => (list ?? []).map((l) => ({ filePath: scrub(l.filePath), startLine: l.startLine, endLine: l.endLine }));
|
|
99
|
+
const components = result.scanResult.components.map((c) => ({
|
|
100
|
+
id: scrub(c.id),
|
|
101
|
+
name: c.name,
|
|
102
|
+
type: c.type,
|
|
103
|
+
subType: c.subType ?? "",
|
|
104
|
+
confidence: c.confidence,
|
|
105
|
+
sourceLocations: locations(c.sourceLocations),
|
|
106
|
+
codeRefs: refsFor(scrub(c.id)),
|
|
107
|
+
}));
|
|
108
|
+
const dataFlows = result.scanResult.dataFlows.map((f) => ({
|
|
109
|
+
id: scrub(f.id),
|
|
110
|
+
sourceComponentId: scrub(f.sourceComponentId),
|
|
111
|
+
targetComponentId: scrub(f.targetComponentId),
|
|
112
|
+
type: f.type,
|
|
113
|
+
...(f.targetScope !== undefined ? { targetScope: f.targetScope } : {}),
|
|
114
|
+
confidence: f.confidence,
|
|
115
|
+
sourceLocations: locations(f.sourceLocations ?? (f.sourceLocation ? [f.sourceLocation] : [])),
|
|
116
|
+
codeRefs: refsFor(scrub(f.id)),
|
|
117
|
+
}));
|
|
118
|
+
// Data items.
|
|
119
|
+
const occurrenceById = new Map(result.occurrences.map((o) => [o.id, o]));
|
|
120
|
+
const names = result.dataItems.map((item) => item.id.replace(/^data_item:/, ""));
|
|
121
|
+
const slugs = (0, slug_1.dataItemSlugs)(names);
|
|
122
|
+
const dataItems = [];
|
|
123
|
+
const dataItemFiles = new Map();
|
|
124
|
+
for (const item of result.dataItems) {
|
|
125
|
+
const name = item.id.replace(/^data_item:/, "");
|
|
126
|
+
const itemId = scrub(item.id);
|
|
127
|
+
const file = (0, slug_1.dataItemFilePath)(slugs.get(name) ?? "item");
|
|
128
|
+
const occurrences = item.occurrenceIds.flatMap((id) => {
|
|
129
|
+
const o = occurrenceById.get(id);
|
|
130
|
+
return o ? [o] : [];
|
|
131
|
+
});
|
|
132
|
+
const groupOf = new Map();
|
|
133
|
+
const groups = (item.groups ?? []).map((g) => {
|
|
134
|
+
const groupId = scrub(`group:${g.id}`);
|
|
135
|
+
for (const id of g.occurrenceIds)
|
|
136
|
+
groupOf.set(id, groupId);
|
|
137
|
+
const basis = mostCommon(g.occurrenceIds.map((id) => occurrenceById.get(id)?.groupBasis));
|
|
138
|
+
const prefix = `${name}:`;
|
|
139
|
+
return {
|
|
140
|
+
id: groupId,
|
|
141
|
+
label: g.id.startsWith(prefix) ? g.id.slice(prefix.length) || g.id : g.id,
|
|
142
|
+
...(basis ? { basis } : {}),
|
|
143
|
+
occurrenceCount: g.occurrenceIds.length,
|
|
144
|
+
handledBy: [...(handledBy.get(groupId) ?? [])].sort(),
|
|
145
|
+
};
|
|
146
|
+
});
|
|
147
|
+
const occurrenceFacts = occurrences.map((o) => {
|
|
148
|
+
const id = scrub(o.id);
|
|
149
|
+
const declared = declaredBy.get(id);
|
|
150
|
+
return {
|
|
151
|
+
id,
|
|
152
|
+
groupId: groupOf.get(o.id) ?? null,
|
|
153
|
+
filePath: scrub(o.filePath),
|
|
154
|
+
startLine: o.startLine,
|
|
155
|
+
endLine: o.endLine,
|
|
156
|
+
...(o.code !== undefined ? { code: o.code } : {}),
|
|
157
|
+
labels: [...o.labels],
|
|
158
|
+
...(o.location ? { location: o.location } : {}),
|
|
159
|
+
codeRefs: refsFor(id),
|
|
160
|
+
...(declared ? { declaredBy: declared } : {}),
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
occurrenceFacts.sort((a, b) => a.filePath.localeCompare(b.filePath) || a.startLine - b.startLine || a.id.localeCompare(b.id));
|
|
164
|
+
dataItems.push({
|
|
165
|
+
id: itemId,
|
|
166
|
+
name,
|
|
167
|
+
labels: [...item.labels],
|
|
168
|
+
occurrenceCount: occurrenceFacts.length,
|
|
169
|
+
file,
|
|
170
|
+
groups: byId(groups),
|
|
171
|
+
});
|
|
172
|
+
dataItemFiles.set(file, {
|
|
173
|
+
schemaVersion: schema_1.SCHEMA_VERSION,
|
|
174
|
+
id: itemId,
|
|
175
|
+
name,
|
|
176
|
+
labels: [...item.labels],
|
|
177
|
+
groups: byId(groups.map((g) => ({ ...g }))),
|
|
178
|
+
occurrences: occurrenceFacts,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
relations.sort((a, b) => a.type.localeCompare(b.type) || a.source.localeCompare(b.source) || a.target.localeCompare(b.target));
|
|
182
|
+
return {
|
|
183
|
+
facts: {
|
|
184
|
+
schemaVersion: schema_1.SCHEMA_VERSION,
|
|
185
|
+
scan: {
|
|
186
|
+
projectName: input.projectName,
|
|
187
|
+
scannedAt: input.scannedAt ?? new Date().toISOString(),
|
|
188
|
+
cliVersion: input.cliVersion,
|
|
189
|
+
scannerVersion: input.scannerVersion,
|
|
190
|
+
},
|
|
191
|
+
components: byId(components),
|
|
192
|
+
dataFlows: byId(dataFlows),
|
|
193
|
+
dataItems: byId(dataItems),
|
|
194
|
+
relations,
|
|
195
|
+
},
|
|
196
|
+
dataItemFiles,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Answer, type AnswersFile, type DataItemFile, type FactsFile } from "./schema";
|
|
2
|
+
import type { OutputLayout } from "./layout";
|
|
3
|
+
export declare class FactsError extends Error {
|
|
4
|
+
readonly code: string;
|
|
5
|
+
constructor(code: string, message: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Write the Facts of a scan. facts.json and data-items/ are replaced as a whole, so
|
|
9
|
+
* nothing from an earlier scan stays behind. answers.json is never read or written here.
|
|
10
|
+
*/
|
|
11
|
+
export declare function writeFactsOutput(layout: OutputLayout, facts: FactsFile, dataItemFiles: Map<string, DataItemFile>): void;
|
|
12
|
+
export declare function readFacts(layout: OutputLayout): FactsFile;
|
|
13
|
+
/** All data item files named by facts.json, in the order of facts.dataItems. */
|
|
14
|
+
export declare function readDataItemFiles(layout: OutputLayout, facts: FactsFile): DataItemFile[];
|
|
15
|
+
export declare function readAnswers(layout: OutputLayout): AnswersFile;
|
|
16
|
+
/** Append answers to answers.json (created when missing). Existing answers are kept as they are. */
|
|
17
|
+
export declare function appendAnswers(layout: OutputLayout, answers: Answer[]): AnswersFile;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FactsError = void 0;
|
|
7
|
+
exports.writeFactsOutput = writeFactsOutput;
|
|
8
|
+
exports.readFacts = readFacts;
|
|
9
|
+
exports.readDataItemFiles = readDataItemFiles;
|
|
10
|
+
exports.readAnswers = readAnswers;
|
|
11
|
+
exports.appendAnswers = appendAnswers;
|
|
12
|
+
const fs_1 = __importDefault(require("fs"));
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const schema_1 = require("./schema");
|
|
15
|
+
class FactsError extends Error {
|
|
16
|
+
constructor(code, message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.code = code;
|
|
19
|
+
this.name = "FactsError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.FactsError = FactsError;
|
|
23
|
+
function writeJson(filePath, value) {
|
|
24
|
+
fs_1.default.mkdirSync(path_1.default.dirname(filePath), { recursive: true });
|
|
25
|
+
fs_1.default.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Write the Facts of a scan. facts.json and data-items/ are replaced as a whole, so
|
|
29
|
+
* nothing from an earlier scan stays behind. answers.json is never read or written here.
|
|
30
|
+
*/
|
|
31
|
+
function writeFactsOutput(layout, facts, dataItemFiles) {
|
|
32
|
+
schema_1.factsFileSchema.parse(facts);
|
|
33
|
+
fs_1.default.rmSync(layout.dataItemsDir, { recursive: true, force: true });
|
|
34
|
+
fs_1.default.mkdirSync(layout.dataItemsDir, { recursive: true });
|
|
35
|
+
for (const [relativePath, file] of dataItemFiles) {
|
|
36
|
+
writeJson(path_1.default.join(layout.dir, relativePath), schema_1.dataItemFileSchema.parse(file));
|
|
37
|
+
}
|
|
38
|
+
writeJson(layout.factsPath, facts);
|
|
39
|
+
}
|
|
40
|
+
function readFacts(layout) {
|
|
41
|
+
if (!fs_1.default.existsSync(layout.factsPath)) {
|
|
42
|
+
throw new FactsError("NO_KB", `No facts.json in ${layout.dir}. Scan first: dataparade scan <repo> -o ${layout.dataflowPath}`);
|
|
43
|
+
}
|
|
44
|
+
return schema_1.factsFileSchema.parse(JSON.parse(fs_1.default.readFileSync(layout.factsPath, "utf8")));
|
|
45
|
+
}
|
|
46
|
+
/** All data item files named by facts.json, in the order of facts.dataItems. */
|
|
47
|
+
function readDataItemFiles(layout, facts) {
|
|
48
|
+
return facts.dataItems.map((item) => {
|
|
49
|
+
const filePath = path_1.default.join(layout.dir, item.file);
|
|
50
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
51
|
+
throw new FactsError("MISSING_DATA_ITEM_FILE", `facts.json names ${item.file}, which does not exist in ${layout.dir}.`);
|
|
52
|
+
}
|
|
53
|
+
return schema_1.dataItemFileSchema.parse(JSON.parse(fs_1.default.readFileSync(filePath, "utf8")));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function readAnswers(layout) {
|
|
57
|
+
if (!fs_1.default.existsSync(layout.answersPath))
|
|
58
|
+
return { schemaVersion: schema_1.SCHEMA_VERSION, answers: [] };
|
|
59
|
+
return schema_1.answersFileSchema.parse(JSON.parse(fs_1.default.readFileSync(layout.answersPath, "utf8")));
|
|
60
|
+
}
|
|
61
|
+
/** Append answers to answers.json (created when missing). Existing answers are kept as they are. */
|
|
62
|
+
function appendAnswers(layout, answers) {
|
|
63
|
+
const file = readAnswers(layout);
|
|
64
|
+
const next = { schemaVersion: schema_1.SCHEMA_VERSION, answers: [...file.answers, ...answers] };
|
|
65
|
+
writeJson(layout.answersPath, schema_1.answersFileSchema.parse(next));
|
|
66
|
+
return next;
|
|
67
|
+
}
|