@commercelayer/cli-plugin-provisioning 2.0.9 → 2.1.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/bin/run.js +3 -3
- package/lib/base.d.ts +8 -8
- package/lib/base.js +224 -165
- package/lib/commands/provisioning/create.d.ts +2 -2
- package/lib/commands/provisioning/create.js +46 -34
- package/lib/commands/provisioning/delete.d.ts +2 -2
- package/lib/commands/provisioning/delete.js +25 -14
- package/lib/commands/provisioning/exec.d.ts +1 -1
- package/lib/commands/provisioning/exec.js +22 -13
- package/lib/commands/provisioning/fetch.d.ts +3 -3
- package/lib/commands/provisioning/fetch.js +25 -17
- package/lib/commands/provisioning/get.d.ts +3 -3
- package/lib/commands/provisioning/get.js +8 -8
- package/lib/commands/provisioning/list.d.ts +3 -3
- package/lib/commands/provisioning/list.js +42 -38
- package/lib/commands/provisioning/noc.d.ts +1 -1
- package/lib/commands/provisioning/noc.js +1 -1
- package/lib/commands/provisioning/relationship.d.ts +3 -3
- package/lib/commands/provisioning/relationship.js +39 -23
- package/lib/commands/provisioning/resources.d.ts +1 -1
- package/lib/commands/provisioning/resources.js +22 -12
- package/lib/commands/provisioning/retrieve.d.ts +2 -2
- package/lib/commands/provisioning/retrieve.js +36 -30
- package/lib/commands/provisioning/update.d.ts +3 -3
- package/lib/commands/provisioning/update.js +60 -41
- package/lib/csv.d.ts +1 -1
- package/lib/csv.js +30 -27
- package/lib/lang/curl.d.ts +2 -2
- package/lib/lang/curl.js +4 -2
- package/lib/lang/index.d.ts +5 -5
- package/lib/lang/index.js +26 -18
- package/lib/lang/node.d.ts +2 -2
- package/lib/lang/node.js +7 -7
- package/lib/lang/request.d.ts +3 -3
- package/lib/lang/request.js +37 -30
- package/lib/lang/ruby.d.ts +2 -2
- package/lib/lang/ruby.js +1 -1
- package/lib/output.d.ts +1 -1
- package/lib/output.js +3 -3
- package/lib/util/resources/available.js +33 -8
- package/lib/util/resources/build.js +35 -19
- package/lib/util/resources/index.d.ts +3 -3
- package/lib/util/resources/index.js +3 -3
- package/lib/util/timezones.js +127 -115
- package/oclif.manifest.json +1 -1
- package/package.json +85 -87
package/lib/base.js
CHANGED
|
@@ -1,53 +1,57 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Flags = exports.cliux = exports.Args = exports.BaseFilterCommand = exports.BaseCommand = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const cli_core_1 = require("@commercelayer/cli-core");
|
|
7
|
+
const provisioning_sdk_1 = tslib_1.__importStar(require("@commercelayer/provisioning-sdk"));
|
|
5
8
|
const core_1 = require("@oclif/core");
|
|
6
|
-
Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return core_1.Flags; } });
|
|
7
9
|
Object.defineProperty(exports, "Args", { enumerable: true, get: function () { return core_1.Args; } });
|
|
8
10
|
Object.defineProperty(exports, "cliux", { enumerable: true, get: function () { return core_1.ux; } });
|
|
9
|
-
|
|
10
|
-
const output_1 = require("./output");
|
|
11
|
+
Object.defineProperty(exports, "Flags", { enumerable: true, get: function () { return core_1.Flags; } });
|
|
11
12
|
const csv_1 = require("./csv");
|
|
12
|
-
const fs_1 = require("fs");
|
|
13
|
-
const provisioning_sdk_1 = tslib_1.__importStar(require("@commercelayer/provisioning-sdk"));
|
|
14
13
|
const lang_1 = require("./lang");
|
|
15
|
-
const
|
|
14
|
+
const output_1 = require("./output");
|
|
15
|
+
const resources_1 = require("./util/resources");
|
|
16
16
|
// import { aliasExists, checkAlias, type CommandParams, loadCommandData, type ResourceOperation, saveCommandData } from './commands'
|
|
17
17
|
// import type { ResourceId, ResourceType } from '@commercelayer/provisioning-sdk/lib/cjs/resource'
|
|
18
|
-
const pkg = require(
|
|
18
|
+
const pkg = require("../package.json");
|
|
19
19
|
// export const FLAG_SAVE_PARAMS = 'save-args'
|
|
20
20
|
// export const FLAG_LOAD_PARAMS = 'load-args'
|
|
21
21
|
class BaseCommand extends core_1.Command {
|
|
22
22
|
static baseFlags = {
|
|
23
23
|
domain: core_1.Flags.string({
|
|
24
|
-
char:
|
|
24
|
+
char: "d",
|
|
25
25
|
required: false,
|
|
26
26
|
hidden: true,
|
|
27
|
-
env:
|
|
27
|
+
env: "CL_CLI_DOMAIN",
|
|
28
28
|
}),
|
|
29
29
|
accessToken: core_1.Flags.string({
|
|
30
30
|
hidden: true,
|
|
31
31
|
required: true,
|
|
32
|
-
env:
|
|
33
|
-
})
|
|
32
|
+
env: "CL_CLI_ACCESS_TOKEN",
|
|
33
|
+
}),
|
|
34
34
|
};
|
|
35
35
|
checkApplication(accessToken, kinds) {
|
|
36
36
|
const info = cli_core_1.clToken.decodeAccessToken(accessToken);
|
|
37
37
|
if (info === null)
|
|
38
|
-
this.error(
|
|
38
|
+
this.error("Invalid access token provided");
|
|
39
39
|
else if (!kinds.includes(info.application.kind))
|
|
40
|
-
this.error(`Invalid application kind: ${cli_core_1.clColor.msg.error(info.application.kind)}. Application kind must be one of the following: ${cli_core_1.clColor.cyanBright(kinds.join(
|
|
40
|
+
this.error(`Invalid application kind: ${cli_core_1.clColor.msg.error(info.application.kind)}. Application kind must be one of the following: ${cli_core_1.clColor.cyanBright(kinds.join(", "))}`);
|
|
41
41
|
return true;
|
|
42
42
|
}
|
|
43
43
|
checkResourceId(resource, resourceId, required = true) {
|
|
44
44
|
let res = resource;
|
|
45
45
|
let id = resourceId;
|
|
46
|
-
const si = res.indexOf(
|
|
46
|
+
const si = res.indexOf("/");
|
|
47
47
|
if (si >= 0) {
|
|
48
|
-
const rt = res.split(
|
|
48
|
+
const rt = res.split("/");
|
|
49
49
|
if (id && rt[1])
|
|
50
|
-
this.error(`Double definition of resource id: [${res}, ${id}]`, {
|
|
50
|
+
this.error(`Double definition of resource id: [${res}, ${id}]`, {
|
|
51
|
+
suggestions: [
|
|
52
|
+
`Define resource id as command argument (${cli_core_1.clColor.italic(id)}) or as part of the resource itself (${cli_core_1.clColor.italic(res)}) but not both`,
|
|
53
|
+
],
|
|
54
|
+
});
|
|
51
55
|
else
|
|
52
56
|
id = rt[1];
|
|
53
57
|
res = rt[0];
|
|
@@ -57,11 +61,11 @@ class BaseCommand extends core_1.Command {
|
|
|
57
61
|
if (id) {
|
|
58
62
|
if (singleton)
|
|
59
63
|
this.error(`Singleton resource ${cli_core_1.clColor.api.resource(res)} does not require id`);
|
|
60
|
-
if (id.includes(
|
|
64
|
+
if (id.includes("/"))
|
|
61
65
|
this.error(`Invalid resource id: ${cli_core_1.clColor.msg.error(id)}`);
|
|
62
66
|
}
|
|
63
67
|
else if (required && !singleton)
|
|
64
|
-
this.error(
|
|
68
|
+
this.error("Resource id not defined");
|
|
65
69
|
return {
|
|
66
70
|
res,
|
|
67
71
|
id,
|
|
@@ -70,10 +74,14 @@ class BaseCommand extends core_1.Command {
|
|
|
70
74
|
}
|
|
71
75
|
checkResource(res, { required = true, singular = false } = {}) {
|
|
72
76
|
if (!res && required)
|
|
73
|
-
this.error(
|
|
77
|
+
this.error("Resource type not defined");
|
|
74
78
|
const resource = (0, resources_1.findResource)(res, { singular });
|
|
75
79
|
if (resource === undefined)
|
|
76
|
-
this.error(`Invalid resource ${cli_core_1.clColor.style.error(res)}`, {
|
|
80
|
+
this.error(`Invalid resource ${cli_core_1.clColor.style.error(res)}`, {
|
|
81
|
+
suggestions: [
|
|
82
|
+
`Execute command ${cli_core_1.clColor.style.command("resources")} to get a list of all available CLI resources`,
|
|
83
|
+
],
|
|
84
|
+
});
|
|
77
85
|
return resource;
|
|
78
86
|
}
|
|
79
87
|
initCommerceLayer(flags, ...options) {
|
|
@@ -81,15 +89,15 @@ class BaseCommand extends core_1.Command {
|
|
|
81
89
|
const accessToken = flags.accessToken;
|
|
82
90
|
const userAgent = cli_core_1.clUtil.userAgent(this.config);
|
|
83
91
|
const cl = (0, provisioning_sdk_1.default)({ domain, accessToken, userAgent, ...options });
|
|
84
|
-
if (
|
|
92
|
+
if ("cl" in this)
|
|
85
93
|
this.cl = cl;
|
|
86
94
|
return cl;
|
|
87
95
|
}
|
|
88
96
|
checkOperation(sdk, name, attributes) {
|
|
89
97
|
if (!sdk[name]) {
|
|
90
98
|
// resource attributes reference, reference_origin and metadata are always updatable
|
|
91
|
-
if (
|
|
92
|
-
if (!Object.keys(attributes).some(attr => ![
|
|
99
|
+
if (name === "update" && attributes) {
|
|
100
|
+
if (!Object.keys(attributes).some((attr) => !["reference", "reference_origin", "metadata"].includes(attr)))
|
|
93
101
|
return true;
|
|
94
102
|
}
|
|
95
103
|
this.error(`Operation not supported for resource ${cli_core_1.clColor.api.resource(sdk.type())}: ${cli_core_1.clColor.msg.error(name)}`);
|
|
@@ -97,29 +105,35 @@ class BaseCommand extends core_1.Command {
|
|
|
97
105
|
return true;
|
|
98
106
|
}
|
|
99
107
|
printOutput(output, flags) {
|
|
100
|
-
if (output && !flags[
|
|
108
|
+
if (output && !flags["headers-only"])
|
|
101
109
|
this.log((0, output_1.formatOutput)(output, flags));
|
|
102
110
|
}
|
|
103
111
|
printHeaders(headers, flags) {
|
|
104
112
|
if (headers) {
|
|
105
|
-
if (flags.headers || flags[
|
|
106
|
-
this.log(
|
|
113
|
+
if (flags.headers || flags["headers-only"]) {
|
|
114
|
+
this.log("---------- Response Headers ----------");
|
|
107
115
|
if (Object.keys(headers).length === 0)
|
|
108
|
-
this.log(cli_core_1.clColor.italic(
|
|
116
|
+
this.log(cli_core_1.clColor.italic("No headers"));
|
|
109
117
|
else
|
|
110
118
|
this.log((0, output_1.formatOutput)(headers, flags));
|
|
111
|
-
this.log(
|
|
119
|
+
this.log("---------- ---------------- ----------");
|
|
112
120
|
}
|
|
113
121
|
}
|
|
114
122
|
}
|
|
115
123
|
printError(error, flags, args) {
|
|
116
124
|
let err = error;
|
|
117
125
|
if (provisioning_sdk_1.CommerceLayerProvisioningStatic.isApiError(err)) {
|
|
118
|
-
err =
|
|
126
|
+
err =
|
|
127
|
+
err.errors ||
|
|
128
|
+
`Unable to find resource of type ${cli_core_1.clColor.msg.error(args.resource)} and id ${cli_core_1.clColor.msg.error(args.id)}`;
|
|
119
129
|
}
|
|
120
130
|
else if (error.response) {
|
|
121
131
|
if (error.response.status === 401)
|
|
122
|
-
this.error(cli_core_1.clColor.bg.red(`${error.response.statusText} [${error.response.status}]`), {
|
|
132
|
+
this.error(cli_core_1.clColor.bg.red(`${error.response.statusText} [${error.response.status}]`), {
|
|
133
|
+
suggestions: [
|
|
134
|
+
"Execute login to get access to the selected resource",
|
|
135
|
+
],
|
|
136
|
+
});
|
|
123
137
|
else if (error.response.status === 500)
|
|
124
138
|
this.error(`We're sorry, but something went wrong (${error.response.status})`);
|
|
125
139
|
else if (error.response.status === 429)
|
|
@@ -131,14 +145,16 @@ class BaseCommand extends core_1.Command {
|
|
|
131
145
|
err = error.message;
|
|
132
146
|
this.error((0, output_1.formatOutput)(err, flags));
|
|
133
147
|
}
|
|
134
|
-
_keyvalFlag(flag, type =
|
|
148
|
+
_keyvalFlag(flag, type = "attribute") {
|
|
135
149
|
const param = {};
|
|
136
|
-
if (flag &&
|
|
137
|
-
flag.forEach(f => {
|
|
138
|
-
const eqi = f.indexOf(
|
|
150
|
+
if (flag && flag.length > 0) {
|
|
151
|
+
flag.forEach((f) => {
|
|
152
|
+
const eqi = f.indexOf("=");
|
|
139
153
|
if (eqi < 1)
|
|
140
154
|
this.error(`Invalid ${type.toLowerCase()} ${cli_core_1.clColor.msg.error(f)}`, {
|
|
141
|
-
suggestions: [
|
|
155
|
+
suggestions: [
|
|
156
|
+
`${cli_core_1.clText.capitalize(type)} flags must be defined using the format ${cli_core_1.clColor.cli.value("name=value")}`,
|
|
157
|
+
],
|
|
142
158
|
});
|
|
143
159
|
const name = f.substr(0, eqi);
|
|
144
160
|
const value = f.substr(eqi + 1);
|
|
@@ -150,10 +166,10 @@ class BaseCommand extends core_1.Command {
|
|
|
150
166
|
return param;
|
|
151
167
|
}
|
|
152
168
|
attributeFlag(flag) {
|
|
153
|
-
const attr = this._keyvalFlag(flag,
|
|
169
|
+
const attr = this._keyvalFlag(flag, "attribute");
|
|
154
170
|
const attributes = {};
|
|
155
171
|
Object.entries(attr).forEach(([k, v]) => {
|
|
156
|
-
attributes[k] =
|
|
172
|
+
attributes[k] = v === "null" ? null : v;
|
|
157
173
|
});
|
|
158
174
|
return attributes;
|
|
159
175
|
}
|
|
@@ -163,93 +179,99 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
163
179
|
static flags = {
|
|
164
180
|
...BaseCommand.flags,
|
|
165
181
|
include: core_1.Flags.string({
|
|
166
|
-
char:
|
|
182
|
+
char: "i",
|
|
167
183
|
multiple: true,
|
|
168
|
-
description:
|
|
184
|
+
description: "comma separated resources to include",
|
|
169
185
|
}),
|
|
170
186
|
fields: core_1.Flags.string({
|
|
171
|
-
char:
|
|
187
|
+
char: "f",
|
|
172
188
|
multiple: true,
|
|
173
|
-
description:
|
|
189
|
+
description: "comma separeted list of fields in the format [resourceType/]field1,field2,field3",
|
|
174
190
|
}),
|
|
175
191
|
json: core_1.Flags.boolean({
|
|
176
|
-
char:
|
|
177
|
-
description:
|
|
192
|
+
char: "j",
|
|
193
|
+
description: "convert output in standard JSON format",
|
|
178
194
|
}),
|
|
179
195
|
unformatted: core_1.Flags.boolean({
|
|
180
|
-
char:
|
|
181
|
-
description:
|
|
182
|
-
dependsOn: [
|
|
196
|
+
char: "u",
|
|
197
|
+
description: "print unformatted JSON output",
|
|
198
|
+
dependsOn: ["json"],
|
|
183
199
|
}),
|
|
184
200
|
raw: core_1.Flags.boolean({
|
|
185
|
-
char:
|
|
186
|
-
description:
|
|
201
|
+
char: "R",
|
|
202
|
+
description: "print out the raw API response",
|
|
187
203
|
hidden: false,
|
|
188
204
|
}),
|
|
189
205
|
doc: core_1.Flags.boolean({
|
|
190
|
-
description:
|
|
191
|
-
exclusive: [
|
|
192
|
-
helpGroup:
|
|
206
|
+
description: "show the CLI command in a specific language",
|
|
207
|
+
exclusive: ["raw"],
|
|
208
|
+
helpGroup: "documentation",
|
|
193
209
|
}),
|
|
194
210
|
lang: core_1.Flags.string({
|
|
195
|
-
char:
|
|
196
|
-
description:
|
|
211
|
+
char: "l",
|
|
212
|
+
description: "show the CLI command in the specified language syntax",
|
|
197
213
|
exclusive: lang_1.availableLanguages,
|
|
198
214
|
options: lang_1.availableLanguages,
|
|
199
|
-
dependsOn: [
|
|
200
|
-
helpGroup:
|
|
215
|
+
dependsOn: ["doc"],
|
|
216
|
+
helpGroup: "documentation",
|
|
201
217
|
}),
|
|
202
218
|
curl: core_1.Flags.boolean({
|
|
203
219
|
description: `show the equivalent ${lang_1.languageInfo.curl.label} of the CLI command`,
|
|
204
|
-
exclusive: [
|
|
205
|
-
parse: async () => await Promise.resolve(
|
|
206
|
-
hidden: !lang_1.availableLanguages.includes(
|
|
207
|
-
dependsOn: [
|
|
208
|
-
helpGroup:
|
|
220
|
+
exclusive: ["lang", ...lang_1.availableLanguages.filter((l) => l !== "curl")],
|
|
221
|
+
parse: async () => await Promise.resolve("curl"),
|
|
222
|
+
hidden: !lang_1.availableLanguages.includes("curl"),
|
|
223
|
+
dependsOn: ["doc"],
|
|
224
|
+
helpGroup: "documentation",
|
|
209
225
|
}),
|
|
210
226
|
node: core_1.Flags.boolean({
|
|
211
227
|
description: `show the equivalent ${lang_1.languageInfo.node.label} of the CLI command`,
|
|
212
|
-
exclusive: [
|
|
213
|
-
parse: async () => await Promise.resolve(
|
|
214
|
-
hidden: !lang_1.availableLanguages.includes(
|
|
215
|
-
dependsOn: [
|
|
216
|
-
helpGroup:
|
|
228
|
+
exclusive: ["lang", ...lang_1.availableLanguages.filter((l) => l !== "node")],
|
|
229
|
+
parse: async () => await Promise.resolve("node"),
|
|
230
|
+
hidden: !lang_1.availableLanguages.includes("node"),
|
|
231
|
+
dependsOn: ["doc"],
|
|
232
|
+
helpGroup: "documentation",
|
|
217
233
|
}),
|
|
218
234
|
/*
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
235
|
+
[FLAG_SAVE_PARAMS]: Flags.string({
|
|
236
|
+
description: 'save command data to file for future use',
|
|
237
|
+
}),
|
|
238
|
+
[FLAG_LOAD_PARAMS]: Flags.string({
|
|
239
|
+
description: 'load previously saved command arguments',
|
|
240
|
+
}),
|
|
241
|
+
*/
|
|
226
242
|
headers: core_1.Flags.boolean({
|
|
227
|
-
char:
|
|
228
|
-
description:
|
|
229
|
-
dependsOn: [
|
|
230
|
-
exclusive: [
|
|
243
|
+
char: "H",
|
|
244
|
+
description: "show response headers",
|
|
245
|
+
dependsOn: ["raw"],
|
|
246
|
+
exclusive: ["headers-only"],
|
|
247
|
+
}),
|
|
248
|
+
"headers-only": core_1.Flags.boolean({
|
|
249
|
+
char: "Y",
|
|
250
|
+
description: "show only response headers",
|
|
251
|
+
dependsOn: ["raw"],
|
|
252
|
+
exclusive: ["headers", "fields", "include"],
|
|
231
253
|
}),
|
|
232
|
-
'headers-only': core_1.Flags.boolean({
|
|
233
|
-
char: 'Y',
|
|
234
|
-
description: 'show only response headers',
|
|
235
|
-
dependsOn: ['raw'],
|
|
236
|
-
exclusive: ['headers', 'fields', 'include'],
|
|
237
|
-
})
|
|
238
254
|
};
|
|
239
255
|
static args = {
|
|
240
|
-
...BaseCommand.args
|
|
256
|
+
...BaseCommand.args,
|
|
241
257
|
};
|
|
242
258
|
// INIT (override)
|
|
243
259
|
async init() {
|
|
244
260
|
// Check for plugin updates only if in visible mode
|
|
245
|
-
if (!this.argv.includes(
|
|
261
|
+
if (!this.argv.includes("--blind") &&
|
|
262
|
+
!this.argv.includes("--silent") &&
|
|
263
|
+
!this.argv.includes("--quiet"))
|
|
246
264
|
cli_core_1.clUpdate.checkUpdate(pkg);
|
|
247
265
|
return await super.init();
|
|
248
266
|
}
|
|
249
267
|
// CATCH (override)
|
|
250
268
|
async catch(error) {
|
|
251
269
|
if (error.message?.match(/Missing \d required args?:\nresource/))
|
|
252
|
-
this.error(`Missing argument ${cli_core_1.clColor.style.error(
|
|
270
|
+
this.error(`Missing argument ${cli_core_1.clColor.style.error("resource")}`, {
|
|
271
|
+
suggestions: [
|
|
272
|
+
`Execute command ${cli_core_1.clColor.style.command("resources")} to get a list of all available Provisioning resources`,
|
|
273
|
+
],
|
|
274
|
+
});
|
|
253
275
|
// else throw error // overwrite command catch method
|
|
254
276
|
else
|
|
255
277
|
return await super.catch(error); // extend command catch method
|
|
@@ -258,13 +280,13 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
258
280
|
includeFlag(flag, relationships, force) {
|
|
259
281
|
const values = [];
|
|
260
282
|
if (flag) {
|
|
261
|
-
const flagValues = flag.map(f => f.split(
|
|
262
|
-
flagValues.forEach(a => values.push(...a));
|
|
263
|
-
if (values.some(f => f.split(
|
|
264
|
-
this.error(
|
|
283
|
+
const flagValues = flag.map((f) => f.split(",").map((t) => t.trim()));
|
|
284
|
+
flagValues.forEach((a) => { values.push(...a); });
|
|
285
|
+
if (values.some((f) => f.split(".").length > 3) && !force)
|
|
286
|
+
this.error("Can be only included resources within the 3rd level of depth");
|
|
265
287
|
}
|
|
266
288
|
if (relationships) {
|
|
267
|
-
Object.keys(relationships).forEach(r => {
|
|
289
|
+
Object.keys(relationships).forEach((r) => {
|
|
268
290
|
if (!values.includes(r))
|
|
269
291
|
values.push(r);
|
|
270
292
|
});
|
|
@@ -273,24 +295,27 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
273
295
|
}
|
|
274
296
|
objectFlag(flag) {
|
|
275
297
|
const objects = {};
|
|
276
|
-
if (flag &&
|
|
277
|
-
flag.forEach(f => {
|
|
278
|
-
const slashSep = f.indexOf(
|
|
298
|
+
if (flag && flag.length > 0) {
|
|
299
|
+
flag.forEach((f) => {
|
|
300
|
+
const slashSep = f.indexOf("/");
|
|
279
301
|
if (slashSep < 0)
|
|
280
302
|
this.error(`No name or fields defined in flag object${cli_core_1.clColor.style.flag(f)}`);
|
|
281
303
|
const name = f.substring(0, slashSep);
|
|
282
|
-
if (name ===
|
|
304
|
+
if (name === "")
|
|
283
305
|
this.error(`No name defined in flag object ${f}`);
|
|
284
|
-
const fields = f
|
|
285
|
-
|
|
306
|
+
const fields = f
|
|
307
|
+
.substring(slashSep + 1)
|
|
308
|
+
.split(/(?<!\\),/g)
|
|
309
|
+
.map((v) => v.trim()); // escape ',' in value with \\ (double back slash)
|
|
310
|
+
if (fields[0].trim() === "")
|
|
286
311
|
this.error(`No fields defined for object field ${cli_core_1.clColor.style.attribute(name)}`);
|
|
287
312
|
const obj = {};
|
|
288
|
-
fields.forEach(f => {
|
|
289
|
-
const eqi = f.indexOf(
|
|
313
|
+
fields.forEach((f) => {
|
|
314
|
+
const eqi = f.indexOf("=");
|
|
290
315
|
if (eqi < 0)
|
|
291
316
|
this.error(`No value defined for object field ${cli_core_1.clColor.style.attribute(f)} of object ${cli_core_1.clColor.style.attribute(name)}`);
|
|
292
317
|
const n = f.substring(0, eqi);
|
|
293
|
-
const v = f.substring(eqi + 1).replace(/\\,/g,
|
|
318
|
+
const v = f.substring(eqi + 1).replace(/\\,/g, ",");
|
|
294
319
|
obj[n] = cli_core_1.clCommand.fixValueType(v);
|
|
295
320
|
});
|
|
296
321
|
if (objects[name] === undefined)
|
|
@@ -302,20 +327,24 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
302
327
|
}
|
|
303
328
|
fieldsFlag(flag, type) {
|
|
304
329
|
const fields = {};
|
|
305
|
-
if (flag &&
|
|
306
|
-
flag.forEach(f => {
|
|
330
|
+
if (flag && flag.length > 0) {
|
|
331
|
+
flag.forEach((f) => {
|
|
307
332
|
let res = type;
|
|
308
333
|
let val = f;
|
|
309
|
-
if (f.includes(
|
|
310
|
-
const kv = f.split(
|
|
334
|
+
if (f.includes("/")) {
|
|
335
|
+
const kv = f.split("/");
|
|
311
336
|
if (kv.length > 2)
|
|
312
|
-
this.error(
|
|
313
|
-
|
|
337
|
+
this.error("Can be defined only one resource for each fields flag", {
|
|
338
|
+
suggestions: [
|
|
339
|
+
`Split the value ${cli_core_1.clColor.style.attribute(f)} into two fields flags`,
|
|
340
|
+
],
|
|
341
|
+
});
|
|
342
|
+
res = kv[0].replace("[", "").replace("]", "");
|
|
314
343
|
this.checkResource(res);
|
|
315
344
|
val = kv[1];
|
|
316
345
|
}
|
|
317
|
-
const values = val.split(
|
|
318
|
-
if (values[0].trim() ===
|
|
346
|
+
const values = val.split(",").map((v) => v.trim());
|
|
347
|
+
if (values[0].trim() === "")
|
|
319
348
|
this.error(`No fields defined for resource ${cli_core_1.clColor.api.resource(res)}`);
|
|
320
349
|
if (fields[res] === undefined)
|
|
321
350
|
fields[res] = [];
|
|
@@ -326,16 +355,18 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
326
355
|
}
|
|
327
356
|
whereFlag(flag) {
|
|
328
357
|
const wheres = {};
|
|
329
|
-
if (flag &&
|
|
330
|
-
flag.forEach(f => {
|
|
331
|
-
const wt = f.split(
|
|
358
|
+
if (flag && flag.length > 0) {
|
|
359
|
+
flag.forEach((f) => {
|
|
360
|
+
const wt = f.split("=");
|
|
332
361
|
if (wt.length < 2)
|
|
333
|
-
this.error(`Filter flag must be in the form ${cli_core_1.clColor.style.attribute(
|
|
362
|
+
this.error(`Filter flag must be in the form ${cli_core_1.clColor.style.attribute("predicate=value")}`);
|
|
334
363
|
const w = wt[0];
|
|
335
364
|
if (!cli_core_1.clFilter.available(w))
|
|
336
365
|
this.error(`Invalid query filter: ${cli_core_1.clColor.style.error(w)}`, {
|
|
337
|
-
suggestions: [
|
|
338
|
-
|
|
366
|
+
suggestions: [
|
|
367
|
+
`Execute command ${cli_core_1.clColor.style.command("resources:filters")} to get a full list of all available filter predicates`,
|
|
368
|
+
],
|
|
369
|
+
ref: "https://docs.commercelayer.io/api/filtering-data#list-of-predicates",
|
|
339
370
|
});
|
|
340
371
|
const v = wt[1];
|
|
341
372
|
wheres[w] = v;
|
|
@@ -345,30 +376,42 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
345
376
|
}
|
|
346
377
|
sortFlag(flag) {
|
|
347
378
|
const sort = {};
|
|
348
|
-
if (flag &&
|
|
349
|
-
if (flag.some(f => {
|
|
350
|
-
const ft = f.split(
|
|
351
|
-
return
|
|
379
|
+
if (flag && flag.length > 0) {
|
|
380
|
+
if (flag.some((f) => {
|
|
381
|
+
const ft = f.split(",");
|
|
382
|
+
return ft.includes("asc") || ft.includes("desc");
|
|
352
383
|
})) {
|
|
353
|
-
flag.forEach(f => {
|
|
354
|
-
const ot = f.split(
|
|
384
|
+
flag.forEach((f) => {
|
|
385
|
+
const ot = f.split(",");
|
|
355
386
|
if (ot.length > 2)
|
|
356
|
-
this.error(
|
|
387
|
+
this.error("Can be defined only one field for each sort flag", {
|
|
388
|
+
suggestions: [
|
|
389
|
+
`Split the value ${cli_core_1.clColor.style.attribute(f)} into two or more sort flags`,
|
|
390
|
+
],
|
|
391
|
+
});
|
|
357
392
|
const of = ot[0];
|
|
358
|
-
if (of.startsWith(
|
|
359
|
-
this.error(
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
393
|
+
if (of.startsWith("-"))
|
|
394
|
+
this.error("You cannot mix two ordering syntaxes", {
|
|
395
|
+
suggestions: [
|
|
396
|
+
`Choose between the style ${cli_core_1.clColor.cli.value("<field>,<order>")} and the style ${cli_core_1.clColor.cli.value("[-]<field>")}`,
|
|
397
|
+
],
|
|
398
|
+
});
|
|
399
|
+
const sd = ot[1] || "asc";
|
|
400
|
+
if (!["asc", "desc"].includes(sd))
|
|
401
|
+
this.error(`Invalid sort flag: ${cli_core_1.clColor.msg.error(f)}`, {
|
|
402
|
+
suggestions: [
|
|
403
|
+
`Sort direction can assume only the values ${cli_core_1.clColor.cli.value("asc")} or ${cli_core_1.clColor.cli.value("desc")}`,
|
|
404
|
+
],
|
|
405
|
+
});
|
|
363
406
|
sort[of] = sd;
|
|
364
407
|
});
|
|
365
408
|
}
|
|
366
409
|
else {
|
|
367
|
-
flag.forEach(fl => {
|
|
368
|
-
fl.split(
|
|
369
|
-
const desc = f.startsWith(
|
|
410
|
+
flag.forEach((fl) => {
|
|
411
|
+
fl.split(",").forEach((f) => {
|
|
412
|
+
const desc = f.startsWith("-");
|
|
370
413
|
const of = desc ? f.slice(1) : f;
|
|
371
|
-
const sd = desc ?
|
|
414
|
+
const sd = desc ? "desc" : "asc";
|
|
372
415
|
sort[of] = sd;
|
|
373
416
|
});
|
|
374
417
|
});
|
|
@@ -377,36 +420,40 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
377
420
|
return sort;
|
|
378
421
|
}
|
|
379
422
|
metadataFlag(flag, { fixTypes = false } = {}) {
|
|
380
|
-
const md = this._keyvalFlag(flag,
|
|
423
|
+
const md = this._keyvalFlag(flag, "metadata");
|
|
381
424
|
const metadata = {};
|
|
382
|
-
Object.keys(md).forEach(k => {
|
|
425
|
+
Object.keys(md).forEach((k) => {
|
|
383
426
|
metadata[k] = fixTypes ? cli_core_1.clCommand.fixValueType(md[k]) : md[k];
|
|
384
427
|
});
|
|
385
428
|
return metadata;
|
|
386
429
|
}
|
|
387
430
|
relationshipFlag(flag) {
|
|
388
431
|
const relationships = {};
|
|
389
|
-
if (flag &&
|
|
390
|
-
flag.forEach(f => {
|
|
432
|
+
if (flag && flag.length > 0) {
|
|
433
|
+
flag.forEach((f) => {
|
|
391
434
|
let rel;
|
|
392
435
|
let name;
|
|
393
436
|
let id;
|
|
394
437
|
let type;
|
|
395
|
-
const rt = f.split(
|
|
438
|
+
const rt = f.split("=");
|
|
396
439
|
if (rt.length === 2) {
|
|
397
|
-
if ((name = rt[0]) ===
|
|
398
|
-
this.error(
|
|
399
|
-
if ((rel = rt[1]) ===
|
|
400
|
-
this.error(
|
|
440
|
+
if ((name = rt[0]) === "")
|
|
441
|
+
this.error("Relationship attribute name is empty");
|
|
442
|
+
if ((rel = rt[1]) === "")
|
|
443
|
+
this.error("Relationship value is empty");
|
|
401
444
|
}
|
|
402
445
|
else
|
|
403
|
-
this.error(`Invalid relationship flag: ${cli_core_1.clColor.msg.error(f)}`, {
|
|
404
|
-
|
|
446
|
+
this.error(`Invalid relationship flag: ${cli_core_1.clColor.msg.error(f)}`, {
|
|
447
|
+
suggestions: [
|
|
448
|
+
`Define the relationship using the format ${cli_core_1.clColor.cli.value("attribute_name=resource_type/resource_id")}`,
|
|
449
|
+
],
|
|
450
|
+
});
|
|
451
|
+
const vt = rel.split("/");
|
|
405
452
|
if (vt.length === 2) {
|
|
406
|
-
if ((type = vt[0]) ===
|
|
407
|
-
this.error(
|
|
408
|
-
if ((id = vt[1]) ===
|
|
409
|
-
this.error(
|
|
453
|
+
if ((type = vt[0]) === "")
|
|
454
|
+
this.error("Relationship type is empty");
|
|
455
|
+
if ((id = vt[1]) === "")
|
|
456
|
+
this.error("Relationship resource id is empty");
|
|
410
457
|
}
|
|
411
458
|
else {
|
|
412
459
|
id = vt[0];
|
|
@@ -414,7 +461,7 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
414
461
|
if (res)
|
|
415
462
|
type = res.api;
|
|
416
463
|
else
|
|
417
|
-
this.error(
|
|
464
|
+
this.error("Relationship type is empty");
|
|
418
465
|
}
|
|
419
466
|
// const res = this.checkResource(type)
|
|
420
467
|
if (relationships[name])
|
|
@@ -426,20 +473,24 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
426
473
|
}
|
|
427
474
|
extractFlag(flag) {
|
|
428
475
|
const extract = {};
|
|
429
|
-
if (flag &&
|
|
430
|
-
flag.forEach(f => {
|
|
431
|
-
const kv = f.split(
|
|
476
|
+
if (flag && flag.length > 0) {
|
|
477
|
+
flag.forEach((f) => {
|
|
478
|
+
const kv = f.split("/");
|
|
432
479
|
if (kv.length > 2)
|
|
433
|
-
this.error(
|
|
480
|
+
this.error("Can be defined only one field for each extract flag", {
|
|
481
|
+
suggestions: [
|
|
482
|
+
`Split the value ${cli_core_1.clColor.cli.value(f)} into two extract flags`,
|
|
483
|
+
],
|
|
484
|
+
});
|
|
434
485
|
else if (kv.length === 1)
|
|
435
486
|
this.error(`No fields defined for object ${cli_core_1.clColor.cli.value(kv[0])}`);
|
|
436
487
|
const name = kv[0];
|
|
437
|
-
if (name ===
|
|
488
|
+
if (name === "")
|
|
438
489
|
this.error(`No name defined in flag extract ${f}`);
|
|
439
|
-
if (kv[1].trim() ===
|
|
490
|
+
if (kv[1].trim() === "")
|
|
440
491
|
this.error(`No fields defined for object ${cli_core_1.clColor.cli.value(kv[0])}`);
|
|
441
|
-
const fields = kv[1].split(/(?<!\\),/g).map(v => v.trim()); // escape ',' in value with \\ (double back slash)
|
|
442
|
-
if (fields[0].trim() ===
|
|
492
|
+
const fields = kv[1].split(/(?<!\\),/g).map((v) => v.trim()); // escape ',' in value with \\ (double back slash)
|
|
493
|
+
if (fields[0].trim() === "")
|
|
443
494
|
this.error(`No fields defined for object field ${cli_core_1.clColor.cli.value(name)}`);
|
|
444
495
|
if (extract[name] === undefined)
|
|
445
496
|
extract[name] = [];
|
|
@@ -450,7 +501,7 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
450
501
|
}
|
|
451
502
|
extractObjectFields(fields, obj) {
|
|
452
503
|
Object.entries(fields).forEach(([extObj, extFields]) => {
|
|
453
|
-
const objPath = extObj.split(
|
|
504
|
+
const objPath = extObj.split(".");
|
|
454
505
|
let curObj = obj;
|
|
455
506
|
for (const op of objPath) {
|
|
456
507
|
if (curObj)
|
|
@@ -459,7 +510,7 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
459
510
|
break; // if undefined stop search in depth
|
|
460
511
|
}
|
|
461
512
|
// if leaf field is an object and it is not a relationship then extract its fields
|
|
462
|
-
if (curObj &&
|
|
513
|
+
if (curObj && typeof curObj === "object" && !curObj.id && !curObj.type)
|
|
463
514
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
464
515
|
for (const k of Object.keys(curObj))
|
|
465
516
|
if (!extFields.includes(k))
|
|
@@ -468,21 +519,25 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
468
519
|
}
|
|
469
520
|
saveOutput(output, flags) {
|
|
470
521
|
try {
|
|
471
|
-
let filePath = flags.save || flags[
|
|
522
|
+
let filePath = flags.save || flags["save-path"];
|
|
472
523
|
if (!filePath)
|
|
473
|
-
this.warn(
|
|
474
|
-
filePath = cli_core_1.clUtil.specialFolder(filePath, flags[
|
|
524
|
+
this.warn("Undefined output save path");
|
|
525
|
+
filePath = cli_core_1.clUtil.specialFolder(filePath, flags["save-path"]);
|
|
475
526
|
const fileExport = flags.csv ? csv_1.exportCsv : output_1.exportOutput;
|
|
476
527
|
fileExport(output, flags, filePath)
|
|
477
528
|
.then(() => {
|
|
478
|
-
if ((0,
|
|
529
|
+
if ((0, node_fs_1.existsSync)(filePath))
|
|
479
530
|
this.log(`Command output saved to file ${cli_core_1.clColor.style.path(filePath)}\n`);
|
|
480
531
|
})
|
|
481
|
-
.catch(() => this.error(`Unable to save command output to file ${cli_core_1.clColor.style.path(filePath)}`, {
|
|
532
|
+
.catch(() => this.error(`Unable to save command output to file ${cli_core_1.clColor.style.path(filePath)}`, {
|
|
533
|
+
suggestions: [
|
|
534
|
+
"Please check you have the right file system permissions",
|
|
535
|
+
],
|
|
536
|
+
}));
|
|
482
537
|
}
|
|
483
538
|
catch (error) {
|
|
484
|
-
if (error.code ===
|
|
485
|
-
this.warn(`Path not found ${cli_core_1.clColor.msg.error(error.path)}: execute command with flag ${cli_core_1.clColor.cli.flag(
|
|
539
|
+
if (error.code === "ENOENT")
|
|
540
|
+
this.warn(`Path not found ${cli_core_1.clColor.msg.error(error.path)}: execute command with flag ${cli_core_1.clColor.cli.flag("-X")} to force path creation`);
|
|
486
541
|
else
|
|
487
542
|
throw error;
|
|
488
543
|
}
|
|
@@ -491,7 +546,7 @@ class BaseFilterCommand extends BaseCommand {
|
|
|
491
546
|
}
|
|
492
547
|
}
|
|
493
548
|
async showLiveDocumentation(request, params, flags) {
|
|
494
|
-
const lang = (0, lang_1.getLanguageArg)(flags) || await (0, lang_1.promptLanguage)();
|
|
549
|
+
const lang = (0, lang_1.getLanguageArg)(flags) || (await (0, lang_1.promptLanguage)());
|
|
495
550
|
const cmd = (0, lang_1.buildCommand)(lang, request, params, flags);
|
|
496
551
|
this.printCommand(lang, cmd);
|
|
497
552
|
return cmd;
|
|
@@ -513,7 +568,11 @@ exports.BaseFilterCommand = BaseFilterCommand;
|
|
|
513
568
|
class default_1 extends BaseFilterCommand {
|
|
514
569
|
static args = {
|
|
515
570
|
...BaseFilterCommand.args,
|
|
516
|
-
resource: core_1.Args.string({
|
|
571
|
+
resource: core_1.Args.string({
|
|
572
|
+
name: "resource",
|
|
573
|
+
description: "the resource type",
|
|
574
|
+
required: true,
|
|
575
|
+
}),
|
|
517
576
|
};
|
|
518
577
|
}
|
|
519
578
|
exports.default = default_1;
|