@modernpoacher/deps 2.2.18 → 2.2.20

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,4 +1,13 @@
1
- import debug from 'debug'
1
+ /**
2
+ * @typedef {import('node:child_process').ExecException} ExecException
3
+ */
4
+
5
+ /**
6
+ * @callback HandleComplete
7
+ * @param {ExecException | null} e
8
+ * @param {string} v
9
+ * @returns {void}
10
+ */
2
11
 
3
12
  import stripAnsi from 'strip-ansi'
4
13
 
@@ -10,6 +19,8 @@ import {
10
19
  exec
11
20
  } from 'node:child_process'
12
21
 
22
+ import debug from '#deps/src/common/debug'
23
+
13
24
  import {
14
25
  BIN,
15
26
  VERSION,
@@ -28,7 +39,7 @@ const ADD = 'package.json package-lock.json'
28
39
 
29
40
  export const MESSAGE = PLATFORM === 'win32'
30
41
  ? 'Updated `package.json` &/ `package-lock.json`'
31
- : 'Updated \\`package.json\\` &/ \\`package-lock.json\\`' /* eslint-disable-line no-useless-escape */
42
+ : 'Updated \\`package.json\\` &/ \\`package-lock.json\\`'
32
43
 
33
44
  export const AUTHOR = 'Modern Poacher Limited <modernpoacher@modernpoacher.com>'
34
45
 
@@ -40,25 +51,19 @@ echo "\${BASH_REMATCH[0]}"
40
51
 
41
52
  const GIT_PULL = `
42
53
  source "${BIN}/common.sh"
43
-
44
54
  source_home "${BIN}"
45
-
46
55
  git pull
47
56
  `
48
57
 
49
58
  const GIT_PUSH = `
50
59
  source "${BIN}/common.sh"
51
-
52
60
  source_home "${BIN}"
53
-
54
61
  git push
55
62
  `
56
63
 
57
64
  const GIT_PUSH_TAGS = `
58
65
  source "${BIN}/common.sh"
59
-
60
66
  source_home "${BIN}"
61
-
62
67
  git push --tags
63
68
  `
64
69
 
@@ -66,41 +71,82 @@ const log = debug('@modernpoacher/deps')
66
71
 
67
72
  log(`\`common/git\` (${VERSION} - ${PLATFORM}) is awake`)
68
73
 
69
- const trim = (v) => v.split(String.fromCharCode(10)).map((v) => v.trimEnd()).join(String.fromCharCode(10)).trim()
74
+ const LF = String.fromCharCode(10)
75
+
76
+ /**
77
+ * @param {string} v
78
+ * @returns {string}
79
+ */
80
+ const trimEnd = (v) => v.trimEnd()
81
+
82
+ /**
83
+ * @param {string} v
84
+ * @returns {string}
85
+ */
86
+ const trim = (v) => v.split(LF).map(trimEnd).join(LF).trim()
70
87
 
88
+ /**
89
+ * @param {string} v
90
+ * @returns {boolean}
91
+ */
71
92
  function filter (v) {
72
93
  return Boolean(stripAnsi(v).trim())
73
94
  }
74
95
 
96
+ /**
97
+ * @param {string} directory
98
+ * @returns {(s: string) => boolean}
99
+ */
75
100
  function getIsDirectory (directory) {
76
101
  return function isDirectory (s) {
77
102
  return directory === s
78
103
  }
79
104
  }
80
105
 
81
- function isFatal (s) {
82
- return s.toLowerCase().startsWith('fatal: not a git repository')
106
+ /**
107
+ * @param {string} v
108
+ * @returns {boolean}
109
+ */
110
+ function isFatal (v) {
111
+ return v.toLowerCase().startsWith('fatal: not a git repository')
83
112
  }
84
113
 
114
+ /**
115
+ * @param {string} key
116
+ * @returns {(value: string) => void}
117
+ */
85
118
  export function use (key) {
86
119
  const log = debug(`@modernpoacher/deps:${key}`)
87
120
 
121
+ /**
122
+ * @param {string} v
123
+ * @returns {void}
124
+ */
88
125
  function write (v) {
89
126
  log(v.trimEnd())
90
127
  }
91
128
 
92
129
  return function use (value) {
93
- value.split(String.fromCharCode(10))
130
+ value.split(LF)
94
131
  .filter(filter)
95
132
  .forEach(write)
96
133
  }
97
134
  }
98
135
 
136
+ /**
137
+ * @param {string} key
138
+ * @param {string} directory
139
+ * @returns {(value: string) => void}
140
+ */
99
141
  export function out (key, directory) {
100
142
  const log = debug(`@modernpoacher/deps:${key}`)
101
143
 
102
144
  const isDirectory = getIsDirectory(directory)
103
145
 
146
+ /**
147
+ * @param {string} v
148
+ * @returns {boolean}
149
+ */
104
150
  function filterOut (v) {
105
151
  const s = v.trim()
106
152
 
@@ -109,22 +155,35 @@ export function out (key, directory) {
109
155
  )
110
156
  }
111
157
 
158
+ /**
159
+ * @param {string} v
160
+ * @returns {void}
161
+ */
112
162
  function write (v) {
113
163
  if (v.trim()) log(v.trimEnd())
114
164
  }
115
165
 
116
166
  return function out (value) {
117
- value.split(String.fromCharCode(10))
167
+ value.split(LF)
118
168
  .filter(filter).filter(filterOut)
119
169
  .forEach(write)
120
170
  }
121
171
  }
122
172
 
173
+ /**
174
+ * @param {string} key
175
+ * @param {string} directory
176
+ * @returns {(value: string) => void}
177
+ */
123
178
  export function err (key, directory) {
124
179
  const log = debug(`@modernpoacher/deps:${key}`)
125
180
 
126
181
  const isDirectory = getIsDirectory(directory)
127
182
 
183
+ /**
184
+ * @param {string} v
185
+ * @returns {boolean}
186
+ */
128
187
  function filterErr (v) {
129
188
  const s = v.trim()
130
189
 
@@ -133,12 +192,16 @@ export function err (key, directory) {
133
192
  )
134
193
  }
135
194
 
195
+ /**
196
+ * @param {string} v
197
+ * @returns {void}
198
+ */
136
199
  function write (v) {
137
200
  if (v.trim()) log(v.trimEnd())
138
201
  }
139
202
 
140
203
  return function err (value) {
141
- value.split(String.fromCharCode(10))
204
+ value.split(LF)
142
205
  .filter(filter).filter(filterErr)
143
206
  .forEach(write)
144
207
  }
@@ -146,30 +209,27 @@ export function err (key, directory) {
146
209
 
147
210
  /**
148
211
  * @function getErrorCode
149
- *
212
+ * @description
150
213
  * Get an error code from an error
151
- *
152
- * @param {Error}
214
+ * @param {{code?: number, message?: string}} e
153
215
  * @returns {number}
154
216
  */
155
217
  const getErrorCode = ({ code = 0 } = {}) => code
156
218
 
157
219
  /**
158
220
  * @function getErrorMessage
159
- *
221
+ * @description
160
222
  * Get an error message from an error
161
- *
162
- * @param {Error}
223
+ * @param {{code?: number, message?: string}} e
163
224
  * @returns {string}
164
225
  */
165
226
  const getErrorMessage = ({ message = '' } = {}) => message
166
227
 
167
228
  /**
168
229
  * @function isCommandError
169
- *
230
+ * @description
170
231
  * Determine whether an error is a safe non-zero exit from a Git command
171
- *
172
- * @param {Error}
232
+ * @param {{code?: number, message?: string}} e
173
233
  * @returns {boolean}
174
234
  */
175
235
  function isCommandError (e) {
@@ -183,9 +243,8 @@ function isCommandError (e) {
183
243
 
184
244
  /**
185
245
  * @function catGitRefsRemotesOriginHead
186
- *
246
+ * @description
187
247
  * Get the default branch from `.git/refs/remotes/origin/HEAD`
188
- *
189
248
  * @param {string} d - A directory configured for Git
190
249
  * @returns {Promise<string>}
191
250
  */
@@ -198,27 +257,32 @@ export function catGitRefsRemotesOriginHead (d = DIRECTORY) {
198
257
  new Promise((resolve, reject) => {
199
258
  const commands = CAT_GIT_REFS_REMOTES_ORIGIN_HEAD.trim()
200
259
  const options = getOptions(directory)
260
+ /**
261
+ * @type {HandleComplete}
262
+ */
263
+ function handleComplete (e, v) {
264
+ if (!e) {
265
+ resolve(trim(v))
266
+ } else {
267
+ reject(e)
268
+ }
269
+ }
201
270
 
202
271
  const {
203
272
  stdout,
204
273
  stderr
205
- } = exec(commands, options, (e, v = '') => {
206
- (!e)
207
- ? resolve(trim(v))
208
- : reject(e)
209
- })
210
-
211
- stdout.on('data', out('cat-git-refs-remotes-origin-head', directory))
212
- stderr.on('data', err('cat-git-refs-remotes-origin-head', directory))
274
+ } = exec(commands, options, handleComplete)
275
+
276
+ if (stdout) stdout.on('data', out('cat-git-refs-remotes-origin-head', directory))
277
+ if (stderr) stderr.on('data', err('cat-git-refs-remotes-origin-head', directory))
213
278
  })
214
279
  )
215
280
  }
216
281
 
217
282
  /**
218
283
  * @function awkGitRemoteShowOriginHead
219
- *
284
+ * @description
220
285
  * Get the remote default branch
221
- *
222
286
  * @param {string} d - A directory configured for Git
223
287
  * @returns {Promise<string>}
224
288
  */
@@ -231,27 +295,32 @@ export function awkGitRemoteShowOriginHead (d = DIRECTORY) {
231
295
  new Promise((resolve, reject) => {
232
296
  const commands = 'git remote show origin | awk \'/HEAD branch/ {print $NF}\''
233
297
  const options = getOptions(directory)
298
+ /**
299
+ * @type {HandleComplete}
300
+ */
301
+ function handleComplete (e = null, v = '') {
302
+ if (!e) {
303
+ resolve(trim(v))
304
+ } else {
305
+ reject(e)
306
+ }
307
+ }
234
308
 
235
309
  const {
236
310
  stdout,
237
311
  stderr
238
- } = exec(commands, options, (e, v = '') => {
239
- (!e)
240
- ? resolve(trim(v))
241
- : reject(e)
242
- })
243
-
244
- stdout.on('data', out('git-remote-show-origin-head', directory))
245
- stderr.on('data', err('git-remote-show-origin-head', directory))
312
+ } = exec(commands, options, handleComplete)
313
+
314
+ if (stdout) stdout.on('data', out('git-remote-show-origin-head', directory))
315
+ if (stderr) stderr.on('data', err('git-remote-show-origin-head', directory))
246
316
  })
247
317
  )
248
318
  }
249
319
 
250
320
  /**
251
321
  * @function gitRevParseShowTopLevel
252
- *
322
+ * @description
253
323
  * Determine whether a directory is configured for Git
254
- *
255
324
  * @param {string} d - A directory
256
325
  * @returns {Promise<string>}
257
326
  */
@@ -264,27 +333,32 @@ export function gitRevParseShowTopLevel (d = DIRECTORY) {
264
333
  new Promise((resolve, reject) => {
265
334
  const commands = 'git rev-parse --show-toplevel'
266
335
  const options = getOptions(directory)
336
+ /**
337
+ * @type {HandleComplete}
338
+ */
339
+ function handleComplete (e, v) {
340
+ if (!e) {
341
+ resolve(trim(v))
342
+ } else {
343
+ reject(e)
344
+ }
345
+ }
267
346
 
268
347
  const {
269
348
  stdout,
270
349
  stderr
271
- } = exec(commands, options, (e, v = '') => {
272
- (!e)
273
- ? resolve(trim(v))
274
- : reject(e)
275
- })
276
-
277
- stdout.on('data', out('git-rev-parse-show-toplevel', directory))
278
- stderr.on('data', err('git-rev-parse-show-toplevel', directory))
350
+ } = exec(commands, options, handleComplete)
351
+
352
+ if (stdout) stdout.on('data', out('git-rev-parse-show-toplevel', directory))
353
+ if (stderr) stderr.on('data', err('git-rev-parse-show-toplevel', directory))
279
354
  })
280
355
  )
281
356
  }
