linux.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const path = require('path');
  3. const execa = require('execa');
  4. const handler = error => {
  5. if (error.code === 'ENOENT') {
  6. throw new Error('Couldn\'t find the required `xsel` binary. On Debian/Ubuntu you can install it with: sudo apt install xsel');
  7. }
  8. throw error;
  9. };
  10. const xsel = path.join(__dirname, '../fallbacks/linux/xsel');
  11. module.exports = {
  12. copy: async options => {
  13. try {
  14. await execa(xsel, ['--clipboard', '--input'], options);
  15. } catch (_) {
  16. try {
  17. await execa('xsel', ['--clipboard', '--input'], options);
  18. } catch (error) {
  19. handler(error);
  20. }
  21. }
  22. },
  23. paste: async options => {
  24. try {
  25. return await execa.stdout(xsel, ['--clipboard', '--output'], options);
  26. } catch (_) {
  27. try {
  28. return await execa.stdout('xsel', ['--clipboard', '--output'], options);
  29. } catch (error) {
  30. handler(error);
  31. }
  32. }
  33. },
  34. copySync: options => {
  35. try {
  36. execa.sync(xsel, ['--clipboard', '--input'], options);
  37. } catch (_) {
  38. try {
  39. execa.sync('xsel', ['--clipboard', '--input'], options);
  40. } catch (error) {
  41. handler(error);
  42. }
  43. }
  44. },
  45. pasteSync: options => {
  46. try {
  47. return execa.sync(xsel, ['--clipboard', '--output'], options);
  48. } catch (_) {
  49. try {
  50. return execa.sync('xsel', ['--clipboard', '--output'], options);
  51. } catch (error) {
  52. handler(error);
  53. }
  54. }
  55. }
  56. };