@ds-sfdc/sfparty 1.3.7 → 1.3.9

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.
@@ -2,73 +2,95 @@ import clc from 'cli-color'
2
2
  import semver from 'semver'
3
3
 
4
4
  class NpmNotInstalledError extends Error {
5
- constructor(message) {
6
- super(message)
7
- this.name = "NpmNotInstalledError"
8
- }
5
+ constructor(message) {
6
+ super(message)
7
+ this.name = 'NpmNotInstalledError'
8
+ }
9
9
  }
10
10
 
11
11
  class PackageNotFoundError extends Error {
12
- constructor(message) {
13
- super(message)
14
- this.name = "PackageNotFoundError"
15
- }
12
+ constructor(message) {
13
+ super(message)
14
+ this.name = 'PackageNotFoundError'
15
+ }
16
16
  }
17
17
 
18
18
  class UpdateError extends Error {
19
- constructor(message) {
20
- super(message)
21
- this.name = "UpdateError"
22
- }
19
+ constructor(message) {
20
+ super(message)
21
+ this.name = 'UpdateError'
22
+ }
23
23
  }
24
24
 
25
- export async function checkVersion({axios, spawnSync, currentVersion, update = false}) {
26
- try {
27
- const { data } = await axios.get('https://registry.npmjs.org/@ds-sfdc/sfparty', {
28
- params: {
29
- field: 'dist-tags.latest'
30
- }
31
- })
32
- const latestVersion = data['dist-tags'].latest
33
- if (semver.gt(latestVersion, currentVersion)) {
34
- let icon
35
- const version = clc.bgCyanBright(data['dist-tags'].latest)
36
- if (update) {
37
- icon = global.icons.working
38
- } else {
39
- icon = global.icons.fail
40
- }
41
- console.log(`${icon} A newer version ${version} is available.`)
42
- if (!update) {
43
- console.log(`Please upgrade by running ${clc.cyanBright('sfparty update')}`)
44
- return 'A newer version'
45
- } else {
46
- let command = 'npm i -g @ds-sfdc/sfparty@latest'.split(' ')
47
- console.log(`Updating the application using ${clc.cyanBright(command.join(' '))}`)
48
- try {
49
- const npmVersion = spawnSync('npm', ['-v'])
50
- if (npmVersion.stderr && npmVersion.stderr.toString().trim() === 'command not found') {
51
- throw new NpmNotInstalledError("npm is not installed on this system. Please install npm and run the command again.")
52
- }
53
- const update = spawnSync(command[0], command.slice(1))
54
- if (update.status !== 0) {
55
- throw new UpdateError("Error updating the application.")
56
- }
57
- console.log("Application updated successfully.")
58
- } catch (err) {
59
- throw err
60
- }
61
- }
62
- } else {
63
- if (update) {
64
- console.log(`${global.icons.success} You are on the latest version.`)
65
- }
66
- return 'You are on the latest version'
67
- }
68
- } catch (error) {
69
- if (error.response && error.response.status === 404) {
70
- error = new PackageNotFoundError("Package not found on the npm registry")
71
- }
72
- throw error
73
- }
74
- }
25
+ export async function checkVersion({
26
+ axios,
27
+ spawnSync,
28
+ currentVersion,
29
+ update = false,
30
+ }) {
31
+ try {
32
+ const { data } = await axios.get(
33
+ 'https://registry.npmjs.org/@ds-sfdc/sfparty',
34
+ {
35
+ params: {
36
+ field: 'dist-tags.latest',
37
+ },
38
+ },
39
+ )
40
+ const latestVersion = data['dist-tags'].latest
41
+ if (semver.gt(latestVersion, currentVersion)) {
42
+ const version = clc.bgCyanBright(data['dist-tags'].latest)
43
+ const icon = update ? global.icons.working : global.icons.fail
44
+ console.log()
45
+ console.log(`${icon} A newer version ${version} is available.`)
46
+ if (!update) {
47
+ console.log(
48
+ `Please upgrade by running ${clc.cyanBright(
49
+ 'sfparty update',
50
+ )}`,
51
+ )
52
+ return 'A newer version'
53
+ } else {
54
+ let command = 'npm i -g @ds-sfdc/sfparty@latest'.split(' ')
55
+ console.log(
56
+ `Updating the application using ${clc.cyanBright(
57
+ command.join(' '),
58
+ )}`,
59
+ )
60
+ try {
61
+ const npmVersion = spawnSync('npm', ['-v'])
62
+ if (
63
+ npmVersion.stderr &&
64
+ npmVersion.stderr.toString().trim() ===
65
+ 'command not found'
66
+ ) {
67
+ throw new NpmNotInstalledError(
68
+ 'npm is not installed on this system. Please install npm and run the command again.',
69
+ )
70
+ }
71
+ const update = spawnSync(command[0], command.slice(1))
72
+ if (update.status !== 0) {
73
+ throw new UpdateError('Error updating the application.')
74
+ }
75
+ console.log('Application updated successfully.')
76
+ } catch (err) {
77
+ throw err
78
+ }
79
+ }
80
+ } else {
81
+ if (update) {
82
+ console.log(
83
+ `${global.icons.success} You are on the latest version.`,
84
+ )
85
+ }
86
+ return 'You are on the latest version'
87
+ }
88
+ } catch (error) {
89
+ if (error.response && error.response.status === 404) {
90
+ error = new PackageNotFoundError(
91
+ 'Package not found on the npm registry',
92
+ )
93
+ }
94
+ throw error
95
+ }
96
+ }
@@ -5,193 +5,212 @@ import * as yaml from 'js-yaml'
5
5
  import { Parser } from 'xml2js'