282
357
 
283
358
  /**
284
359
  * @function gitRevParseAbbrevRefHead
285
- *
360
+ * @description
286
361
  * Determine which branch is HEAD
287
- *
288
362
  * @param {string} d - A directory
289
363
  * @returns {Promise<string>}
290
364
  */
@@ -297,27 +371,32 @@ export function gitRevParseAbbrevRefHead (d = DIRECTORY) {
297
371
  new Promise((resolve, reject) => {
298
372
  const commands = 'git rev-parse --abbrev-ref HEAD'
299
373
  const options = getOptions(directory)
374
+ /**
375
+ * @type {HandleComplete}
376
+ */
377
+ function handleComplete (e, v) {
378
+ if (!e) {
379
+ resolve(trim(v))
380
+ } else {
381
+ reject(e)
382
+ }
383
+ }
300
384
 
301
385
  const {
302
386
  stdout,
303
387
  stderr
304
- } = exec(commands, options, (e, v = '') => {
305
- (!e)
306
- ? resolve(trim(v))
307
- : reject(e)
308
- })
309
-
310
- stdout.on('data', out('git-rev-parse-abbrev-ref-head', directory))
311
- stderr.on('data', err('git-rev-parse-abbrev-ref-head', directory))
388
+ } = exec(commands, options, handleComplete)
389
+
390
+ if (stdout) stdout.on('data', out('git-rev-parse-abbrev-ref-head', directory))
391
+ if (stderr) stderr.on('data', err('git-rev-parse-abbrev-ref-head', directory))
312
392
  })
