jsrepo 1.2.3 → 1.2.4

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.
package/dist/index.js CHANGED
@@ -677,7 +677,7 @@ var schema = v.object({
677
677
  watermark: v.optional(v.boolean(), true)
678
678
  });
679
679
  var getConfig = (cwd) => {
680
- if (!fs.existsSync(CONFIG_NAME)) {
680
+ if (!fs.existsSync(path.join(cwd, CONFIG_NAME))) {
681
681
  return Err("Could not find your configuration file! Please run `init`.");
682
682
  }
683
683
  const config = v.safeParse(
@@ -1581,6 +1581,20 @@ var join = (lines, { lineNumbers = false, prefix } = {}) => {
1581
1581
  };
1582
1582
 
1583
1583
  // src/utils/diff.ts
1584
+ var isWhitespace = (str) => /^\s+$/g.test(str);
1585
+ var trimSingleNewLine = (str) => {
1586
+ let i = str.length - 1;
1587
+ while (isWhitespace(str[i]) && i >= 0) {
1588
+ if (str[i] === "\n") {
1589
+ if (str[i - 1] === "\r") {
1590
+ return str.slice(0, i - 1);
1591
+ }
1592
+ return str.slice(0, i);
1593
+ }
1594
+ i--;
1595
+ }
1596
+ return str;
1597
+ };
1584
1598
  var formatDiff = ({
1585
1599
  from,
1586
1600
  to,
@@ -1589,6 +1603,8 @@ var formatDiff = ({
1589
1603
  maxUnchanged = 5,
1590
1604
  colorRemoved = color7.red,
1591
1605
  colorAdded = color7.green,
1606
+ colorCharsRemoved = color7.bgRed,
1607
+ colorCharsAdded = color7.bgGreen,
1592
1608
  prefix,
1593
1609
  onUnchanged,
1594
1610
  intro: intro2
@@ -1630,7 +1646,7 @@ var formatDiff = ({
1630
1646
  if (!change.added && !change.removed) {
1631
1647
  if (!expand && change.count !== void 0 && change.count > maxUnchanged) {
1632
1648
  const prevLineOffset = lineOffset;
1633
- const ls = get(change.value.trimEnd());
1649
+ const ls = get(trimSingleNewLine(change.value));
1634
1650
  let shownLines = 0;
1635
1651
  if (hasNextChange) shownLines += maxUnchanged;
1636
1652
  if (hasPreviousChange) shownLines += maxUnchanged;
@@ -1672,35 +1688,54 @@ var formatDiff = ({
1672
1688
  lineOffset = prevLineOffset + change.count;
1673
1689
  continue;
1674
1690
  }
1675
- result += `${join(get(change.value.trimEnd()), {
1691
+ result += `${join(get(trimSingleNewLine(change.value)), {
1676
1692
  prefix: linePrefix
1677
1693
  })}
1678
1694
  `;
1679
1695
  lineOffset += change.count ?? 0;
1680
1696
  continue;
1681
1697
  }
1682
- const colorChange = (change2) => {
1698
+ const colorLineChange = (change2) => {
1699
+ if (change2.added) {
1700
+ return colorAdded(trimSingleNewLine(change2.value));
1701
+ }
1702
+ if (change2.removed) {
1703
+ return colorRemoved(trimSingleNewLine(change2.value));
1704
+ }
1705
+ return change2.value;
1706
+ };
1707
+ const colorCharChange = (change2) => {
1683
1708
  if (change2.added) {
1684
- return colorAdded(change2.value.trimEnd());
1709
+ return colorCharsAdded(trimSingleNewLine(change2.value));
1685
1710
  }
1686
1711
  if (change2.removed) {
1687
- return colorRemoved(change2.value.trimEnd());
1712
+ return colorCharsRemoved(trimSingleNewLine(change2.value));
1688
1713
  }
1689
1714
  return change2.value;
1690
1715
  };
1691
- if (change.removed && change.count === 1) {
1716
+ if (change.removed && change.count === 1 && changes[i + 1]?.added && changes[i + 1]?.count === 1) {
1692
1717
  const diffedChars = diffChars(change.value, changes[i + 1].value);
1693
- const sentence = diffedChars.map((chg) => colorChange(chg)).join("");
1718
+ const sentence = diffedChars.map((chg) => colorCharChange(chg)).join("");
1694
1719
  result += `${linePrefix(0)}${sentence}`;
1695
1720
  lineOffset += 1;
1696
1721
  i++;
1697
1722
  } else {
1698
- result += `${join(get(colorChange(change)), {
1699
- prefix: linePrefix
1700
- })}
1723
+ if (isWhitespace(change.value)) {
1724
+ result += `${join(get(colorCharChange(change)), {
1725
+ prefix: (line) => `${linePrefix(line)}${colorCharChange({ removed: true, value: " ", added: false })}`
1726
+ })}
1701
1727
  `;
1702
- if (!change.removed) {
1703
- lineOffset += change.count ?? 0;
1728
+ if (!change.removed) {
1729
+ lineOffset += change.count ?? 0;
1730
+ }
1731
+ } else {
1732
+ result += `${join(get(colorLineChange(change)), {
1733
+ prefix: linePrefix
1734
+ })}
1735
+ `;
1736
+ if (!change.removed) {
1737
+ lineOffset += change.count ?? 0;
1738
+ }
1704
1739
  }
1705
1740
  }
1706
1741
  }
@@ -1830,6 +1865,8 @@ ${remoteContent}`;
1830
1865
  maxUnchanged: options.maxUnchanged,
1831
1866
  colorAdded: color8.greenBright,
1832
1867
  colorRemoved: color8.redBright,
1868
+ colorCharsAdded: color8.bgGreenBright,
1869
+ colorCharsRemoved: color8.bgRedBright,
1833
1870
  prefix: () => `${L} `,
1834
1871
  onUnchanged: ({ from: from2, to: to2, prefix }) => `${prefix?.() ?? ""}${color8.cyan(from2)} \u2192 ${color8.gray(to2)} ${color8.gray("(unchanged)")}
1835
1872
  `,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jsrepo",
3
3
  "description": "A CLI to add shared code from remote repositories.",
4
- "version": "1.2.3",
4
+ "version": "1.2.4",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ieedan/jsrepo"
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*
2
- jsrepo 1.2.1
2
+ jsrepo 1.2.3
3
3
  Installed from github/ieedan/std
4
4
  11-21-2024
5
5
  */
@@ -193,6 +193,8 @@ const _diff = async (options: Options) => {
193
193
  maxUnchanged: options.maxUnchanged,
194
194
  colorAdded: color.greenBright,
195
195
  colorRemoved: color.redBright,
196
+ colorCharsAdded: color.bgGreenBright,
197
+ colorCharsRemoved: color.bgRedBright,
196
198
  prefix: () => `${L} `,
197
199
  onUnchanged: ({ from, to, prefix }) =>
198
200
  `${prefix?.() ?? ''}${color.cyan(from)} → ${color.gray(to)} ${color.gray('(unchanged)')}\n`,
@@ -14,7 +14,7 @@ const schema = v.object({
14
14
  });
15
15
 
16
16
  const getConfig = (cwd: string): Result<Config, string> => {
17
- if (!fs.existsSync(CONFIG_NAME)) {
17
+ if (!fs.existsSync(path.join(cwd, CONFIG_NAME))) {
18
18
  return Err('Could not find your configuration file! Please run `init`.');
19
19
  }
20
20
 
package/src/utils/diff.ts CHANGED
@@ -19,12 +19,46 @@ type Options = {
19
19
  colorRemoved?: (line: string) => string;
20
20
  /** Color the added lines */
21
21
  colorAdded?: (line: string) => string;
22
+ /** Color the removed chars */
23
+ colorCharsRemoved?: (line: string) => string;
24
+ /** Color the added chars */
25
+ colorCharsAdded?: (line: string) => string;
22
26
  /** Prefixes each line with the string returned from this function. */
23
27
  prefix: () => string;
24
28
  intro: (options: Options) => string;
25
29
  onUnchanged: (options: Options) => string;
26
30
  };
27
31
 
32
+ /** Check if a character is whitespace
33
+ *
34
+ * @param str
35
+ * @returns
36
+ */
37
+ const isWhitespace = (str: string) => /^\s+$/g.test(str);
38
+
39
+ /** We need to add a newline at the end of each change to make sure
40
+ * the next change can start correctly. So we take off just 1.
41
+ *
42
+ * @param str
43
+ * @returns
44
+ */
45
+ const trimSingleNewLine = (str: string): string => {
46
+ let i = str.length - 1;
47
+ while (isWhitespace(str[i]) && i >= 0) {
48
+ if (str[i] === '\n') {
49
+ if (str[i - 1] === '\r') {
50
+ return str.slice(0, i - 1);
51
+ }
52
+
53
+ return str.slice(0, i);
54
+ }
55
+
56
+ i--;
57
+ }
58
+
59
+ return str;
60
+ };
61
+
28
62
  const formatDiff = ({
29
63
  from,
30
64
  to,
@@ -33,6 +67,8 @@ const formatDiff = ({
33
67
  maxUnchanged = 5,
34
68
  colorRemoved = color.red,
35
69
  colorAdded = color.green,
70
+ colorCharsRemoved = color.bgRed,
71
+ colorCharsAdded = color.bgGreen,
36
72
  prefix,
37
73
  onUnchanged,
38
74
  intro,
@@ -85,7 +121,7 @@ const formatDiff = ({
85
121
  // show collapsed
86
122
  if (!expand && change.count !== undefined && change.count > maxUnchanged) {
87
123
  const prevLineOffset = lineOffset;
88
- const ls = lines.get(change.value.trimEnd());
124
+ const ls = lines.get(trimSingleNewLine(change.value));
89
125
 
90
126
  let shownLines = 0;
91
127
 
@@ -136,7 +172,7 @@ const formatDiff = ({
136
172
 
137
173
  // show expanded
138
174
 
139
- result += `${lines.join(lines.get(change.value.trimEnd()), {
175
+ result += `${lines.join(lines.get(trimSingleNewLine(change.value)), {
140
176
  prefix: linePrefix,
141
177
  })}\n`;
142
178
  lineOffset += change.count ?? 0;
@@ -144,23 +180,40 @@ const formatDiff = ({
144
180
  continue;
145
181
  }
146
182
 
147
- const colorChange = (change: Change) => {
183
+ const colorLineChange = (change: Change) => {
148
184
  if (change.added) {
149
- return colorAdded(change.value.trimEnd());
185
+ return colorAdded(trimSingleNewLine(change.value));
150
186
  }
151
187
 
152
188
  if (change.removed) {
153
- return colorRemoved(change.value.trimEnd());
189
+ return colorRemoved(trimSingleNewLine(change.value));
154
190
  }
155
191
 
156
192
  return change.value;
157
193
  };
158
194
 
159
- if (change.removed && change.count === 1) {
195
+ const colorCharChange = (change: Change) => {
196
+ if (change.added) {
197
+ return colorCharsAdded(trimSingleNewLine(change.value));
198
+ }
199
+
200
+ if (change.removed) {
201
+ return colorCharsRemoved(trimSingleNewLine(change.value));
202
+ }
203
+
204
+ return change.value;
205
+ };
206
+
207
+ if (
208
+ change.removed &&
209
+ change.count === 1 &&
210
+ changes[i + 1]?.added &&
211
+ changes[i + 1]?.count === 1
212
+ ) {
160
213
  // single line change
161
214
  const diffedChars = diffChars(change.value, changes[i + 1].value);
162
215
 
163
- const sentence = diffedChars.map((chg) => colorChange(chg)).join('');
216
+ const sentence = diffedChars.map((chg) => colorCharChange(chg)).join('');
164
217
 
165
218
  result += `${linePrefix(0)}${sentence}`;
166
219
 
@@ -168,12 +221,24 @@ const formatDiff = ({
168
221
 
169
222
  i++;
170
223
  } else {
171
- result += `${lines.join(lines.get(colorChange(change)), {
172
- prefix: linePrefix,
173
- })}\n`;
224
+ if (isWhitespace(change.value)) {
225
+ // adds some spaces to make sure that you can see the change
226
+ result += `${lines.join(lines.get(colorCharChange(change)), {
227
+ prefix: (line) =>
228
+ `${linePrefix(line)}${colorCharChange({ removed: true, value: ' ', added: false })}`,
229
+ })}\n`;
230
+
231
+ if (!change.removed) {
232
+ lineOffset += change.count ?? 0;
233
+ }
234
+ } else {
235
+ result += `${lines.join(lines.get(colorLineChange(change)), {
236
+ prefix: linePrefix,
237
+ })}\n`;
174
238
 
175
- if (!change.removed) {
176
- lineOffset += change.count ?? 0;
239
+ if (!change.removed) {
240
+ lineOffset += change.count ?? 0;
241
+ }
177
242
  }
178
243
  }
179
244
  }