6
6
 
7
7
  export function directoryExists(dirPath, fsTmp = fs) {
8
- return fsTmp.existsSync(dirPath) && fsTmp.statSync(dirPath).isDirectory()
8
+ return fsTmp.existsSync(dirPath) && fsTmp.statSync(dirPath).isDirectory()
9
9
  }
10
10
 
11
11
  export function fileExists(filePath, fsTmp = fs) {
12
- return fsTmp.existsSync(filePath) && fsTmp.statSync(filePath).isFile()
12
+ return fsTmp.existsSync(filePath) && fsTmp.statSync(filePath).isFile()
13
13
  }
14
14
 
15
15
  export function createDirectory(dirPath, fsTmp = fs) {
16
- if (!fsTmp.existsSync(dirPath)) {
17
- fsTmp.mkdirSync(dirPath, { recursive: true })
18
- }
16
+ if (!fsTmp.existsSync(dirPath)) {
17
+ fsTmp.mkdirSync(dirPath, { recursive: true })
18
+ }
19
19
  }
20
20
 
21
21
  export function deleteDirectory(dirPath, recursive = false, fsTmp = fs) {
22
- if (!directoryExists(dirPath, fsTmp)) {
23
- return false
24
- } else {
25
- if (fsTmp.existsSync(dirPath)) {
26
- fsTmp.readdirSync(dirPath).forEach(function (file) {
27
- var curPath = path.join(dirPath, file)
28
- if (fsTmp.lstatSync(curPath).isDirectory() && recursive) { // recurse
29
- deleteDirectory(curPath, recursive, fsTmp);
30
- } else { // delete file
31
- fsTmp.unlinkSync(curPath);
32
- }
33
- })
34
- return fsTmp.rmdirSync(dirPath);
35
- }
36
- }
22
+ if (!directoryExists(dirPath, fsTmp)) {
23
+ return false
24
+ } else {
25
+ if (fsTmp.existsSync(dirPath)) {
26
+ fsTmp.readdirSync(dirPath).forEach(function (file) {
27
+ var curPath = path.join(dirPath, file)
28
+ if (fsTmp.lstatSync(curPath).isDirectory() && recursive) {
29
+ // recurse
30
+ deleteDirectory(curPath, recursive, fsTmp)
31
+ } else {
32
+ // delete file
33
+ fsTmp.unlinkSync(curPath)
34
+ }
35
+ })
36
+ return fsTmp.rmdirSync(dirPath)
37
+ }
38
+ }
37
39
  }
38
40
 
39
41
  export function getFiles(dirPath, filter = undefined, fsTmp = fs) {
40
- const filesList = []
41
- if (directoryExists(dirPath, fsTmp)) {
42
- fsTmp.readdirSync(dirPath).forEach(file => {
43
- if (!filter) {
44
- filesList.push(file)
45
- } else {
46
- if (file.toLocaleLowerCase().endsWith(filter.toLocaleLowerCase())) {
47
- filesList.push(file)
48
- }
49
- }
50
- })
51
- filesList.sort()
52
- return filesList
53
- } else {
54
- return []
55
- }
42
+ const filesList = []
43
+ if (directoryExists(dirPath, fsTmp)) {
44
+ fsTmp.readdirSync(dirPath).forEach((file) => {
45
+ if (!filter) {
46
+ filesList.push(file)
47
+ } else {
48
+ if (
49
+ file
50
+ .toLocaleLowerCase()
51
+ .endsWith(filter.toLocaleLowerCase())
52
+ ) {
53
+ filesList.push(file)
54
+ }
55
+ }
56
+ })
57
+ filesList.sort()
58
+ return filesList
59
+ } else {
60
+ return []
61
+ }
56
62
  }
57
63
 
