markdown-patch 0.1.0 → 0.1.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.
package/package.json CHANGED
@@ -15,13 +15,13 @@
15
15
  "typescript": "^5.5.4"
16
16
  },
17
17
  "name": "markdown-patch",
18
- "version": "0.1.0",
19
- "main": "index.js",
18
+ "version": "0.1.1",
19
+ "main": "./dist/index.js",
20
20
  "scripts": {
21
21
  "test": "node node_modules/.bin/jest"
22
22
  },
23
23
  "bin": {
24
- "mdpatch": "./dist/index.js"
24
+ "mdpatch": "./dist/cli.js"
25
25
  },
26
26
  "keywords": [],
27
27
  "author": "",
package/src/cli.ts ADDED
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from "commander";
3
+ import fs from "fs/promises";
4
+ import { getDocumentMap } from "./map.js";
5
+ import { printMap } from "./debug.js";
6
+ import { PatchInstruction } from "./types.js";
7
+ import { applyPatch } from "./patch.js";
8
+ import packageJson from "../package.json" assert { type: "json" };
9
+
10
+ async function readStdin(): Promise<string> {
11
+ return new Promise((resolve, reject) => {
12
+ let data = "";
13
+ process.stdin.on("data", (chunk) => (data += chunk));
14
+ process.stdin.on("end", () => resolve(data));
15
+ process.stdin.on("error", reject);
16
+ });
17
+ }
18
+
19
+ const program = new Command();
20
+
21
+ // Configure the CLI
22
+ program
23
+ .name(Object.keys(packageJson.bin)[0])
24
+ .description(packageJson.description)
25
+ .version(packageJson.version);
26
+
27
+ program
28
+ .command("print-map")
29
+ .argument("<path>", "filepath to show identified patchable paths for")
30
+ .argument(
31
+ "[regex]",
32
+ "limit displayed matches to those matching the supplied regular expression"
33
+ )
34
+ .action(async (path: string, regex: string | undefined) => {
35
+ const document = await fs.readFile(path, "utf-8");
36
+ const documentMap = getDocumentMap(document);
37
+
38
+ printMap(document, documentMap, regex ? new RegExp(regex) : undefined);
39
+ });
40
+
41
+ program
42
+ .command("apply")
43
+ .argument("<path>", "file to patch")
44
+ .argument("<patch>", "patch file to apply")
45
+ .option(
46
+ "-o, --output <output>",
47
+ "write output to the specified path instead of applying in-place; use '-' for stdout"
48
+ )
49
+ .action(async (path: string, patch: string, options) => {
50
+ let patchParsed: PatchInstruction[];
51
+ let patchData: string;
52
+ try {
53
+ if (patch === "-") {
54
+ patchData = await readStdin();
55
+ } else {
56
+ patchData = await fs.readFile(patch, "utf-8");
57
+ }
58
+ } catch (e) {
59
+ console.error("Failed to read patch: ", e);
60
+ process.exit(1);
61
+ }
62
+
63
+ try {
64
+ const parsedData = JSON.parse(patchData);
65
+ if (!Array.isArray(parsedData)) {
66
+ patchParsed = [parsedData];
67
+ } else {
68
+ patchParsed = parsedData;
69
+ }
70
+ } catch (e) {
71
+ console.error("Could not parse patch file as JSON");
72
+ process.exit(1);
73
+ }
74
+
75
+ let document = await fs.readFile(path, "utf-8");
76
+ console.log("Document", document);
77
+ for (const instruction of patchParsed) {
78
+ document = applyPatch(document, instruction);
79
+ }
80
+
81
+ if (options.output === "-") {
82
+ process.stdout.write(document);
83
+ } else {
84
+ await fs.writeFile(options.output ? options.output : path, document);
85
+ }
86
+ });
87
+
88
+ program.parse(process.argv);
package/src/index.ts CHANGED
@@ -1,88 +1,9 @@
1
- #!/usr/bin/env node
2
- import { Command } from "commander";
3
- import fs from "fs/promises";
4
- import { getDocumentMap } from "./map.js";
5
- import { printMap } from "./debug.js";
6
- import { PatchInstruction } from "./types.js";
7
- import { applyPatch } from "./patch.js";
8
- import packageJson from "../package.json" assert { type: "json" };
9
-
10
- async function readStdin(): Promise<string> {
11
- return new Promise((resolve, reject) => {
12
- let data = "";
13
- process.stdin.on("data", (chunk) => (data += chunk));
14
- process.stdin.on("end", () => resolve(data));
15
- process.stdin.on("error", reject);
16
- });
17
- }
18
-
19
- const program = new Command();
20
-
21
- // Configure the CLI
22
- program
23
- .name(Object.keys(packageJson.bin)[0])
24
- .description(packageJson.description)
25
- .version(packageJson.version);
26
-
27
- program
28
- .command("print-map")
29
- .argument("<path>", "filepath to show identified patchable paths for")
30
- .argument(
31
- "[regex]",
32
- "limit displayed matches to those matching the supplied regular expression"
33
- )
34
- .action(async (path: string, regex: string | undefined) => {
35
- const document = await fs.readFile(path, "utf-8");
36
- const documentMap = getDocumentMap(document);
37
-
38
- printMap(document, documentMap, regex ? new RegExp(regex) : undefined);
39
- });
40
-
41
- program
42
- .command("apply")
43
- .argument("<path>", "file to patch")
44
- .argument("<patch>", "patch file to apply")
45
- .option(
46
- "-o, --output <output>",
47
- "write output to the specified path instead of applying in-place; use '-' for stdout"
48
- )
49
- .action(async (path: string, patch: string, options) => {
50
- let patchParsed: PatchInstruction[];
51
- let patchData: string;
52
- try {
53
- if (patch === "-") {
54
- patchData = await readStdin();
55
- } else {
56
- patchData = await fs.readFile(patch, "utf-8");
57
- }
58
- } catch (e) {
59
- console.error("Failed to read patch: ", e);
60
- process.exit(1);
61
- }
62
-
63
- try {
64
- const parsedData = JSON.parse(patchData);
65
- if (!Array.isArray(parsedData)) {
66
- patchParsed = [parsedData];
67
- } else {
68
- patchParsed = parsedData;
69
- }
70
- } catch (e) {
71
- console.error("Could not parse patch file as JSON");
72
- process.exit(1);
73
- }
74
-
75
- let document = await fs.readFile(path, "utf-8");
76
- console.log("Document", document);
77
- for (const instruction of patchParsed) {
78
- document = applyPatch(document, instruction);
79
- }
80
-
81
- if (options.output === "-") {
82
- process.stdout.write(document);
83
- } else {
84
- await fs.writeFile(options.output ? options.output : path, document);
85
- }
86
- });
87
-
88
- program.parse(process.argv);
1
+ import {
2
+ PatchFailureReason,
3
+ PatchFailed,
4
+ PatchError,
5
+ TablePartsNotFound,
6
+ applyPatch,
7
+ } from "./patch.js";
8
+
9
+ export * from "./types.js";
package/src/patch.ts CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  ReplaceTableRowsBlockPatchInstruction,
11
11
  } from "./types.js";