313
393
  )
314
394
  }
315
395
 
316
396
  /**
317
397
  * @function gitCheckout
318
- *
398
+ * @description
319
399
  * Switch to the default value if none is supplied
320
- *
321
400
  * @param {string} d - A directory configured for Git
322
401
  * @param {string} branch - The Git branch
323
402
  * @returns {Promise<string>}
@@ -332,30 +411,34 @@ export function gitCheckout (d = DIRECTORY, branch = BRANCH) {
332
411
  const commands = `git checkout ${branch}`
333
412
  const options = getOptions(directory)
334
413
 
414
+ /**
415
+ * @type {HandleComplete}
416
+ */
417
+ function handleComplete (e, v) {
418
+ if (!e || isCommandError(e)) {
419
+ resolve(v)
420
+ } else {
421
+ reject(e)
422
+ }
423
+ }
424
+
335
425
  const {
336
426
  stdout,
337
427
  stderr
338
- } = exec(commands, options, (e, v) => {
339
- (!e)
340
- ? resolve(v)
341
- : isCommandError(e)
342
- ? resolve(v)
343
- : reject(e)
344
- })
428
+ } = exec(commands, options, handleComplete)
345
429
 
346
430
  const log = use('git-checkout')
347
431
 
348
- stdout.on('data', log)
349
- stderr.on('data', log)
432
+ if (stdout) stdout.on('data', log)
433
+ if (stderr) stderr.on('data', log)
350
434
  })
