@xyne/xyne-cli-linux-x64 0.2.6 → 0.3.1

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 (150) hide show
  1. package/node_modules/@opentui/core/node_modules/diff/CONTRIBUTING.md +28 -0
  2. package/node_modules/@opentui/core/node_modules/diff/LICENSE +29 -0
  3. package/node_modules/@opentui/core/node_modules/diff/README.md +378 -0
  4. package/node_modules/@opentui/core/node_modules/diff/dist/diff.js +1674 -0
  5. package/node_modules/@opentui/core/node_modules/diff/dist/diff.min.js +1 -0
  6. package/node_modules/@opentui/core/node_modules/diff/eslint.config.mjs +182 -0
  7. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/dmp.d.ts +8 -0
  8. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/dmp.d.ts.map +1 -0
  9. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/dmp.js +24 -0
  10. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/xml.d.ts +6 -0
  11. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/xml.d.ts.map +1 -0
  12. package/node_modules/@opentui/core/node_modules/diff/libcjs/convert/xml.js +34 -0
  13. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/array.d.ts +19 -0
  14. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/array.d.ts.map +1 -0
  15. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/array.js +40 -0
  16. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/base.d.ts +20 -0
  17. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/base.d.ts.map +1 -0
  18. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/base.js +265 -0
  19. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/character.d.ts +19 -0
  20. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/character.d.ts.map +1 -0
  21. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/character.js +31 -0
  22. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/css.d.ts +18 -0
  23. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/css.d.ts.map +1 -0
  24. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/css.js +34 -0
  25. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/json.d.ts +24 -0
  26. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/json.d.ts.map +1 -0
  27. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/json.js +105 -0
  28. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/line.d.ts +24 -0
  29. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/line.d.ts.map +1 -0
  30. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/line.js +89 -0
  31. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/sentence.d.ts +21 -0
  32. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/sentence.d.ts.map +1 -0
  33. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/sentence.js +67 -0
  34. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/word.d.ts +35 -0
  35. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/word.d.ts.map +1 -0
  36. package/node_modules/@opentui/core/node_modules/diff/libcjs/diff/word.js +307 -0
  37. package/node_modules/@opentui/core/node_modules/diff/libcjs/index.d.ts +20 -0
  38. package/node_modules/@opentui/core/node_modules/diff/libcjs/index.d.ts.map +1 -0
  39. package/node_modules/@opentui/core/node_modules/diff/libcjs/index.js +61 -0
  40. package/node_modules/@opentui/core/node_modules/diff/libcjs/package.json +1 -0
  41. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/apply.d.ts +62 -0
  42. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/apply.d.ts.map +1 -0
  43. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/apply.js +267 -0
  44. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/create.d.ts +100 -0
  45. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/create.d.ts.map +1 -0
  46. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/create.js +223 -0
  47. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/line-endings.d.ts +17 -0
  48. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/line-endings.d.ts.map +1 -0
  49. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/line-endings.js +61 -0
  50. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/parse.d.ts +8 -0
  51. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/parse.d.ts.map +1 -0
  52. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/parse.js +133 -0
  53. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/reverse.d.ts +9 -0
  54. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/reverse.d.ts.map +1 -0
  55. package/node_modules/@opentui/core/node_modules/diff/libcjs/patch/reverse.js +37 -0
  56. package/node_modules/@opentui/core/node_modules/diff/libcjs/types.d.ts +213 -0
  57. package/node_modules/@opentui/core/node_modules/diff/libcjs/types.d.ts.map +1 -0
  58. package/node_modules/@opentui/core/node_modules/diff/libcjs/types.js +2 -0
  59. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/array.d.ts +3 -0
  60. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/array.d.ts.map +1 -0
  61. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/array.js +21 -0
  62. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/distance-iterator.d.ts +2 -0
  63. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/distance-iterator.d.ts.map +1 -0
  64. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/distance-iterator.js +40 -0
  65. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/params.d.ts +4 -0
  66. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/params.d.ts.map +1 -0
  67. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/params.js +17 -0
  68. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/string.d.ts +18 -0
  69. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/string.d.ts.map +1 -0
  70. package/node_modules/@opentui/core/node_modules/diff/libcjs/util/string.js +141 -0
  71. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/dmp.d.ts +8 -0
  72. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/dmp.d.ts.map +1 -0
  73. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/dmp.js +21 -0
  74. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/xml.d.ts +6 -0
  75. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/xml.d.ts.map +1 -0
  76. package/node_modules/@opentui/core/node_modules/diff/libesm/convert/xml.js +31 -0
  77. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/array.d.ts +19 -0
  78. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/array.d.ts.map +1 -0
  79. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/array.js +16 -0
  80. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/base.d.ts +20 -0
  81. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/base.d.ts.map +1 -0
  82. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/base.js +253 -0
  83. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/character.d.ts +19 -0
  84. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/character.d.ts.map +1 -0
  85. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/character.js +7 -0
  86. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/css.d.ts +18 -0
  87. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/css.d.ts.map +1 -0
  88. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/css.js +10 -0
  89. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/json.d.ts +24 -0
  90. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/json.d.ts.map +1 -0
  91. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/json.js +78 -0
  92. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/line.d.ts +24 -0
  93. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/line.d.ts.map +1 -0
  94. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/line.js +65 -0
  95. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/sentence.d.ts +21 -0
  96. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/sentence.d.ts.map +1 -0
  97. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/sentence.js +43 -0
  98. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/word.d.ts +35 -0
  99. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/word.d.ts.map +1 -0
  100. package/node_modules/@opentui/core/node_modules/diff/libesm/diff/word.js +276 -0
  101. package/node_modules/@opentui/core/node_modules/diff/libesm/index.d.ts +20 -0
  102. package/node_modules/@opentui/core/node_modules/diff/libesm/index.d.ts.map +1 -0
  103. package/node_modules/@opentui/core/node_modules/diff/libesm/index.js +30 -0
  104. package/node_modules/@opentui/core/node_modules/diff/libesm/package.json +1 -0
  105. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/apply.d.ts +62 -0
  106. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/apply.d.ts.map +1 -0
  107. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/apply.js +257 -0
  108. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/create.d.ts +100 -0
  109. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/create.d.ts.map +1 -0
  110. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/create.js +201 -0
  111. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/line-endings.d.ts +17 -0
  112. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/line-endings.d.ts.map +1 -0
  113. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/line-endings.js +44 -0
  114. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/parse.d.ts +8 -0
  115. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/parse.d.ts.map +1 -0
  116. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/parse.js +130 -0
  117. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/reverse.d.ts +9 -0
  118. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/reverse.d.ts.map +1 -0
  119. package/node_modules/@opentui/core/node_modules/diff/libesm/patch/reverse.js +23 -0
  120. package/node_modules/@opentui/core/node_modules/diff/libesm/types.d.ts +213 -0
  121. package/node_modules/@opentui/core/node_modules/diff/libesm/types.d.ts.map +1 -0
  122. package/node_modules/@opentui/core/node_modules/diff/libesm/types.js +1 -0
  123. package/node_modules/@opentui/core/node_modules/diff/libesm/util/array.d.ts +3 -0
  124. package/node_modules/@opentui/core/node_modules/diff/libesm/util/array.d.ts.map +1 -0
  125. package/node_modules/@opentui/core/node_modules/diff/libesm/util/array.js +17 -0
  126. package/node_modules/@opentui/core/node_modules/diff/libesm/util/distance-iterator.d.ts +2 -0
  127. package/node_modules/@opentui/core/node_modules/diff/libesm/util/distance-iterator.d.ts.map +1 -0
  128. package/node_modules/@opentui/core/node_modules/diff/libesm/util/distance-iterator.js +37 -0
  129. package/node_modules/@opentui/core/node_modules/diff/libesm/util/params.d.ts +4 -0
  130. package/node_modules/@opentui/core/node_modules/diff/libesm/util/params.d.ts.map +1 -0
  131. package/node_modules/@opentui/core/node_modules/diff/libesm/util/params.js +14 -0
  132. package/node_modules/@opentui/core/node_modules/diff/libesm/util/string.d.ts +18 -0
  133. package/node_modules/@opentui/core/node_modules/diff/libesm/util/string.d.ts.map +1 -0
  134. package/node_modules/@opentui/core/node_modules/diff/libesm/util/string.js +128 -0
  135. package/node_modules/@opentui/core/node_modules/diff/package.json +131 -0
  136. package/node_modules/@opentui/core/node_modules/diff/release-notes.md +397 -0
  137. package/node_modules/@opentui/core/node_modules/marked/LICENSE.md +44 -0
  138. package/node_modules/@opentui/core/node_modules/marked/README.md +107 -0
  139. package/node_modules/@opentui/core/node_modules/marked/bin/main.js +283 -0
  140. package/node_modules/@opentui/core/node_modules/marked/bin/marked.js +16 -0
  141. package/node_modules/@opentui/core/node_modules/marked/lib/marked.d.ts +756 -0
  142. package/node_modules/@opentui/core/node_modules/marked/lib/marked.esm.js +72 -0
  143. package/node_modules/@opentui/core/node_modules/marked/lib/marked.esm.js.map +7 -0
  144. package/node_modules/@opentui/core/node_modules/marked/lib/marked.umd.js +74 -0
  145. package/node_modules/@opentui/core/node_modules/marked/lib/marked.umd.js.map +7 -0
  146. package/node_modules/@opentui/core/node_modules/marked/man/marked.1 +113 -0
  147. package/node_modules/@opentui/core/node_modules/marked/man/marked.1.md +93 -0
  148. package/node_modules/@opentui/core/node_modules/marked/package.json +103 -0
  149. package/package.json +1 -1
  150. package/xyne-linux-x64 +0 -0
