@commercelayer/cli-plugin-provisioning 1.0.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 -0
- package/README.md +413 -0
- package/bin/dev +18 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +5 -0
- package/bin/run.cmd +3 -0
- package/lib/base.d.ts +59 -0
- package/lib/base.js +496 -0
- package/lib/commands/provisioning/create.d.ts +21 -0
- package/lib/commands/provisioning/create.js +137 -0
- package/lib/commands/provisioning/delete.d.ts +20 -0
- package/lib/commands/provisioning/delete.js +52 -0
- package/lib/commands/provisioning/exec.d.ts +15 -0
- package/lib/commands/provisioning/exec.js +55 -0
- package/lib/commands/provisioning/fetch.d.ts +28 -0
- package/lib/commands/provisioning/fetch.js +57 -0
- package/lib/commands/provisioning/get.d.ts +27 -0
- package/lib/commands/provisioning/get.js +31 -0
- package/lib/commands/provisioning/list.d.ts +22 -0
- package/lib/commands/provisioning/list.js +137 -0
- package/lib/commands/provisioning/noc.d.ts +6 -0
- package/lib/commands/provisioning/noc.js +13 -0
- package/lib/commands/provisioning/relationship.d.ts +30 -0
- package/lib/commands/provisioning/relationship.js +114 -0
- package/lib/commands/provisioning/resources.d.ts +11 -0
- package/lib/commands/provisioning/resources.js +36 -0
- package/lib/commands/provisioning/retrieve.d.ts +21 -0
- package/lib/commands/provisioning/retrieve.js +95 -0
- package/lib/commands/provisioning/update.d.ts +26 -0
- package/lib/commands/provisioning/update.js +157 -0
- package/lib/csv.d.ts +3 -0
- package/lib/csv.js +98 -0
- package/lib/output.d.ts +5 -0
- package/lib/output.js +15 -0
- package/lib/util/resources/available.d.ts +35 -0
- package/lib/util/resources/available.js +13 -0
- package/lib/util/resources/build.d.ts +1 -0
- package/lib/util/resources/build.js +47 -0
- package/lib/util/resources/index.d.ts +12 -0
- package/lib/util/resources/index.js +19 -0
- package/oclif.manifest.json +1502 -0
- package/package.json +86 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Command from '../../base';
|
|
2
|
+
export default class ProvisioningRetrieve extends Command {
|
|
3
|
+
static description: string;
|
|
4
|
+
static aliases: string[];
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
extract: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
8
|
+
include: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
9
|
+
fields: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
10
|
+
json: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
11
|
+
unformatted: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
12
|
+
raw: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
headers: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
14
|
+
'headers-only': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
15
|
+
};
|
|
16
|
+
static args: {
|
|
17
|
+
id: import("@oclif/core/lib/interfaces/parser").Arg<string | undefined, Record<string, unknown>>;
|
|
18
|
+
resource: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
19
|
+
};
|
|
20
|
+
run(): Promise<any>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const base_1 = tslib_1.__importStar(require("../../base"));
|
|
5
|
+
// import { addRequestReader, isRequestInterrupted } from '../../lang'
|
|
6
|
+
// import { mergeCommandParams } from '../../commands'
|
|
7
|
+
const OPERATION = 'retrieve';
|
|
8
|
+
class ProvisioningRetrieve extends base_1.default {
|
|
9
|
+
async run() {
|
|
10
|
+
const { args, flags } = await this.parse(ProvisioningRetrieve);
|
|
11
|
+
const { res, id } = this.checkResourceId(args.resource, args.id);
|
|
12
|
+
const resource = this.checkResource(res, { singular: true });
|
|
13
|
+
// const loadParams = flags[FLAG_LOAD_PARAMS]
|
|
14
|
+
// const saveCmd = flags[FLAG_SAVE_PARAMS]
|
|
15
|
+
// if (saveCmd) this.checkAlias(saveCmd, resource.api, OPERATION, this.config)
|
|
16
|
+
const showHeaders = flags.headers || flags['headers-only'];
|
|
17
|
+
// Include flags
|
|
18
|
+
const include = this.includeFlag(flags.include);
|
|
19
|
+
// Fields flags
|
|
20
|
+
const fields = this.fieldsFlag(flags.fields, resource.api);
|
|
21
|
+
const cl = this.initCommerceLayer(flags);
|
|
22
|
+
const rawReader = flags.raw ? cl.addRawResponseReader({ headers: showHeaders }) : undefined;
|
|
23
|
+
// const reqReader = flags.doc ? addRequestReader(cl) : undefined
|
|
24
|
+
const params = {};
|
|
25
|
+
try {
|
|
26
|
+
const resSdk = cl[resource.api];
|
|
27
|
+
this.checkOperation(resSdk, OPERATION);
|
|
28
|
+
if (include && (include.length > 0))
|
|
29
|
+
params.include = include;
|
|
30
|
+
if (fields && (Object.keys(fields).length > 0))
|
|
31
|
+
params.fields = fields;
|
|
32
|
+
// Load saved command arguments
|
|
33
|
+
// if (loadParams) {
|
|
34
|
+
// const savedParams = this.loadParams(loadParams, resource.api, OPERATION)
|
|
35
|
+
// if (savedParams) mergeCommandParams(params, savedParams)
|
|
36
|
+
// }
|
|
37
|
+
const res = await resSdk.retrieve(id, params);
|
|
38
|
+
const out = (flags.raw && rawReader) ? rawReader.rawResponse : res;
|
|
39
|
+
if (flags.extract) {
|
|
40
|
+
const ext = this.extractFlag(flags.extract);
|
|
41
|
+
this.extractObjectFields(ext, out);
|
|
42
|
+
}
|
|
43
|
+
this.printHeaders(rawReader?.headers, flags);
|
|
44
|
+
this.printOutput(out, flags);
|
|
45
|
+
// Save command output
|
|
46
|
+
// if (flags.save || flags['save-path']) this.saveOutput(out, flags)
|
|
47
|
+
// Save command arguments
|
|
48
|
+
// if (saveCmd) this.saveParams(saveCmd, { type: resource.api, id }, OPERATION, params)
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
/*
|
|
53
|
+
if (isRequestInterrupted(error) && reqReader) {
|
|
54
|
+
await this.showLiveDocumentation(reqReader.request, params, flags)
|
|
55
|
+
cl.removeInterceptor('request', reqReader.id)
|
|
56
|
+
} else */ this.printError(error, flags, args);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
ProvisioningRetrieve.description = 'fetch a single resource';
|
|
61
|
+
ProvisioningRetrieve.aliases = ['prov:retrieve', 'pr', 'pretrieve'];
|
|
62
|
+
ProvisioningRetrieve.examples = [
|
|
63
|
+
'$ commercelayer provisioning:retrieve roles/<roleId>',
|
|
64
|
+
'$ commercelayer prov:retrieve roles <roleId>',
|
|
65
|
+
'$ cl prov:retrieve roles <roleId>',
|
|
66
|
+
'$ clayer pr roles/<roleId>',
|
|
67
|
+
];
|
|
68
|
+
ProvisioningRetrieve.flags = {
|
|
69
|
+
...base_1.default.flags,
|
|
70
|
+
/*
|
|
71
|
+
save: Flags.string({
|
|
72
|
+
char: 'x',
|
|
73
|
+
description: 'save command output to file',
|
|
74
|
+
multiple: false,
|
|
75
|
+
exclusive: ['save-path'],
|
|
76
|
+
}),
|
|
77
|
+
'save-path': Flags.string({
|
|
78
|
+
char: 'X',
|
|
79
|
+
description: 'save command output to file and create missing path directories',
|
|
80
|
+
multiple: false,
|
|
81
|
+
exclusive: ['save'],
|
|
82
|
+
}),
|
|
83
|
+
*/
|
|
84
|
+
extract: base_1.Flags.string({
|
|
85
|
+
char: 'e',
|
|
86
|
+
description: 'extract subfields from object attributes',
|
|
87
|
+
multiple: true,
|
|
88
|
+
exclusive: ['raw'],
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
ProvisioningRetrieve.args = {
|
|
92
|
+
...base_1.default.args,
|
|
93
|
+
id: base_1.Args.string({ name: 'id', description: 'id of the resource to retrieve', required: false }),
|
|
94
|
+
};
|
|
95
|
+
exports.default = ProvisioningRetrieve;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Command from '../../base';
|
|
2
|
+
export default class ProvisioningUpdate extends Command {
|
|
3
|
+
static description: string;
|
|
4
|
+
static aliases: string[];
|
|
5
|
+
static examples: string[];
|
|
6
|
+
static flags: {
|
|
7
|
+
attribute: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
8
|
+
object: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
9
|
+
relationship: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
10
|
+
metadata: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
11
|
+
'metadata-replace': import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
12
|
+
data: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
13
|
+
include: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
14
|
+
fields: import("@oclif/core/lib/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/lib/interfaces/parser").CustomOptions>;
|
|
15
|
+
json: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
unformatted: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
17
|
+
raw: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
18
|
+
headers: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
19
|
+
'headers-only': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
20
|
+
};
|
|
21
|
+
static args: {
|
|
22
|
+
id: import("@oclif/core/lib/interfaces/parser").Arg<string | undefined, Record<string, unknown>>;
|
|
23
|
+
resource: import("@oclif/core/lib/interfaces/parser").Arg<string, Record<string, unknown>>;
|
|
24
|
+
};
|
|
25
|
+
run(): Promise<any>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const base_1 = tslib_1.__importStar(require("../../base"));
|
|
5
|
+
const cli_core_1 = require("@commercelayer/cli-core");
|
|
6
|
+
const OPERATION = 'update';
|
|
7
|
+
class ProvisioningUpdate extends base_1.default {
|
|
8
|
+
async run() {
|
|
9
|
+
const { args, flags } = await this.parse(ProvisioningUpdate);
|
|
10
|
+
const { res, id } = this.checkResourceId(args.resource, args.id);
|
|
11
|
+
const resource = this.checkResource(res, { singular: true });
|
|
12
|
+
// const loadParams = flags[FLAG_LOAD_PARAMS]
|
|
13
|
+
// const saveCmd = flags[FLAG_SAVE_PARAMS]
|
|
14
|
+
// if (saveCmd) this.checkAlias(saveCmd, resource.api, OPERATION, this.config)
|
|
15
|
+
const showHeaders = flags.headers || flags['headers-only'];
|
|
16
|
+
// Raw request
|
|
17
|
+
if (flags.data) {
|
|
18
|
+
try {
|
|
19
|
+
const baseUrl = cli_core_1.clApi.baseURL(undefined, flags.domain, true);
|
|
20
|
+
const accessToken = flags.accessToken;
|
|
21
|
+
const rawRes = await cli_core_1.clApi.request.raw({ operation: cli_core_1.clApi.Operation.Update, baseUrl, accessToken, resource: resource.api }, cli_core_1.clApi.request.readDataFile(flags.data), id);
|
|
22
|
+
const out = flags.raw ? rawRes : cli_core_1.clApi.response.denormalize(rawRes);
|
|
23
|
+
this.printOutput(out, flags);
|
|
24
|
+
this.log(`\n${cli_core_1.clColor.style.success('Successfully')} updated resource of type ${cli_core_1.clColor.style.resource(resource.api)} with id ${cli_core_1.clColor.style.id(rawRes.id)}\n`);
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
this.printError(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const cl = this.initCommerceLayer(flags);
|
|
32
|
+
// Attributes flags
|
|
33
|
+
const attributes = this.attributeFlag(flags.attribute);
|
|
34
|
+
// Objects flags
|
|
35
|
+
const objects = this.objectFlag(flags.object);
|
|
36
|
+
// Relationships flags
|
|
37
|
+
const relationships = this.relationshipFlag(flags.relationship);
|
|
38
|
+
// Metadata flags
|
|
39
|
+
const metadata = this.metadataFlag(flags.metadata || flags['metadata-replace']);
|
|
40
|
+
// Relationships
|
|
41
|
+
if (relationships && Object.keys(relationships).length > 0)
|
|
42
|
+
Object.entries(relationships).forEach(([key, value]) => {
|
|
43
|
+
const relSdk = cl[value.type];
|
|
44
|
+
const rel = relSdk.relationship(((value.id === null) || (value.id === 'null')) ? null : value);
|
|
45
|
+
attributes[key] = rel;
|
|
46
|
+
});
|
|
47
|
+
// Objects
|
|
48
|
+
if (objects && (Object.keys(objects).length > 0)) {
|
|
49
|
+
for (const o of Object.keys(objects)) {
|
|
50
|
+
if (Object.keys(attributes).includes(o))
|
|
51
|
+
this.warn(`Object ${o} will overwrite attribute ${o}`);
|
|
52
|
+
else
|
|
53
|
+
attributes[o] = objects[o];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Metadata
|
|
57
|
+
if (metadata && (Object.keys(metadata).length > 0)) {
|
|
58
|
+
if (attributes.metadata)
|
|
59
|
+
this.warn(`Attribute ${cli_core_1.clColor.style.attribute('metadata')} will be overwritten by the content defined with the flags ${cli_core_1.clColor.style.flag('-m/-M')}`);
|
|
60
|
+
attributes.metadata = metadata;
|
|
61
|
+
}
|
|
62
|
+
// Include flags
|
|
63
|
+
const include = this.includeFlag(flags.include, relationships);
|
|
64
|
+
// Fields flags
|
|
65
|
+
const fields = this.fieldsFlag(flags.fields, resource.api);
|
|
66
|
+
const rawReader = flags.raw ? cl.addRawResponseReader({ headers: showHeaders }) : undefined;
|
|
67
|
+
// const reqReader = flags.doc ? addRequestReader(cl) : undefined
|
|
68
|
+
const params = {};
|
|
69
|
+
try {
|
|
70
|
+
const resSdk = cl[resource.api];
|
|
71
|
+
this.checkOperation(resSdk, OPERATION, attributes);
|
|
72
|
+
if (include && (include.length > 0))
|
|
73
|
+
params.include = include;
|
|
74
|
+
if (fields && (Object.keys(fields).length > 0))
|
|
75
|
+
params.fields = fields;
|
|
76
|
+
// Metadata attributes merge
|
|
77
|
+
if (flags.metadata) {
|
|
78
|
+
const params = { fields: { [resource.api]: ['metadata'] } };
|
|
79
|
+
const remRes = await resSdk.retrieve(id, params);
|
|
80
|
+
const remMeta = remRes.metadata;
|
|
81
|
+
if (remMeta && (Object.keys(remMeta).length > 0))
|
|
82
|
+
attributes.metadata = { ...remMeta, ...metadata };
|
|
83
|
+
}
|
|
84
|
+
attributes.id = id;
|
|
85
|
+
// Load saved command arguments
|
|
86
|
+
// if (loadParams) {
|
|
87
|
+
// const savedParams = this.loadParams(loadParams, resource.api, OPERATION)
|
|
88
|
+
// if (savedParams) mergeCommandParams(params, savedParams)
|
|
89
|
+
// }
|
|
90
|
+
const res = await resSdk.update(attributes, params);
|
|
91
|
+
const out = (flags.raw && rawReader) ? rawReader.rawResponse : res;
|
|
92
|
+
this.printHeaders(rawReader?.headers, flags);
|
|
93
|
+
this.printOutput(out, flags);
|
|
94
|
+
this.log(`\n${cli_core_1.clColor.style.success('Successfully')} updated resource of type ${cli_core_1.clColor.style.resource(resource.api)} with id ${cli_core_1.clColor.style.id(res.id)}\n`);
|
|
95
|
+
// Save command arguments
|
|
96
|
+
// if (saveCmd) this.saveParams(saveCmd, { type: resource.api }, OPERATION, params)
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
/*
|
|
101
|
+
if (isRequestInterrupted(error) && reqReader) {
|
|
102
|
+
await this.showLiveDocumentation(reqReader.request, undefined, flags)
|
|
103
|
+
cl.removeInterceptor('request', reqReader.id)
|
|
104
|
+
} else */ this.printError(error, flags, args);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
ProvisioningUpdate.description = 'update an existing resource';
|
|
109
|
+
ProvisioningUpdate.aliases = ['prov:update', 'pu', 'pupdate', 'pupd'];
|
|
110
|
+
ProvisioningUpdate.examples = [
|
|
111
|
+
'$ commercelayer provisioning:update roles/<roleId> -a reference=referenceId',
|
|
112
|
+
'$ commercelayer prov:update roles <roleId> -a reference_origin="Ref Origin"',
|
|
113
|
+
'$ cl prov:update roles/<roleId> -m meta_key="meta value"',
|
|
114
|
+
'$ cl pu roles <roleId> -M meta_key="metadata overwrite',
|
|
115
|
+
'$ clayer prov:update roles <roleId> -D /path/to/data/file/data.json',
|
|
116
|
+
];
|
|
117
|
+
ProvisioningUpdate.flags = {
|
|
118
|
+
...base_1.default.flags,
|
|
119
|
+
attribute: base_1.Flags.string({
|
|
120
|
+
char: 'a',
|
|
121
|
+
description: 'define a resource attribute',
|
|
122
|
+
multiple: true,
|
|
123
|
+
}),
|
|
124
|
+
object: base_1.Flags.string({
|
|
125
|
+
char: 'O',
|
|
126
|
+
description: 'define a resource object attribute',
|
|
127
|
+
multiple: true,
|
|
128
|
+
}),
|
|
129
|
+
relationship: base_1.Flags.string({
|
|
130
|
+
char: 'r',
|
|
131
|
+
description: 'define a relationship with another resource',
|
|
132
|
+
multiple: true,
|
|
133
|
+
}),
|
|
134
|
+
metadata: base_1.Flags.string({
|
|
135
|
+
char: 'm',
|
|
136
|
+
description: 'define a metadata attribute and merge it with the metadata already present in the remote resource',
|
|
137
|
+
multiple: true,
|
|
138
|
+
exclusive: ['metadata-replace'],
|
|
139
|
+
}),
|
|
140
|
+
'metadata-replace': base_1.Flags.string({
|
|
141
|
+
char: 'M',
|
|
142
|
+
description: 'define a metadata attribute and replace every item already present in the remote resource',
|
|
143
|
+
multiple: true,
|
|
144
|
+
exclusive: ['metadata'],
|
|
145
|
+
}),
|
|
146
|
+
data: base_1.Flags.string({
|
|
147
|
+
char: 'D',
|
|
148
|
+
description: 'the data file to use as request body',
|
|
149
|
+
multiple: false,
|
|
150
|
+
exclusive: ['attribute', 'relationship', 'metadata', 'metadata-replace', 'doc' /*, FLAG_LOAD_PARAMS, FLAG_SAVE_PARAMS */],
|
|
151
|
+
})
|
|
152
|
+
};
|
|
153
|
+
ProvisioningUpdate.args = {
|
|
154
|
+
...base_1.default.args,
|
|
155
|
+
id: base_1.Args.string({ name: 'id', description: 'id of the resource to update', required: false }),
|
|
156
|
+
};
|
|
157
|
+
exports.default = ProvisioningUpdate;
|
package/lib/csv.d.ts
ADDED
package/lib/csv.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.exportCsv = exports.formatCsv = void 0;
|
|
5
|
+
const json_2_csv_1 = require("json-2-csv");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const cli_core_1 = require("@commercelayer/cli-core");
|
|
8
|
+
const formatCsv = (obj, flags) => {
|
|
9
|
+
return cli_core_1.clOutput.printCSV(obj, flags);
|
|
10
|
+
};
|
|
11
|
+
exports.formatCsv = formatCsv;
|
|
12
|
+
const analyzeItem = (name, item, flags) => {
|
|
13
|
+
const keys = new Set();
|
|
14
|
+
if (item)
|
|
15
|
+
if (Array.isArray(item)) {
|
|
16
|
+
for (const i of item) {
|
|
17
|
+
const ks = analyzeItem(name, i, flags);
|
|
18
|
+
ks.forEach(k => keys.add(k));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (typeof item === 'object') {
|
|
22
|
+
const type = item.type;
|
|
23
|
+
const fields = [];
|
|
24
|
+
flags.fields.forEach((f) => {
|
|
25
|
+
if (!name && (!f.includes('/')))
|
|
26
|
+
fields.push(...f.split(','));
|
|
27
|
+
else if (type && f.startsWith(type + '/'))
|
|
28
|
+
fields.push(...f.substring(f.indexOf('/') + 1).split(','));
|
|
29
|
+
});
|
|
30
|
+
for (const [k, v] of Object.entries(item)) {
|
|
31
|
+
// exclude 'type' fields
|
|
32
|
+
if (k === 'type')
|
|
33
|
+
continue;
|
|
34
|
+
// exclude 'id' fields that are not explicitly included
|
|
35
|
+
if ((k === 'id') && !fields.includes(k))
|
|
36
|
+
continue;
|
|
37
|
+
const ks = analyzeItem(`${name ? name + '.' : ''}${k}`, v, flags);
|
|
38
|
+
ks.forEach(k => keys.add(k));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else if (name && (name !== 'type') && !name.endsWith('.type')) {
|
|
42
|
+
// 'type' field can't be included in CLI filter but is always included in API response
|
|
43
|
+
keys.add(name);
|
|
44
|
+
}
|
|
45
|
+
return keys;
|
|
46
|
+
};
|
|
47
|
+
const exportCsv = async (output, flags, path) => {
|
|
48
|
+
// Rename header fields
|
|
49
|
+
const header = {};
|
|
50
|
+
if (flags.header) {
|
|
51
|
+
flags.header.join(',').split(',').forEach((h) => {
|
|
52
|
+
const ft = h.split(':');
|
|
53
|
+
header[ft[0]] = ft[1];
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
// Include fields
|
|
57
|
+
const keys = analyzeItem(undefined, output, flags);
|
|
58
|
+
const includeKeys = [...keys].map(k => {
|
|
59
|
+
return { field: k, title: header[k] || k };
|
|
60
|
+
});
|
|
61
|
+
// Exclude fields
|
|
62
|
+
const excludeKeys = [];
|
|
63
|
+
if (flags.fields) {
|
|
64
|
+
if (!flags.fields.includes('id'))
|
|
65
|
+
excludeKeys.push('id');
|
|
66
|
+
if (!flags.fields.includes('type'))
|
|
67
|
+
excludeKeys.push('type');
|
|
68
|
+
}
|
|
69
|
+
if (flags.include) {
|
|
70
|
+
const include = [];
|
|
71
|
+
flags.include.forEach((i) => include.push(...i.split(',')));
|
|
72
|
+
include.forEach((i) => {
|
|
73
|
+
// excludeKeys.push(`${i}.id`)
|
|
74
|
+
excludeKeys.push(`${i}.type`);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
// Delimiter
|
|
78
|
+
let delimiter = flags.delimiter || ',';
|
|
79
|
+
if (delimiter && (delimiter === 'TAB'))
|
|
80
|
+
delimiter = '\t';
|
|
81
|
+
const csv = (0, json_2_csv_1.json2csv)(output, {
|
|
82
|
+
excelBOM: true,
|
|
83
|
+
expandArrayObjects: true,
|
|
84
|
+
prependHeader: true,
|
|
85
|
+
sortHeader: false,
|
|
86
|
+
unwindArrays: true,
|
|
87
|
+
useDateIso8601Format: true,
|
|
88
|
+
emptyFieldValue: '',
|
|
89
|
+
// excludeKeys,
|
|
90
|
+
keys: includeKeys,
|
|
91
|
+
delimiter: {
|
|
92
|
+
field: delimiter,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
(0, fs_1.writeFileSync)(path, csv);
|
|
96
|
+
return await Promise.resolve(true);
|
|
97
|
+
};
|
|
98
|
+
exports.exportCsv = exportCsv;
|
package/lib/output.d.ts
ADDED
package/lib/output.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exportOutput = exports.formatOutput = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const cli_core_1 = require("@commercelayer/cli-core");
|
|
6
|
+
const formatOutput = (out, flags, { color = true } = {}) => {
|
|
7
|
+
return cli_core_1.clOutput.formatOutput(out, flags, { color });
|
|
8
|
+
};
|
|
9
|
+
exports.formatOutput = formatOutput;
|
|
10
|
+
const exportOutput = async (output, flags, filePath) => {
|
|
11
|
+
const out = formatOutput(output, flags, { color: false });
|
|
12
|
+
(0, fs_1.writeFileSync)(filePath, out);
|
|
13
|
+
return await Promise.resolve(true);
|
|
14
|
+
};
|
|
15
|
+
exports.exportOutput = exportOutput;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
declare const RESOURCES: readonly [{
|
|
2
|
+
readonly name: "api_credential";
|
|
3
|
+
readonly api: "api_credentials";
|
|
4
|
+
readonly model: "ApiCredential";
|
|
5
|
+
}, {
|
|
6
|
+
readonly name: "application_membership";
|
|
7
|
+
readonly api: "application_memberships";
|
|
8
|
+
readonly model: "ApplicationMembership";
|
|
9
|
+
}, {
|
|
10
|
+
readonly name: "membership";
|
|
11
|
+
readonly api: "memberships";
|
|
12
|
+
readonly model: "Membership";
|
|
13
|
+
}, {
|
|
14
|
+
readonly name: "organization";
|
|
15
|
+
readonly api: "organizations";
|
|
16
|
+
readonly model: "Organization";
|
|
17
|
+
}, {
|
|
18
|
+
readonly name: "permission";
|
|
19
|
+
readonly api: "permissions";
|
|
20
|
+
readonly model: "Permission";
|
|
21
|
+
}, {
|
|
22
|
+
readonly name: "role";
|
|
23
|
+
readonly api: "roles";
|
|
24
|
+
readonly model: "Role";
|
|
25
|
+
}, {
|
|
26
|
+
readonly name: "user";
|
|
27
|
+
readonly api: "user";
|
|
28
|
+
readonly model: "User";
|
|
29
|
+
readonly singleton: true;
|
|
30
|
+
}, {
|
|
31
|
+
readonly name: "version";
|
|
32
|
+
readonly api: "versions";
|
|
33
|
+
readonly model: "Version";
|
|
34
|
+
}];
|
|
35
|
+
export default RESOURCES;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RESOURCES = [
|
|
4
|
+
{ name: 'api_credential', api: 'api_credentials', model: 'ApiCredential' },
|
|
5
|
+
{ name: 'application_membership', api: 'application_memberships', model: 'ApplicationMembership' },
|
|
6
|
+
{ name: 'membership', api: 'memberships', model: 'Membership' },
|
|
7
|
+
{ name: 'organization', api: 'organizations', model: 'Organization' },
|
|
8
|
+
{ name: 'permission', api: 'permissions', model: 'Permission' },
|
|
9
|
+
{ name: 'role', api: 'roles', model: 'Role' },
|
|
10
|
+
{ name: 'user', api: 'user', model: 'User', singleton: true },
|
|
11
|
+
{ name: 'version', api: 'versions', model: 'Version' },
|
|
12
|
+
];
|
|
13
|
+
exports.default = RESOURCES;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const provisioning_sdk_1 = require("@commercelayer/provisioning-sdk");
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const cli_core_1 = require("@commercelayer/cli-core");
|
|
7
|
+
const isSingleton = (res) => {
|
|
8
|
+
return (cli_core_1.clText.singularize(res) === res);
|
|
9
|
+
};
|
|
10
|
+
const parseResourcesSdk = async () => {
|
|
11
|
+
const resList = provisioning_sdk_1.CommerceLayerProvisioningStatic.resources().map(r => {
|
|
12
|
+
const singular = cli_core_1.clText.singularize(r);
|
|
13
|
+
const item = {
|
|
14
|
+
name: singular,
|
|
15
|
+
api: r,
|
|
16
|
+
model: cli_core_1.clText.camelize(singular),
|
|
17
|
+
singleton: isSingleton(r),
|
|
18
|
+
};
|
|
19
|
+
return item;
|
|
20
|
+
});
|
|
21
|
+
return resList;
|
|
22
|
+
};
|
|
23
|
+
const exportResources = async ({ source = 'sdk', variable = false, name = 'resources', array = false, tab = false, immutable = false } = {}) => {
|
|
24
|
+
if (source !== 'sdk')
|
|
25
|
+
throw new Error(`Only 'sdk' source is currently supported`);
|
|
26
|
+
const resources = await parseResourcesSdk();
|
|
27
|
+
console.log('Parsed resources from source: ' + source);
|
|
28
|
+
const lines = [''];
|
|
29
|
+
if (variable || array)
|
|
30
|
+
lines.push((variable ? `const ${name} = ` : '') + (array ? '[' : ''));
|
|
31
|
+
const resLines = resources.map(res => {
|
|
32
|
+
let item = `${tab ? '\t' : ''}{ `;
|
|
33
|
+
item += `name: '${res.name}', api: '${res.api}', model: '${res.model}'`;
|
|
34
|
+
if (res.singleton)
|
|
35
|
+
item += ', singleton: true';
|
|
36
|
+
item += ' },';
|
|
37
|
+
return item;
|
|
38
|
+
});
|
|
39
|
+
lines.push(...resLines);
|
|
40
|
+
if (array)
|
|
41
|
+
lines.push(`]${immutable ? ' as const' : ''}\n`);
|
|
42
|
+
lines.push(`\n\nexport default ${name}\n`);
|
|
43
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(__dirname, 'available.ts'), lines.join('\n'), { encoding: 'utf-8' });
|
|
44
|
+
console.log('Generated resource list');
|
|
45
|
+
};
|
|
46
|
+
const source = (process.argv.length > 2) ? (['sdk', 'online'].includes(process.argv[2]) ? process.argv[2] : 'sdk') : undefined;
|
|
47
|
+
void exportResources({ source, variable: true, name: 'RESOURCES', array: true, tab: true, immutable: true });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ResourceTypeLock } from '@commercelayer/provisioning-sdk/lib/cjs/api';
|
|
2
|
+
interface Resource {
|
|
3
|
+
name: string;
|
|
4
|
+
api: ResourceTypeLock;
|
|
5
|
+
model: string;
|
|
6
|
+
singleton?: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const findResource: (res: string, { singular }?: {
|
|
9
|
+
singular?: boolean | undefined;
|
|
10
|
+
}) => (Resource | undefined);
|
|
11
|
+
declare const resourceList: (field: 'name' | 'api' | 'model') => string[];
|
|
12
|
+
export { findResource, resourceList, type Resource };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resourceList = exports.findResource = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const available_1 = tslib_1.__importDefault(require("./available"));
|
|
6
|
+
const resources = available_1.default;
|
|
7
|
+
const findResource = (res, { singular = false } = {}) => {
|
|
8
|
+
if (res === undefined)
|
|
9
|
+
return undefined;
|
|
10
|
+
const lowRes = res.toLowerCase();
|
|
11
|
+
return resources.find(r => {
|
|
12
|
+
return (lowRes === r.api) || (singular && (lowRes === r.name));
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
exports.findResource = findResource;
|
|
16
|
+
const resourceList = (field) => {
|
|
17
|
+
return resources.map(r => r[field]);
|
|
18
|
+
};
|
|
19
|
+
exports.resourceList = resourceList;
|