generated from salesforcecli/lerna-template
/
status.ts
65 lines (60 loc) · 2.19 KB
/
status.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import { Job, JobInfo, BatchInfo } from 'jsforce';
import { flags, FlagsConfig } from '@salesforce/command';
import { Connection, Messages, SfdxError } from '@salesforce/core';
import { Batcher } from '../../../../batcher';
import { DataCommand } from '../../../../dataCommand';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-data', 'bulk.status');
export default class Status extends DataCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
batchid: flags.string({
char: 'b',
description: messages.getMessage('flags.batchid'),
}),
jobid: flags.string({
char: 'i',
description: messages.getMessage('flags.jobid'),
required: true,
}),
};
public async run(): Promise<BatchInfo[] | JobInfo> {
this.ux.startSpinner('Getting Status');
const conn: Connection = this.ensureOrg().getConnection();
const batcher = new Batcher(conn, this.ux);
if (this.flags.jobid && this.flags.batchid) {
// view batch status
const job: Job = conn.bulk.job(this.flags.jobid);
let found = false;
const batches: BatchInfo[] = await job.list();
batches.forEach((batch: BatchInfo) => {
if (batch.id === this.flags.batchid) {
batcher.bulkStatus(batch);
found = true;
}
});
if (!found) {
throw SfdxError.create('@salesforce/plugin-data', 'bulk.status', 'NoBatchFound', [
this.flags.batchid,
this.flags.jobid,
]);
}
this.ux.stopSpinner();
return batches;
} else {
// view job status
const jobStatus = await batcher.fetchAndDisplayJobStatus(this.flags.jobid);
this.ux.stopSpinner();
return jobStatus;
}
}
}