12
12
 
13
- enum PatchFailureReason {
13
+ export enum PatchFailureReason {
14
14
  InvalidTarget = "invalid-target",
15
15
  ContentAlreadyPreexistsInTarget = "content-already-preexists-in-target",
16
16
  TableContentIncorrectColumnCount = "table-content-incorrect-column-count",
package/dist/index.js DELETED
@@ -1,75 +0,0 @@
1
- #!/usr/bin/env node
2
- import { Command } from "commander";
3
- import fs from "fs/promises";
4
- import { getDocumentMap } from "./map.js";
5
- import { printMap } from "./debug.js";
6
- import { applyPatch } from "./patch.js";
7
- async function readStdin() {
8
- return new Promise((resolve, reject) => {
9
- let data = "";
10
- process.stdin.on("data", (chunk) => (data += chunk));
11
- process.stdin.on("end", () => resolve(data));
12
- process.stdin.on("error", reject);
13
- });
14
- }
15
- const program = new Command();
16
- // Configure the CLI
17
- program
18
- .name("mdpatch")
19
- .description("Change markdown documents by inserting or changing content relative to headings or other parst of a document's structure.")
20
- .version("0.1.0");
21
- program
22
- .command("print-map")
23
- .argument("<path>", "filepath to show identified patchable paths for")
24
- .argument("[regex]", "limit displayed matches to those matching the supplied regular expression")
25
- .action(async (path, regex) => {
26
- const document = await fs.readFile(path, "utf-8");
27
- const documentMap = getDocumentMap(document);
28
- printMap(document, documentMap, regex ? new RegExp(regex) : undefined);
29
- });
30
- program
31
- .command("apply")
32
- .argument("<path>", "file to patch")
33
- .argument("<patch>", "patch file to apply")
34
- .option("-o, --output <output>", "write output to the specified path instead of applying in-place; use '-' for stdout")
35
- .action(async (path, patch, options) => {
36
- let patchParsed;
37
- let patchData;
38
- try {
39
- if (patch === "-") {
40
- patchData = await readStdin();
41
- }
42
- else {
43
- patchData = await fs.readFile(patch, "utf-8");
44
- }
45
- }
46
- catch (e) {
47
- console.error("Failed to read patch: ", e);
48
- process.exit(1);
49
- }
50
- try {
51
- const parsedData = JSON.parse(patchData);
52
- if (!Array.isArray(parsedData)) {
53
- patchParsed = [parsedData];
54
- }
55
- else {
56
- patchParsed = parsedData;
57
- }
58
- }
59
- catch (e) {
60
- console.error("Could not parse patch file as JSON");
61
- process.exit(1);
62
- }
63
- let document = await fs.readFile(path, "utf-8");
64
- console.log("Document", document);
65
- for (const instruction of patchParsed) {
66
- document = applyPatch(document, instruction);
67
- }
68
- if (options.output === "-") {
69
- process.stdout.write(document);
70
- }
71
- else {
72
- await fs.writeFile(options.output ? options.output : path, document);
73
- }
74
- });
75
- program.parse(process.argv);