@@ -0,0 +1,62 @@
1
+ import type { StructuredPatch } from '../types.js';
2
+ export interface ApplyPatchOptions {
3
+ /**
4
+ * Maximum Levenshtein distance (in lines deleted, added, or subtituted) between the context shown in a patch hunk and the lines found in the file.
5
+ * @default 0
6
+ */
7
+ fuzzFactor?: number;
8
+ /**
9
+ * If `true`, and if the file to be patched consistently uses different line endings to the patch (i.e. either the file always uses Unix line endings while the patch uses Windows ones, or vice versa), then `applyPatch` will behave as if the line endings in the patch were the same as those in the source file.
10
+ * (If `false`, the patch will usually fail to apply in such circumstances since lines deleted in the patch won't be considered to match those in the source file.)
11
+ * @default true
12
+ */
13
+ autoConvertLineEndings?: boolean;
14
+ /**
15
+ * Callback used to compare to given lines to determine if they should be considered equal when patching.
16
+ * Defaults to strict equality but may be overridden to provide fuzzier comparison.
17
+ * Should return false if the lines should be rejected.
18
+ */
19
+ compareLine?: (lineNumber: number, line: string, operation: string, patchContent: string) => boolean;
20
+ }
21
+ /**
22
+ * attempts to apply a unified diff patch.
23
+ *
24
+ * Hunks are applied first to last.
25
+ * `applyPatch` first tries to apply the first hunk at the line number specified in the hunk header, and with all context lines matching exactly.
26
+ * If that fails, it tries scanning backwards and forwards, one line at a time, to find a place to apply the hunk where the context lines match exactly.
27
+ * If that still fails, and `fuzzFactor` is greater than zero, it increments the maximum number of mismatches (missing, extra, or changed context lines) that there can be between the hunk context and a region where we are trying to apply the patch such that the hunk will still be considered to match.
28
+ * Regardless of `fuzzFactor`, lines to be deleted in the hunk *must* be present for a hunk to match, and the context lines *immediately* before and after an insertion must match exactly.
29
+ *
30
+ * Once a hunk is successfully fitted, the process begins again with the next hunk.
31
+ * Regardless of `fuzzFactor`, later hunks must be applied later in the file than earlier hunks.
32
+ *
33
+ * If a hunk cannot be successfully fitted *anywhere* with fewer than `fuzzFactor` mismatches, `applyPatch` fails and returns `false`.
34
+ *
35
+ * If a hunk is successfully fitted but not at the line number specified by the hunk header, all subsequent hunks have their target line number adjusted accordingly.
36
+ * (e.g. if the first hunk is applied 10 lines below where the hunk header said it should fit, `applyPatch` will *start* looking for somewhere to apply the second hunk 10 lines below where its hunk header says it goes.)
37
+ *
38
+ * If the patch was applied successfully, returns a string containing the patched text.
39
+ * If the patch could not be applied (because some hunks in the patch couldn't be fitted to the text in `source`), `applyPatch` returns false.
40
+ *
41
+ * @param patch a string diff or the output from the `parsePatch` or `structuredPatch` methods.
42
+ */
43
+ export declare function applyPatch(source: string, patch: string | StructuredPatch | [StructuredPatch], options?: ApplyPatchOptions): string | false;
44
+ export interface ApplyPatchesOptions extends ApplyPatchOptions {
45
+ loadFile: (index: StructuredPatch, callback: (err: any, data: string) => void) => void;
46
+ patched: (index: StructuredPatch, content: string | false, callback: (err: any) => void) => void;
47
+ complete: (err?: any) => void;
48
+ }
49
+ /**
50
+ * applies one or more patches.
51
+ *
52
+ * `patch` may be either an array of structured patch objects, or a string representing a patch in unified diff format (which may patch one or more files).
53
+ *
54
+ * This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:
55
+ *
56
+ * - `options.loadFile(index, callback)` is called. The caller should then load the contents of the file and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
57
+ * - `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be the return value from `applyPatch`. When it's ready, the caller should call `callback(err)` callback. Passing an `err` will terminate further patch execution.
58
+ *
59
+ * Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.
60
+ */
61
+ export declare function applyPatches(uniDiff: string | StructuredPatch[], options: ApplyPatchesOptions): void;
62
+ //# sourceMappingURL=apply.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../src/patch/apply.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC;CACtG;AAOD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,CAAC,eAAe,CAAC,EACnD,OAAO,GAAE,iBAAsB,GAC9B,MAAM,GAAG,KAAK,CAehB;AAyPD,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAC5D,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,KAAK,IAAI,CAAC;IACvF,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,KAAK,IAAI,CAAC;IACjG,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;CAC/B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,EAAE,EAAE,OAAO,EAAE,mBAAmB,GAAG,IAAI,CA0BpG"}
@@ -0,0 +1,257 @@
1
+ import { hasOnlyWinLineEndings, hasOnlyUnixLineEndings } from '../util/string.js';
2
+ import { isWin, isUnix, unixToWin, winToUnix } from './line-endings.js';
3
+ import { parsePatch } from './parse.js';
4
+ import distanceIterator from '../util/distance-iterator.js';
5
+ /**
6
+ * attempts to apply a unified diff patch.
7
+ *
8
+ * Hunks are applied first to last.
9
+ * `applyPatch` first tries to apply the first hunk at the line number specified in the hunk header, and with all context lines matching exactly.
10
+ * If that fails, it tries scanning backwards and forwards, one line at a time, to find a place to apply the hunk where the context lines match exactly.
11
+ * If that still fails, and `fuzzFactor` is greater than zero, it increments the maximum number of mismatches (missing, extra, or changed context lines) that there can be between the hunk context and a region where we are trying to apply the patch such that the hunk will still be considered to match.
12
+ * Regardless of `fuzzFactor`, lines to be deleted in the hunk *must* be present for a hunk to match, and the context lines *immediately* before and after an insertion must match exactly.
13
+ *
14
+ * Once a hunk is successfully fitted, the process begins again with the next hunk.
15
+ * Regardless of `fuzzFactor`, later hunks must be applied later in the file than earlier hunks.
16
+ *
17
+ * If a hunk cannot be successfully fitted *anywhere* with fewer than `fuzzFactor` mismatches, `applyPatch` fails and returns `false`.
18
+ *
19
+ * If a hunk is successfully fitted but not at the line number specified by the hunk header, all subsequent hunks have their target line number adjusted accordingly.
20
+ * (e.g. if the first hunk is applied 10 lines below where the hunk header said it should fit, `applyPatch` will *start* looking for somewhere to apply the second hunk 10 lines below where its hunk header says it goes.)
21
+ *
22
+ * If the patch was applied successfully, returns a string containing the patched text.
23
+ * If the patch could not be applied (because some hunks in the patch couldn't be fitted to the text in `source`), `applyPatch` returns false.
24
+ *
25
+ * @param patch a string diff or the output from the `parsePatch` or `structuredPatch` methods.
26
+ */
27
+ export function applyPatch(source, patch, options = {}) {
28
+ let patches;
29
+ if (typeof patch === 'string') {
30
+ patches = parsePatch(patch);
31
+ }
32
+ else if (Array.isArray(patch)) {
33
+ patches = patch;
34
+ }
35
+ else {
36
+ patches = [patch];
37
+ }
38
+ if (patches.length > 1) {
39
+ throw new Error('applyPatch only works with a single input.');
40
+ }
41
+ return applyStructuredPatch(source, patches[0], options);
42
+ }
43
+ function applyStructuredPatch(source, patch, options = {}) {
44
+ if (options.autoConvertLineEndings || options.autoConvertLineEndings == null) {
45
+ if (hasOnlyWinLineEndings(source) && isUnix(patch)) {
46
+ patch = unixToWin(patch);
47
+ }
48
+ else if (hasOnlyUnixLineEndings(source) && isWin(patch)) {
49
+ patch = winToUnix(patch);
50
+ }
51
+ }
52
+ // Apply the diff to the input
53
+ const lines = source.split('\n'), hunks = patch.hunks, compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent), fuzzFactor = options.fuzzFactor || 0;
54
+ let minLine = 0;
55
+ if (fuzzFactor < 0 || !Number.isInteger(fuzzFactor)) {
56
+ throw new Error('fuzzFactor must be a non-negative integer');
57
+ }
58
+ // Special case for empty patch.
59
+ if (!hunks.length) {
60
+ return source;
61
+ }
62
+ // Before anything else, handle EOFNL insertion/removal. If the patch tells us to make a change
63
+ // to the EOFNL that is redundant/impossible - i.e. to remove a newline that's not there, or add a
64
+ // newline that already exists - then we either return false and fail to apply the patch (if
65
+ // fuzzFactor is 0) or simply ignore the problem and do nothing (if fuzzFactor is >0).
66
+ // If we do need to remove/add a newline at EOF, this will always be in the final hunk:
67
+ let prevLine = '', removeEOFNL = false, addEOFNL = false;
68
+ for (let i = 0; i < hunks[hunks.length - 1].lines.length; i++) {
69
+ const line = hunks[hunks.length - 1].lines[i];
70
+ if (line[0] == '\\') {
71
+ if (prevLine[0] == '+') {
72
+ removeEOFNL = true;
73
+ }
74
+ else if (prevLine[0] == '-') {
75
+ addEOFNL = true;
76
+ }
77
+ }
78
+ prevLine = line;
79
+ }
80
+ if (removeEOFNL) {
81
+ if (addEOFNL) {
82
+ // This means the final line gets changed but doesn't have a trailing newline in either the
83
+ // original or patched version. In that case, we do nothing if fuzzFactor > 0, and if
84
+ // fuzzFactor is 0, we simply validate that the source file has no trailing newline.
85
+ if (!fuzzFactor && lines[lines.length - 1] == '') {
86
+ return false;
87
+ }
88
+ }
89
+ else if (lines[lines.length - 1] == '') {
90
+ lines.pop();
91
+ }
92
+ else if (!fuzzFactor) {
93
+ return false;
94
+ }
95
+ }
96
+ else if (addEOFNL) {
97
+ if (lines[lines.length - 1] != '') {
98
+ lines.push('');
99
+ }
100
+ else if (!fuzzFactor) {
101
+ return false;
102
+ }
103
+ }
104
+ /**
105
+ * Checks if the hunk can be made to fit at the provided location with at most `maxErrors`
106
+ * insertions, substitutions, or deletions, while ensuring also that:
107
+ * - lines deleted in the hunk match exactly, and
108
+ * - wherever an insertion operation or block of insertion operations appears in the hunk, the
109
+ * immediately preceding and following lines of context match exactly
110
+ *
111
+ * `toPos` should be set such that lines[toPos] is meant to match hunkLines[0].
112
+ *
113
+ * If the hunk can be applied, returns an object with properties `oldLineLastI` and
114
+ * `replacementLines`. Otherwise, returns null.
115
+ */
116
+ function applyHunk(hunkLines, toPos, maxErrors, hunkLinesI = 0, lastContextLineMatched = true, patchedLines = [], patchedLinesLength = 0) {
117
+ let nConsecutiveOldContextLines = 0;
118
+ let nextContextLineMustMatch = false;
119
+ for (; hunkLinesI < hunkLines.length; hunkLinesI++) {
120
+ const hunkLine = hunkLines[hunkLinesI], operation = (hunkLine.length > 0 ? hunkLine[0] : ' '), content = (hunkLine.length > 0 ? hunkLine.substr(1) : hunkLine);
121
+ if (operation === '-') {
122
+ if (compareLine(toPos + 1, lines[toPos], operation, content)) {
123
+ toPos++;
124
+ nConsecutiveOldContextLines = 0;
125
+ }
126
+ else {
127
+ if (!maxErrors || lines[toPos] == null) {
128
+ return null;
129
+ }
130
+ patchedLines[patchedLinesLength] = lines[toPos];
131
+ return applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1);
132
+ }
133
+ }
134
+ if (operation === '+') {
135
+ if (!lastContextLineMatched) {
136
+ return null;
137
+ }
138
+ patchedLines[patchedLinesLength] = content;
139
+ patchedLinesLength++;
140
+ nConsecutiveOldContextLines = 0;
141
+ nextContextLineMustMatch = true;
142
+ }
143
+ if (operation === ' ') {
144
+ nConsecutiveOldContextLines++;
145
+ patchedLines[patchedLinesLength] = lines[toPos];
146
+ if (compareLine(toPos + 1, lines[toPos], operation, content)) {
147
+ patchedLinesLength++;
148
+ lastContextLineMatched = true;
149
+ nextContextLineMustMatch = false;
150
+ toPos++;
151
+ }
152
+ else {
153
+ if (nextContextLineMustMatch || !maxErrors) {
154
+ return null;
155
+ }
156
+ // Consider 3 possibilities in sequence:
157
+ // 1. lines contains a *substitution* not included in the patch context, or
158
+ // 2. lines contains an *insertion* not included in the patch context, or
159
+ // 3. lines contains a *deletion* not included in the patch context
160
+ // The first two options are of course only possible if the line from lines is non-null -
161
+ // i.e. only option 3 is possible if we've overrun the end of the old file.
162
+ return (lines[toPos] && (applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength + 1) || applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1)) || applyHunk(hunkLines, toPos, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength));
163
+ }
164
+ }
165
+ }
166
+ // Before returning, trim any unmodified context lines off the end of patchedLines and reduce
167
+ // toPos (and thus oldLineLastI) accordingly. This allows later hunks to be applied to a region
168
+ // that starts in this hunk's trailing context.
169
+ patchedLinesLength -= nConsecutiveOldContextLines;
170
+ toPos -= nConsecutiveOldContextLines;
171
+ patchedLines.length = patchedLinesLength;
172
+ return {
173
+ patchedLines,
174
+ oldLineLastI: toPos - 1
175
+ };
176
+ }
177
+ const resultLines = [];
178
+ // Search best fit offsets for each hunk based on the previous ones
179
+ let prevHunkOffset = 0;
180
+ for (let i = 0; i < hunks.length; i++) {
181
+ const hunk = hunks[i];
182
+ let hunkResult;
183
+ const maxLine = lines.length - hunk.oldLines + fuzzFactor;
184
+ let toPos;
185
+ for (let maxErrors = 0; maxErrors <= fuzzFactor; maxErrors++) {
186
+ toPos = hunk.oldStart + prevHunkOffset - 1;
187
+ const iterator = distanceIterator(toPos, minLine, maxLine);
188
+ for (; toPos !== undefined; toPos = iterator()) {
189
+ hunkResult = applyHunk(hunk.lines, toPos, maxErrors);
190
+ if (hunkResult) {
191
+ break;
192
+ }
193
+ }
194
+ if (hunkResult) {
195
+ break;
196
+ }
197
+ }
198
+ if (!hunkResult) {
199
+ return false;
200
+ }
201
+ // Copy everything from the end of where we applied the last hunk to the start of this hunk
202
+ for (let i = minLine; i < toPos; i++) {
203
+ resultLines.push(lines[i]);
204
+ }
205
+ // Add the lines produced by applying the hunk:
206
+ for (let i = 0; i < hunkResult.patchedLines.length; i++) {
207
+ const line = hunkResult.patchedLines[i];
208
+ resultLines.push(line);
209
+ }
210
+ // Set lower text limit to end of the current hunk, so next ones don't try
211
+ // to fit over already patched text
212
+ minLine = hunkResult.oldLineLastI + 1;
213
+ // Note the offset between where the patch said the hunk should've applied and where we
214
+ // applied it, so we can adjust future hunks accordingly:
215
+ prevHunkOffset = toPos + 1 - hunk.oldStart;
216
+ }
217
+ // Copy over the rest of the lines from the old text
218
+ for (let i = minLine; i < lines.length; i++) {
219
+ resultLines.push(lines[i]);
220
+ }
221
+ return resultLines.join('\n');
222
+ }
223
+ /**
224
+ * applies one or more patches.
225
+ *
226
+ * `patch` may be either an array of structured patch objects, or a string representing a patch in unified diff format (which may patch one or more files).
227
+ *
228
+ * This method will iterate over the contents of the patch and apply to data provided through callbacks. The general flow for each patch index is:
229
+ *
230
+ * - `options.loadFile(index, callback)` is called. The caller should then load the contents of the file and then pass that to the `callback(err, data)` callback. Passing an `err` will terminate further patch execution.
231
+ * - `options.patched(index, content, callback)` is called once the patch has been applied. `content` will be the return value from `applyPatch`. When it's ready, the caller should call `callback(err)` callback. Passing an `err` will terminate further patch execution.
232
+ *
233
+ * Once all patches have been applied or an error occurs, the `options.complete(err)` callback is made.
234
+ */
235
+ export function applyPatches(uniDiff, options) {
236
+ const spDiff = typeof uniDiff === 'string' ? parsePatch(uniDiff) : uniDiff;
237
+ let currentIndex = 0;
238
+ function processIndex() {
239
+ const index = spDiff[currentIndex++];
240
+ if (!index) {
241
+ return options.complete();
242
+ }
243
+ options.loadFile(index, function (err, data) {
244
+ if (err) {
245
+ return options.complete(err);
246
+ }
247
+ const updatedContent = applyPatch(data, index, options);
248
+ options.patched(index, updatedContent, function (err) {
249
+ if (err) {
250
+ return options.complete(err);
251
+ }
252
+ processIndex();
253
+ });
254
+ });
255
+ }
256
+ processIndex();
257
+ }
@@ -0,0 +1,100 @@
1
+ import type { StructuredPatch, DiffLinesOptionsAbortable, DiffLinesOptionsNonabortable, AbortableDiffOptions } from '../types.js';
2
+ type StructuredPatchCallbackAbortable = (patch: StructuredPatch | undefined) => void;
3
+ type StructuredPatchCallbackNonabortable = (patch: StructuredPatch) => void;
4
+ interface _StructuredPatchOptionsAbortable extends Pick<DiffLinesOptionsAbortable, 'ignoreWhitespace' | 'stripTrailingCr'> {
5
+ /**
6
+ * describes how many lines of context should be included.
7
+ * You can set this to `Number.MAX_SAFE_INTEGER` or `Infinity` to include the entire file content in one hunk.
8
+ * @default 4
9
+ */
10
+ context?: number;
11
+ callback?: StructuredPatchCallbackAbortable;
12
+ }
13
+ export type StructuredPatchOptionsAbortable = _StructuredPatchOptionsAbortable & AbortableDiffOptions;
14
+ export interface StructuredPatchOptionsNonabortable extends Pick<DiffLinesOptionsNonabortable, 'ignoreWhitespace' | 'stripTrailingCr'> {
15
+ context?: number;
16
+ callback?: StructuredPatchCallbackNonabortable;
17
+ }
18
+ interface StructuredPatchCallbackOptionAbortable {
19
+ /**
20
+ * If provided, the diff will be computed in async mode to avoid blocking the event loop while the diff is calculated.
21
+ * The value of the `callback` option should be a function and will be passed the computed diff or patch as its first argument.
22
+ */
23
+ callback: StructuredPatchCallbackAbortable;
24
+ }
25
+ interface StructuredPatchCallbackOptionNonabortable {
26
+ /**
27
+ * If provided, the diff will be computed in async mode to avoid blocking the event loop while the diff is calculated.
28
+ * The value of the `callback` option should be a function and will be passed the computed diff or patch as its first argument.
29
+ */
30
+ callback: StructuredPatchCallbackNonabortable;
31
+ }
32
+ /**
33
+ * returns an object with an array of hunk objects.
34
+ *
35
+ * This method is similar to createTwoFilesPatch, but returns a data structure suitable for further processing.
36
+ * @param oldFileName String to be output in the filename section of the patch for the removals
37
+ * @param newFileName String to be output in the filename section of the patch for the additions
38
+ * @param oldStr Original string value
39
+ * @param newStr New string value
40
+ * @param oldHeader Optional additional information to include in the old file header.
41
+ * @param newHeader Optional additional information to include in the new file header.
42
+ */
43
+ export declare function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: StructuredPatchCallbackNonabortable): undefined;
44
+ export declare function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: StructuredPatchOptionsAbortable & StructuredPatchCallbackOptionAbortable): undefined;
45
+ export declare function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: StructuredPatchOptionsNonabortable & StructuredPatchCallbackOptionNonabortable): undefined;
46
+ export declare function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: StructuredPatchOptionsAbortable): StructuredPatch | undefined;
47
+ export declare function structuredPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: StructuredPatchOptionsNonabortable): StructuredPatch;
48
+ /**
49
+ * creates a unified diff patch.
50
+ * @param patch either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`)
51
+ */
52
+ export declare function formatPatch(patch: StructuredPatch | StructuredPatch[]): string;
53
+ type CreatePatchCallbackAbortable = (patch: string | undefined) => void;
54
+ type CreatePatchCallbackNonabortable = (patch: string) => void;
55
+ interface _CreatePatchOptionsAbortable extends Pick<DiffLinesOptionsAbortable, 'ignoreWhitespace' | 'stripTrailingCr'> {
56
+ context?: number;
57
+ callback?: CreatePatchCallbackAbortable;
58
+ }
59
+ export type CreatePatchOptionsAbortable = _CreatePatchOptionsAbortable & AbortableDiffOptions;
60
+ export interface CreatePatchOptionsNonabortable extends Pick<DiffLinesOptionsNonabortable, 'ignoreWhitespace' | 'stripTrailingCr'> {
61
+ context?: number;
62
+ callback?: CreatePatchCallbackNonabortable;
63
+ }
64
+ interface CreatePatchCallbackOptionAbortable {
65
+ callback: CreatePatchCallbackAbortable;
66
+ }
67
+ interface CreatePatchCallbackOptionNonabortable {
68
+ callback: CreatePatchCallbackNonabortable;
69
+ }
70
+ /**
71
+ * creates a unified diff patch by first computing a diff with `diffLines` and then serializing it to unified diff format.
72
+ * @param oldFileName String to be output in the filename section of the patch for the removals
73
+ * @param newFileName String to be output in the filename section of the patch for the additions
74
+ * @param oldStr Original string value
75
+ * @param newStr New string value
76
+ * @param oldHeader Optional additional information to include in the old file header.
77
+ * @param newHeader Optional additional information to include in the new file header.
78
+ */
79
+ export declare function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchCallbackNonabortable): undefined;
80
+ export declare function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsAbortable & CreatePatchCallbackOptionAbortable): undefined;
81
+ export declare function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsNonabortable & CreatePatchCallbackOptionNonabortable): undefined;
82
+ export declare function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsAbortable): string | undefined;
83
+ export declare function createTwoFilesPatch(oldFileName: string, newFileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: CreatePatchOptionsNonabortable): string;
84
+ /**
85
+ * creates a unified diff patch.
86
+ *
87
+ * Just like createTwoFilesPatch, but with oldFileName being equal to newFileName.
88
+ * @param fileName String to be output in the filename section of the patch
89
+ * @param oldStr Original string value
90
+ * @param newStr New string value
91
+ * @param oldHeader Optional additional information to include in the old file header.
92
+ * @param newHeader Optional additional information to include in the new file header.
93
+ */
94
+ export declare function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchCallbackNonabortable): undefined;
95
+ export declare function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsAbortable & CreatePatchCallbackOptionAbortable): undefined;
96
+ export declare function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsNonabortable & CreatePatchCallbackOptionNonabortable): undefined;
97
+ export declare function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader: string | undefined, newHeader: string | undefined, options: CreatePatchOptionsAbortable): string | undefined;
98
+ export declare function createPatch(fileName: string, oldStr: string, newStr: string, oldHeader?: string, newHeader?: string, options?: CreatePatchOptionsNonabortable): string;
99
+ export {};
100
+ //# sourceMappingURL=create.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/patch/create.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAyB,EAAE,4BAA4B,EAAE,oBAAoB,EAAgB,MAAM,aAAa,CAAC;AAEhJ,KAAK,gCAAgC,GAAG,CAAC,KAAK,EAAE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,CAAC,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,8BAA8B,GACvC,MAAM,CAAA"}
@@ -0,0 +1,201 @@
1
+ import { diffLines } from '../diff/line.js';
2
+ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
3
+ let optionsObj;
4
+ if (!options) {
5
+ optionsObj = {};
6
+ }
7
+ else if (typeof options === 'function') {
8
+ optionsObj = { callback: options };
9
+ }
10
+ else {
11
+ optionsObj = options;
12
+ }
13
+ if (typeof optionsObj.context === 'undefined') {
14
+ optionsObj.context = 4;
15
+ }
16
+ // We copy this into its own variable to placate TypeScript, which thinks
17
+ // optionsObj.context might be undefined in the callbacks below.
18
+ const context = optionsObj.context;
19
+ // @ts-expect-error (runtime check for something that is correctly a static type error)
20
+ if (optionsObj.newlineIsToken) {
21
+ throw new Error('newlineIsToken may not be used with patch-generation functions, only with diffing functions');
22
+ }
23
+ if (!optionsObj.callback) {
24
+ return diffLinesResultToPatch(diffLines(oldStr, newStr, optionsObj));
25
+ }
26
+ else {
27
+ const { callback } = optionsObj;
28
+ diffLines(oldStr, newStr, Object.assign(Object.assign({}, optionsObj), { callback: (diff) => {
29
+ const patch = diffLinesResultToPatch(diff);
30
+ // TypeScript is unhappy without the cast because it does not understand that `patch` may
31
+ // be undefined here only if `callback` is StructuredPatchCallbackAbortable:
32
+ callback(patch);
33
+ } }));
34
+ }
35
+ function diffLinesResultToPatch(diff) {
36
+ // STEP 1: Build up the patch with no "" lines and with the arrays
37
+ // of lines containing trailing newline characters. We'll tidy up later...
38
+ if (!diff) {
39
+ return;
40
+ }
41
+ diff.push({ value: '', lines: [] }); // Append an empty value to make cleanup easier
42
+ function contextLines(lines) {
43
+ return lines.map(function (entry) { return ' ' + entry; });
44
+ }
45
+ const hunks = [];
46
+ let oldRangeStart = 0, newRangeStart = 0, curRange = [], oldLine = 1, newLine = 1;
47
+ for (let i = 0; i < diff.length; i++) {
48
+ const current = diff[i], lines = current.lines || splitLines(current.value);
49
+ current.lines = lines;
50
+ if (current.added || current.removed) {
51
+ // If we have previous context, start with that
52
+ if (!oldRangeStart) {
53
+ const prev = diff[i - 1];
54
+ oldRangeStart = oldLine;
55
+ newRangeStart = newLine;
56
+ if (prev) {
57
+ curRange = context > 0 ? contextLines(prev.lines.slice(-context)) : [];
58
+ oldRangeStart -= curRange.length;
59
+ newRangeStart -= curRange.length;
60
+ }
61
+ }
62
+ // Output our changes
63
+ for (const line of lines) {
64
+ curRange.push((current.added ? '+' : '-') + line);
65
+ }
66
+ // Track the updated file position
67
+ if (current.added) {
68
+ newLine += lines.length;
69
+ }
70
+ else {
71
+ oldLine += lines.length;
72
+ }
73
+ }
74
+ else {
75
+ // Identical context lines. Track line changes
76
+ if (oldRangeStart) {
77
+ // Close out any changes that have been output (or join overlapping)
78
+ if (lines.length <= context * 2 && i < diff.length - 2) {
79
+ // Overlapping
80
+ for (const line of contextLines(lines)) {
81
+ curRange.push(line);
82
+ }
83
+ }
84
+ else {
85
+ // end the range and output
86
+ const contextSize = Math.min(lines.length, context);
87
+ for (const line of contextLines(lines.slice(0, contextSize))) {
88
+ curRange.push(line);
89
+ }
90
+ const hunk = {
91
+ oldStart: oldRangeStart,
92
+ oldLines: (oldLine - oldRangeStart + contextSize),
93
+ newStart: newRangeStart,
94
+ newLines: (newLine - newRangeStart + contextSize),
95
+ lines: curRange
96
+ };
97
+ hunks.push(hunk);
98
+ oldRangeStart = 0;
99
+ newRangeStart = 0;
100
+ curRange = [];
101
+ }
102
+ }
103
+ oldLine += lines.length;
104
+ newLine += lines.length;
105
+ }
106
+ }
107
+ // Step 2: eliminate the trailing `\n` from each line of each hunk, and, where needed, add
108
+ // "".
109
+ for (const hunk of hunks) {
110
+ for (let i = 0; i < hunk.lines.length; i++) {
111
+ if (hunk.lines[i].endsWith('\n')) {
112
+ hunk.lines[i] = hunk.lines[i].slice(0, -1);
113
+ }
114
+ else {
115
+ hunk.lines.splice(i + 1, 0, '\');
116
+ i++; // Skip the line we just added, then continue iterating
117
+ }
118
+ }
119
+ }
120
+ return {
121
+ oldFileName: oldFileName, newFileName: newFileName,
122
+ oldHeader: oldHeader, newHeader: newHeader,
123
+ hunks: hunks
124
+ };
125
+ }
126
+ }
127
+ /**
128
+ * creates a unified diff patch.
129
+ * @param patch either a single structured patch object (as returned by `structuredPatch`) or an array of them (as returned by `parsePatch`)
130
+ */
131
+ export function formatPatch(patch) {
132
+ if (Array.isArray(patch)) {
133
+ return patch.map(formatPatch).join('\n');
134
+ }
135
+ const ret = [];
136
+ if (patch.oldFileName == patch.newFileName) {
137
+ ret.push('Index: ' + patch.oldFileName);
138
+ }
139
+ ret.push('===================================================================');
140
+ ret.push('--- ' + patch.oldFileName + (typeof patch.oldHeader === 'undefined' ? '' : '\t' + patch.oldHeader));
141
+ ret.push('+++ ' + patch.newFileName + (typeof patch.newHeader === 'undefined' ? '' : '\t' + patch.newHeader));
142
+ for (let i = 0; i < patch.hunks.length; i++) {
143
+ const hunk = patch.hunks[i];
144
+ // Unified Diff Format quirk: If the chunk size is 0,
145
+ // the first number is one lower than one would expect.
146
+ // https://www.artima.com/weblogs/viewpost.jsp?thread=164293
147
+ if (hunk.oldLines === 0) {
148
+ hunk.oldStart -= 1;
149
+ }
150
+ if (hunk.newLines === 0) {
151
+ hunk.newStart -= 1;
152
+ }
153
+ ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines
154
+ + ' +' + hunk.newStart + ',' + hunk.newLines
155
+ + ' @@');
156
+ for (const line of hunk.lines) {
157
+ ret.push(line);
158
+ }
159
+ }
160
+ return ret.join('\n') + '\n';
161
+ }
162
+ export function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) {
163
+ if (typeof options === 'function') {
164
+ options = { callback: options };
165
+ }
166
+ if (!(options === null || options === void 0 ? void 0 : options.callback)) {
167
+ const patchObj = structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options);
168
+ if (!patchObj) {
169
+ return;
170
+ }
171
+ return formatPatch(patchObj);
172
+ }
173
+ else {
174
+ const { callback } = options;
175
+ structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, Object.assign(Object.assign({}, options), { callback: patchObj => {
176
+ if (!patchObj) {
177
+ callback(undefined);
178
+ }
179
+ else {
180
+ callback(formatPatch(patchObj));
181
+ }
182
+ } }));
183
+ }
184
+ }
185
+ export function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options) {
186
+ return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options);
187
+ }
188
+ /**
189
+ * Split `text` into an array of lines, including the trailing newline character (where present)
190
+ */
191
+ function splitLines(text) {
192
+ const hasTrailingNl = text.endsWith('\n');
193
+ const result = text.split('\n').map(line => line + '\n');
194
+ if (hasTrailingNl) {
195
+ result.pop();
196
+ }
197
+ else {
198
+ result.push(result.pop().slice(0, -1));
199
+ }
200
+ return result;
201
+ }
@@ -0,0 +1,17 @@
1
+ import type { StructuredPatch } from '../types.js';
2
+ export declare function unixToWin(patch: StructuredPatch): StructuredPatch;
3
+ export declare function unixToWin(patches: StructuredPatch[]): StructuredPatch[];
4
+ export declare function unixToWin(patch: StructuredPatch | StructuredPatch[]): StructuredPatch | StructuredPatch[];
5
+ export declare function winToUnix(patch: StructuredPatch): StructuredPatch;
6
+ export declare function winToUnix(patches: StructuredPatch[]): StructuredPatch[];
7
+ export declare function winToUnix(patch: StructuredPatch | StructuredPatch[]): StructuredPatch | StructuredPatch[];
8
+ /**
9
+ * Returns true if the patch consistently uses Unix line endings (or only involves one line and has
10
+ * no line endings).
11
+ */
12
+ export declare function isUnix(patch: StructuredPatch | StructuredPatch[]): boolean;
13
+ /**
14
+ * Returns true if the patch uses Windows line endings and only Windows line endings.
15
+ */
16
+ export declare function isWin(patch: StructuredPatch | StructuredPatch[]): boolean;
17
+ //# sourceMappingURL=line-endings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"line-endings.d.ts","sourceRoot":"","sources":["../../src/patch/line-endings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,CAAC;AACnE,wBAAgB,SAAS,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,eAAe,EAAE,CAAC;AACzE,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,eAAe,GAAG,eAAe,EAAE,CAAC;AA0B3G,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,CAAC;AACnE,wBAAgB,SAAS,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,eAAe,EAAE,CAAC;AACzE,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,eAAe,GAAG,eAAe,EAAE,CAAC;AAgB3G;;;GAGG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,OAAO,CAS1E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,EAAE,GAAG,OAAO,CAUzE"}