@sharinpix/sharinpix-sf-cli 1.8.0 → 1.9.1
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 +74 -2
- package/lib/commands/sharinpix/form/csv2json.d.ts +12 -0
- package/lib/commands/sharinpix/form/csv2json.js +159 -0
- package/lib/commands/sharinpix/form/csv2json.js.map +1 -0
- package/lib/commands/sharinpix/form/json2csv.d.ts +12 -0
- package/lib/commands/sharinpix/form/json2csv.js +97 -0
- package/lib/commands/sharinpix/form/json2csv.js.map +1 -0
- package/lib/commands/sharinpix/form/pull.d.ts +1 -0
- package/lib/commands/sharinpix/form/pull.js +9 -1
- package/lib/commands/sharinpix/form/pull.js.map +1 -1
- package/lib/commands/sharinpix/form/push.d.ts +1 -0
- package/lib/commands/sharinpix/form/push.js +47 -60
- package/lib/commands/sharinpix/form/push.js.map +1 -1
- package/lib/commands/sharinpix/permission/push.js +29 -40
- package/lib/commands/sharinpix/permission/push.js.map +1 -1
- package/lib/helpers/form/elementKeyOrder.d.ts +1 -0
- package/lib/helpers/form/elementKeyOrder.js +53 -0
- package/lib/helpers/form/elementKeyOrder.js.map +1 -0
- package/lib/helpers/utils.d.ts +9 -0
- package/lib/helpers/utils.js +57 -0
- package/lib/helpers/utils.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/messages/sharinpix.form.csv2json.md +49 -0
- package/messages/sharinpix.form.json2csv.md +37 -0
- package/messages/sharinpix.form.pull.md +12 -0
- package/messages/sharinpix.form.push.md +12 -0
- package/oclif.lock +55 -11
- package/oclif.manifest.json +123 -3
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -127,20 +127,73 @@ Both `form push` and `permission push` commands support a `--delete/-d` flag tha
|
|
|
127
127
|
## Commands
|
|
128
128
|
|
|
129
129
|
<!-- commands -->
|
|
130
|
+
* [`sf sharinpix form csv2json`](#sf-sharinpix-form-csv2json)
|
|
131
|
+
* [`sf sharinpix form json2csv`](#sf-sharinpix-form-json2csv)
|
|
130
132
|
* [`sf sharinpix form pull`](#sf-sharinpix-form-pull)
|
|
131
133
|
* [`sf sharinpix form push`](#sf-sharinpix-form-push)
|
|
132
134
|
* [`sf sharinpix permission pull`](#sf-sharinpix-permission-pull)
|
|
133
135
|
* [`sf sharinpix permission push`](#sf-sharinpix-permission-push)
|
|
134
136
|
|
|
137
|
+
## `sf sharinpix form csv2json`
|
|
138
|
+
|
|
139
|
+
Generate JSON files from SharinPix form CSV definitions.
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
USAGE
|
|
143
|
+
$ sf sharinpix form csv2json [--json] [--flags-dir <value>]
|
|
144
|
+
|
|
145
|
+
GLOBAL FLAGS
|
|
146
|
+
--flags-dir=<value> Import flag values from a directory.
|
|
147
|
+
--json Format output as json.
|
|
148
|
+
|
|
149
|
+
DESCRIPTION
|
|
150
|
+
Generate JSON files from SharinPix form CSV definitions.
|
|
151
|
+
|
|
152
|
+
Scans the local `sharinpix/forms` directory for SharinPix form CSV definition files and generates corresponding JSON
|
|
153
|
+
files. Each CSV file is expected to have a header row of element keys and one row per element, as produced by the
|
|
154
|
+
json2csv command.
|
|
155
|
+
|
|
156
|
+
EXAMPLES
|
|
157
|
+
Generate JSON files for all local SharinPix form CSV definitions:
|
|
158
|
+
|
|
159
|
+
$ sf sharinpix form csv2json
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## `sf sharinpix form json2csv`
|
|
163
|
+
|
|
164
|
+
Generate CSV files from SharinPix form JSON definitions.
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
USAGE
|
|
168
|
+
$ sf sharinpix form json2csv [--json] [--flags-dir <value>]
|
|
169
|
+
|
|
170
|
+
GLOBAL FLAGS
|
|
171
|
+
--flags-dir=<value> Import flag values from a directory.
|
|
172
|
+
--json Format output as json.
|
|
173
|
+
|
|
174
|
+
DESCRIPTION
|
|
175
|
+
Generate CSV files from SharinPix form JSON definitions.
|
|
176
|
+
|
|
177
|
+
Scans the local `sharinpix/forms` directory for SharinPix form JSON definition files and generates corresponding CSV
|
|
178
|
+
files. The CSV files contain a header row built from all element keys and one row per element. Nested values are
|
|
179
|
+
stringified.
|
|
180
|
+
|
|
181
|
+
EXAMPLES
|
|
182
|
+
Generate CSV files for all local SharinPix form JSON definitions:
|
|
183
|
+
|
|
184
|
+
$ sf sharinpix form json2csv
|
|
185
|
+
```
|
|
186
|
+
|
|
135
187
|
## `sf sharinpix form pull`
|
|
136
188
|
|
|
137
189
|
Pull SharinPix form templates from Salesforce org.
|
|
138
190
|
|
|
139
191
|
```
|
|
140
192
|
USAGE
|
|
141
|
-
$ sf sharinpix form pull -o <value> [--json] [--flags-dir <value>]
|
|
193
|
+
$ sf sharinpix form pull -o <value> [--json] [--flags-dir <value>] [-c]
|
|
142
194
|
|
|
143
195
|
FLAGS
|
|
196
|
+
-c, --csv Also generate CSV files from the downloaded JSON form definitions.
|
|
144
197
|
-o, --org=<value> (required) The Salesforce org to pull form templates from.
|
|
145
198
|
|
|
146
199
|
GLOBAL FLAGS
|
|
@@ -163,7 +216,16 @@ EXAMPLES
|
|
|
163
216
|
|
|
164
217
|
$ sf sharinpix form pull --target-org myorg@example.com
|
|
165
218
|
|
|
219
|
+
Pull form templates and also generate CSV files:
|
|
220
|
+
|
|
221
|
+
$ sf sharinpix form pull --csv
|
|
222
|
+
|
|
166
223
|
FLAG DESCRIPTIONS
|
|
224
|
+
-c, --csv Also generate CSV files from the downloaded JSON form definitions.
|
|
225
|
+
|
|
226
|
+
When enabled, after downloading all form templates as JSON into `sharinpix/forms`, the command runs the JSON-to-CSV
|
|
227
|
+
conversion (same as `sharinpix:form:json2csv`).
|
|
228
|
+
|
|
167
229
|
-o, --org=<value> The Salesforce org to pull form templates from.
|
|
168
230
|
|
|
169
231
|
The target Salesforce org containing the SharinPix form templates to be pulled.
|
|
@@ -175,9 +237,10 @@ Push SharinPix form templates to Salesforce org.
|
|
|
175
237
|
|
|
176
238
|
```
|
|
177
239
|
USAGE
|
|
178
|
-
$ sf sharinpix form push -o <value> [--json] [--flags-dir <value>] [-d]
|
|
240
|
+
$ sf sharinpix form push -o <value> [--json] [--flags-dir <value>] [-d] [-c]
|
|
179
241
|
|
|
180
242
|
FLAGS
|
|
243
|
+
-c, --csv Convert local CSV files to JSON before pushing.
|
|
181
244
|
-d, --delete Delete form templates from org that no longer have corresponding local files.
|
|
182
245
|
-o, --org=<value> (required) The Salesforce org to push form templates to.
|
|
183
246
|
|
|
@@ -205,7 +268,16 @@ EXAMPLES
|
|
|
205
268
|
|
|
206
269
|
$ sf sharinpix form push --delete
|
|
207
270
|
|
|
271
|
+
Convert CSV to JSON, then push:
|
|
272
|
+
|
|
273
|
+
$ sf sharinpix form push --csv
|
|
274
|
+
|
|
208
275
|
FLAG DESCRIPTIONS
|
|
276
|
+
-c, --csv Convert local CSV files to JSON before pushing.
|
|
277
|
+
|
|
278
|
+
When enabled, the command first runs the CSV-to-JSON conversion (same as `sharinpix:form:csv2json`) on files in
|
|
279
|
+
`sharinpix/forms`, then proceeds to push the resulting JSON files.
|
|
280
|
+
|
|
209
281
|
-d, --delete Delete form templates from org that no longer have corresponding local files.
|
|
210
282
|
|
|
211
283
|
When enabled, this flag will delete form template records from the Salesforce org if their corresponding local JSON
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SfCommand } from '@salesforce/sf-plugins-core';
|
|
2
|
+
export type Csv2JsonResult = {
|
|
3
|
+
name: string;
|
|
4
|
+
converted: number;
|
|
5
|
+
skipped: number;
|
|
6
|
+
};
|
|
7
|
+
export default class Csv2Json extends SfCommand<Csv2JsonResult> {
|
|
8
|
+
static readonly summary: string;
|
|
9
|
+
static readonly description: string;
|
|
10
|
+
static readonly examples: string[];
|
|
11
|
+
run(): Promise<Csv2JsonResult>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { parse } from 'csv-parse/sync';
|
|
4
|
+
import { SfCommand } from '@salesforce/sf-plugins-core';
|
|
5
|
+
import { Messages } from '@salesforce/core';
|
|
6
|
+
import { getCsvFiles, formatErrorMessage, orderElementKeys } from '../../../helpers/utils.js';
|
|
7
|
+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
|
|
8
|
+
const messages = Messages.loadMessages('@sharinpix/sharinpix-sf-cli', 'sharinpix.form.csv2json');
|
|
9
|
+
const FORMS_DIRECTORY = 'sharinpix/forms';
|
|
10
|
+
const NUMERIC_FIELDS = ['index', 'max', 'min', 'step', 'rows', 'cols'];
|
|
11
|
+
const CSV_PARSE_OPTIONS = {
|
|
12
|
+
skipEmptyLines: true,
|
|
13
|
+
relaxColumnCount: true,
|
|
14
|
+
relaxQuotes: true,
|
|
15
|
+
};
|
|
16
|
+
function parseCsvRows(content) {
|
|
17
|
+
return parse(content, CSV_PARSE_OPTIONS);
|
|
18
|
+
}
|
|
19
|
+
function parseCellValue(header, rawValue) {
|
|
20
|
+
const trimmed = rawValue.trim();
|
|
21
|
+
if (trimmed === '')
|
|
22
|
+
return '';
|
|
23
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
24
|
+
try {
|
|
25
|
+
const parsed = JSON.parse(trimmed);
|
|
26
|
+
if (typeof parsed === 'object' && parsed !== null)
|
|
27
|
+
return parsed;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Ignore parse errors, continue with other type detection
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (NUMERIC_FIELDS.includes(header)) {
|
|
34
|
+
const numValue = Number(trimmed);
|
|
35
|
+
const numberPattern = /^-?\d+(\.\d+)?([eE][+-]?\d+)?$/;
|
|
36
|
+
if (!Number.isNaN(numValue) && isFinite(numValue) && numberPattern.test(trimmed)) {
|
|
37
|
+
return numValue;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return trimmed;
|
|
41
|
+
}
|
|
42
|
+
function parseElementRow(row, headers) {
|
|
43
|
+
const values = new Map();
|
|
44
|
+
for (let index = 0; index < headers.length; index++) {
|
|
45
|
+
const header = headers[index];
|
|
46
|
+
const rawValue = row[index] ?? '';
|
|
47
|
+
if (header && rawValue !== '') {
|
|
48
|
+
values.set(header, parseCellValue(header, rawValue));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const orderedKeys = orderElementKeys(Array.from(values.keys()));
|
|
52
|
+
const element = {};
|
|
53
|
+
for (const key of orderedKeys) {
|
|
54
|
+
element[key] = values.get(key);
|
|
55
|
+
}
|
|
56
|
+
return element;
|
|
57
|
+
}
|
|
58
|
+
function preserveElementTypes(csvElements, originalElements) {
|
|
59
|
+
const originalElementsById = new Map(originalElements.map((el) => [String(el.id), el]));
|
|
60
|
+
return csvElements.map((csvElement) => {
|
|
61
|
+
const typedElement = { ...csvElement };
|
|
62
|
+
const originalElement = originalElementsById.get(String(csvElement.id));
|
|
63
|
+
if (originalElement) {
|
|
64
|
+
for (const [key, csvValue] of Object.entries(csvElement)) {
|
|
65
|
+
const originalValue = originalElement[key];
|
|
66
|
+
if (typeof csvValue === 'string' && (csvValue === 'true' || csvValue === 'false'))
|
|
67
|
+
if (typeof originalValue === 'boolean')
|
|
68
|
+
typedElement[key] = csvValue === 'true';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return typedElement;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function mergeElementsIntoJson(existingJson, elements) {
|
|
75
|
+
const originalElements = Array.isArray(existingJson.elements) ? existingJson.elements : [];
|
|
76
|
+
const typedElements = preserveElementTypes(elements, originalElements);
|
|
77
|
+
const merged = {};
|
|
78
|
+
for (const [key, value] of Object.entries(existingJson)) {
|
|
79
|
+
merged[key] = key === 'elements' ? typedElements : value;
|
|
80
|
+
}
|
|
81
|
+
return merged;
|
|
82
|
+
}
|
|
83
|
+
async function loadAndValidateJson(jsonPath) {
|
|
84
|
+
const jsonFileName = path.basename(jsonPath);
|
|
85
|
+
let content;
|
|
86
|
+
try {
|
|
87
|
+
content = await fs.promises.readFile(jsonPath, 'utf8');
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error.code === 'ENOENT')
|
|
91
|
+
throw new Error(messages.getMessage('error.jsonNotFound', [jsonFileName]));
|
|
92
|
+
throw new Error(messages.getMessage('error.jsonReadFailed', [jsonFileName, formatErrorMessage(error)]));
|
|
93
|
+
}
|
|
94
|
+
let existingJson;
|
|
95
|
+
try {
|
|
96
|
+
existingJson = JSON.parse(content);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
throw new Error(messages.getMessage('error.jsonInvalid', [jsonFileName, formatErrorMessage(error)]));
|
|
100
|
+
}
|
|
101
|
+
if (typeof existingJson !== 'object' || existingJson === null || Array.isArray(existingJson))
|
|
102
|
+
throw new Error(messages.getMessage('error.jsonNotObject', [jsonFileName]));
|
|
103
|
+
return existingJson;
|
|
104
|
+
}
|
|
105
|
+
async function processCsvFile(filePath) {
|
|
106
|
+
const fileName = path.basename(filePath);
|
|
107
|
+
try {
|
|
108
|
+
const content = await fs.promises.readFile(filePath, 'utf8');
|
|
109
|
+
const rows = parseCsvRows(content);
|
|
110
|
+
if (rows.length < 2)
|
|
111
|
+
throw new Error(messages.getMessage('error.noDataRows'));
|
|
112
|
+
const headers = rows[0];
|
|
113
|
+
const elements = [];
|
|
114
|
+
for (let i = 1; i < rows.length; i++) {
|
|
115
|
+
elements.push(parseElementRow(rows[i], headers));
|
|
116
|
+
}
|
|
117
|
+
const jsonPath = filePath.replace(/\.csv$/i, '.json');
|
|
118
|
+
const existingJson = await loadAndValidateJson(jsonPath);
|
|
119
|
+
const mergedJson = mergeElementsIntoJson(existingJson, elements);
|
|
120
|
+
await fs.promises.writeFile(jsonPath, JSON.stringify(mergedJson, null, 2), 'utf8');
|
|
121
|
+
return { success: true, fileName };
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
return { success: false, fileName, reason: formatErrorMessage(error) };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export default class Csv2Json extends SfCommand {
|
|
128
|
+
static summary = messages.getMessage('summary');
|
|
129
|
+
static description = messages.getMessage('description');
|
|
130
|
+
static examples = messages.getMessages('examples');
|
|
131
|
+
async run() {
|
|
132
|
+
let files;
|
|
133
|
+
try {
|
|
134
|
+
files = getCsvFiles(FORMS_DIRECTORY);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
this.warn(messages.getMessage('error.directoryNotFound', [FORMS_DIRECTORY, formatErrorMessage(error)]));
|
|
138
|
+
return { name: 'OK', converted: 0, skipped: 0 };
|
|
139
|
+
}
|
|
140
|
+
const results = await Promise.all(files.map((file) => processCsvFile(file)));
|
|
141
|
+
let converted = 0;
|
|
142
|
+
let skipped = 0;
|
|
143
|
+
results.forEach((result) => {
|
|
144
|
+
if (result.success) {
|
|
145
|
+
converted++;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.log(messages.getMessage('info.skipped', [result.fileName, result.reason]));
|
|
149
|
+
skipped++;
|
|
150
|
+
});
|
|
151
|
+
this.log(messages.getMessage('info.summary', [converted, skipped]));
|
|
152
|
+
return {
|
|
153
|
+
name: 'OK',
|
|
154
|
+
converted,
|
|
155
|
+
skipped,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=csv2json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv2json.js","sourceRoot":"","sources":["../../../../src/commands/sharinpix/form/csv2json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM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,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,cAAc,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;QAEpE,OAAO;YACL,IAAI,EAAE,IAAI;YACV,SAAS;YACT,OAAO;SACR,CAAC;IACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SfCommand } from '@salesforce/sf-plugins-core';
|
|
2
|
+
export type Json2CsvResult = {
|
|
3
|
+
name: string;
|
|
4
|
+
converted: number;
|
|
5
|
+
skipped: number;
|
|
6
|
+
};
|
|
7
|
+
export default class Json2Csv extends SfCommand<Json2CsvResult> {
|
|
8
|
+
static readonly summary: string;
|
|
9
|
+
static readonly description: string;
|
|
10
|
+
static readonly examples: string[];
|
|
11
|
+
run(): Promise<Json2CsvResult>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { stringify } from 'csv-stringify/sync';
|
|
4
|
+
import { SfCommand } from '@salesforce/sf-plugins-core';
|
|
5
|
+
import { Messages } from '@salesforce/core';
|
|
6
|
+
import { getJsonFiles, formatErrorMessage, orderElementKeys } from '../../../helpers/utils.js';
|
|
7
|
+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
|
|
8
|
+
const messages = Messages.loadMessages('@sharinpix/sharinpix-sf-cli', 'sharinpix.form.json2csv');
|
|
9
|
+
const FORMS_DIRECTORY = 'sharinpix/forms';
|
|
10
|
+
const CSV_OPTIONS = {
|
|
11
|
+
quotedMatch: /[",\n\r]/,
|
|
12
|
+
quotedEmpty: false,
|
|
13
|
+
escape: '"',
|
|
14
|
+
};
|
|
15
|
+
function formatCell(value) {
|
|
16
|
+
if (value === null || value === undefined)
|
|
17
|
+
return '';
|
|
18
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
19
|
+
return String(value);
|
|
20
|
+
if (Array.isArray(value) || typeof value === 'object')
|
|
21
|
+
return JSON.stringify(value);
|
|
22
|
+
return String(value);
|
|
23
|
+
}
|
|
24
|
+
function buildHeaders(elements) {
|
|
25
|
+
const allKeys = new Set();
|
|
26
|
+
for (const element of elements) {
|
|
27
|
+
for (const key of Object.keys(element)) {
|
|
28
|
+
allKeys.add(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return orderElementKeys(Array.from(allKeys));
|
|
32
|
+
}
|
|
33
|
+
function generateCsvContent(headers, elements) {
|
|
34
|
+
const rows = [headers];
|
|
35
|
+
for (const element of elements) {
|
|
36
|
+
const row = headers.map((key) => formatCell(element[key]));
|
|
37
|
+
rows.push(row);
|
|
38
|
+
}
|
|
39
|
+
return stringify(rows, CSV_OPTIONS);
|
|
40
|
+
}
|
|
41
|
+
function extractElements(parsed) {
|
|
42
|
+
const { elements } = parsed;
|
|
43
|
+
if (!Array.isArray(elements))
|
|
44
|
+
throw new Error(messages.getMessage('error.noElementsArray'));
|
|
45
|
+
if (elements.length === 0)
|
|
46
|
+
throw new Error(messages.getMessage('error.emptyElementsArray'));
|
|
47
|
+
return elements;
|
|
48
|
+
}
|
|
49
|
+
async function processJsonFile(filePath) {
|
|
50
|
+
const fileName = path.basename(filePath);
|
|
51
|
+
try {
|
|
52
|
+
const content = await fs.promises.readFile(filePath, 'utf8');
|
|
53
|
+
const parsed = JSON.parse(content);
|
|
54
|
+
const elements = extractElements(parsed);
|
|
55
|
+
const headers = buildHeaders(elements);
|
|
56
|
+
const csvContent = generateCsvContent(headers, elements);
|
|
57
|
+
const csvPath = filePath.replace(/\.json$/i, '.csv');
|
|
58
|
+
await fs.promises.writeFile(csvPath, csvContent, 'utf8');
|
|
59
|
+
return { success: true, fileName };
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return { success: false, fileName, reason: formatErrorMessage(error) };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export default class Json2Csv extends SfCommand {
|
|
66
|
+
static summary = messages.getMessage('summary');
|
|
67
|
+
static description = messages.getMessage('description');
|
|
68
|
+
static examples = messages.getMessages('examples');
|
|
69
|
+
async run() {
|
|
70
|
+
let files;
|
|
71
|
+
try {
|
|
72
|
+
files = getJsonFiles(FORMS_DIRECTORY);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.warn(messages.getMessage('error.directoryNotFound', [FORMS_DIRECTORY, formatErrorMessage(error)]));
|
|
76
|
+
return { name: 'OK', converted: 0, skipped: 0 };
|
|
77
|
+
}
|
|
78
|
+
const results = await Promise.all(files.map((file) => processJsonFile(file)));
|
|
79
|
+
let converted = 0;
|
|
80
|
+
let skipped = 0;
|
|
81
|
+
results.forEach((result) => {
|
|
82
|
+
if (result.success) {
|
|
83
|
+
converted++;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.log(messages.getMessage('info.skipped', [result.fileName, result.reason]));
|
|
87
|
+
skipped++;
|
|
88
|
+
});
|
|
89
|
+
this.log(messages.getMessage('info.summary', [converted, skipped]));
|
|
90
|
+
return {
|
|
91
|
+
name: 'OK',
|
|
92
|
+
converted,
|
|
93
|
+
skipped,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=json2csv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json2csv.js","sourceRoot":"","sources":["../../../../src/commands/sharinpix/form/json2csv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAA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CAAC,CAAC;QAChB,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YACzB,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,SAAS,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,cAAc,EAAE,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAChF,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,cAAc,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;QAEpE,OAAO;YACL,IAAI,EAAE,IAAI;YACV,SAAS;YACT,OAAO;SACR,CAAC;IACJ,CAAC"}
|
|
@@ -10,6 +10,7 @@ export default class Pull extends SfCommand<PullResult> {
|
|
|
10
10
|
static readonly examples: string[];
|
|
11
11
|
static readonly flags: {
|
|
12
12
|
org: import("@oclif/core/interfaces").OptionFlag<import("@salesforce/core").Org, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
csv: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
13
14
|
};
|
|
14
15
|
run(): Promise<PullResult>;
|
|
15
16
|
}
|
|
@@ -16,6 +16,12 @@ export default class Pull extends SfCommand {
|
|
|
16
16
|
summary: messages.getMessage('flags.org.summary'),
|
|
17
17
|
description: messages.getMessage('flags.org.description'),
|
|
18
18
|
}),
|
|
19
|
+
csv: Flags.boolean({
|
|
20
|
+
char: 'c',
|
|
21
|
+
summary: messages.getMessage('flags.csv.summary'),
|
|
22
|
+
description: messages.getMessage('flags.csv.description'),
|
|
23
|
+
default: false,
|
|
24
|
+
}),
|
|
19
25
|
};
|
|
20
26
|
async run() {
|
|
21
27
|
const { flags } = await this.parse(Pull);
|
|
@@ -41,12 +47,14 @@ export default class Pull extends SfCommand {
|
|
|
41
47
|
formsDownloaded++;
|
|
42
48
|
}
|
|
43
49
|
catch (error) {
|
|
44
|
-
// Skip forms that can't be fetched (e.g., network errors, invalid URLs)
|
|
45
50
|
this.warn(`Failed to fetch form template ${record.Name}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
46
51
|
formsFailed++;
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
54
|
this.log(`Successfully downloaded ${formsDownloaded} form template(s). ${formsFailed} failed.`);
|
|
55
|
+
if (flags.csv) {
|
|
56
|
+
await this.config.runCommand('sharinpix:form:json2csv', []);
|
|
57
|
+
}
|
|
50
58
|
return {
|
|
51
59
|
name: 'OK',
|
|
52
60
|
formsDownloaded,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pull.js","sourceRoot":"","sources":["../../../../src/commands/sharinpix/form/pull.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,qCAAqC;AACrC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAE/D,QAAQ,CAAC,kCAAkC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7D,MAAM,QAAQ,GAAG,QAAQ,CAAC,YAAY,CAAC,6BAA6B,EAAE,qBAAqB,CAAC,CAAC;AAe7F,MAAM,CAAC,OAAO,OAAO,IAAK,SAAQ,SAAqB;IAC9C,MAAM,CAAU,OAAO,GAAG,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IACzD,MAAM,CAAU,WAAW,GAAG,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;IACjE,MAAM,CAAU,QAAQ,GAAG,QAAQ,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;IAE5D,MAAM,CAA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AAC;gBACrD,EAAE,CAAC,aAAa,CAAC,mBAAmB,YAAY,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;gBACxF,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;gBACzF,eAAe,EAAE,CAAC;YACpB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,IAAI,CACP,iCAAiC,MAAM,CAAC,IAAI,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CAC5G,CAAC;gBACF,WAAW,EAAE,CAAC;YAChB,CAAC;QACH,CAAC;QAED,IAAI,CAAC,GAAG,CAAC,2BAA2B,eAAe,sBAAsB,WAAW,UAAU,CAAC,CAAC;QAEhG,IAAI,KAAK,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC;QAC9D,CAAC;QAED,OAAO;YACL,IAAI,EAAE,IAAI;YACV,eAAe;YACf,WAAW;SACZ,CAAC;IACJ,CAAC"}
|
|
@@ -13,6 +13,7 @@ export default class Push extends SfCommand<PushResult> {
|
|
|
13
13
|
static readonly flags: {
|
|
14
14
|
org: import("@oclif/core/interfaces").OptionFlag<import("@salesforce/core").Org, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
15
|
delete: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
csv: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
16
17
|
};
|
|
17
18
|
run(): Promise<PushResult>;
|
|
18
19
|
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
/* eslint-disable
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
2
|
/* eslint-disable no-await-in-loop */
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import path from 'node:path';
|
|
5
3
|
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
|
|
6
4
|
import { Messages } from '@salesforce/core';
|
|
7
|
-
import { isJsonEqual } from '../../../helpers/utils.js';
|
|
5
|
+
import { isJsonEqual, readJsonFile, getNameFromJson, getJsonFiles, fetchJson, formatErrorMessage, } from '../../../helpers/utils.js';
|
|
8
6
|
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
|
|
9
7
|
const messages = Messages.loadMessages('@sharinpix/sharinpix-sf-cli', 'sharinpix.form.push');
|
|
10
8
|
export default class Push extends SfCommand {
|
|
@@ -23,14 +21,20 @@ export default class Push extends SfCommand {
|
|
|
23
21
|
description: messages.getMessage('flags.delete.description'),
|
|
24
22
|
default: false,
|
|
25
23
|
}),
|
|
24
|
+
csv: Flags.boolean({
|
|
25
|
+
char: 'c',
|
|
26
|
+
summary: messages.getMessage('flags.csv.summary'),
|
|
27
|
+
description: messages.getMessage('flags.csv.description'),
|
|
28
|
+
default: false,
|
|
29
|
+
}),
|
|
26
30
|
};
|
|
27
31
|
async run() {
|
|
28
32
|
const { flags } = await this.parse(Push);
|
|
29
33
|
const connection = flags.org.getConnection('63.0');
|
|
30
|
-
|
|
31
|
-
.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
if (flags.csv) {
|
|
35
|
+
await this.config.runCommand('sharinpix:form:csv2json', []);
|
|
36
|
+
}
|
|
37
|
+
const files = getJsonFiles('sharinpix/forms');
|
|
34
38
|
const body = {
|
|
35
39
|
// eslint-disable-next-line camelcase
|
|
36
40
|
form_template_create: true,
|
|
@@ -45,40 +49,23 @@ export default class Push extends SfCommand {
|
|
|
45
49
|
let skipped = 0;
|
|
46
50
|
let deleted = 0;
|
|
47
51
|
for (const file of files) {
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const fileName = json.name;
|
|
52
|
+
const processFile = async () => {
|
|
53
|
+
const json = readJsonFile(file);
|
|
54
|
+
const fileName = getNameFromJson(json);
|
|
52
55
|
const existingRecord = existingMap.get(fileName);
|
|
53
56
|
const existingId = existingRecord?.Id ?? null;
|
|
54
57
|
if (existingRecord) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
Accept: 'application/json',
|
|
59
|
-
},
|
|
58
|
+
const existingJson = await fetchJson(existingRecord.sharinpix__FormUrl__c + '.json').catch((error) => {
|
|
59
|
+
this.warn(`Failed to check changes for ${fileName}, proceeding with update: ${formatErrorMessage(error)}`);
|
|
60
|
+
return null;
|
|
60
61
|
});
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (existingJson) {
|
|
66
|
-
try {
|
|
67
|
-
if (isJsonEqual(json, existingJson)) {
|
|
68
|
-
this.log(messages.getMessage('info.skipped', [fileName]));
|
|
69
|
-
skipped++;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
catch (parseError) {
|
|
74
|
-
this.warn(`Failed to parse stored JSON for ${fileName}, proceeding with update`);
|
|
75
|
-
failed++;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
62
|
+
if (existingJson && isJsonEqual(json, existingJson)) {
|
|
63
|
+
this.log(messages.getMessage('info.skipped', [fileName]));
|
|
64
|
+
skipped++;
|
|
65
|
+
return;
|
|
78
66
|
}
|
|
79
67
|
}
|
|
80
|
-
|
|
81
|
-
const responseData = await (await fetch(`${responseToken.host}/api/v1/form/templates`, {
|
|
68
|
+
const response = await fetch(`${responseToken.host}/api/v1/form/templates`, {
|
|
82
69
|
method: 'POST',
|
|
83
70
|
body: JSON.stringify({
|
|
84
71
|
sfid: existingId ?? undefined,
|
|
@@ -92,7 +79,11 @@ export default class Push extends SfCommand {
|
|
|
92
79
|
Accept: 'application/json',
|
|
93
80
|
Authorization: `Bearer ${responseToken.token}`,
|
|
94
81
|
},
|
|
95
|
-
})
|
|
82
|
+
});
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
85
|
+
}
|
|
86
|
+
const responseData = (await response.json());
|
|
96
87
|
const formTemplateJson = JSON.stringify({
|
|
97
88
|
...json,
|
|
98
89
|
url: responseData.url,
|
|
@@ -103,41 +94,37 @@ export default class Push extends SfCommand {
|
|
|
103
94
|
url: responseData.url,
|
|
104
95
|
formTemplateJson,
|
|
105
96
|
});
|
|
106
|
-
|
|
107
|
-
|
|
97
|
+
this.log(messages.getMessage(existingId ? 'info.updated' : 'info.created', [fileName]));
|
|
98
|
+
uploaded++;
|
|
99
|
+
};
|
|
100
|
+
await processFile().catch((error) => {
|
|
101
|
+
try {
|
|
102
|
+
const json = readJsonFile(file);
|
|
103
|
+
const fileName = getNameFromJson(json);
|
|
104
|
+
this.warn(`Failed to push form template ${fileName}: ${formatErrorMessage(error)}`);
|
|
108
105
|
}
|
|
109
|
-
|
|
110
|
-
this.
|
|
106
|
+
catch {
|
|
107
|
+
this.warn(`Failed to parse JSON for form template in ${file}: ${formatErrorMessage(error)}`);
|
|
111
108
|
}
|
|
112
|
-
uploaded++;
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
const fileContent = fs.readFileSync(file, 'utf8');
|
|
116
|
-
const json = JSON.parse(fileContent);
|
|
117
|
-
const fileName = json.name;
|
|
118
|
-
this.warn(`Failed to push form template ${fileName}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
119
109
|
failed++;
|
|
120
|
-
|
|
121
|
-
}
|
|
110
|
+
});
|
|
122
111
|
}
|
|
123
|
-
// Handle deletion of records that no longer have corresponding local files
|
|
124
112
|
if (flags.delete) {
|
|
125
113
|
const localFileNames = new Set(files.map((file) => {
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
return json.name;
|
|
114
|
+
const json = readJsonFile(file);
|
|
115
|
+
return getNameFromJson(json);
|
|
129
116
|
}));
|
|
130
117
|
for (const [recordName, record] of existingMap) {
|
|
131
118
|
if (!localFileNames.has(recordName)) {
|
|
132
|
-
|
|
133
|
-
|
|
119
|
+
await connection.sobject('sharinpix__FormTemplate__c').delete(record.Id)
|
|
120
|
+
.then(() => {
|
|
134
121
|
this.log(messages.getMessage('info.deleted', [recordName]));
|
|
135
122
|
deleted++;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
this.warn(`Failed to delete form template ${recordName}: ${error
|
|
123
|
+
})
|
|
124
|
+
.catch((error) => {
|
|
125
|
+
this.warn(`Failed to delete form template ${recordName}: ${formatErrorMessage(error)}`);
|
|
139
126
|
failed++;
|
|
140
|
-
}
|
|
127
|
+
});
|
|
141
128
|
}
|
|
142
129
|
}
|
|
143
130
|
}
|