@commercelayer/cli-plugin-resources
Advanced tools
@@ -30,2 +30,3 @@ import Command from '../../base'; | ||
run(): Promise<any>; | ||
private multiDeleteOperation; | ||
} |
@@ -8,2 +8,3 @@ "use strict"; | ||
const OPERATION = 'delete'; | ||
const OPERATION_LIMIT = (cli_core_1.clConfig.api.requests_max_num_burst_test - 5); | ||
class ResourcesDelete extends base_1.default { | ||
@@ -17,21 +18,30 @@ static description = 'delete an existing resource'; | ||
static flags = { | ||
...(cli_core_1.clCommand.commandFlags(base_1.default.flags, ['save-params', 'load-params'])) | ||
...(cli_core_1.clCommand.commandFlags(base_1.default.flags, [base_1.FLAG_SAVE_PARAMS, base_1.FLAG_LOAD_PARAMS, 'fields', 'include', 'json', 'unformatted'])) | ||
}; | ||
static args = { | ||
...base_1.default.args, | ||
id: base_1.Args.string({ name: 'id', description: 'id of the resource to delete', required: false }) | ||
id: base_1.Args.string({ name: 'id', description: `id of the resources to delete (max ${OPERATION_LIMIT})`, required: false }) | ||
}; | ||
async run() { | ||
const { args, flags } = await this.parse(ResourcesDelete); | ||
const invalidFlags = ['fields', 'include']; | ||
/* | ||
const invalidFlags: string[] = ['fields', 'include'] | ||
invalidFlags.forEach(x => { | ||
if (flags[x]) | ||
this.error(`Flag not supported in ${cli_core_1.clColor.cli.command(OPERATION)} operation: ${cli_core_1.clColor.style.error(x)}`); | ||
}); | ||
if (flags[x as keyof typeof flags]) this.error(`Flag not supported in ${clColor.cli.command(OPERATION)} operation: ${clColor.style.error(x)}`) | ||
}) | ||
*/ | ||
const resId = this.checkResourceId(args.resource, args.id); | ||
const resource = this.checkResource(resId.res, { singular: true }); | ||
const resource = this.checkResource(resId.res, { singular: ((resId.id !== undefined) && !resId.id.includes(',')) }); | ||
const id = this.checkLastId(flags.organization, resource.api, resId.id); | ||
const showHeaders = flags.headers || flags['headers-only']; | ||
const idList = id ? id.split(',') : []; | ||
const multiDelete = (idList.length > 1); | ||
if (multiDelete) { | ||
if (showHeaders) | ||
this.error(`Flags ${cli_core_1.clColor.style.error('--headers')} and $${cli_core_1.clColor.style.error('--headers-only')} not supported in multi delete operation`); | ||
if (idList.length > OPERATION_LIMIT) | ||
this.error(`Max number of resources to delete in a single operation is ${cli_core_1.clColor.yellowBright(OPERATION_LIMIT)}`); | ||
} | ||
const cl = this.initCommerceLayer(flags); | ||
const rawReader = (flags.raw && showHeaders) ? cl.addRawResponseReader({ headers: showHeaders }) : undefined; | ||
const rawReader = (!multiDelete && flags.raw && showHeaders) ? cl.addRawResponseReader({ headers: showHeaders }) : undefined; | ||
const reqReader = flags.doc ? (0, lang_1.addRequestReader)(cl) : undefined; | ||
@@ -41,6 +51,10 @@ try { | ||
this.checkOperation(resSdk, OPERATION); | ||
await resSdk.delete(id); | ||
if (showHeaders) | ||
this.printHeaders(rawReader?.headers, flags); | ||
this.log(`\n${cli_core_1.clColor.style.success('Successfully')} deleted resource of type ${cli_core_1.clColor.style.resource(resource.api)} with id ${cli_core_1.clColor.style.id(id)}\n`); | ||
if (multiDelete) | ||
await this.multiDeleteOperation(resSdk, idList, resource.type); | ||
else { | ||
await resSdk.delete(resource.singleton ? undefined : id); | ||
if (showHeaders) | ||
this.printHeaders(rawReader?.headers, flags); | ||
this.log(`\n${cli_core_1.clColor.style.success('Successfully')} deleted resource of type ${cli_core_1.clColor.style.resource(resource.api)}${resource.singleton ? '' : ` with id ${cli_core_1.clColor.style.id(id)}`}\n`); | ||
} | ||
} | ||
@@ -56,3 +70,36 @@ catch (error) { | ||
} | ||
async multiDeleteOperation(resSdk, idList, type) { | ||
const errors = {}; | ||
const deletes = idList.map(id => resSdk.delete(id).catch((err) => { | ||
errors[id] = err; | ||
throw err; | ||
})); | ||
const results = await Promise.allSettled(deletes); | ||
const oks = []; | ||
const kos = []; | ||
for (let i = 0; i < results.length; i++) { | ||
const result = results[i]; | ||
if (result.status === 'fulfilled') | ||
oks.push(idList[i]); | ||
else | ||
kos.push(idList[i]); | ||
} | ||
const humanized = cli_core_1.clApi.humanizeResource(type); | ||
this.log(); | ||
if (kos.length === 0) | ||
this.log(`All ${idList.length} ${cli_core_1.clColor.style.resource(humanized)} have been ${cli_core_1.clColor.style.success('successfully')} deleted`); | ||
else if (oks.length === 0) | ||
this.log(`${cli_core_1.clColor.style.error('Failed')} to delete all ${cli_core_1.clColor.style.resource(humanized)}`); | ||
else { | ||
this.log(`${cli_core_1.clColor.style.success('Successfully')} deleted ${cli_core_1.clColor.style.resource(humanized)} with id: ${oks.join(', ')}`); | ||
this.log(); | ||
this.log(`${cli_core_1.clColor.style.error('Failed')} to delete the following ${cli_core_1.clColor.style.resource(humanized)}:`); | ||
} | ||
this.log(); | ||
if ((Object.keys(errors).length > 0) && (kos.length > 0)) { | ||
Object.entries(errors).forEach(([id, err]) => { this.log(`- ${cli_core_1.clColor.style.id(id)}: ${cli_core_1.clColor.italic(err.message)}`); }); | ||
this.log(); | ||
} | ||
} | ||
} | ||
exports.default = ResourcesDelete; |
@@ -579,2 +579,7 @@ declare const RESOURCES: readonly [{ | ||
}, { | ||
readonly name: "stripe_tax_account"; | ||
readonly type: "stripe_tax_accounts"; | ||
readonly api: "stripe_tax_accounts"; | ||
readonly model: "StripeTaxAccount"; | ||
}, { | ||
readonly name: "subscription_model"; | ||
@@ -625,2 +630,7 @@ readonly type: "subscription_models"; | ||
}, { | ||
readonly name: "vertex_account"; | ||
readonly type: "vertex_accounts"; | ||
readonly api: "vertex_accounts"; | ||
readonly model: "VertexAccount"; | ||
}, { | ||
readonly name: "void"; | ||
@@ -627,0 +637,0 @@ readonly type: "voids"; |
@@ -119,2 +119,3 @@ "use strict"; | ||
{ name: 'stripe_payment', type: 'stripe_payments', api: 'stripe_payments', model: 'StripePayment' }, | ||
{ name: 'stripe_tax_account', type: 'stripe_tax_accounts', api: 'stripe_tax_accounts', model: 'StripeTaxAccount' }, | ||
{ name: 'subscription_model', type: 'subscription_models', api: 'subscription_models', model: 'SubscriptionModel' }, | ||
@@ -129,2 +130,3 @@ { name: 'tag', type: 'tags', api: 'tags', model: 'Tag' }, | ||
{ name: 'version', type: 'versions', api: 'versions', model: 'Version' }, | ||
{ name: 'vertex_account', type: 'vertex_accounts', api: 'vertex_accounts', model: 'VertexAccount' }, | ||
{ name: 'void', type: 'voids', api: 'voids', model: 'Void' }, | ||
@@ -131,0 +133,0 @@ { name: 'webhook', type: 'webhooks', api: 'webhooks', model: 'Webhook' }, |
{ | ||
"name": "@commercelayer/cli-plugin-resources", | ||
"description": "Commerce Layer CLI Resources plugin", | ||
"version": "6.10.0", | ||
"version": "6.11.0", | ||
"author": "Pierluigi Viti <pierluigi@commercelayer.io>", | ||
@@ -76,3 +76,3 @@ "homepage": "https://github.com/commercelayer/commercelayer-cli-plugin-resources", | ||
"nyc": "^15.1.0", | ||
"oclif": "^4.17.20", | ||
"oclif": "^4.17.21", | ||
"semantic-release": "^23.1.1", | ||
@@ -83,5 +83,5 @@ "tsx": "^4.19.2", | ||
"dependencies": { | ||
"@commercelayer/cli-core": "^5.6.4", | ||
"@commercelayer/cli-core": "^5.7.2", | ||
"@commercelayer/cli-ux": "^1.0.7", | ||
"@commercelayer/sdk": "^6.29.2", | ||
"@commercelayer/sdk": "^6.30.0", | ||
"@oclif/core": "^3.27.0", | ||
@@ -88,0 +88,0 @@ "inquirer": "^8.2.6", |
@@ -252,19 +252,12 @@ # @commercelayer/cli-plugin-resources | ||
USAGE | ||
$ commercelayer resources:delete RESOURCE [ID] [-i <value>...] [-f <value>...] [-u -j] [-l curl|node [--doc | -R]] | ||
[--curl ] [--node ] [--save-args <value>] [--load-args <value>] [-H ] [-Y ] | ||
$ commercelayer resources:delete RESOURCE [ID] [-l curl|node [--doc | -R]] [--curl ] [--node ] [-H ] [-Y ] | ||
ARGUMENTS | ||
RESOURCE the resource type | ||
ID id of the resource to delete | ||
ID id of the resources to delete (max 20) | ||
FLAGS | ||
-H, --headers show response headers | ||
-R, --raw print out the raw API response | ||
-Y, --headers-only show only response headers | ||
-f, --fields=<value>... comma separeted list of fields in the format [resourceType/]field1,field2,field3 | ||
-i, --include=<value>... comma separated resources to include | ||
-j, --json convert output in standard JSON format | ||
-u, --unformatted print unformatted JSON output | ||
--load-args=<value> load previously saved command arguments | ||
--save-args=<value> save command data to file for future use | ||
-H, --headers show response headers | ||
-R, --raw print out the raw API response | ||
-Y, --headers-only show only response headers | ||
@@ -271,0 +264,0 @@ DOCUMENTATION FLAGS |
Sorry, the diff of this file is too big to display
312072
0.41%7134
0.18%692
-1%