@lvce-editor/shared-process 0.0.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -0
  3. package/bin/sharedProcess.js +3 -0
  4. package/index.js +6 -0
  5. package/package.json +96 -0
  6. package/src/parts/Assert/Assert.js +57 -0
  7. package/src/parts/Callback/Callback.js +48 -0
  8. package/src/parts/ChildProcess/ChildProcess.js +98 -0
  9. package/src/parts/ClipBoard/ClipBoard.ipc.js +7 -0
  10. package/src/parts/ClipBoard/ClipBoard.js +23 -0
  11. package/src/parts/ClipBoard/ClipBoardGnome.js +78 -0
  12. package/src/parts/ClipBoard/ClipBoardNoop.js +14 -0
  13. package/src/parts/ClipBoard/ClipBoardWindows.js +22 -0
  14. package/src/parts/Command/Command.js +264 -0
  15. package/src/parts/Configuration/Configuration.ipc.js +7 -0
  16. package/src/parts/Configuration/Configuration.js +39 -0
  17. package/src/parts/Credentials/Credentials.ipc.js +10 -0
  18. package/src/parts/Credentials/Credentials.js +69 -0
  19. package/src/parts/Debug/Debug.js +3 -0
  20. package/src/parts/Developer/Developer.ipc.js +20 -0
  21. package/src/parts/Developer/Developer.js +132 -0
  22. package/src/parts/Download/Download.js +33 -0
  23. package/src/parts/Electron/Electron.ipc.js +19 -0
  24. package/src/parts/Electron/Electron.js +99 -0
  25. package/src/parts/Env/Env.js +3 -0
  26. package/src/parts/Error/Error.js +30 -0
  27. package/src/parts/ErrorHandling/ErrorHandling.js +40 -0
  28. package/src/parts/Exec/Exec.js +16 -0
  29. package/src/parts/Extension/Extension.js +7 -0
  30. package/src/parts/ExtensionHost/ExtensionHost.ipc.js +80 -0
  31. package/src/parts/ExtensionHost/ExtensionHost.js +163 -0
  32. package/src/parts/ExtensionHost/ExtensionHostBraceCompletion.js +14 -0
  33. package/src/parts/ExtensionHost/ExtensionHostClosingTag.js +14 -0
  34. package/src/parts/ExtensionHost/ExtensionHostColorTheme.js +60 -0
  35. package/src/parts/ExtensionHost/ExtensionHostCommand.js +19 -0
  36. package/src/parts/ExtensionHost/ExtensionHostCompletion.js +12 -0
  37. package/src/parts/ExtensionHost/ExtensionHostDefinition.js +12 -0
  38. package/src/parts/ExtensionHost/ExtensionHostDiagnostic.js +7 -0
  39. package/src/parts/ExtensionHost/ExtensionHostFileSystem.js +23 -0
  40. package/src/parts/ExtensionHost/ExtensionHostFormatting.js +4 -0
  41. package/src/parts/ExtensionHost/ExtensionHostHover.js +12 -0
  42. package/src/parts/ExtensionHost/ExtensionHostIconTheme.js +35 -0
  43. package/src/parts/ExtensionHost/ExtensionHostImplementation.js +12 -0
  44. package/src/parts/ExtensionHost/ExtensionHostKeyBindings.js +23 -0
  45. package/src/parts/ExtensionHost/ExtensionHostLanguages.js +84 -0
  46. package/src/parts/ExtensionHost/ExtensionHostManagement.js +26 -0
  47. package/src/parts/ExtensionHost/ExtensionHostOutput.js +3 -0
  48. package/src/parts/ExtensionHost/ExtensionHostQuickPick.js +6 -0
  49. package/src/parts/ExtensionHost/ExtensionHostReference.js +12 -0
  50. package/src/parts/ExtensionHost/ExtensionHostRename.js +27 -0
  51. package/src/parts/ExtensionHost/ExtensionHostSemanticTokens.js +10 -0
  52. package/src/parts/ExtensionHost/ExtensionHostSourceControl.js +15 -0
  53. package/src/parts/ExtensionHost/ExtensionHostStats.js +3 -0
  54. package/src/parts/ExtensionHost/ExtensionHostStatusBar.js +7 -0
  55. package/src/parts/ExtensionHost/ExtensionHostTabCompletion.js +12 -0
  56. package/src/parts/ExtensionHost/ExtensionHostTextDocument.js +35 -0
  57. package/src/parts/ExtensionHost/ExtensionHostTypeDefinition.js +12 -0
  58. package/src/parts/ExtensionHost/ExtensionHostWorkspace.js +3 -0
  59. package/src/parts/ExtensionManagement/ExtensionManagement.ipc.js +11 -0
  60. package/src/parts/ExtensionManagement/ExtensionManagement.js +319 -0
  61. package/src/parts/FileSystem/FileSystem.ipc.js +32 -0
  62. package/src/parts/FileSystem/FileSystem.js +228 -0
  63. package/src/parts/Git/Git.js +58 -0
  64. package/src/parts/Json/Json.js +67 -0
  65. package/src/parts/JsonFile/JsonFile.js +14 -0
  66. package/src/parts/JsonRpc/JsonRpc.js +22 -0
  67. package/src/parts/Native/Native.ipc.js +6 -0
  68. package/src/parts/Native/Native.js +14 -0
  69. package/src/parts/OutputChannel/OutputChannel.ipc.js +51 -0
  70. package/src/parts/OutputChannel/OutputChannel.js +37 -0
  71. package/src/parts/ParentIpc/ParentIpc.js +203 -0
  72. package/src/parts/Path/Path.js +17 -0
  73. package/src/parts/Platform/Platform.ipc.js +18 -0
  74. package/src/parts/Platform/Platform.js +165 -0
  75. package/src/parts/Preferences/Preferences.ipc.js +6 -0
  76. package/src/parts/Preferences/Preferences.js +75 -0
  77. package/src/parts/PrettyError/PrettyError.js +86 -0
  78. package/src/parts/Process/Process.ipc.js +7 -0
  79. package/src/parts/Process/Process.js +7 -0
  80. package/src/parts/Queue/Queue.js +35 -0
  81. package/src/parts/RequiresSocket/RequiresSocket.js +11 -0
  82. package/src/parts/RgPath/RgPath.js +1 -0
  83. package/src/parts/Root/Root.js +6 -0
  84. package/src/parts/Search/Search.ipc.js +6 -0
  85. package/src/parts/Search/Search.js +101 -0
  86. package/src/parts/SearchFile/SearchFile.ipc.js +6 -0
  87. package/src/parts/SearchFile/SearchFile.js +51 -0
  88. package/src/parts/Socket/Socket.js +132 -0
  89. package/src/parts/Stats/Stats.js +45 -0
  90. package/src/parts/Terminal/Terminal.ipc.js +45 -0
  91. package/src/parts/Terminal/Terminal.js +161 -0
  92. package/src/parts/TextDocument/TextDocument.ipc.js +6 -0
  93. package/src/parts/TextDocument/TextDocument.js +37 -0
  94. package/src/parts/Trace/Trace.js +15 -0
  95. package/src/parts/Trash/Trash.js +11 -0
  96. package/src/parts/WebSocketServer/WebSocketServer.ipc.js +6 -0
  97. package/src/parts/WebSocketServer/WebSocketServer.js +43 -0
  98. package/src/parts/Workspace/Workspace.ipc.js +7 -0
  99. package/src/parts/Workspace/Workspace.js +107 -0
  100. package/src/sharedProcessMain.js +61 -0
  101. package/test/Callback.test.js +42 -0
  102. package/test/ClipBoard.test.js +105 -0
  103. package/test/Credentials.test.js +147 -0
  104. package/test/Developer.test.js +34 -0
  105. package/test/Electron.test.js +93 -0
  106. package/test/Error.test.js +16 -0
  107. package/test/Exec.test.js +25 -0
  108. package/test/ExtensionHost.test.js +127 -0
  109. package/test/ExtensionHostColorTheme.test.js +141 -0
  110. package/test/ExtensionHostCommand.test.js +27 -0
  111. package/test/ExtensionHostCompletion.test.js +45 -0
  112. package/test/ExtensionHostDefinition.test.js +35 -0
  113. package/test/ExtensionHostDiagnostic.test.js +23 -0
  114. package/test/ExtensionHostFileSystem.test.js +91 -0
  115. package/test/ExtensionHostFormatting.test.js +27 -0
  116. package/test/ExtensionHostIconTheme.test.js +73 -0
  117. package/test/ExtensionHostLanguages.test.js +212 -0
  118. package/test/ExtensionHostOutput.test.js +23 -0
  119. package/test/ExtensionHostRename.test.js +59 -0
  120. package/test/ExtensionHostSemanticTokens.test.js +26 -0
  121. package/test/ExtensionHostTabCompletion.test.js +35 -0
  122. package/test/ExtensionHostTextDocument.test.js +61 -0
  123. package/test/ExtensionHostTypeDefinition.test.js +35 -0
  124. package/test/ExtensionHostWorkspace.test.js +21 -0
  125. package/test/ExtensionManagement.test.js +408 -0
  126. package/test/FileSystem.test.js +294 -0
  127. package/test/Json.test.js +25 -0
  128. package/test/Native.test.js +31 -0
  129. package/test/OutputChannel.test.js +68 -0
  130. package/test/Path.test.js +17 -0
  131. package/test/Platform.test.js +61 -0
  132. package/test/Preferences.test.js +140 -0
  133. package/test/Process.test.js +9 -0
  134. package/test/RgPath.test.js +5 -0
  135. package/test/Search.test.js +45 -0
  136. package/test/SearchFile.test.js +80 -0
  137. package/test/Terminal.test.js +53 -0
  138. package/test/Trash.test.js +34 -0
  139. package/test/WebSocketServer.test.js +98 -0
  140. package/test/fixture-search-1/index.html +10 -0
  141. package/test/fixture-search-file-1/fileA +0 -0
  142. package/test/fixture-search-file-1/fileB +0 -0
  143. package/test/fixture-search-file-1/nested/fileC +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present, Le Vivilet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # shared process