58
64
  export function getDirectories(dirPath, fsTmp = fs) {
59
- if (directoryExists(dirPath, fsTmp)) {
60
- return fsTmp.readdirSync(dirPath, { withFileTypes: true })
61
- .filter(dirent => dirent.isDirectory())
62
- .map(dirent => dirent.name)
63
- } else {
64
- return []
65
- }
65
+ if (directoryExists(dirPath, fsTmp)) {
66
+ return fsTmp
67
+ .readdirSync(dirPath, { withFileTypes: true })
68
+ .filter((dirent) => dirent.isDirectory())
69
+ .map((dirent) => dirent.name)
70
+ } else {
71
+ return []
72
+ }
66
73
  }
67
74
 
68
75
  export function deleteFile(filePath, fsTmp = fs) {
69
- if (!fileExists(filePath, fsTmp)) {
70
- return false
71
- } else {
72
- return fsTmp.unlinkSync(filePath, { recursive: false, force: true });
73
- }
76
+ if (!fileExists(filePath, fsTmp)) {
77
+ return false
78
+ } else {
79
+ return fsTmp.unlinkSync(filePath, { recursive: false, force: true })
80
+ }
74
81
  }
75
82
 
76
83
  export function fileInfo(filePath, fsTmp = fs) {
77
- return {
78
- "dirname": path.join(path.dirname(filePath)), //something/folder/example
79
- "basename": path.basename(filePath, path.extname(filePath)), //example
80
- "filename": path.basename(filePath), //example.txt
81
- "extname": path.extname(filePath), //txt
82
- "exists": fsTmp.existsSync(filePath), //true if exists or false if not exists
83
- "stats": fsTmp.existsSync(filePath) ? fsTmp.statSync(filePath) : undefined //stats object if exists or undefined if not exists
84
- }
84
+ return {
85
+ dirname: path.join(path.dirname(filePath)), //something/folder/example
86
+ basename: path.basename(filePath, path.extname(filePath)), //example
87
+ filename: path.basename(filePath), //example.txt
88
+ extname: path.extname(filePath), //txt
89
+ exists: fsTmp.existsSync(filePath), //true if exists or false if not exists
90
+ stats: fsTmp.existsSync(filePath)
91
+ ? fsTmp.statSync(filePath)
92
+ : undefined, //stats object if exists or undefined if not exists
93
+ }
85
94
  }
86
95
 