351
435
  )
352
436
  }
353
437
 
354
438
  /**
355
439
  * @function gitPull
356
- *
440
+ * @description
357
441
  * Pull changes from the Git remote
358
- *
359
442
  * @param {string} d - A directory configured for Git
360
443
  * @returns {Promise<string>}
361
444
  */
@@ -368,31 +451,34 @@ export function gitPull (d = DIRECTORY) {
368
451
  new Promise((resolve, reject) => {
369
452
  const commands = GIT_PULL.trim()
370
453
  const options = getOptions(directory)
454
+ /**
455
+ * @type {HandleComplete}
456
+ */
457
+ function handleComplete (e, v) {
458
+ if (!e || isCommandError(e)) {
459
+ resolve(v)
460
+ } else {
461
+ reject(e)
462
+ }
463
+ }
371
464
 
372
465
  const {
373
466
  stdout,
374
467
  stderr
375
- } = exec(commands, options, (e, v) => {
376
- (!e)
377
- ? resolve(v)
378
- : isCommandError(e)
379
- ? resolve(v)
380
- : reject(e)
381
- })
468
+ } = exec(commands, options, handleComplete)
382
469
 
383
470
  const log = use('git-pull')
384
471
 
385
- stdout.on('data', log)
386
- stderr.on('data', log)
472
+ if (stdout) stdout.on('data', log)
473
+ if (stderr) stderr.on('data', log)
387
474
  })
388
475
  )
389
476
  }
390
477
 
391
478
  /**
392
479
  * @function gitPush
393
- *
480
+ * @description
394
481
  * Push changes to the Git remote
395
- *
396
482
  * @param {string} d - A directory configured for Git
397
483
  * @returns {Promise<string>}
398
484
  */
@@ -405,31 +491,34 @@ export function gitPush (d = DIRECTORY) {
405
491
  new Promise((resolve, reject) => {
406
492
  const commands = GIT_PUSH.trim()
407
493
  const options = getOptions(directory)
494
+ /**
495
+ * @type {HandleComplete}
496
+ */
497
+ function handleComplete (e, v) {
498
+ if (!e || isCommandError(e)) {
499
+ resolve(v)
500
+ } else {
501
+ reject(e)
502
+ }
503
+ }
408
504
 
409
505
  const {
410
506
  stdout,
411
507
  stderr
412
- } = exec(commands, options, (e, v) => {
413
- (!e)
414
- ? resolve(v)
415
- : isCommandError(e)
416
- ? resolve(v)
417
- : reject(e)
418
- })
508
+ } = exec(commands, options, handleComplete)
419
509
 
420
510
  const log = use('git-push')
421
511
 
422
- stdout.on('data', log)
423
- stderr.on('data', log)
512
+ if (stdout) stdout.on('data', log)
513
+ if (stderr) stderr.on('data', log)
424
514
  })
425
515
  )
426
516
  }
427
517
 