2
+
3
+ The `shared-process` is a NodeJS process that is used for sending files to the Browser via WebSockets.
4
+
5
+ - `shared-process` is created by the `web` process
6
+ - `shared-process` launches the `extension-host`
7
+ - `shared-process` communicates with `renderer-worker` via WebSockets
8
+
9
+ <!--
10
+ For example, this is how readFile is implemented in renderer-worker:
11
+
12
+ ```js
13
+ export const readFile = (path) => {
14
+ return SharedProcess.invoke(/* readFile */ 101, /* path */ path)
15
+ }
16
+ ```
17
+
18
+ This will be send to the shared-process
19
+
20
+ ```json
21
+ {
22
+ "jsonrpc": "2.0",
23
+ "method": 101,
24
+ "params": ["/tmp/index.css"],
25
+ "id": 19
26
+ }
27
+ ```
28
+
29
+ And the shared-process will send back
30
+
31
+ ````json
32
+ {
33
+ "jsonrpc": "2.0",
34
+ "result": "h1 {\n font-size: 20px;\n}\n",
35
+ "id": 19
36
+ }
37
+ ``` -->
38
+
39
+ ```
40
+
41
+ ```
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ import '../src/sharedProcessMain.js'
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import { dirname, join } from 'path'
2
+ import { fileURLToPath } from 'url'
3
+
4
+ const __dirname = dirname(fileURLToPath(import.meta.url))
5
+
6
+ export const sharedProcessPath = join(__dirname, 'src', 'sharedProcessMain.js')
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@lvce-editor/shared-process",
3
+ "version": "0.0.2",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node --unhandled-rejections=warn --experimental-vm-modules ../../node_modules/jest/bin/jest.js --detectOpenHandles --forceExit",
9
+ "test:watch": "node --unhandled-rejections=warn --experimental-vm-modules ../../node_modules/jest/bin/jest.js --watch"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "MIT",
14
+ "dependencies": {
15
+ "chokidar": "^3.5.3",
16
+ "clipboardy": "^3.0.0",
17
+ "debug": "^4.3.4",
18
+ "execa": "^6.1.0",
19
+ "exit-hook": "^3.0.0",
20
+ "fs-extra": "^10.1.0",
21
+ "got": "^12.1.0",
22
+ "is-object": "^1.0.2",
23
+ "json-parse-even-better-errors": "^2.3.1",
24
+ "jsonc-parser": "^3.0.0",
25
+ "lines-and-columns": "^2.0.3",
26
+ "open": "^8.4.0",
27
+ "p-queue": "^7.2.0",
28
+ "parse-json": "^6.0.2",
29
+ "tail": "^2.2.4",
30
+ "tar-fs": "^2.1.1",
31
+ "trash": "^8.1.0",
32
+ "verror": "^1.10.1",
33
+ "ws": "^8.8.0",
34
+ "xdg-basedir": "^5.1.0",
35
+ "@lvce-editor/pty-host": "0.0.2",
36
+ "@lvce-editor/extension-host": "0.0.2"
37
+ },
38
+ "optionalDependencies": {
39
+ "electron-clipboard-ex": "^1.3.3",
40
+ "keytar": "^7.9.0",
41
+ "vscode-ripgrep-with-github-api-error-fix": "^2.4.1"
42
+ },
43
+ "devDependencies": {
44
+ "@types/debug": "^4.1.7",
45
+ "@types/fs-extra": "^9.0.13",
46
+ "@types/is-ci": "^3.0.0",
47
+ "@types/is-object": "^1.0.2",
48
+ "@types/tail": "^2.2.1",
49
+ "@types/tar-fs": "^2.0.1",
50
+ "@types/verror": "^1.10.5",
51
+ "@types/ws": "^8.5.3",
52
+ "get-port": "^6.1.2",
53
+ "is-ci": "^3.0.1"
54
+ },
55
+ "xo": {
56
+ "rules": {
57
+ "unicorn/filename-case": "off",
58
+ "indent": "off",
59
+ "semi": "off",
60
+ "no-unused-vars": "off",
61
+ "unicorn/numeric-separators-style": "off",
62
+ "no-extra-semi": "off",
63
+ "arrow-body-style": "off",
64
+ "padded-blocks": "off",
65
+ "capitalized-comments": "off",
66
+ "padding-line-between-statements": "off",
67
+ "arrow-parens": "off",
68
+ "no-warning-comments": "off",
69
+ "array-bracket-spacing": "off",
70
+ "comma-spacing": "off",
71
+ "unicorn/no-array-callback-reference": "off",
72
+ "comma-dangle": "off",
73
+ "operator-linebreak": "off",
74
+ "no-case-declarations": "off",
75
+ "no-undef": "off",
76
+ "object-curly-spacing": "off",
77
+ "object-shorthand": "off",
78
+ "complexity": "off",
79
+ "no-labels": "off",
80
+ "no-multi-assign": "off",
81
+ "max-params": "off",
82
+ "no-bitwise": "off",
83
+ "unicorn/prefer-math-trunc": "off",
84
+ "no-await-in-loop": "off",
85
+ "unicorn/prefer-add-event-listener": "off",
86
+ "no-unused-expressions": "off",
87
+ "node/prefer-global/process": "off",
88
+ "unicorn/prevent-abbreviations": "off",
89
+ "unicorn/no-process-exit": "off",
90
+ "quotes": "off"
91
+ },
92
+ "ignores": [
93
+ "distmin"
94
+ ]
95
+ }
96
+ }
@@ -0,0 +1,57 @@
1
+ const getType = (value) => {
2
+ switch (typeof value) {
3
+ case 'number':
4
+ return 'number'
5
+ case 'function':
6
+ return 'function'
7
+ case 'string':
8
+ return 'string'
9
+ case 'object':
10
+ if (value === null) {
11
+ return 'null'
12
+ }
13
+ if (Array.isArray(value)) {
14
+ return 'array'
15
+ }
16
+ return 'object'
17
+ case 'boolean':
18
+ return 'boolean'
19
+ default:
20
+ return 'unknown'
21
+ }
22
+ }
23
+
24
+ export const object = (value) => {
25
+ const type = getType(value)
26
+ if (type !== 'object') {
27
+ throw new Error('expected value to be of type object')
28
+ }
29
+ }
30
+
31
+ export const number = (value) => {
32
+ const type = getType(value)
33
+ if (type !== 'number') {
34
+ throw new Error('expected value to be of type number')
35
+ }
36
+ }
37
+
38
+ export const array = (value) => {
39
+ const type = getType(value)
40
+ if (type !== 'array') {
41
+ throw new Error('expected value to be of type array')
42
+ }
43
+ }
44
+
45
+ export const string = (value) => {
46
+ const type = getType(value)
47
+ if (type !== 'string') {
48
+ throw new Error('expected value to be of type string')
49
+ }
50
+ }
51
+
52
+ export const boolean = (value) => {
53
+ const type = getType(value)
54
+ if (type !== 'boolean') {
55
+ throw new Error('expected value to be of type boolean')
56
+ }
57
+ }
@@ -0,0 +1,48 @@
1
+ export const state = {
2
+ callbacks: Object.create(null),
3
+ onceListeners: new Set(),
4
+ id: 2,
5
+ }
6
+
7
+ export const register = (resolve, reject) => {
8
+ state.callbacks[++state.id] = {
9
+ resolve,
10
+ reject,
11
+ }
12
+ return state.id
13
+ }
14
+
15
+ export const unregister = (id) => {
16
+ delete state.callbacks[id]
17
+ }
18
+
19
+ // TODO merge resolve and resolveEmpty
20
+ export const resolve = (id, args) => {
21
+ if (!(id in state.callbacks)) {
22
+ console.warn(`callback ${id} may already be disposed`)
23
+ return
24
+ }
25
+ state.callbacks[id].resolve(args)
26
+ }
27
+
28
+ export const resolveEmpty = (id) => {
29
+ if (!(id in state.callbacks)) {
30
+ console.warn(`callback (resolved) ${id} may already be disposed`)
31
+ return
32
+ }
33
+ state.callbacks[id].resolve()
34
+ }
35
+
36
+ export const reject = (id, error) => {
37
+ if (!(id in state.callbacks)) {
38
+ console.warn(`callback (rejected) ${id} may already be disposed`)
39
+ return
40
+ }
41
+ state.callbacks[id].reject(error)
42
+ }
43
+
44
+ export const isAllEmpty = () => {
45
+ return (
46
+ Object.keys(state.callbacks).length === 0 && state.onceListeners.size === 0
47
+ )
48
+ }
@@ -0,0 +1,98 @@
1
+ import * as NodeChildProcess from 'node:child_process'
2
+ import { Worker } from 'node:worker_threads'
3
+ import exitHook from 'exit-hook'
4
+ import * as Debug from '../Debug/Debug.js'
5
+
6
+ export const state = {
7
+ /** @type{ChildProcess[]} */
8
+ childProcesses: [],
9
+ }
10
+
11
+ const cleanUpAll = () => {
12
+ for (const childProcess of state.childProcesses) {
13
+ childProcess.kill()
14
+ }
15
+ }
16
+
17
+ const handleProcessExit = () => {
18
+ Debug.debug('exiting')
19
+ console.info('[shared process] exiting')
20
+ cleanUpAll()
21
+ }
22
+
23
+ /**
24
+ *
25
+ * @param {string} path
26
+ * @param {NodeChildProcess.ForkOptions } options
27
+ * @returns
28
+ */
29
+ export const fork = (path, options) => {
30
+ exitHook(handleProcessExit)
31
+ const childProcess = NodeChildProcess.fork(path, options)
32
+
33
+ // childProcess.on('error', (error) => {
34
+ // onError(error)
35
+ // })
36
+
37
+ // childProcess.on('exit', (exitCode) => {
38
+ // onExit(exitCode)
39
+ // })
40
+
41
+ // childProcess.once('message', (message) => {
42
+ // if (message === 'ready') {
43
+ // onReady()
44
+ // childProcess.on('message', onMessage)
45
+ // // TODO remove jsonrpc part from here
46
+ // // childProcess.on('message', (message) => {
47
+ // // // @ts-ignore
48
+ // // if ('result' in message) {
49
+ // // // @ts-ignore
50
+ // // callbacks[message.id](message.result)
51
+ // // // @ts-ignore
52
+ // // delete callbacks[message.id]
53
+ // // } else {
54
+ // // onMessage(message)
55
+ // // }
56
+ // // })
57
+ // }
58
+ // })
59
+
60
+ state.childProcesses.push(childProcess)
61
+
62
+ return childProcess
63
+ }
64
+
65
+ export const disposeFork = (childProcess) => {
66
+ childProcess.kill()
67
+ }
68
+
69
+ export const createWorker = (path, { onError, onExit, onMessage }) => {
70
+ const worker = new Worker(path, {
71
+ resourceLimits: {
72
+ maxOldGenerationSizeMb: 10,
73
+ maxYoungGenerationSizeMb: 10,
74
+ },
75
+ })
76
+
77
+ worker.on('error', (error) => {
78
+ onError(error)
79
+ })
80
+
81
+ worker.on('exit', (exitCode) => {
82
+ onExit(exitCode)
83
+ })
84
+
85
+ worker.on('message', (message) => {
86
+ onMessage(message)
87
+ })
88
+
89
+ return {
90
+ dispose() {
91
+ worker.terminate()
92
+ },
93
+ send(message) {
94
+ worker.postMessage(message)
95
+ },
96
+ async invoke(commandId, ...args) {},
97
+ }
98
+ }
@@ -0,0 +1,7 @@
1
+ import * as Command from '../Command/Command.js'
2
+ import * as ClipBoard from './ClipBoard.js'
3
+
4
+ export const __initialize__ = () => {
5
+ Command.register('ClipBoard.readFiles', ClipBoard.readFiles)
6
+ Command.register('ClipBoard.writeFiles', ClipBoard.writeFiles)
7
+ }
@@ -0,0 +1,23 @@
1
+ import * as Platform from '../Platform/Platform.js'
2
+
3
+ const getClipboard = () => {
4
+ const desktop = Platform.getDesktop()
5
+ switch (desktop) {
6
+ case 'gnome':
7
+ return import('./ClipBoardGnome.js')
8
+ case 'windows':
9
+ return import('./ClipBoardWindows.js')
10
+ default:
11
+ return import('./ClipBoardNoop.js')
12
+ }
13
+ }
14
+
15
+ export const readFiles = async () => {
16
+ const clipboard = await getClipboard()
17
+ return clipboard.readFiles()
18
+ }
19
+
20
+ export const writeFiles = async (type, files) => {
21
+ const clipboard = await getClipboard()
22
+ await clipboard.writeFiles(type, files)
23
+ }
@@ -0,0 +1,78 @@
1
+ // see https://www.cyberciti.biz/faq/how-do-i-copy-a-file-to-the-clipboard-in-linux
2
+ // see also http://manpages.ubuntu.com/manpages/bionic/man1/xclip.1.html
3
+ // on gnome, get current selection targets with `xclip -selection clipboard -t TARGETS -o`
4
+
5
+ import VError from 'verror'
6
+ import * as Exec from '../Exec/Exec.js'
7
+
8
+ const removePrefix = (file) => {
9
+ if (file.startsWith('file://')) {
10
+ return file.slice('file://'.length)
11
+ }
12
+ return file
13
+ }
14
+
15
+ const addPrefix = (file) => {
16
+ return `file://${file}`
17
+ }
18
+
19
+ export const readFiles = async () => {
20
+ let result
21
+ try {
22
+ result = await Exec.exec(
23
+ 'xclip',
24
+ ['-selection', 'clipboard', '-o', '-t', 'x-special/gnome-copied-files'],
25
+ {}
26
+ )
27
+ } catch (error) {
28
+ if (
29
+ error &&
30
+ // @ts-ignore
31
+ error.stderr ===
32
+ 'Error: target x-special/gnome-copied-files not available'
33
+ ) {
34
+ return
35
+ }
36
+ throw error
37
+ }
38
+ const [type, ...files] = result.stdout.split('\n')
39
+ const actualFiles = files.map(removePrefix)
40
+ return {
41
+ source: 'gnomeCopiedFiles',
42
+ type,
43
+ files: actualFiles,
44
+ }
45
+ }
46
+
47
+ export const writeFiles = async (type, files) => {
48
+ const filesWithPrefix = files.map(addPrefix)
49
+ const gnomeCopiedFilesContent = [type, ...filesWithPrefix].join('\n')
50
+ const uriListContent = filesWithPrefix.join('\n')
51
+ const plainContent = files.join('\n')
52
+ try {
53
+ await Exec.exec(
54
+ 'xclip',
55
+ ['-i', '-selection', 'clipboard', '-t', 'x-special/gnome-copied-files'],
56
+ {
57
+ input: gnomeCopiedFilesContent,
58
+ }
59
+ )
60
+ await Exec.exec(
61
+ 'xclip',
62
+ ['-i', '-selection', 'clipboard', '-t', 'text/uri-list'],
63
+ {
64
+ input: uriListContent,
65
+ }
66
+ )
67
+ await Exec.exec(
68
+ 'xclip',
69
+ ['-i', '-selection', 'clipboard', '-t', 'text/plain'],
70
+ {
71
+ input: plainContent,
72
+ }
73
+ )
74
+ } catch (error) {
75
+ // @ts-ignore
76
+ throw new VError(error, 'Failed to copy files to clipboard')
77
+ }
78
+ }
@@ -0,0 +1,14 @@
1
+ import * as Platform from '../Platform/Platform.js'
2
+
3
+ export const readFiles = async () => {
4
+ return {
5
+ source: 'notSupported',
6
+ type: 'none',
7
+ files: [],
8
+ }
9
+ }
10
+
11
+ export const writeFiles = (type, files) => {
12
+ const desktop = Platform.getDesktop()
13
+ console.info(`writing files to clipboard is not yet supported on ${desktop}`)
14
+ }
@@ -0,0 +1,22 @@
1
+ import clipboardEx from 'electron-clipboard-ex'
2
+
3
+ import VError from 'verror'
4
+
5
+ export const readFiles = async () => {
6
+ const filePaths = clipboardEx.readFilePaths()
7
+ console.log(filePaths)
8
+ return {
9
+ source: 'electron-clipboard-ex',
10
+ type: 'copy', // TODO can't be sure on this
11
+ files: filePaths,
12
+ }
13
+ }
14
+
15
+ export const writeFiles = async (type, files) => {
16
+ try {
17
+ clipboardEx.writeFilePaths(files)
18
+ } catch (error) {
19
+ // @ts-ignore
20
+ throw new VError(error, 'Failed to copy files to clipboard')
21
+ }
22
+ }