87
- export function saveFile(json, fileName, format = path.extname(fileName).replace('.', ''), fsTmp = fs) {
88
- try {
89
- switch (format) {
90
- case 'json':
91
- let jsonString = JSON.stringify(json, null, '\t')
92
- fsTmp.writeFileSync(fileName, jsonString)
93
- break
94
- case 'yaml':
95
- let doc = yaml.dump(json)
96
- fsTmp.writeFileSync(fileName, doc)
97
- break
98
- }
99
- return true
100
- } catch (error) {
101
- global.logger.error(error)
102
- throw error
103
- }
96
+ export function saveFile(
97
+ json,
98
+ fileName,
99
+ format = path.extname(fileName).replace('.', ''),
100
+ fsTmp = fs,
101
+ ) {
102
+ try {
103
+ switch (format) {
104
+ case 'json':
105
+ let jsonString = JSON.stringify(json, null, '\t')
106
+ fsTmp.writeFileSync(fileName, jsonString)
107
+ break
108
+ case 'yaml':
109
+ let doc = yaml.dump(json)
110
+ fsTmp.writeFileSync(fileName, doc)
111
+ break
112
+ }
113
+ return true
114
+ } catch (error) {
115
+ global.logger.error(error)
116
+ throw error
117
+ }
104
118
  }
105
119
 
106
120
  export function readFile(fileName, convert = true, fsTmp = fs) {
107
- try {
108
- let result = undefined
109
- if (fileExists(fileName, fsTmp)) {
110
- const data = fsTmp.readFileSync(fileName, { encoding: 'utf8', flag: 'r' })
111
- if (convert && fileName.indexOf('.yaml') != -1) {
112
- result = yaml.load(data)
113
- } else if (convert && fileName.indexOf('.json') != -1) {
114
- result = JSON.parse(data)
115
- } else if (convert && fileName.indexOf('.xml') != -1) {
116
- // returns a promise
117
- result = convertXML(data)
118
- } else {
119
- result = data
120
- }
121
- return result
122
- } else {
123
- return undefined
124
- }
125
- } catch (error) {
126
- global.logger.error(error)
127
- throw error
128
- }
121
+ try {
122
+ let result = undefined
123
+ if (fileExists(fileName, fsTmp)) {
124
+ const data = fsTmp.readFileSync(fileName, {
125
+ encoding: 'utf8',
126
+ flag: 'r',
127
+ })
128
+ if (convert && fileName.indexOf('.yaml') != -1) {
129
+ result = yaml.load(data)
130
+ } else if (convert && fileName.indexOf('.json') != -1) {
131
+ result = JSON.parse(data)
132
+ } else if (convert && fileName.indexOf('.xml') != -1) {
133
+ // returns a promise
134
+ result = convertXML(data)
135
+ } else {
136
+ result = data
137
+ }
138
+ return result
139
+ } else {
140
+ return undefined
141
+ }
142
+ } catch (error) {
143
+ global.logger.error(error)
144
+ throw error
145
+ }
129
146
  }
130
147
 
131
148
  async function convertXML(data) {
132
- return new Promise((resolve, reject) => {
133
- try {
134
- let parser = new Parser()
135
- parser.parseString(data, function (err, result) {
136
- if (err) throw err
137
- resolve(result)
138
- })
139
- } catch (error) {
140
- reject(error)
141
- }
142
- })
149
+ return new Promise((resolve, reject) => {
150
+ try {
151
+ let parser = new Parser()
152
+ parser.parseString(data, function (err, result) {
153
+ if (err) throw err
154
+ resolve(result)
155
+ })
156
+ } catch (error) {
157
+ reject(error)
158
+ }
159
+ })
143
160
  }
144
161
 
145
- export function writeFile(fileName, data, atime = new Date(), mtime = new Date(), fsTmp = fs) {
146
- try {
147
- // write data to the file
148
- fsTmp.writeFileSync(fileName, data)
149
-
150
- // if atime or mtime are undefined, use current date/time
151
- if (atime === undefined) atime = new Date()
152
- if (mtime === undefined) mtime = new Date()
153
-
154
- // update XML file to match the latest atime and mtime of the files processed
155
- fsTmp.utimesSync(fileName, atime, mtime)
156
-
157
- } catch (error) {
158
- global.logger.error(error)
159
- throw error
160
- }
162
+ export function writeFile(
163
+ fileName,
164
+ data,
165
+ atime = new Date(),
166
+ mtime = new Date(),
167
+ fsTmp = fs,
168
+ ) {
169
+ try {
170
+ // write data to the file
171
+ fsTmp.writeFileSync(fileName, data)
172
+
173
+ // if atime or mtime are undefined, use current date/time
174
+ if (atime === undefined) atime = new Date()
175
+ if (mtime === undefined) mtime = new Date()
176
+
177
+ // update XML file to match the latest atime and mtime of the files processed
178
+ fsTmp.utimesSync(fileName, atime, mtime)
179
+ } catch (error) {
180
+ global.logger.error(error)
181
+ throw error
182
+ }
161
183
  }
162
184
 
163
185
  export function find(filename, root, fsTmp = fs) {
164
- // code Copyright (c) 2014, Ben Gourley
165
- // https://github.com/bengourley/find-nearest-file
166
- root = root || process.cwd();
167
-
168
- if (!filename) throw new Error('filename is required')
169
-
170
- if (filename.indexOf('/') !== -1 || filename === '..') {
171
- throw new Error('filename must be just a filename and not a path')
172
- }
173
-
174
-
175
- function findFile(directory, filename) {
176
-
177
- var file = path.join(directory, filename)
178
-
179
- try {
180
- if (fsTmp.statSync(file).isFile()) return file
181
- // stat existed, but isFile() returned false
182
- return nextLevelUp()
183
- } catch (e) {
184
- // stat did not exist
185
- return nextLevelUp()
186
- }
187
-
188
- function nextLevelUp() {
189
- // Don't proceed to the next directory when already at the fs root
190
- if (directory === path.resolve('/')) return null
191
- return findFile(path.dirname(directory), filename)
192
- }
193
-
194
- }
195
-
196
- return findFile(root, filename)
197
- }
186
+ // code Copyright (c) 2014, Ben Gourley
187
+ // https://github.com/bengourley/find-nearest-file
188
+ root = root || process.cwd()
189
+
190
+ if (!filename) throw new Error('filename is required')
191
+
192
+ if (filename.indexOf('/') !== -1 || filename === '..') {
193
+ throw new Error('filename must be just a filename and not a path')
194
+ }
195
+
196
+ function findFile(directory, filename) {
197
+ var file = path.join(directory, filename)
198
+
199
+ try {
200
+ if (fsTmp.statSync(file).isFile()) return file
201
+ // stat existed, but isFile() returned false
202
+ return nextLevelUp()
203
+ } catch (e) {
204
+ // stat did not exist
205
+ return nextLevelUp()
206
+ }
207
+
208
+ function nextLevelUp() {
209
+ // Don't proceed to the next directory when already at the fs root
210
+ if (directory === path.resolve('/')) return null
211
+ return findFile(path.dirname(directory), filename)
212
+ }
213
+ }
214
+
215
+ return findFile(root, filename)
216
+ }