@modernpoacher/deps 2.2.19 → 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,
@@ -60,11 +71,19 @@ const log = debug('@modernpoacher/deps')
60
71
 
61
72
  log(`\`common/git\` (${VERSION} - ${PLATFORM}) is awake`)
62
73
 
74
+ const LF = String.fromCharCode(10)
75
+
63
76
  /**
64
77
  * @param {string} v
65
78
  * @returns {string}
66
79
  */
67
- const trim = (v) => v.split(String.fromCharCode(10)).map((v) => v.trimEnd()).join(String.fromCharCode(10)).trim()
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()
68
87
 
69
88
  /**
70
89
  * @param {string} v
@@ -108,7 +127,7 @@ export function use (key) {
108
127
  }
109
128
 
110
129
  return function use (value) {
111
- value.split(String.fromCharCode(10))
130
+ value.split(LF)
112
131
  .filter(filter)
113
132
  .forEach(write)
114
133
  }
@@ -145,7 +164,7 @@ export function out (key, directory) {
145
164
  }
146
165
 
147
166
  return function out (value) {
148
- value.split(String.fromCharCode(10))
167
+ value.split(LF)
149
168
  .filter(filter).filter(filterOut)
150
169
  .forEach(write)
151
170
  }
@@ -182,7 +201,7 @@ export function err (key, directory) {
182
201
  }
183
202
 
184
203
  return function err (value) {
185
- value.split(String.fromCharCode(10))
204
+ value.split(LF)
186
205
  .filter(filter).filter(filterErr)
187
206
  .forEach(write)
188
207
  }
@@ -190,9 +209,8 @@ export function err (key, directory) {
190
209
 
191
210
  /**
192
211
  * @function getErrorCode
193
- *
212
+ * @description
194
213
  * Get an error code from an error
195
- *
196
214
  * @param {{code?: number, message?: string}} e
197
215
  * @returns {number}
198
216
  */
@@ -200,9 +218,8 @@ const getErrorCode = ({ code = 0 } = {}) => code
200
218
 
201
219
  /**
202
220
  * @function getErrorMessage
203
- *
221
+ * @description
204
222
  * Get an error message from an error
205
- *
206
223
  * @param {{code?: number, message?: string}} e
207
224
  * @returns {string}
208
225
  */
@@ -210,9 +227,8 @@ const getErrorMessage = ({ message = '' } = {}) => message
210
227
 
211
228
  /**
212
229
  * @function isCommandError
213
- *
230
+ * @description
214
231
  * Determine whether an error is a safe non-zero exit from a Git command
215
- *
216
232
  * @param {{code?: number, message?: string}} e
217
233
  * @returns {boolean}
218
234
  */
@@ -227,9 +243,8 @@ function isCommandError (e) {
227
243
 
228
244
  /**
229
245
  * @function catGitRefsRemotesOriginHead
230
- *
246
+ * @description
231
247
  * Get the default branch from `.git/refs/remotes/origin/HEAD`
232
- *
233
248
  * @param {string} d - A directory configured for Git
234
249
  * @returns {Promise<string>}
235
250
  */
@@ -242,13 +257,21 @@ export function catGitRefsRemotesOriginHead (d = DIRECTORY) {
242
257
  new Promise((resolve, reject) => {
243
258
  const commands = CAT_GIT_REFS_REMOTES_ORIGIN_HEAD.trim()
244
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
+ }
245
270
 
246
271
  const {
247
272
  stdout,
248
273
  stderr
249
- } = exec(commands, options, (e = null, v = '') => {
250
- return (!e) ? resolve(trim(v)) : reject(e)
251
- })
274
+ } = exec(commands, options, handleComplete)
252
275
 
253
276
  if (stdout) stdout.on('data', out('cat-git-refs-remotes-origin-head', directory))
254
277
  if (stderr) stderr.on('data', err('cat-git-refs-remotes-origin-head', directory))
@@ -258,9 +281,8 @@ export function catGitRefsRemotesOriginHead (d = DIRECTORY) {
258
281
 
259
282
  /**
260
283
  * @function awkGitRemoteShowOriginHead
261
- *
284
+ * @description
262
285
  * Get the remote default branch
263
- *
264
286
  * @param {string} d - A directory configured for Git
265
287
  * @returns {Promise<string>}
266
288
  */
@@ -273,13 +295,21 @@ export function awkGitRemoteShowOriginHead (d = DIRECTORY) {
273
295
  new Promise((resolve, reject) => {
274
296
  const commands = 'git remote show origin | awk \'/HEAD branch/ {print $NF}\''
275
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
+ }
276
308
 
277
309
  const {
278
310
  stdout,
279
311
  stderr
280
- } = exec(commands, options, (e = null, v = '') => {
281
- return (!e) ? resolve(trim(v)) : reject(e)
282
- })
312
+ } = exec(commands, options, handleComplete)
283
313
 
284
314
  if (stdout) stdout.on('data', out('git-remote-show-origin-head', directory))
285
315
  if (stderr) stderr.on('data', err('git-remote-show-origin-head', directory))
@@ -289,9 +319,8 @@ export function awkGitRemoteShowOriginHead (d = DIRECTORY) {
289
319
 
290
320
  /**
291
321
  * @function gitRevParseShowTopLevel
292
- *
322
+ * @description
293
323
  * Determine whether a directory is configured for Git
294
- *
295
324
  * @param {string} d - A directory
296
325
  * @returns {Promise<string>}
297
326
  */
@@ -304,13 +333,21 @@ export function gitRevParseShowTopLevel (d = DIRECTORY) {
304
333
  new Promise((resolve, reject) => {
305
334
  const commands = 'git rev-parse --show-toplevel'
306
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
+ }
307
346
 
308
347
  const {
309
348
  stdout,
310
349
  stderr
311
- } = exec(commands, options, (e = null, v = '') => {
312
- return (!e) ? resolve(trim(v)) : reject(e)
313
- })
350
+ } = exec(commands, options, handleComplete)
314
351
 
315
352
  if (stdout) stdout.on('data', out('git-rev-parse-show-toplevel', directory))
316
353
  if (stderr) stderr.on('data', err('git-rev-parse-show-toplevel', directory))
@@ -320,9 +357,8 @@ export function gitRevParseShowTopLevel (d = DIRECTORY) {
320
357
 
321
358
  /**
322
359
  * @function gitRevParseAbbrevRefHead
323
- *
360
+ * @description
324
361
  * Determine which branch is HEAD
325
- *
326
362
  * @param {string} d - A directory
327
363
  * @returns {Promise<string>}
328
364
  */
@@ -335,13 +371,21 @@ export function gitRevParseAbbrevRefHead (d = DIRECTORY) {
335
371
  new Promise((resolve, reject) => {
336
372
  const commands = 'git rev-parse --abbrev-ref HEAD'
337
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
+ }
338
384
 
339
385
  const {
340
386
  stdout,
341
387
  stderr
342
- } = exec(commands, options, (e = null, v = '') => {
343
- return (!e) ? resolve(trim(v)) : reject(e)
344
- })
388
+ } = exec(commands, options, handleComplete)
345
389
 
346
390
  if (stdout) stdout.on('data', out('git-rev-parse-abbrev-ref-head', directory))
347
391
  if (stderr) stderr.on('data', err('git-rev-parse-abbrev-ref-head', directory))
@@ -351,9 +395,8 @@ export function gitRevParseAbbrevRefHead (d = DIRECTORY) {
351
395
 
352
396
  /**
353
397
  * @function gitCheckout
354
- *
398
+ * @description
355
399
  * Switch to the default value if none is supplied
356
- *
357
400
  * @param {string} d - A directory configured for Git
358
401
  * @param {string} branch - The Git branch
359
402
  * @returns {Promise<string>}
@@ -368,16 +411,21 @@ export function gitCheckout (d = DIRECTORY, branch = BRANCH) {
368
411
  const commands = `git checkout ${branch}`
369
412
  const options = getOptions(directory)
370
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
+
371
425
  const {
372
426
  stdout,
373
427
  stderr
374
- } = exec(commands, options, (e = null, v = '') => {
375
- return (!e)
376
- ? resolve(v)
377
- : isCommandError(e)
378
- ? resolve(v)
379
- : reject(e)
380
- })
428
+ } = exec(commands, options, handleComplete)
381
429
 
382
430
  const log = use('git-checkout')
383
431
 
@@ -389,9 +437,8 @@ export function gitCheckout (d = DIRECTORY, branch = BRANCH) {
389
437
 
390
438
  /**
391
439
  * @function gitPull
392
- *
440
+ * @description
393
441
  * Pull changes from the Git remote
394
- *
395
442
  * @param {string} d - A directory configured for Git
396
443
  * @returns {Promise<string>}
397
444
  */
@@ -404,17 +451,21 @@ export function gitPull (d = DIRECTORY) {
404
451
  new Promise((resolve, reject) => {
405
452
  const commands = GIT_PULL.trim()
406
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
+ }
407
464
 
408
465
  const {
409
466
  stdout,
410
467
  stderr
411
- } = exec(commands, options, (e = null, v = '') => {
412
- return (!e)
413
- ? resolve(v)
414
- : isCommandError(e)
415
- ? resolve(v)
416
- : reject(e)
417
- })
468
+ } = exec(commands, options, handleComplete)
418
469
 
419
470
  const log = use('git-pull')
420
471
 
@@ -426,9 +477,8 @@ export function gitPull (d = DIRECTORY) {
426
477
 
427
478
  /**
428
479
  * @function gitPush
429
- *
480
+ * @description
430
481
  * Push changes to the Git remote
431
- *
432
482
  * @param {string} d - A directory configured for Git
433
483
  * @returns {Promise<string>}
434
484
  */
@@ -441,17 +491,21 @@ export function gitPush (d = DIRECTORY) {
441
491
  new Promise((resolve, reject) => {
442
492
  const commands = GIT_PUSH.trim()
443
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
+ }
444
504
 
445
505
  const {
446
506
  stdout,
447
507
  stderr
448
- } = exec(commands, options, (e = null, v = '') => {
449
- return (!e)
450
- ? resolve(v)
451
- : isCommandError(e)
452
- ? resolve(v)
453
- : reject(e)
454
- })
508
+ } = exec(commands, options, handleComplete)
455
509
 
456
510
  const log = use('git-push')
457
511
 
@@ -463,9 +517,8 @@ export function gitPush (d = DIRECTORY) {
463
517
 
464
518
  /**
465
519
  * @function gitPushTags
466
- *
520
+ * @description
467
521
  * Push tags changes to the Git remote
468
- *
469
522
  * @param {string} d - A directory configured for Git
470
523
  * @returns {Promise<string>}
471
524
  */
@@ -478,17 +531,21 @@ export function gitPushTags (d = DIRECTORY) {
478
531
  new Promise((resolve, reject) => {
479
532
  const commands = GIT_PUSH_TAGS.trim()
480
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
+ }
481
544
 
482
545
  const {
483
546
  stdout,
484
547
  stderr
485
- } = exec(commands, options, (e = null, v = '') => {
486
- return (!e)
487
- ? resolve(v)
488
- : isCommandError(e)
489
- ? resolve(v)
490
- : reject(e)
491
- })
548
+ } = exec(commands, options, handleComplete)
492
549
 
493
550
  const log = use('git-push-tags')
494
551
 
@@ -500,9 +557,8 @@ export function gitPushTags (d = DIRECTORY) {
500
557
 
501
558
  /**
502
559
  * @function gitAdd
503
- *
560
+ * @description
504
561
  * Add changes to Git with a default value if none is supplied
505
- *
506
562
  * @param {string} d - A directory configured for Git
507
563
  * @param {string} add - An add parameter
508
564
  * @returns {Promise<string>}
@@ -516,17 +572,21 @@ export function gitAdd (d = DIRECTORY, add = ADD) {
516
572
  new Promise((resolve, reject) => {
517
573
  const commands = `git add ${add}`
518
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
+ }
519
585
 
520
586
  const {
521
587
  stdout,
522
588
  stderr
523
- } = exec(commands, options, (e = null, v = '') => {
524
- return (!e)
525
- ? resolve(v)
526
- : isCommandError(e)
527
- ? resolve(v)
528
- : reject(e)
529
- })
589
+ } = exec(commands, options, handleComplete)
530
590
 
531
591
  const log = use('git-add')
532
592
 
@@ -538,9 +598,8 @@ export function gitAdd (d = DIRECTORY, add = ADD) {
538
598
 
539
599
  /**
540
600
  * @function gitCommit
541
- *
601
+ * @description
542
602
  * Commit changes to Git with a default value if none is supplied
543
- *
544
603
  * @param {string} d - A directory configured for Git
545
604
  * @param {string} message - A commit message
546
605
  * @param {string | { name: string; email: string }} author - The commit author
@@ -555,17 +614,21 @@ export function gitCommit (d = DIRECTORY, message = MESSAGE, author = AUTHOR) {
555
614
  new Promise((resolve, reject) => {
556
615
  const commands = `git commit -m "${message}" --author "${author}"`
557
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
+ }
558
627
 
559
628
  const {
560
629
  stdout,
561
630
  stderr
562
- } = exec(commands, options, (e = null, v = '') => {
563
- return (!e)
564
- ? resolve(v)
565
- : isCommandError(e)
566
- ? resolve(v)
567
- : reject(e)
568
- })
631
+ } = exec(commands, options, handleComplete)
569
632
 
570
633
  const log = use('git-commit')
571
634