index.js 1.0 KB

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