|
| 1 | +import omit from 'lodash/omit'; |
| 2 | +import isEmpty from 'lodash/isEmpty'; |
| 3 | +import { resolve as pResolve } from 'node:path'; |
| 4 | +import { handleAndLogError, log } from '@contentstack/cli-utilities'; |
| 5 | + |
| 6 | +import BaseClass from './base-class'; |
| 7 | +import { fsUtil } from '../../utils'; |
| 8 | +import { PublishingRulesConfig, ModuleClassParams } from '../../types'; |
| 9 | + |
| 10 | +export default class ExportPublishingRules extends BaseClass { |
| 11 | + private readonly publishingRules: Record<string, Record<string, unknown>> = {}; |
| 12 | + private readonly publishingRulesConfig: PublishingRulesConfig; |
| 13 | + private publishingRulesFolderPath: string; |
| 14 | + private readonly qs: { include_count: boolean; skip?: number }; |
| 15 | + |
| 16 | + constructor({ exportConfig, stackAPIClient }: ModuleClassParams) { |
| 17 | + super({ exportConfig, stackAPIClient }); |
| 18 | + this.publishingRulesConfig = exportConfig.modules['publishing-rules']; |
| 19 | + this.qs = { include_count: true }; |
| 20 | + this.exportConfig.context.module = 'publishing-rules'; |
| 21 | + } |
| 22 | + |
| 23 | + async start(): Promise<void> { |
| 24 | + this.publishingRulesFolderPath = pResolve( |
| 25 | + this.exportConfig.data, |
| 26 | + this.exportConfig.branchName || '', |
| 27 | + this.publishingRulesConfig.dirName, |
| 28 | + ); |
| 29 | + log.debug(`Publishing rules folder path: ${this.publishingRulesFolderPath}`, this.exportConfig.context); |
| 30 | + |
| 31 | + await fsUtil.makeDirectory(this.publishingRulesFolderPath); |
| 32 | + log.debug('Created publishing rules directory', this.exportConfig.context); |
| 33 | + |
| 34 | + await this.fetchAllPublishingRules(); |
| 35 | + |
| 36 | + if (isEmpty(this.publishingRules)) { |
| 37 | + log.info('No Publishing Rules found', this.exportConfig.context); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const outPath = pResolve(this.publishingRulesFolderPath, this.publishingRulesConfig.fileName); |
| 42 | + fsUtil.writeFile(outPath, this.publishingRules); |
| 43 | + log.success( |
| 44 | + `Publishing rules exported successfully! Total count: ${Object.keys(this.publishingRules).length}`, |
| 45 | + this.exportConfig.context, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + private async fetchAllPublishingRules(skip = 0): Promise<void> { |
| 50 | + try { |
| 51 | + if (skip > 0) { |
| 52 | + this.qs.skip = skip; |
| 53 | + } |
| 54 | + |
| 55 | + const data: { items?: Record<string, unknown>[]; count?: number } = await this.stack |
| 56 | + .workflow() |
| 57 | + .publishRule() |
| 58 | + .fetchAll(this.qs); |
| 59 | + |
| 60 | + const items = data.items ?? []; |
| 61 | + const total = data.count ?? items.length; |
| 62 | + |
| 63 | + if (!items.length) { |
| 64 | + log.debug('No publishing rules returned for this page', this.exportConfig.context); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + for (const rule of items) { |
| 69 | + const uid = rule.uid as string | undefined; |
| 70 | + if (uid) { |
| 71 | + this.publishingRules[uid] = omit(rule, this.publishingRulesConfig.invalidKeys) as Record< |
| 72 | + string, |
| 73 | + unknown |
| 74 | + >; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const nextSkip = skip + items.length; |
| 79 | + if (nextSkip < total) { |
| 80 | + await this.fetchAllPublishingRules(nextSkip); |
| 81 | + } |
| 82 | + } catch (error: unknown) { |
| 83 | + handleAndLogError(error as Error, { ...this.exportConfig.context }); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments