ts-repo-utils 6.0.5 → 6.2.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.
Files changed (44) hide show
  1. package/README.md +11 -7
  2. package/dist/cmd/assert-repo-is-clean.mjs +1 -1
  3. package/dist/cmd/check-should-run-type-checks.mjs +1 -1
  4. package/dist/cmd/format-diff-from.mjs +29 -8
  5. package/dist/cmd/format-diff-from.mjs.map +1 -1
  6. package/dist/cmd/format-uncommitted.d.mts +3 -0
  7. package/dist/cmd/format-uncommitted.d.mts.map +1 -0
  8. package/dist/cmd/format-uncommitted.mjs +59 -0
  9. package/dist/cmd/format-uncommitted.mjs.map +1 -0
  10. package/dist/cmd/gen-index-ts.mjs +1 -1
  11. package/dist/functions/diff.d.mts +32 -2
  12. package/dist/functions/diff.d.mts.map +1 -1
  13. package/dist/functions/diff.mjs +47 -29
  14. package/dist/functions/diff.mjs.map +1 -1
  15. package/dist/functions/exec-async.d.mts +2 -2
  16. package/dist/functions/exec-async.d.mts.map +1 -1
  17. package/dist/functions/format.d.mts +20 -11
  18. package/dist/functions/format.d.mts.map +1 -1
  19. package/dist/functions/format.mjs +134 -95
  20. package/dist/functions/format.mjs.map +1 -1
  21. package/dist/functions/index.mjs +2 -2
  22. package/dist/index.d.mts +1 -0
  23. package/dist/index.d.mts.map +1 -1
  24. package/dist/index.mjs +3 -2
  25. package/dist/index.mjs.map +1 -1
  26. package/package.json +2 -2
  27. package/src/cmd/assert-repo-is-clean.mts +1 -1
  28. package/src/cmd/check-should-run-type-checks.mts +1 -1
  29. package/src/cmd/format-diff-from.mts +35 -9
  30. package/src/cmd/format-uncommitted.mts +67 -0
  31. package/src/cmd/gen-index-ts.mts +1 -1
  32. package/src/functions/diff.mts +85 -32
  33. package/src/functions/diff.test.mts +569 -102
  34. package/src/functions/exec-async.mts +2 -2
  35. package/src/functions/exec-async.test.mts +77 -47
  36. package/src/functions/format.mts +224 -141
  37. package/src/functions/format.test.mts +625 -20
  38. package/src/functions/workspace-utils/run-cmd-in-stages.test.mts +266 -0
  39. package/src/index.mts +2 -0
  40. package/dist/cmd/format-untracked.d.mts +0 -3
  41. package/dist/cmd/format-untracked.d.mts.map +0 -1
  42. package/dist/cmd/format-untracked.mjs +0 -34
  43. package/dist/cmd/format-untracked.mjs.map +0 -1
  44. package/src/cmd/format-untracked.mts +0 -31
@@ -2,7 +2,10 @@ import { type ExecException } from 'node:child_process';
2
2
  import { Result } from 'ts-data-forge';
3
3
  import '../node-global.mjs';
4
4
 
5
- /** Get files that have been changed (git status). */
5
+ /**
6
+ * Get untracked files from the working tree (files not added to git). Runs `git
7
+ * ls-files --others --exclude-standard [--deleted]`
8
+ */
6
9
  export const getUntrackedFiles = async (
7
10
  options?: Readonly<{
8
11
  /** @default true */
@@ -12,35 +15,60 @@ export const getUntrackedFiles = async (
12
15
  }>,
13
16
  ): Promise<
14
17
  Result<readonly string[], ExecException | Readonly<{ message: string }>>
15
- > => {
16
- // Get changed files from git status
17
- const result = await $(
18
- [
19
- `git ls-files --others --exclude-standard`,
20
- // Append '--deleted' to include deleted files only if excludeDeleted is explicitly false
21
- (options?.excludeDeleted ?? true) ? '' : '--deleted',
22
- ]
23
- .filter((s) => s !== '')
24
- .join(' '),
25
- { silent: options?.silent ?? false },
26
- );
18
+ > =>
19
+ cmdResultToFiles({
20
+ cmd: `git ls-files --others --exclude-standard`,
21
+ cmdOptionToExcludeDeleted: '',
22
+ cmdOptionToIncludeDeleted: '--deleted',
23
+ options,
24
+ });
27
25
 
28
- if (Result.isErr(result)) {
29
- return result;
30
- }
31
-
32
- const { stdout } = result.value;
33
-
34
- // Parse git status output
35
- const files = stdout
36
- .split('\n')
37
- .map((s) => s.trim())
38
- .filter((s) => s !== '');
26
+ /**
27
+ * Get untracked files from the working tree (files not added to git). Runs `git
28
+ * git diff --staged --name-only [--diff-filter=d]`
29
+ */
30
+ export const getModifiedFiles = async (
31
+ options?: Readonly<{
32
+ /** @default true */
33
+ excludeDeleted?: boolean;
34
+ /** @default false */
35
+ silent?: boolean;
36
+ }>,
37
+ ): Promise<
38
+ Result<readonly string[], ExecException | Readonly<{ message: string }>>
39
+ > =>
40
+ cmdResultToFiles({
41
+ cmd: `git diff --name-only`,
42
+ cmdOptionToExcludeDeleted: '--diff-filter=d',
43
+ cmdOptionToIncludeDeleted: '',
44
+ options,
45
+ });
39
46
 
40
- return Result.ok(files);
41
- };
47
+ /**
48
+ * Get files that are staged for commit (files added with git add). Runs `git
49
+ * diff --staged --name-only [--diff-filter=d]`
50
+ */
51
+ export const getStagedFiles = async (
52
+ options?: Readonly<{
53
+ /** @default true */
54
+ excludeDeleted?: boolean;
55
+ /** @default false */
56
+ silent?: boolean;
57
+ }>,
58
+ ): Promise<
59
+ Result<readonly string[], ExecException | Readonly<{ message: string }>>
60
+ > =>
61
+ cmdResultToFiles({
62
+ cmd: `git diff --staged --name-only`,
63
+ cmdOptionToExcludeDeleted: '--diff-filter=d',
64
+ cmdOptionToIncludeDeleted: '',
65
+ options,
66
+ });
42
67
 
43
- /** Get files that differ from the specified base branch or commit */
68
+ /**
69
+ * Get files that differ from the specified base branch or commit. Runs `git
70
+ * diff --name-only <base> [--diff-filter=d]`
71
+ */
44
72
  export const getDiffFrom = async (
45
73
  base: string,
46
74
  options?: Readonly<{
@@ -51,13 +79,38 @@ export const getDiffFrom = async (
51
79
  }>,
52
80
  ): Promise<
53
81
  Result<readonly string[], ExecException | Readonly<{ message: string }>>
82
+ > =>
83
+ cmdResultToFiles({
84
+ cmd: `git diff --name-only ${base}`,
85
+ cmdOptionToExcludeDeleted: '--diff-filter=d',
86
+ cmdOptionToIncludeDeleted: '',
87
+ options,
88
+ });
89
+
90
+ const cmdResultToFiles = async ({
91
+ cmd,
92
+ cmdOptionToExcludeDeleted,
93
+ cmdOptionToIncludeDeleted,
94
+ options,
95
+ }: Readonly<{
96
+ cmd: string;
97
+ cmdOptionToExcludeDeleted: string;
98
+ cmdOptionToIncludeDeleted: string;
99
+ options?: Readonly<{
100
+ /** @default true */
101
+ excludeDeleted?: boolean;
102
+ /** @default false */
103
+ silent?: boolean;
104
+ }>;
105
+ }>): Promise<
106
+ Result<readonly string[], ExecException | Readonly<{ message: string }>>
54
107
  > => {
55
- // Get files that differ from base branch/commit (excluding deleted files)
56
108
  const result = await $(
57
109
  [
58
- `git diff --name-only`,
59
- base,
60
- (options?.excludeDeleted ?? true) ? '--diff-filter=d' : '',
110
+ cmd,
111
+ (options?.excludeDeleted ?? true)
112
+ ? cmdOptionToExcludeDeleted
113
+ : cmdOptionToIncludeDeleted,
61
114
  ]
62
115
  .filter((s) => s !== '')
63
116
  .join(' '),
@@ -70,7 +123,7 @@ export const getDiffFrom = async (
70
123
 
71
124
  const { stdout } = result.value;
72
125
 
73
- // Parse git diff output
126
+ // Parse git output
74
127
  const files = stdout
75
128
  .split('\n')
76
129
  .map((line) => line.trim())