johankit 0.1.0 → 0.1.3

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.
@@ -31,6 +31,7 @@ async function tree(dir) {
31
31
  for (const file of project.getSourceFiles()) {
32
32
  const absolutePath = file.getFilePath();
33
33
  const filePath = path_1.default.relative(dir, absolutePath);
34
+ console.log(filePath);
34
35
  if (filePath.includes("node_modules") || filePath.includes("/dist/")) {
35
36
  continue;
36
37
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.copyToClipboard = void 0;
3
+ exports.readClipboard = exports.copyToClipboard = void 0;
4
4
  // src/core/clipboard.ts
5
5
  const child_process_1 = require("child_process");
6
+ let memoryClipboard = "";
6
7
  function copyToClipboard(text) {
7
8
  return new Promise((resolve, reject) => {
8
9
  let command = "xclip";
@@ -18,18 +19,71 @@ function copyToClipboard(text) {
18
19
  const child = (0, child_process_1.spawn)(command, args, {
19
20
  stdio: ["pipe", "ignore", "ignore"]
20
21
  });
21
- child.on("error", reject);
22
+ let resolved = false;
23
+ child.on("error", (err) => {
24
+ memoryClipboard = text;
25
+ resolved = true;
26
+ resolve();
27
+ });
22
28
  child.stdin.on("error", (err) => {
23
29
  if (err.code === "EPIPE") {
30
+ resolved = true;
24
31
  resolve();
25
32
  }
26
33
  else {
27
- reject(err);
34
+ memoryClipboard = text;
35
+ if (!resolved)
36
+ resolve();
28
37
  }
29
38
  });
30
39
  child.stdin.write(text);
31
40
  child.stdin.end();
32
- child.on("close", () => resolve());
41
+ child.on("close", () => {
42
+ if (!resolved)
43
+ resolve();
44
+ });
33
45
  });
34
46
  }
35
47
  exports.copyToClipboard = copyToClipboard;
48
+ function readClipboard() {
49
+ return new Promise((resolve, reject) => {
50
+ let command;
51
+ let args = [];
52
+ if (process.platform === "darwin") {
53
+ command = "pbpaste";
54
+ }
55
+ else if (process.platform === "win32") {
56
+ command = "powershell";
57
+ args = ["-Command", "Get-Clipboard"];
58
+ }
59
+ else {
60
+ command = "xclip";
61
+ args = ["-selection", "clipboard", "-o"];
62
+ }
63
+ const child = (0, child_process_1.spawn)(command, args, {
64
+ stdio: ["ignore", "pipe", "pipe"]
65
+ });
66
+ let data = "";
67
+ let error = "";
68
+ let fallback = false;
69
+ child.stdout.on("data", chunk => {
70
+ data += chunk.toString();
71
+ });
72
+ child.stderr.on("data", chunk => {
73
+ error += chunk.toString();
74
+ });
75
+ child.on("error", () => {
76
+ fallback = true;
77
+ resolve(memoryClipboard);
78
+ });
79
+ child.on("close", code => {
80
+ if (code !== 0 || fallback) {
81
+ resolve(memoryClipboard);
82
+ }
83
+ else {
84
+ resolve(data);
85
+ }
86
+ });
87
+ });
88
+ }
89
+ exports.readClipboard = readClipboard;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "johankit",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -2,22 +2,17 @@ import { scanDir } from "../../core/scan";
2
2
  import { copyToClipboard } from "../../core/clipboard";
3
3
 
4
4
  export async function copy(input: string | string[]) {
5
- let snapshot;
5
+ let snapshot: any[] = [];
6
6
 
7
- if (Array.isArray(input)) {
8
- snapshot = input.map(path => {
9
- const fileSnapshot = scanDir(path);
10
- if (fileSnapshot.length !== 1) {
11
- throw new Error(`Expected single file for path: ${path}`);
12
- }
13
- return fileSnapshot[0];
14
- });
15
- } else {
16
- snapshot = scanDir(input);
7
+ const paths = Array.isArray(input) ? input : [input];
8
+
9
+ for (const p of paths) {
10
+ const scanned = scanDir(p);
11
+ snapshot.push(...scanned);
17
12
  }
18
13
 
19
14
  const clipboardJSON = JSON.stringify(snapshot, null, 2);
20
-
15
+
21
16
  try {
22
17
  await copyToClipboard(clipboardJSON);
23
18
  } catch (err) {
@@ -25,4 +20,4 @@ export async function copy(input: string | string[]) {
25
20
  }
26
21
 
27
22
  return clipboardJSON;
28
- }
23
+ }
@@ -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
  }