sf-plugin-data 0.2.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.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Salesforce CLI exit code for a command envelope's top-level status.
3
+ * success and accepted (--async) are 0, partial is 68, processing is 69, anything else is 1.
4
+ * @param {object} envelope Command envelope (or any object with its status).
5
+ * @param {string} envelope.status Top-level envelope status.
6
+ * @returns {0 | 1 | 68 | 69} The exit code.
7
+ */
8
+ export function exitCodeOf(envelope) {
9
+ switch (envelope.status) {
10
+ case 'success':
11
+ case 'accepted': {
12
+ return 0;
13
+ }
14
+ case 'partial': {
15
+ return 68;
16
+ }
17
+ case 'processing': {
18
+ return 69;
19
+ }
20
+ default: {
21
+ return 1;
22
+ }
23
+ }
24
+ }
@@ -23,7 +23,9 @@ import { explainInput, printableKey } from './operation-diagnostic.js';
23
23
  export async function crossSpaceContext(connection, options) {
24
24
  const sender = openBoundSsot('', '68.0', async () => connection);
25
25
  const open = async () => sender;
26
- const spaces = await readOrgDataSpaceEntries(open);
26
+ // The context names each space and its prefix only, as the rename rules read them.
27
+ const entries = await readOrgDataSpaceEntries(open);
28
+ const spaces = entries.map(({ name, prefix }) => prefix === undefined ? { name } : { name, prefix });
27
29
  const known = spaces.map((space) => space.name);
28
30
  const unknown = sortedDataSpaces([options.from, options.to].filter((s) => !known.includes(s)));
29
31
  if (unknown.length > 0)
@@ -8,12 +8,14 @@ export declare class DataSpaceError extends Error {
8
8
  constructor(code: string, status?: number | undefined);
9
9
  }
10
10
  /**
11
- * One data space of the org: its name and, for every space but `default`, the prefix Salesforce
12
- * gives the copies of standard DMOs in it (`ds2` for `ds2_Individual__dlm`). Only this route
13
- * names the prefix (live API 68, DB entry data360:dataspace-name-spelling-api68).
11
+ * One data space of the org: its name, its label (the name when the list gives none) and, for
12
+ * every space but `default`, the prefix Salesforce gives the copies of standard DMOs in it (`ds2`
13
+ * for `ds2_Individual__dlm`). Only this route names the prefix (live API 68, DB entries
14
+ * data360:dataspace-name-spelling-api68 and data360:dataspace-list-fields-api68).
14
15
  */
15
16
  export type OrgDataSpace = {
16
17
  name: string;
18
+ label: string;
17
19
  prefix?: string;
18
20
  };
19
21
  /**
@@ -25,15 +27,16 @@ export type OrgDataSpace = {
25
27
  */
26
28
  export declare function readOrgDataSpaces(open: DataSpaceSender): Promise<string[]>;
27
29
  /**
28
- * Read every data space of the org as readOrgDataSpaces does, keeping each one's prefix when it
29
- * names a usable one (letters and digits).
30
+ * Read every data space of the org as readOrgDataSpaces does, keeping each one's label (the name
31
+ * when the entry has no usable label) and its prefix when it names a usable one (letters and
32
+ * digits).
30
33
  * @param {DataSpaceSender} open Opens the command's shared bound sender.
31
34
  * @returns {Promise<OrgDataSpace[]>} Data spaces.
32
35
  */
33
36
  export declare function readOrgDataSpaceEntries(open: DataSpaceSender): Promise<OrgDataSpace[]>;
34
37
  /**
35
38
  * Reads the org's data spaces; tests replace `read` with an offline fake, which may answer plain
36
- * names (no prefixes).
39
+ * names (no labels, no prefixes).
37
40
  */
38
41
  export declare const dataSpaceDirectory: {
39
42
  read: (open: DataSpaceSender) => Promise<Array<string | OrgDataSpace>>;
@@ -30,8 +30,9 @@ export async function readOrgDataSpaces(open) {
30
30
  return entries.map((entry) => entry.name);
31
31
  }
32
32
  /**
33
- * Read every data space of the org as readOrgDataSpaces does, keeping each one's prefix when it
34
- * names a usable one (letters and digits).
33
+ * Read every data space of the org as readOrgDataSpaces does, keeping each one's label (the name
34
+ * when the entry has no usable label) and its prefix when it names a usable one (letters and
35
+ * digits).
35
36
  * @param {DataSpaceSender} open Opens the command's shared bound sender.
36
37
  * @returns {Promise<OrgDataSpace[]>} Data spaces.
37
38
  */
@@ -59,12 +60,18 @@ export async function readOrgDataSpaceEntries(open) {
59
60
  total < names.length + entries.length)
60
61
  throw new DataSpaceError('DATASPACE_READ_FAILED');
61
62
  for (const entry of entries) {
62
- const { name, prefix } = (entry ?? {});
63
+ const { name, label, prefix } = (entry ?? {});
63
64
  if (!isDataSpaceName(name))
64
65
  throw new DataSpaceError('DATASPACE_READ_FAILED');
66
+ // A label is printed as it is: one without text or with control characters is not used.
67
+ const shown =
68
+ // eslint-disable-next-line no-control-regex -- control characters are refused, not matched
69
+ typeof label === 'string' && label.trim() && !/[\u0000-\u001F\u007F]/u.test(label)
70
+ ? label
71
+ : name;
65
72
  names.push(typeof prefix === 'string' && /^[A-Za-z][A-Za-z0-9]{0,39}$/u.test(prefix)
66
- ? { name, prefix }
67
- : { name });
73
+ ? { name, label: shown, prefix }
74
+ : { name, label: shown });
68
75
  }
69
76
  if (names.length === total)
70
77
  return names;
@@ -75,7 +82,7 @@ export async function readOrgDataSpaceEntries(open) {
75
82
  }
76
83
  /**
77
84
  * Reads the org's data spaces; tests replace `read` with an offline fake, which may answer plain
78
- * names (no prefixes).
85
+ * names (no labels, no prefixes).
79
86
  */
80
87
  export const dataSpaceDirectory = { read: readOrgDataSpaceEntries };
81
88
  /**
@@ -156,7 +163,7 @@ export class DataSpaceCheck {
156
163
  async spaces() {
157
164
  this.known ??= dataSpaceDirectory
158
165
  .read(this.sender)
159
- .then((entries) => entries.map((entry) => (typeof entry === 'string' ? { name: entry } : entry)));
166
+ .then((entries) => entries.map((entry) => (typeof entry === 'string' ? { name: entry, label: entry } : entry)));
160
167
  return this.known;
161
168
  }
162
169
  }
@@ -0,0 +1,17 @@
1
+ import type { LookupConnection } from './segment-lookup.js';
2
+ /** One data space of the org as `listDataSpaces` returns it. */
3
+ export type DataSpaceListing = {
4
+ name: string;
5
+ label: string;
6
+ prefix?: string;
7
+ };
8
+ /**
9
+ * Every data space of the org, complete: one paged `GET /ssot/data-spaces` read (API 68). The
10
+ * label is the name when Salesforce gives none; `prefix` is set for every space that has one
11
+ * (all but `default`).
12
+ * @param {LookupConnection} connection Authenticated connection.
13
+ * @returns {Promise<DataSpaceListing[]>} Data spaces in list order.
14
+ * @throws {DataSpaceError} DATASPACE_READ_FAILED when the list is not complete or not readable,
15
+ * AUTHENTICATION_FAILED when Salesforce refuses the session.
16
+ */
17
+ export declare function listDataSpaces(connection: LookupConnection): Promise<DataSpaceListing[]>;
@@ -0,0 +1,24 @@
1
+ import { ConnectError } from '../transport/connect-client.js';
2
+ import { getConnectDiagnostic, retainConnectDiagnostic } from '../transport/connect-diagnostic.js';
3
+ import { openBoundSsot } from '../transport/bound-ssot.js';
4
+ import { DataSpaceError, readOrgDataSpaceEntries } from './data-space-check.js';
5
+ /**
6
+ * Every data space of the org, complete: one paged `GET /ssot/data-spaces` read (API 68). The
7
+ * label is the name when Salesforce gives none; `prefix` is set for every space that has one
8
+ * (all but `default`).
9
+ * @param {LookupConnection} connection Authenticated connection.
10
+ * @returns {Promise<DataSpaceListing[]>} Data spaces in list order.
11
+ * @throws {DataSpaceError} DATASPACE_READ_FAILED when the list is not complete or not readable,
12
+ * AUTHENTICATION_FAILED when Salesforce refuses the session.
13
+ */
14
+ export async function listDataSpaces(connection) {
15
+ const sender = openBoundSsot('', '68.0', async () => connection);
16
+ try {
17
+ return await readOrgDataSpaceEntries(async () => sender);
18
+ }
19
+ catch (error) {
20
+ if (error instanceof ConnectError)
21
+ throw retainConnectDiagnostic(new DataSpaceError(error.code, error.statusCode), getConnectDiagnostic(error));
22
+ throw error;
23
+ }
24
+ }
@@ -0,0 +1,24 @@
1
+ import type { Connection } from '@salesforce/core';
2
+ import type { RetrieveEnvelope } from '../commands/data360/retrieve.js';
3
+ /** The flags of `sf data360 retrieve`, for a caller that holds its own Connection. */
4
+ export type RetrieveOptions = {
5
+ /** Run data space (`--dataspace`); default `default`. */
6
+ dataspace?: string;
7
+ /** Selectors (`-m`); omitted or empty selects every family. */
8
+ metadata?: string[];
9
+ /** Project root (`--output-dir`); default the current directory. */
10
+ outputDir?: string;
11
+ /** Permit replacing differing files (`--overwrite`). */
12
+ overwrite?: boolean;
13
+ /** `--api-version`; default `auto`. */
14
+ apiVersion?: string;
15
+ };
16
+ /**
17
+ * Run `sf data360 retrieve` over a Connection the caller holds and return its envelope (what
18
+ * `--json` shows). Nothing is printed and `process.exitCode` is left alone; use exitCodeOf on the
19
+ * envelope for the Salesforce CLI exit code. The Connection's API version is restored afterwards.
20
+ * @param {Connection} connection Authenticated @salesforce/core Connection of the source org.
21
+ * @param {RetrieveOptions} [options] The command's flags.
22
+ * @returns {Promise<RetrieveEnvelope>} The retrieve envelope.
23
+ */
24
+ export declare function runRetrieve(connection: Connection, options?: RetrieveOptions): Promise<RetrieveEnvelope>;
@@ -0,0 +1,51 @@
1
+ import Retrieve from '../commands/data360/retrieve.js';
2
+ /** The retrieve command over a Connection the caller holds; it never prints or exits. */
3
+ class ConnectionRetrieve extends Retrieve {
4
+ connection;
5
+ // The caller's Connection answers every family, at the version each family pins.
6
+ resolveOrg = async () => ({
7
+ getOrgId: () => this.connection.getAuthInfoFields().orgId,
8
+ getConnection: (version) => {
9
+ // Only a numeric version reaches the connection; @salesforce/core rejects `auto`.
10
+ if (version && /^[1-9]\d\.0$/u.test(version))
11
+ this.connection.setApiVersion(version);
12
+ return this.connection;
13
+ },
14
+ });
15
+ constructor(connection) {
16
+ // No oclif configuration: the command is never parsed, logged or rendered here.
17
+ super([], {});
18
+ this.connection = connection;
19
+ }
20
+ /**
21
+ * Retrieve as the command does with these options.
22
+ * @param {RetrieveOptions} options Flags of the run.
23
+ * @returns {Promise<RetrieveEnvelope>} The envelope the command returns.
24
+ */
25
+ async retrieveFor(options) {
26
+ return this.retrieveEnvelope({
27
+ 'api-version': options.apiVersion ?? 'auto',
28
+ dataspace: options.dataspace,
29
+ metadata: options.metadata,
30
+ 'output-dir': options.outputDir,
31
+ overwrite: options.overwrite ?? false,
32
+ });
33
+ }
34
+ }
35
+ /**
36
+ * Run `sf data360 retrieve` over a Connection the caller holds and return its envelope (what
37
+ * `--json` shows). Nothing is printed and `process.exitCode` is left alone; use exitCodeOf on the
38
+ * envelope for the Salesforce CLI exit code. The Connection's API version is restored afterwards.
39
+ * @param {Connection} connection Authenticated @salesforce/core Connection of the source org.
40
+ * @param {RetrieveOptions} [options] The command's flags.
41
+ * @returns {Promise<RetrieveEnvelope>} The retrieve envelope.
42
+ */
43
+ export async function runRetrieve(connection, options = {}) {
44
+ const version = connection.getApiVersion();
45
+ try {
46
+ return await new ConnectionRetrieve(connection).retrieveFor(options);
47
+ }
48
+ finally {
49
+ connection.setApiVersion(version);
50
+ }
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sf-plugin-data",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Salesforce CLI bootstrap for portable Data360 configuration",
5
5
  "author": {
6
6
  "name": "Jörn Berkefeld",