johankit 0.1.0 → 0.1.2

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "johankit",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -1,6 +1,8 @@
1
1
  // src/core/clipboard.ts
2
2
  import { spawn } from "child_process";
3
3
 
4
+ let memoryClipboard = "";
5
+
4
6
  export function copyToClipboard(text: string): Promise<void> {
5
7
  return new Promise((resolve, reject) => {
6
8
  let command = "xclip";
@@ -18,19 +20,75 @@ export function copyToClipboard(text: string): Promise<void> {
18
20
  stdio: ["pipe", "ignore", "ignore"]
19
21
  });
20
22
 
21
- child.on("error", reject);
23
+ let resolved = false;
24
+
25
+ child.on("error", (err) => {
26
+ memoryClipboard = text;
27
+ resolved = true;
28
+ resolve();
29
+ });
22
30
 
23
31
  child.stdin.on("error", (err: any) => {
24
32
  if (err.code === "EPIPE") {
33
+ resolved = true;
25
34
  resolve();
26
35
  } else {
27
- reject(err);
36
+ memoryClipboard = text;
37
+ if (!resolved) resolve();
28
38
  }
29
39
  });
30
40
 
31
41
  child.stdin.write(text);
32
42
  child.stdin.end();
33
43
 
34
- child.on("close", () => resolve());
44
+ child.on("close", () => {
45
+ if (!resolved) resolve();
46
+ });
47
+ });
48
+ }
49
+
50
+ export function readClipboard(): Promise<string> {
51
+ return new Promise((resolve, reject) => {
52
+ let command: string;
53
+ let args: string[] = [];
54
+
55
+ if (process.platform === "darwin") {
56
+ command = "pbpaste";
57
+ } else if (process.platform === "win32") {
58
+ command = "powershell";
59
+ args = ["-Command", "Get-Clipboard"];
60
+ } else {
61
+ command = "xclip";
62
+ args = ["-selection", "clipboard", "-o"];
63
+ }
64
+
65
+ const child = spawn(command, args, {
66
+ stdio: ["ignore", "pipe", "pipe"]
67
+ });
68
+
69
+ let data = "";
70
+ let error = "";
71
+ let fallback = false;
72
+
73
+ child.stdout.on("data", chunk => {
74
+ data += chunk.toString();
75
+ });
76
+
77
+ child.stderr.on("data", chunk => {
78
+ error += chunk.toString();
79
+ });
80
+
81
+ child.on("error", () => {
82
+ fallback = true;
83
+ resolve(memoryClipboard);
84
+ });
85
+
86
+ child.on("close", code => {
87
+ if (code !== 0 || fallback) {
88
+ resolve(memoryClipboard);
89
+ } else {
90
+ resolve(data);
91
+ }
92
+ });
35
93
  });
36
94
  }