sf-plugin-data 0.1.0 → 0.3.0
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/LICENSE +21 -21
- package/README.md +618 -240
- package/docs/artifact-format.md +528 -633
- package/docs/capabilities.md +1160 -168
- package/docs/git-workflow.md +135 -12
- package/docs/handover.md +7826 -12
- package/lib/api.d.ts +13 -0
- package/lib/api.js +10 -0
- package/lib/artifacts/code-fields.d.ts +49 -0
- package/lib/artifacts/code-fields.js +142 -0
- package/lib/artifacts/data-graph-document.d.ts +29 -0
- package/lib/artifacts/data-graph-document.js +52 -0
- package/lib/artifacts/data-space-field.d.ts +65 -0
- package/lib/artifacts/data-space-field.js +98 -0
- package/lib/artifacts/data-stream-document.d.ts +264 -0
- package/lib/artifacts/data-stream-document.js +680 -0
- package/lib/artifacts/data-transform-document.d.ts +152 -0
- package/lib/artifacts/data-transform-document.js +560 -0
- package/lib/artifacts/dependency-selection.d.ts +51 -0
- package/lib/artifacts/dependency-selection.js +154 -0
- package/lib/artifacts/dlo-document.d.ts +93 -0
- package/lib/artifacts/dlo-document.js +253 -0
- package/lib/artifacts/enum-case.d.ts +39 -0
- package/lib/artifacts/enum-case.js +67 -0
- package/lib/artifacts/git-deploy-selection.d.ts +68 -0
- package/lib/artifacts/git-deploy-selection.js +474 -0
- package/lib/artifacts/identity-resolution-document.d.ts +133 -0
- package/lib/artifacts/identity-resolution-document.js +625 -0
- package/lib/artifacts/mapping-document.d.ts +1 -0
- package/lib/artifacts/mapping-document.js +1 -0
- package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
- package/lib/artifacts/native-calculated-insight-document.js +321 -0
- package/lib/artifacts/native-data-graph-document.d.ts +124 -0
- package/lib/artifacts/native-data-graph-document.js +780 -0
- package/lib/artifacts/native-dmo-document.d.ts +151 -0
- package/lib/artifacts/native-dmo-document.js +313 -0
- package/lib/artifacts/native-mapping-document.d.ts +67 -0
- package/lib/artifacts/native-mapping-document.js +187 -0
- package/lib/artifacts/native-relationship-document.d.ts +76 -0
- package/lib/artifacts/native-relationship-document.js +232 -0
- package/lib/artifacts/path-selector.d.ts +55 -0
- package/lib/artifacts/path-selector.js +167 -0
- package/lib/artifacts/read-deploy-input.d.ts +222 -6
- package/lib/artifacts/read-deploy-input.js +1213 -53
- package/lib/artifacts/segment-document.d.ts +183 -0
- package/lib/artifacts/segment-document.js +570 -0
- package/lib/artifacts/verbatim-document.d.ts +203 -0
- package/lib/artifacts/verbatim-document.js +339 -0
- package/lib/artifacts/write-data-graph.d.ts +17 -0
- package/lib/artifacts/write-data-graph.js +162 -0
- package/lib/artifacts/write-data-stream.d.ts +11 -0
- package/lib/artifacts/write-data-stream.js +175 -0
- package/lib/artifacts/write-data-transform.d.ts +11 -0
- package/lib/artifacts/write-data-transform.js +180 -0
- package/lib/artifacts/write-dlo.d.ts +11 -0
- package/lib/artifacts/write-dlo.js +158 -0
- package/lib/artifacts/write-identity-resolution.d.ts +18 -0
- package/lib/artifacts/write-identity-resolution.js +165 -0
- package/lib/artifacts/write-mapping-definition.d.ts +17 -0
- package/lib/artifacts/write-mapping-definition.js +127 -0
- package/lib/artifacts/write-member-export.d.ts +88 -0
- package/lib/artifacts/write-member-export.js +199 -0
- package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
- package/lib/artifacts/write-native-calculated-insight.js +176 -0
- package/lib/artifacts/write-native-dmo.d.ts +11 -0
- package/lib/artifacts/write-native-dmo.js +158 -0
- package/lib/artifacts/write-native-mapping.d.ts +11 -0
- package/lib/artifacts/write-native-mapping.js +157 -0
- package/lib/artifacts/write-native-relationship.d.ts +11 -0
- package/lib/artifacts/write-native-relationship.js +157 -0
- package/lib/artifacts/write-segment.d.ts +26 -0
- package/lib/artifacts/write-segment.js +212 -0
- package/lib/commands/data360/delete.d.ts +280 -0
- package/lib/commands/data360/delete.js +2347 -0
- package/lib/commands/data360/deploy.d.ts +235 -16
- package/lib/commands/data360/deploy.js +2339 -102
- package/lib/commands/data360/list/dataspace.d.ts +53 -0
- package/lib/commands/data360/list/dataspace.js +109 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +298 -0
- package/lib/commands/data360/retrieve.d.ts +283 -14
- package/lib/commands/data360/retrieve.js +1945 -95
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +320 -0
- package/lib/errors/codes/calculated-insight.d.ts +2 -0
- package/lib/errors/codes/calculated-insight.js +25 -0
- package/lib/errors/codes/data-graph.d.ts +2 -0
- package/lib/errors/codes/data-graph.js +22 -0
- package/lib/errors/codes/data-stream.d.ts +2 -0
- package/lib/errors/codes/data-stream.js +70 -0
- package/lib/errors/codes/data-transform.d.ts +2 -0
- package/lib/errors/codes/data-transform.js +35 -0
- package/lib/errors/codes/dependency.d.ts +2 -0
- package/lib/errors/codes/dependency.js +11 -0
- package/lib/errors/codes/dlo.d.ts +2 -0
- package/lib/errors/codes/dlo.js +31 -0
- package/lib/errors/codes/dmo.d.ts +2 -0
- package/lib/errors/codes/dmo.js +28 -0
- package/lib/errors/codes/general.d.ts +2 -0
- package/lib/errors/codes/general.js +58 -0
- package/lib/errors/codes/identity-resolution.d.ts +2 -0
- package/lib/errors/codes/identity-resolution.js +24 -0
- package/lib/errors/codes/mapping.d.ts +2 -0
- package/lib/errors/codes/mapping.js +42 -0
- package/lib/errors/codes/relationship.d.ts +2 -0
- package/lib/errors/codes/relationship.js +21 -0
- package/lib/errors/codes/segment.d.ts +2 -0
- package/lib/errors/codes/segment.js +49 -0
- package/lib/errors/registry.d.ts +60 -0
- package/lib/errors/registry.js +123 -0
- package/lib/exit-code.d.ts +10 -0
- package/lib/exit-code.js +24 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +3 -1
- package/lib/services/acquire-retrieve-selection.d.ts +43 -0
- package/lib/services/acquire-retrieve-selection.js +145 -0
- package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
- package/lib/services/bind-identity-resolution-dependencies.js +238 -0
- package/lib/services/bind-segment-dependencies.d.ts +49 -0
- package/lib/services/bind-segment-dependencies.js +115 -0
- package/lib/services/bounded-reads.d.ts +17 -0
- package/lib/services/bounded-reads.js +42 -0
- package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
- package/lib/services/calculated-insight-sql-checks.js +193 -0
- package/lib/services/command-dlo.d.ts +48 -0
- package/lib/services/command-dlo.js +103 -0
- package/lib/services/command-native-data-graph.d.ts +54 -0
- package/lib/services/command-native-data-graph.js +485 -0
- package/lib/services/command-native-data-stream.d.ts +104 -0
- package/lib/services/command-native-data-stream.js +257 -0
- package/lib/services/command-native-dmo.d.ts +124 -0
- package/lib/services/command-native-dmo.js +338 -0
- package/lib/services/command-native-mapping.d.ts +65 -0
- package/lib/services/command-native-mapping.js +129 -0
- package/lib/services/command-native-relationship.d.ts +46 -0
- package/lib/services/command-native-relationship.js +103 -0
- package/lib/services/cross-dataspace-deploy.d.ts +111 -0
- package/lib/services/cross-dataspace-deploy.js +493 -0
- package/lib/services/cross-dataspace-rename.d.ts +74 -0
- package/lib/services/cross-dataspace-rename.js +1314 -0
- package/lib/services/cross-dataspace-sql.d.ts +109 -0
- package/lib/services/cross-dataspace-sql.js +1301 -0
- package/lib/services/cross-space-context.d.ts +23 -0
- package/lib/services/cross-space-context.js +38 -0
- package/lib/services/crud-plan.d.ts +137 -0
- package/lib/services/crud-plan.js +408 -0
- package/lib/services/data-space-check.d.ts +92 -0
- package/lib/services/data-space-check.js +169 -0
- package/lib/services/data-space-prefix.d.ts +77 -0
- package/lib/services/data-space-prefix.js +127 -0
- package/lib/services/data-transform-space.d.ts +31 -0
- package/lib/services/data-transform-space.js +79 -0
- package/lib/services/delete-calculated-insight.d.ts +44 -0
- package/lib/services/delete-calculated-insight.js +101 -0
- package/lib/services/delete-data-transform.d.ts +52 -0
- package/lib/services/delete-data-transform.js +222 -0
- package/lib/services/delete-dmo-field.d.ts +211 -0
- package/lib/services/delete-dmo-field.js +472 -0
- package/lib/services/delete-identity-resolution.d.ts +43 -0
- package/lib/services/delete-identity-resolution.js +295 -0
- package/lib/services/delete-segment.d.ts +42 -0
- package/lib/services/delete-segment.js +248 -0
- package/lib/services/deploy-result.d.ts +54 -0
- package/lib/services/deploy-result.js +60 -0
- package/lib/services/escaped-copy.d.ts +23 -0
- package/lib/services/escaped-copy.js +88 -0
- package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
- package/lib/services/execute-calculated-insight-deploy.js +96 -0
- package/lib/services/execute-data-transform-deploy.d.ts +56 -0
- package/lib/services/execute-data-transform-deploy.js +168 -0
- package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
- package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
- package/lib/services/execute-segment-deploy.d.ts +45 -0
- package/lib/services/execute-segment-deploy.js +393 -0
- package/lib/services/list-data-spaces.d.ts +17 -0
- package/lib/services/list-data-spaces.js +24 -0
- package/lib/services/local-file-removal.d.ts +36 -0
- package/lib/services/local-file-removal.js +103 -0
- package/lib/services/native-calculated-insight.d.ts +87 -0
- package/lib/services/native-calculated-insight.js +164 -0
- package/lib/services/native-crm-data-stream.d.ts +91 -0
- package/lib/services/native-crm-data-stream.js +272 -0
- package/lib/services/native-data-graph-context.d.ts +12 -0
- package/lib/services/native-data-graph-context.js +139 -0
- package/lib/services/native-data-graph-crd.d.ts +261 -0
- package/lib/services/native-data-graph-crd.js +866 -0
- package/lib/services/native-data-stream.d.ts +329 -0
- package/lib/services/native-data-stream.js +1830 -0
- package/lib/services/native-dlo.d.ts +163 -0
- package/lib/services/native-dlo.js +402 -0
- package/lib/services/native-dmo.d.ts +191 -0
- package/lib/services/native-dmo.js +614 -0
- package/lib/services/native-mapping.d.ts +255 -0
- package/lib/services/native-mapping.js +1226 -0
- package/lib/services/native-relationship.d.ts +132 -0
- package/lib/services/native-relationship.js +512 -0
- package/lib/services/operation-diagnostic.d.ts +83 -0
- package/lib/services/operation-diagnostic.js +272 -0
- package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
- package/lib/services/preflight-calculated-insight-deploy.js +95 -0
- package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
- package/lib/services/preflight-data-transform-deploy.js +104 -0
- package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
- package/lib/services/preflight-identity-resolution-deploy.js +281 -0
- package/lib/services/preflight-segment-deploy.d.ts +31 -0
- package/lib/services/preflight-segment-deploy.js +109 -0
- package/lib/services/publish-calculated-insight.d.ts +40 -0
- package/lib/services/publish-calculated-insight.js +95 -0
- package/lib/services/publish-segment.d.ts +65 -0
- package/lib/services/publish-segment.js +194 -0
- package/lib/services/readback-pace.d.ts +16 -0
- package/lib/services/readback-pace.js +23 -0
- package/lib/services/retrieve-data-transform.d.ts +26 -0
- package/lib/services/retrieve-data-transform.js +65 -0
- package/lib/services/retrieve-identity-resolution.d.ts +40 -0
- package/lib/services/retrieve-identity-resolution.js +87 -0
- package/lib/services/retrieve-inventory.d.ts +32 -0
- package/lib/services/retrieve-inventory.js +151 -0
- package/lib/services/retrieve-segment.d.ts +120 -0
- package/lib/services/retrieve-segment.js +502 -0
- package/lib/services/run-retrieve.d.ts +24 -0
- package/lib/services/run-retrieve.js +51 -0
- package/lib/services/segment-lookup.d.ts +46 -0
- package/lib/services/segment-lookup.js +108 -0
- package/lib/services/segment-members.d.ts +144 -0
- package/lib/services/segment-members.js +402 -0
- package/lib/services/select-native-retrieve.d.ts +60 -0
- package/lib/services/select-native-retrieve.js +226 -0
- package/lib/services/settle-calculated-insight.d.ts +22 -0
- package/lib/services/settle-calculated-insight.js +53 -0
- package/lib/services/sql-tokens.d.ts +58 -0
- package/lib/services/sql-tokens.js +147 -0
- package/lib/transport/api-version.d.ts +36 -0
- package/lib/transport/api-version.js +89 -0
- package/lib/transport/bound-ssot.d.ts +27 -0
- package/lib/transport/bound-ssot.js +116 -0
- package/lib/transport/calculated-insight-rest.d.ts +28 -0
- package/lib/transport/calculated-insight-rest.js +62 -0
- package/lib/transport/connect-client.d.ts +94 -32
- package/lib/transport/connect-client.js +318 -178
- package/lib/transport/connect-diagnostic.d.ts +89 -0
- package/lib/transport/connect-diagnostic.js +331 -0
- package/lib/transport/data-graph-rest.d.ts +27 -0
- package/lib/transport/data-graph-rest.js +76 -0
- package/lib/transport/data-stream-rest.d.ts +62 -0
- package/lib/transport/data-stream-rest.js +348 -0
- package/lib/transport/data-transform-rest.d.ts +25 -0
- package/lib/transport/data-transform-rest.js +100 -0
- package/lib/transport/dlo-native-rest.d.ts +22 -0
- package/lib/transport/dlo-native-rest.js +89 -0
- package/lib/transport/dlo-rest.d.ts +23 -0
- package/lib/transport/dlo-rest.js +135 -0
- package/lib/transport/dmo-native-rest.d.ts +18 -0
- package/lib/transport/dmo-native-rest.js +55 -0
- package/lib/transport/dmo-rest.d.ts +30 -0
- package/lib/transport/dmo-rest.js +163 -0
- package/lib/transport/mapping-rest.d.ts +63 -0
- package/lib/transport/mapping-rest.js +341 -0
- package/lib/transport/project-metadata.d.ts +36 -0
- package/lib/transport/project-metadata.js +118 -0
- package/lib/transport/relationship-rest.d.ts +43 -0
- package/lib/transport/relationship-rest.js +201 -0
- package/package.json +29 -6
- package/lib/artifacts/dmo-document.d.ts +0 -181
- package/lib/artifacts/dmo-document.js +0 -848
- package/lib/artifacts/dmo-update-delta.d.ts +0 -28
- package/lib/artifacts/dmo-update-delta.js +0 -86
- package/lib/artifacts/write-other-dmo.d.ts +0 -17
- package/lib/artifacts/write-other-dmo.js +0 -140
- package/lib/services/execute-dmo-create.d.ts +0 -24
- package/lib/services/execute-dmo-create.js +0 -118
- package/lib/services/execute-dmo-deploy.d.ts +0 -33
- package/lib/services/execute-dmo-deploy.js +0 -197
- package/lib/services/preflight-dmo-deploy.d.ts +0 -69
- package/lib/services/preflight-dmo-deploy.js +0 -161
- package/lib/services/retrieve-other-dmo.d.ts +0 -46
- package/lib/services/retrieve-other-dmo.js +0 -140
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, mkdir, mkdtemp, open, rename, unlink } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { NativeMappingError, validateNativeMappingDocument } from './native-mapping-document.js';
|
|
6
|
+
import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
|
|
7
|
+
import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
|
|
8
|
+
/**
|
|
9
|
+
* Validate real ancestors before publication.
|
|
10
|
+
* @param {string} directory Destination directory.
|
|
11
|
+
*/
|
|
12
|
+
async function ensure(directory) {
|
|
13
|
+
const parent = path.dirname(directory);
|
|
14
|
+
if (parent !== directory)
|
|
15
|
+
await ensure(parent);
|
|
16
|
+
try {
|
|
17
|
+
const stat = await inspectSegmentPath(directory);
|
|
18
|
+
if (!stat.isDirectory())
|
|
19
|
+
throw new NativeMappingError('UNSAFE_PATH');
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error.code !== 'ENOENT')
|
|
23
|
+
throw error;
|
|
24
|
+
try {
|
|
25
|
+
await mkdir(directory);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error.code !== 'EEXIST')
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
const stat = await inspectSegmentPath(directory);
|
|
32
|
+
if (!stat.isDirectory())
|
|
33
|
+
throw new NativeMappingError('UNSAFE_PATH');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Bind existing bytes to one unchanged regular file.
|
|
38
|
+
* @param {string} file Exact destination.
|
|
39
|
+
* @returns {Promise<Buffer | undefined>} Existing bytes or confirmed absence.
|
|
40
|
+
*/
|
|
41
|
+
async function existing(file) {
|
|
42
|
+
try {
|
|
43
|
+
const before = await inspectSegmentPath(file);
|
|
44
|
+
if (!before.isFile() || before.nlink !== 1 || before.size > 1_048_576)
|
|
45
|
+
throw new NativeMappingError('UNSAFE_PATH');
|
|
46
|
+
const handle = await open(file, 'r');
|
|
47
|
+
try {
|
|
48
|
+
const opened = await handle.stat();
|
|
49
|
+
if (opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink !== 1)
|
|
50
|
+
throw new NativeMappingError('OUTPUT_CHANGED');
|
|
51
|
+
const buffer = Buffer.alloc(1_048_577);
|
|
52
|
+
let length = 0;
|
|
53
|
+
while (length < buffer.length) {
|
|
54
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
55
|
+
if (!bytesRead)
|
|
56
|
+
break;
|
|
57
|
+
length += bytesRead;
|
|
58
|
+
}
|
|
59
|
+
const after = await handle.stat();
|
|
60
|
+
const current = await inspectSegmentPath(file);
|
|
61
|
+
if (length !== before.size ||
|
|
62
|
+
length > 1_048_576 ||
|
|
63
|
+
after.size !== before.size ||
|
|
64
|
+
after.mtimeMs !== before.mtimeMs ||
|
|
65
|
+
after.ctimeMs !== before.ctimeMs ||
|
|
66
|
+
current.dev !== before.dev ||
|
|
67
|
+
current.ino !== before.ino ||
|
|
68
|
+
current.nlink !== 1 ||
|
|
69
|
+
current.mtimeMs !== before.mtimeMs ||
|
|
70
|
+
current.ctimeMs !== before.ctimeMs)
|
|
71
|
+
throw new NativeMappingError('OUTPUT_CHANGED');
|
|
72
|
+
return buffer.subarray(0, length);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
await handle.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error.code === 'ENOENT')
|
|
80
|
+
return undefined;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Publish saved mapping evidence without applying create or update send policies.
|
|
86
|
+
* @param {unknown} value Portable definition including generated pairs and observed selectors.
|
|
87
|
+
* @param {string} projectRoot Explicit project root.
|
|
88
|
+
* @param {boolean} overwrite Permit replacing differing bytes.
|
|
89
|
+
* @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Publication receipt.
|
|
90
|
+
*/
|
|
91
|
+
export async function writeNativeMappingDocument(value, projectRoot, overwrite) {
|
|
92
|
+
const verbatim = isVerbatimDocument(value, 'mapping') ? value : undefined;
|
|
93
|
+
const document = verbatim ?? validateNativeMappingDocument(value, 'retrieve');
|
|
94
|
+
const bytes = Buffer.from(verbatim ? serializeVerbatimDocument(verbatim) : `${JSON.stringify(document, undefined, 2)}\n`);
|
|
95
|
+
if (!path.isAbsolute(projectRoot) ||
|
|
96
|
+
projectRoot !== projectRoot.trim() ||
|
|
97
|
+
/[*?\0]/u.test(projectRoot) ||
|
|
98
|
+
projectRoot.split(/[\\/]/u).includes('..') ||
|
|
99
|
+
bytes.length > 1_048_576)
|
|
100
|
+
throw new NativeMappingError('UNSAFE_PATH');
|
|
101
|
+
const root = path.resolve(projectRoot);
|
|
102
|
+
if (path.dirname(root) === root)
|
|
103
|
+
throw new NativeMappingError('UNSAFE_PATH');
|
|
104
|
+
const destination = path.join(root, 'data360', 'mapping', `${document.identity.apiName}.mapping-meta.json`);
|
|
105
|
+
let published = false;
|
|
106
|
+
try {
|
|
107
|
+
await ensure(path.dirname(destination));
|
|
108
|
+
const previous = await existing(destination);
|
|
109
|
+
if (previous?.equals(bytes))
|
|
110
|
+
return { path: destination, outcome: 'unchanged' };
|
|
111
|
+
// The name is unique in the org (whatever its data space), so the file at the path is the same
|
|
112
|
+
// component.
|
|
113
|
+
if (previous && !overwrite)
|
|
114
|
+
throw new NativeMappingError('OVERWRITE_REQUIRED');
|
|
115
|
+
verifyTemporaryDirectory();
|
|
116
|
+
const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-mapping-output-'));
|
|
117
|
+
const marker = await open(path.join(staging, 'ownership'), 'wx');
|
|
118
|
+
try {
|
|
119
|
+
await marker.writeFile(randomUUID());
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
await marker.close();
|
|
123
|
+
}
|
|
124
|
+
const stagedPath = path.join(staging, 'document.json');
|
|
125
|
+
const handle = await open(stagedPath, 'wx');
|
|
126
|
+
try {
|
|
127
|
+
await handle.writeFile(bytes);
|
|
128
|
+
await handle.sync();
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
await handle.close();
|
|
132
|
+
}
|
|
133
|
+
await ensure(path.dirname(destination));
|
|
134
|
+
const current = await existing(destination);
|
|
135
|
+
if (previous === undefined ? current !== undefined : !current?.equals(previous))
|
|
136
|
+
throw new NativeMappingError('OUTPUT_CHANGED');
|
|
137
|
+
if (previous)
|
|
138
|
+
await rename(stagedPath, destination);
|
|
139
|
+
else {
|
|
140
|
+
await link(stagedPath, destination);
|
|
141
|
+
published = true;
|
|
142
|
+
await unlink(stagedPath);
|
|
143
|
+
}
|
|
144
|
+
published = true;
|
|
145
|
+
const completed = await existing(destination);
|
|
146
|
+
if (!completed?.equals(bytes))
|
|
147
|
+
throw new NativeMappingError('OUTPUT_CHANGED');
|
|
148
|
+
return { path: destination, outcome: 'retrieved' };
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
if (published)
|
|
152
|
+
throw new NativeMappingError('PARTIAL_MAPPING_OUTPUT');
|
|
153
|
+
if (error instanceof NativeMappingError)
|
|
154
|
+
throw error;
|
|
155
|
+
throw new NativeMappingError('OUTPUT_FAILED');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publish one saved relationship; the name is the generated source name, not a send key.
|
|
3
|
+
* @param {unknown} value Portable relationship definition.
|
|
4
|
+
* @param {string} projectRoot Explicit project root.
|
|
5
|
+
* @param {boolean} overwrite Permit replacing differing bytes.
|
|
6
|
+
* @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Publication receipt.
|
|
7
|
+
*/
|
|
8
|
+
export declare function writeNativeRelationshipDocument(value: unknown, projectRoot: string, overwrite: boolean): Promise<{
|
|
9
|
+
path: string;
|
|
10
|
+
outcome: 'retrieved' | 'unchanged';
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, mkdir, mkdtemp, open, rename, unlink } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { NativeRelationshipError, validateNativeRelationshipDocument, } from './native-relationship-document.js';
|
|
6
|
+
import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
|
|
7
|
+
import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
|
|
8
|
+
/**
|
|
9
|
+
* Validate real ancestors before publication.
|
|
10
|
+
* @param {string} directory Destination directory.
|
|
11
|
+
*/
|
|
12
|
+
async function ensure(directory) {
|
|
13
|
+
const parent = path.dirname(directory);
|
|
14
|
+
if (parent !== directory)
|
|
15
|
+
await ensure(parent);
|
|
16
|
+
try {
|
|
17
|
+
const stat = await inspectSegmentPath(directory);
|
|
18
|
+
if (!stat.isDirectory())
|
|
19
|
+
throw new NativeRelationshipError('UNSAFE_PATH');
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error.code !== 'ENOENT')
|
|
23
|
+
throw error;
|
|
24
|
+
try {
|
|
25
|
+
await mkdir(directory);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error.code !== 'EEXIST')
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
const stat = await inspectSegmentPath(directory);
|
|
32
|
+
if (!stat.isDirectory())
|
|
33
|
+
throw new NativeRelationshipError('UNSAFE_PATH');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Bind existing bytes to one unchanged regular file.
|
|
38
|
+
* @param {string} file Exact destination.
|
|
39
|
+
* @returns {Promise<Buffer | undefined>} Existing bytes or confirmed absence.
|
|
40
|
+
*/
|
|
41
|
+
async function existing(file) {
|
|
42
|
+
try {
|
|
43
|
+
const before = await inspectSegmentPath(file);
|
|
44
|
+
if (!before.isFile() || before.nlink !== 1 || before.size > 1_048_576)
|
|
45
|
+
throw new NativeRelationshipError('UNSAFE_PATH');
|
|
46
|
+
const handle = await open(file, 'r');
|
|
47
|
+
try {
|
|
48
|
+
const opened = await handle.stat();
|
|
49
|
+
if (opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink !== 1)
|
|
50
|
+
throw new NativeRelationshipError('OUTPUT_CHANGED');
|
|
51
|
+
const buffer = Buffer.alloc(1_048_577);
|
|
52
|
+
let length = 0;
|
|
53
|
+
while (length < buffer.length) {
|
|
54
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
55
|
+
if (!bytesRead)
|
|
56
|
+
break;
|
|
57
|
+
length += bytesRead;
|
|
58
|
+
}
|
|
59
|
+
const after = await handle.stat();
|
|
60
|
+
const current = await inspectSegmentPath(file);
|
|
61
|
+
if (length !== before.size ||
|
|
62
|
+
length > 1_048_576 ||
|
|
63
|
+
after.size !== before.size ||
|
|
64
|
+
after.mtimeMs !== before.mtimeMs ||
|
|
65
|
+
after.ctimeMs !== before.ctimeMs ||
|
|
66
|
+
current.dev !== before.dev ||
|
|
67
|
+
current.ino !== before.ino ||
|
|
68
|
+
current.nlink !== 1 ||
|
|
69
|
+
current.mtimeMs !== before.mtimeMs ||
|
|
70
|
+
current.ctimeMs !== before.ctimeMs)
|
|
71
|
+
throw new NativeRelationshipError('OUTPUT_CHANGED');
|
|
72
|
+
return buffer.subarray(0, length);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
await handle.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (error.code === 'ENOENT')
|
|
80
|
+
return undefined;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Publish one saved relationship; the name is the generated source name, not a send key.
|
|
86
|
+
* @param {unknown} value Portable relationship definition.
|
|
87
|
+
* @param {string} projectRoot Explicit project root.
|
|
88
|
+
* @param {boolean} overwrite Permit replacing differing bytes.
|
|
89
|
+
* @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Publication receipt.
|
|
90
|
+
*/
|
|
91
|
+
export async function writeNativeRelationshipDocument(value, projectRoot, overwrite) {
|
|
92
|
+
const verbatim = isVerbatimDocument(value, 'relationship') ? value : undefined;
|
|
93
|
+
const document = verbatim ?? validateNativeRelationshipDocument(value, 'retrieve');
|
|
94
|
+
const bytes = Buffer.from(verbatim ? serializeVerbatimDocument(verbatim) : `${JSON.stringify(document, undefined, 2)}\n`);
|
|
95
|
+
if (!path.isAbsolute(projectRoot) ||
|
|
96
|
+
projectRoot !== projectRoot.trim() ||
|
|
97
|
+
/[*?\0]/u.test(projectRoot) ||
|
|
98
|
+
projectRoot.split(/[\\/]/u).includes('..') ||
|
|
99
|
+
bytes.length > 1_048_576)
|
|
100
|
+
throw new NativeRelationshipError('UNSAFE_PATH');
|
|
101
|
+
const root = path.resolve(projectRoot);
|
|
102
|
+
if (path.dirname(root) === root)
|
|
103
|
+
throw new NativeRelationshipError('UNSAFE_PATH');
|
|
104
|
+
const destination = path.join(root, 'data360', 'relationship', `${document.identity.apiName}.relationship-meta.json`);
|
|
105
|
+
let published = false;
|
|
106
|
+
try {
|
|
107
|
+
await ensure(path.dirname(destination));
|
|
108
|
+
const previous = await existing(destination);
|
|
109
|
+
if (previous?.equals(bytes))
|
|
110
|
+
return { path: destination, outcome: 'unchanged' };
|
|
111
|
+
// The name is unique in the org (whatever its data space), so the file at the path is the same
|
|
112
|
+
// component.
|
|
113
|
+
if (previous && !overwrite)
|
|
114
|
+
throw new NativeRelationshipError('OVERWRITE_REQUIRED');
|
|
115
|
+
verifyTemporaryDirectory();
|
|
116
|
+
const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-relationship-output-'));
|
|
117
|
+
const marker = await open(path.join(staging, 'ownership'), 'wx');
|
|
118
|
+
try {
|
|
119
|
+
await marker.writeFile(randomUUID());
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
await marker.close();
|
|
123
|
+
}
|
|
124
|
+
const stagedPath = path.join(staging, 'document.json');
|
|
125
|
+
const handle = await open(stagedPath, 'wx');
|
|
126
|
+
try {
|
|
127
|
+
await handle.writeFile(bytes);
|
|
128
|
+
await handle.sync();
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
await handle.close();
|
|
132
|
+
}
|
|
133
|
+
await ensure(path.dirname(destination));
|
|
134
|
+
const current = await existing(destination);
|
|
135
|
+
if (previous === undefined ? current !== undefined : !current?.equals(previous))
|
|
136
|
+
throw new NativeRelationshipError('OUTPUT_CHANGED');
|
|
137
|
+
if (previous)
|
|
138
|
+
await rename(stagedPath, destination);
|
|
139
|
+
else {
|
|
140
|
+
await link(stagedPath, destination);
|
|
141
|
+
published = true;
|
|
142
|
+
await unlink(stagedPath);
|
|
143
|
+
}
|
|
144
|
+
published = true;
|
|
145
|
+
const completed = await existing(destination);
|
|
146
|
+
if (!completed?.equals(bytes))
|
|
147
|
+
throw new NativeRelationshipError('OUTPUT_CHANGED');
|
|
148
|
+
return { path: destination, outcome: 'retrieved' };
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
if (published)
|
|
152
|
+
throw new NativeRelationshipError('PARTIAL_RELATIONSHIP_OUTPUT');
|
|
153
|
+
if (error instanceof NativeRelationshipError)
|
|
154
|
+
throw error;
|
|
155
|
+
throw new NativeRelationshipError('OUTPUT_FAILED');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { link, rename, unlink } from 'node:fs/promises';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve configured package output without permitting escapes or ambiguous default packages.
|
|
4
|
+
* @param {string} root Absolute selected project root.
|
|
5
|
+
* @returns {Promise<string>} Configured package source root or standard force-app default.
|
|
6
|
+
*/
|
|
7
|
+
export declare function segmentSourceRoot(root: string): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* Publish one segment as `data360/segments/<apiName>.segment.json` plus one
|
|
10
|
+
* `<apiName>.segment.<model>.sql` per model, with exact-byte conflict and drift admission. A SQL
|
|
11
|
+
* file of a model the document no longer has is removed (it needs `overwrite`).
|
|
12
|
+
* @param {unknown} value Complete authored candidate (SQL included).
|
|
13
|
+
* @param {string} projectRoot Selected output project root.
|
|
14
|
+
* @param {boolean} overwrite Permit differing existing files.
|
|
15
|
+
* @param {object} publication Narrow filesystem seam for publication-failure regression tests.
|
|
16
|
+
* @returns {Promise<{path: string; codePaths: string[]; outcome: 'retrieved' | 'unchanged'}>} JSON path, SQL paths and outcome.
|
|
17
|
+
*/
|
|
18
|
+
export declare function writeSegmentDocument(value: unknown, projectRoot: string, overwrite: boolean, publication?: {
|
|
19
|
+
link: typeof link;
|
|
20
|
+
rename: typeof rename;
|
|
21
|
+
unlink: typeof unlink;
|
|
22
|
+
}): Promise<{
|
|
23
|
+
path: string;
|
|
24
|
+
codePaths: string[];
|
|
25
|
+
outcome: 'retrieved' | 'unchanged';
|
|
26
|
+
}>;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, mkdir, mkdtemp, open, readdir, readFile, rename, unlink } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { codeFilePath } from './code-fields.js';
|
|
6
|
+
import { SegmentError, joinSegmentFiles, serializeSegmentFiles, validateSegmentDocument, } from './segment-document.js';
|
|
7
|
+
import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
|
|
8
|
+
import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
|
|
9
|
+
const filesystemPublication = { link, rename, unlink };
|
|
10
|
+
/**
|
|
11
|
+
* Create only missing real directories, validating each ancestor first.
|
|
12
|
+
* @param {string} directory Absolute directory.
|
|
13
|
+
* @returns {Promise<void>} Resolves for a real no-link chain.
|
|
14
|
+
*/
|
|
15
|
+
async function ensure(directory) {
|
|
16
|
+
const parent = path.dirname(directory);
|
|
17
|
+
if (parent !== directory)
|
|
18
|
+
await ensure(parent);
|
|
19
|
+
try {
|
|
20
|
+
const present = await inspectSegmentPath(directory);
|
|
21
|
+
if (!present.isDirectory())
|
|
22
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code !== 'ENOENT')
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
await mkdir(directory);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error.code !== 'EEXIST')
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
const stat = await inspectSegmentPath(directory);
|
|
37
|
+
if (!stat.isDirectory())
|
|
38
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Read an exact regular single-link file or distinguish ENOENT only.
|
|
42
|
+
* @param {string} file Absolute path.
|
|
43
|
+
* @returns {Promise<Buffer | undefined>} Original bytes or absence.
|
|
44
|
+
*/
|
|
45
|
+
async function existing(file) {
|
|
46
|
+
try {
|
|
47
|
+
const stat = await inspectSegmentPath(file);
|
|
48
|
+
if (!stat.isFile() || stat.nlink !== 1 || stat.size > 1_048_576)
|
|
49
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
50
|
+
return await readFile(file);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error.code === 'ENOENT')
|
|
54
|
+
return undefined;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolve configured package output without permitting escapes or ambiguous default packages.
|
|
60
|
+
* @param {string} root Absolute selected project root.
|
|
61
|
+
* @returns {Promise<string>} Configured package source root or standard force-app default.
|
|
62
|
+
*/
|
|
63
|
+
export async function segmentSourceRoot(root) {
|
|
64
|
+
const config = await existing(path.join(root, 'sfdx-project.json'));
|
|
65
|
+
let directory = 'force-app';
|
|
66
|
+
if (config) {
|
|
67
|
+
const project = JSON.parse(config.toString('utf8'));
|
|
68
|
+
const packages = project.packageDirectories;
|
|
69
|
+
if (!Array.isArray(packages) || packages.length === 0)
|
|
70
|
+
throw new SegmentError('INVALID_SELECTION');
|
|
71
|
+
const defaults = packages.filter((entry) => entry.default === true);
|
|
72
|
+
const selected = defaults.length === 1 ? defaults[0] : packages.length === 1 ? packages[0] : undefined;
|
|
73
|
+
if (!selected ||
|
|
74
|
+
typeof selected.path !== 'string' ||
|
|
75
|
+
!selected.path ||
|
|
76
|
+
path.isAbsolute(selected.path) ||
|
|
77
|
+
/[:*?]/u.test(selected.path) ||
|
|
78
|
+
selected.path.split(/[\\/]/u).includes('..'))
|
|
79
|
+
throw new SegmentError('INVALID_SELECTION');
|
|
80
|
+
directory = selected.path;
|
|
81
|
+
}
|
|
82
|
+
const destination = path.resolve(root, directory, 'main', 'default');
|
|
83
|
+
const relative = path.relative(root, destination);
|
|
84
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative))
|
|
85
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
86
|
+
return destination;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* SQL files of a segment in its folder that are not among the files being written (a model the
|
|
90
|
+
* org no longer has).
|
|
91
|
+
* @param {string} directory Absolute segments folder.
|
|
92
|
+
* @param {string} apiName Segment name.
|
|
93
|
+
* @param {readonly string[]} files Absolute files being written.
|
|
94
|
+
* @returns {Promise<string[]>} Absolute stale SQL files, sorted.
|
|
95
|
+
*/
|
|
96
|
+
async function staleSqlFiles(directory, apiName, files) {
|
|
97
|
+
const prefix = `${apiName}.segment.`.toLowerCase();
|
|
98
|
+
const own = new Set(files.map((file) => path.basename(file).toLowerCase()));
|
|
99
|
+
const entries = await readdir(directory);
|
|
100
|
+
const stale = entries.filter((entry) => {
|
|
101
|
+
const lower = entry.toLowerCase();
|
|
102
|
+
return lower.startsWith(prefix) && lower.endsWith('.sql') && !own.has(lower);
|
|
103
|
+
});
|
|
104
|
+
stale.sort();
|
|
105
|
+
return stale.map((entry) => path.join(directory, entry));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Publish one segment as `data360/segments/<apiName>.segment.json` plus one
|
|
109
|
+
* `<apiName>.segment.<model>.sql` per model, with exact-byte conflict and drift admission. A SQL
|
|
110
|
+
* file of a model the document no longer has is removed (it needs `overwrite`).
|
|
111
|
+
* @param {unknown} value Complete authored candidate (SQL included).
|
|
112
|
+
* @param {string} projectRoot Selected output project root.
|
|
113
|
+
* @param {boolean} overwrite Permit differing existing files.
|
|
114
|
+
* @param {object} publication Narrow filesystem seam for publication-failure regression tests.
|
|
115
|
+
* @returns {Promise<{path: string; codePaths: string[]; outcome: 'retrieved' | 'unchanged'}>} JSON path, SQL paths and outcome.
|
|
116
|
+
*/
|
|
117
|
+
export async function writeSegmentDocument(value, projectRoot, overwrite, publication = filesystemPublication) {
|
|
118
|
+
// A verbatim segment is its JSON alone; every SQL file of that segment is then stale.
|
|
119
|
+
const verbatim = isVerbatimDocument(value, 'segment') ? value : undefined;
|
|
120
|
+
// Retrieve writes keep what the plugin does not model; deploy refuses such a file.
|
|
121
|
+
const strict = verbatim ? undefined : validateSegmentDocument(value, 'retrieve');
|
|
122
|
+
const document = verbatim ?? strict;
|
|
123
|
+
const { json, files: code } = verbatim
|
|
124
|
+
? { json: serializeVerbatimDocument(verbatim), files: [] }
|
|
125
|
+
: serializeSegmentFiles(strict, 'retrieve');
|
|
126
|
+
if (!projectRoot.trim())
|
|
127
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
128
|
+
const root = path.resolve(projectRoot);
|
|
129
|
+
let published = 0;
|
|
130
|
+
try {
|
|
131
|
+
const jsonPath = path.join(root, 'data360', 'segments', `${document.identity.apiName}.segment.json`);
|
|
132
|
+
const files = [jsonPath, ...code.map((file) => codeFilePath(jsonPath, file))];
|
|
133
|
+
const codePaths = files.slice(1);
|
|
134
|
+
const bytes = [Buffer.from(json), ...code.map((file) => Buffer.from(file.text))];
|
|
135
|
+
if (bytes.some((content) => content.length > 1_048_576))
|
|
136
|
+
throw new SegmentError('UNSAFE_PATH');
|
|
137
|
+
for (const file of files)
|
|
138
|
+
await ensure(path.dirname(file));
|
|
139
|
+
const previous = await Promise.all(files.map((file) => existing(file)));
|
|
140
|
+
const stale = await staleSqlFiles(path.dirname(jsonPath), document.identity.apiName, files);
|
|
141
|
+
for (const file of stale)
|
|
142
|
+
await existing(file);
|
|
143
|
+
const changed = files.map((_, index) => !previous[index]?.equals(bytes[index]));
|
|
144
|
+
if (!changed.some(Boolean) && stale.length === 0)
|
|
145
|
+
return { path: files[0], codePaths, outcome: 'unchanged' };
|
|
146
|
+
if (!overwrite &&
|
|
147
|
+
(stale.length > 0 || previous.some((value, index) => value && changed[index])))
|
|
148
|
+
throw new SegmentError('OVERWRITE_REQUIRED');
|
|
149
|
+
verifyTemporaryDirectory();
|
|
150
|
+
const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-segment-output-'));
|
|
151
|
+
const marker = await open(path.join(staging, 'ownership'), 'wx');
|
|
152
|
+
await marker.writeFile(randomUUID());
|
|
153
|
+
await marker.close();
|
|
154
|
+
for (const [index, content] of bytes.entries()) {
|
|
155
|
+
const handle = await open(path.join(staging, `${index}`), 'wx');
|
|
156
|
+
try {
|
|
157
|
+
await handle.writeFile(content);
|
|
158
|
+
await handle.sync();
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
await handle.close();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Bind the observed destination again before publication.
|
|
165
|
+
for (const [index, file] of files.entries()) {
|
|
166
|
+
await ensure(path.dirname(file));
|
|
167
|
+
const current = await existing(file);
|
|
168
|
+
if (previous[index] === undefined ? current !== undefined : !current?.equals(previous[index]))
|
|
169
|
+
throw new SegmentError('OUTPUT_CHANGED');
|
|
170
|
+
}
|
|
171
|
+
for (const [index, file] of files.entries()) {
|
|
172
|
+
if (!changed[index])
|
|
173
|
+
continue;
|
|
174
|
+
const staged = path.join(staging, `${index}`);
|
|
175
|
+
await ensure(path.dirname(file));
|
|
176
|
+
const current = await existing(file);
|
|
177
|
+
if (previous[index] === undefined ? current !== undefined : !current?.equals(previous[index]))
|
|
178
|
+
throw new SegmentError('OUTPUT_CHANGED');
|
|
179
|
+
if (previous[index])
|
|
180
|
+
await publication.rename(staged, file);
|
|
181
|
+
else {
|
|
182
|
+
await publication.link(staged, file);
|
|
183
|
+
// The destination exists even when removing the staging link fails.
|
|
184
|
+
published++;
|
|
185
|
+
await publication.unlink(staged);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
published++;
|
|
189
|
+
}
|
|
190
|
+
for (const file of stale) {
|
|
191
|
+
await existing(file);
|
|
192
|
+
published++;
|
|
193
|
+
await publication.unlink(file);
|
|
194
|
+
}
|
|
195
|
+
const completed = await Promise.all(files.map((file) => existing(file)));
|
|
196
|
+
if (completed.some((value, index) => !value?.equals(bytes[index])))
|
|
197
|
+
throw new SegmentError('OUTPUT_CHANGED');
|
|
198
|
+
const left = await staleSqlFiles(path.dirname(jsonPath), document.identity.apiName, files);
|
|
199
|
+
if (left.length > 0)
|
|
200
|
+
throw new SegmentError('OUTPUT_CHANGED');
|
|
201
|
+
if (!verbatim)
|
|
202
|
+
joinSegmentFiles(JSON.parse(completed[0].toString('utf8')), code.map((file, index) => ({ ...file, text: completed[index + 1].toString('utf8') })), 'retrieve');
|
|
203
|
+
return { path: files[0], codePaths, outcome: 'retrieved' };
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
if (published)
|
|
207
|
+
throw new SegmentError('PARTIAL_SEGMENT_OUTPUT');
|
|
208
|
+
if (error instanceof SegmentError)
|
|
209
|
+
throw error;
|
|
210
|
+
throw new SegmentError('OUTPUT_FAILED');
|
|
211
|
+
}
|
|
212
|
+
}
|