428
518
  /**
429
519
  * @function gitPushTags
430
- *
520
+ * @description
431
521
  * Push tags changes to the Git remote
432
- *
433
522
  * @param {string} d - A directory configured for Git
434
523
  * @returns {Promise<string>}
435
524
  */
@@ -442,31 +531,34 @@ export function gitPushTags (d = DIRECTORY) {
442
531
  new Promise((resolve, reject) => {
443
532
  const commands = GIT_PUSH_TAGS.trim()
444
533
  const options = getOptions(directory)
534
+ /**
535
+ * @type {HandleComplete}
536
+ */
537
+ function handleComplete (e, v) {
538
+ if (!e || isCommandError(e)) {
539
+ resolve(v)
540
+ } else {
541
+ reject(e)
542
+ }
543
+ }
445
544
 
446
545
  const {
447
546
  stdout,
448
547
  stderr
449
- } = exec(commands, options, (e, v) => {
450
- (!e)
451
- ? resolve(v)
452
- : isCommandError(e)
453
- ? resolve(v)
454
- : reject(e)
455
- })
548
+ } = exec(commands, options, handleComplete)
456
549
 
457
550
  const log = use('git-push-tags')
458
551
 
459
- stdout.on('data', log)
460
- stderr.on('data', log)
552
+ if (stdout) stdout.on('data', log)
553
+ if (stderr) stderr.on('data', log)
461
554
  })
462
555
  )
463
556
  }
464
557
 
465
558
  /**
466
559
  * @function gitAdd
467
- *
560
+ * @description
468
561
  * Add changes to Git with a default value if none is supplied
469
- *
470
562
  * @param {string} d - A directory configured for Git
471
563
  * @param {string} add - An add parameter
472
564
  * @returns {Promise<string>}
@@ -480,34 +572,37 @@ export function gitAdd (d = DIRECTORY, add = ADD) {
480
572
  new Promise((resolve, reject) => {
481
573
  const commands = `git add ${add}`
482
574
  const options = getOptions(directory)
575
+ /**
576
+ * @type {HandleComplete}
577
+ */
578
+ function handleComplete (e, v) {
579
+ if (!e || isCommandError(e)) {
580
+ resolve(v)
581
+ } else {
582
+ reject(e)
583
+ }
584
+ }
483
585
 
484
586
  const {
485
587
  stdout,
486
588
  stderr
487
- } = exec(commands, options, (e, v) => {
488
- (!e)
489
- ? resolve(v)
490
- : isCommandError(e)
491
- ? resolve(v)
492
- : reject(e)
493
- })
589
+ } = exec(commands, options, handleComplete)
494
590
 
495
591
  const log = use('git-add')
496
592
 
497
- stdout.on('data', log)
498
- stderr.on('data', log)
593
+ if (stdout) stdout.on('data', log)
594
+ if (stderr) stderr.on('data', log)
499
595
  })
500
596
  )
501
597
  }
502
598
 
503
599
  /**
504
600
  * @function gitCommit
505
- *
601
+ * @description
506
602
  * Commit changes to Git with a default value if none is supplied
507
- *
508
- * @param {string} c - A directory configured for Git
603
+ * @param {string} d - A directory configured for Git
509
604
  * @param {string} message - A commit message
510
- * @param {string} author - The commit author
605
+ * @param {string | { name: string; email: string }} author - The commit author
511
606
  * @returns {Promise<string>}
512
607
  */
513
608
  export function gitCommit (d = DIRECTORY, message = MESSAGE, author = AUTHOR) {
@@ -519,22 +614,26 @@ export function gitCommit (d = DIRECTORY, message = MESSAGE, author = AUTHOR) {
519
614
  new Promise((resolve, reject) => {
520
615
  const commands = `git commit -m "${message}" --author "${author}"`
521
616
  const options = getOptions(directory)
617
+ /**
618
+ * @type {HandleComplete}
619
+ */
620
+ function handleComplete (e, v) {
621
+ if (!e || isCommandError(e)) {
622
+ resolve(v)
623
+ } else {
624
+ reject(e)
625
+ }
626
+ }
522
627
 
523
628
  const {
524
629
  stdout,
525
630
  stderr
526
- } = exec(commands, options, (e, v) => {
527
- (!e)
528
- ? resolve(v)
529
- : isCommandError(e)
530
- ? resolve(v)
531
- : reject(e)
532
- })
631
+ } = exec(commands, options, handleComplete)
533
632
 
534
633
  const log = use('git-commit')
535
634
 
536
- stdout.on('data', log)
537
- stderr.on('data', log)
635
+ if (stdout) stdout.on('data', log)
636
+ if (stderr) stderr.on('data', log)
538
637
  })
539
638
  )
540
639
  }