index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const isWSL = require('is-wsl');
  3. const termux = require('./lib/termux.js');
  4. const linux = require('./lib/linux.js');
  5. const macos = require('./lib/macos.js');
  6. const windows = require('./lib/windows.js');
  7. const platformLib = (() => {
  8. switch (process.platform) {
  9. case 'darwin':
  10. return macos;
  11. case 'win32':
  12. return windows;
  13. case 'android':
  14. if (process.env.PREFIX !== '/data/data/com.termux/files/usr') {
  15. throw new Error('You need to install Termux for this module to work on Android: https://termux.com');
  16. }
  17. return termux;
  18. default:
  19. // `process.platform === 'linux'` for WSL.
  20. if (isWSL) {
  21. return windows;
  22. }
  23. return linux;
  24. }
  25. })();
  26. exports.write = async text => {
  27. if (typeof text !== 'string') {
  28. throw new TypeError(`Expected a string, got ${typeof text}`);
  29. }
  30. await platformLib.copy({input: text});
  31. };
  32. exports.read = async () => platformLib.paste({stripEof: false});
  33. exports.writeSync = text => {
  34. if (typeof text !== 'string') {
  35. throw new TypeError(`Expected a string, got ${typeof text}`);
  36. }
  37. platformLib.copySync({input: text});
  38. };
  39. exports.readSync = () => platformLib.pasteSync({stripEof: false}).stdout;