@ds-sfdc/sfparty 0.0.0 → 1.0.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.
@@ -1,213 +0,0 @@
1
- 'use strict'
2
-
3
- import path from 'path'
4
- import fs from 'fs'
5
- import os from 'os'
6
- import { readFile } from 'fs'
7
- import { Parser } from 'xml2js'
8
- import logUpdate from 'log-update'
9
- import chalk from 'chalk'
10
- import convertHrtime from 'convert-hrtime'
11
- import cliSpinners from 'cli-spinners'
12
- import * as yaml from 'js-yaml'
13
- import * as fileUtils from '../fileUtils.js'
14
- import { labelDefinition } from './definition.js'
15
-
16
- const spinner = cliSpinners['dots']
17
-
18
- export class CustomLabel {
19
- #fileName = {
20
- 'fullName': undefined,
21
- 'shortName': undefined,
22
- }
23
- #json
24
- #errorMessage = ''
25
- #index = 0
26
- #startTime = 0
27
- #spinnerMessage = ''
28
-
29
- constructor(config) {
30
- this.sourceDir = config.sourceDir
31
- this.targetDir = config.targetDir
32
- this.metaFilePath = config.metaFilePath
33
- this.sequence = config.sequence
34
- }
35
-
36
- get metaFilePath() {
37
- return this._metaFilePath
38
- }
39
-
40
- set metaFilePath(value) {
41
- value = value.trim()
42
- if (value === '') {
43
- throw 'The file path cannot be empty'
44
- }
45
- this._metaFilePath = value
46
- this.#fileName.fullName = fileUtils.fileInfo(value).filename
47
- this.#fileName.shortName = fileUtils.fileInfo(value).filename.replace('.labels-meta.xml', '')
48
- }
49
-
50
- split() {
51
- const that = this
52
- return new Promise((resolve, reject) => {
53
-
54
- if (!that.#fileName || !that.sourceDir || !that.targetDir || !that.metaFilePath) {
55
- global.logger.error('Invalid information passed to split')
56
- process.exit(1)
57
- }
58
- if (!fileUtils.fileExists(that.metaFilePath)) {
59
- global.logger.error(`file not found: ${that.metaFilePath}`)
60
- process.exit(1)
61
- }
62
-
63
- // that.targetDir = path.join(that.targetDir, that.#fileName.shortName)
64
- let parser = new Parser()
65
- const getJSON = new Promise((resolve, reject) => {
66
- readFile(that.metaFilePath, function (err, data) {
67
- parser.parseString(data, function (err, result) {
68
- if (result) {
69
- resolve(result)
70
- } else {
71
- global.logger.error(`error converting xml to json: ${that.metaFilePath}`)
72
- process.exit(1)
73
- }
74
- })
75
- })
76
- })
77
- getJSON.then((result) => {
78
- // modify the json to remove unwanted arrays
79
- delete result.CustomLabels['$']
80
- let jsonString = JSON.stringify(result, (name, value) => {
81
- if (name == '' || !isNaN(name) || labelDefinition.directories.includes(name)) {
82
- return value
83
- } else {
84
- return xml2json(value)
85
- }
86
- })
87
- that.#json = JSON.parse(jsonString)
88
-
89
- Object.keys(that.#json.CustomLabels).forEach(key => {
90
- const keyOrder = labelDefinition.keyOrder[key]
91
- const sortKey = labelDefinition.sortKeys[key]
92
-
93
- if (Array.isArray(that.#json.CustomLabels[key])) {
94
- // sort json to order by sortKey
95
- that.#json.CustomLabels[key].sort((a, b) => {
96
- if (a[sortKey] < b[sortKey]) {
97
- return -1;
98
- }
99
- if (a[sortKey] > b[sortKey]) {
100
- return 1;
101
- }
102
- return 0;
103
- })
104
-
105
- // sort json keys in specified order
106
- that.#json.CustomLabels[key].forEach(function (part, index) {
107
- this[index] = Object.keys(this[index])
108
- .sort((a, b) => {
109
- if (keyOrder.indexOf(a) < keyOrder.indexOf(b)) return -1
110
- if (keyOrder.indexOf(a) > keyOrder.indexOf(b)) return 1
111
- return 0
112
- })
113
- .reduce((accumulator, key) => {
114
- accumulator[key] = this[index][key]
115
- return accumulator
116
- }, {})
117
- }, that.#json.CustomLabels[key])
118
- }
119
- })
120
-
121
- processFile(that)
122
- completeFile(that)
123
- resolve(true)
124
- })
125
- })
126
-
127
- function nextFrame(that) {
128
- return spinner.frames[that.#index = ++that.#index % spinner.frames.length]
129
- }
130
-
131
- function completeFile(that) {
132
- let executionTime = getTimeDiff(BigInt(that.#startTime))
133
- let durationMessage = `${executionTime.seconds}.${executionTime.milliseconds}s`
134
- let stateIcon = (that.#errorMessage == '') ? global.statusLevel.success : global.statusLevel.fail
135
- logUpdate(that.#spinnerMessage
136
- .replace('[%1]', that.sequence.toString().padStart(global.processed.total.toString().length, ' '))
137
- .replace('[%2]', `. Processed in ${durationMessage}.`)
138
- .replace('[%3]', `${that.#errorMessage}`)
139
- .replace('[%4]', `${stateIcon} `)
140
- )
141
- logUpdate.done()
142
- }
143
-
144
- function processFile(that) {
145
- that.#startTime = process.hrtime.bigint()
146
- that.#spinnerMessage = `[%1] of ${global.processed.total} - Custom Labels: [%4]${chalk.yellowBright(that.#fileName.shortName)}[%2][%3]`
147
-
148
- fileUtils.deleteDirectory(that.targetDir, true) // recursive delete existing directory
149
- fileUtils.createDirectory(that.targetDir) // create directory
150
-
151
- Object.keys(that.#json.CustomLabels).forEach(key => {
152
- that.sequence = global.processed.current
153
- logUpdate(that.#spinnerMessage
154
- .replace('[%1]', that.sequence.toString().padStart(global.processed.total.toString().length, ' '))
155
- .replace('[%2]', `\n${chalk.magentaBright(nextFrame(that))} ${key}`)
156
- .replace('[%3]', `${that.#errorMessage}`)
157
- .replace('[%4]', `${global.statusLevel.working} `)
158
- )
159
- if (labelDefinition.directories.includes(key)) {
160
- processDirectory(that, key)
161
- } else {
162
- that.#errorMessage += `\n${global.statusLevel.warn} Not processed: ${key}`
163
- }
164
- })
165
-
166
- return true
167
- }
168
-
169
- function processDirectory(that, key) {
170
- const myKey = labelDefinition.sortKeys[key]
171
-
172
- // populate objects with data per object
173
- that.#json.CustomLabels[key].forEach(element => {
174
- let fileName = path.join(that.targetDir, `${element[myKey]}.${global.format}`)
175
- const labelJSON = {}
176
- labelJSON[myKey] = element[myKey]
177
- delete element[myKey]
178
- labelJSON[key.slice(0, -1)] = element //use slice to remove the s
179
-
180
- let jsonString = JSON.stringify(labelJSON, null, '\t')
181
- switch (global.format) {
182
- case 'json':
183
- fs.writeFileSync(fileName, jsonString)
184
- break
185
- case 'yaml':
186
- let doc = yaml.dump(JSON.parse(jsonString))
187
- fs.writeFileSync(fileName, doc)
188
- break
189
- }
190
- })
191
- }
192
- }
193
- }
194
-
195
- function getTimeDiff(startTime, endTime = process.hrtime.bigint()) {
196
- const diff = BigInt(endTime) - BigInt(startTime)
197
- let executionTime = convertHrtime(diff)
198
- executionTime.seconds = Math.round(executionTime.seconds)
199
- executionTime.milliseconds = Math.round(executionTime.milliseconds / 1000)
200
- if (executionTime.milliseconds == 0 && executionTime.nanoseconds > 0) executionTime.milliseconds = 1
201
- return executionTime
202
- }
203
-
204
- function xml2json(currentValue) {
205
- if (Array.isArray(currentValue)) {
206
- if (currentValue.length == 1) {
207
- currentValue = currentValue[0].toString().trim()
208
- }
209
- }
210
- if (currentValue == 'true') currentValue = true
211
- if (currentValue == 'false') currentValue = false
212
- return currentValue
213
- }