helpers.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.exists = exists;
  6. exports.mkdirSftp = mkdirSftp;
  7. exports.normalizeConfig = normalizeConfig;
  8. exports.normalizePutFilesOptions = normalizePutFilesOptions;
  9. exports.normalizePutDirectoryOptions = normalizePutDirectoryOptions;
  10. exports.generateCallback = generateCallback;
  11. exports.readdir = exports.stat = void 0;
  12. var _fs = _interopRequireDefault(require("fs"));
  13. var _path = _interopRequireDefault(require("path"));
  14. var _sbPromisify = _interopRequireDefault(require("sb-promisify"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  17. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  18. const CODE_REGEXP = /Error: (E[\S]+): /;
  19. const DEFAULT_CONCURRENCY = 5;
  20. const readFile = (0, _sbPromisify.default)(_fs.default.readFile);
  21. const stat = (0, _sbPromisify.default)(_fs.default.stat);
  22. exports.stat = stat;
  23. const readdir = (0, _sbPromisify.default)(_fs.default.readdir);
  24. exports.readdir = readdir;
  25. function transformError(givenError) {
  26. const code = CODE_REGEXP.exec(givenError);
  27. if (code) {
  28. // eslint-disable-next-line no-param-reassign,prefer-destructuring
  29. givenError.code = code[1];
  30. }
  31. return givenError;
  32. }
  33. function exists(filePath) {
  34. return new Promise(function (resolve) {
  35. _fs.default.access(filePath, _fs.default.R_OK, function (error) {
  36. resolve(!error);
  37. });
  38. });
  39. }
  40. function mkdirSftp(_x, _x2) {
  41. return _mkdirSftp.apply(this, arguments);
  42. }
  43. function _mkdirSftp() {
  44. _mkdirSftp = _asyncToGenerator(function* (path, sftp) {
  45. let stats;
  46. try {
  47. stats = yield (0, _sbPromisify.default)(sftp.stat).call(sftp, path);
  48. } catch (_) {
  49. /* No Op */
  50. }
  51. if (stats) {
  52. if (stats.isDirectory()) {
  53. // Already exists, nothing to worry about
  54. return;
  55. }
  56. throw new Error('mkdir() failed, target already exists and is not a directory');
  57. }
  58. try {
  59. yield (0, _sbPromisify.default)(sftp.mkdir).call(sftp, path);
  60. } catch (error) {
  61. throw transformError(error);
  62. }
  63. });
  64. return _mkdirSftp.apply(this, arguments);
  65. }
  66. function normalizeConfig(_x3) {
  67. return _normalizeConfig.apply(this, arguments);
  68. }
  69. function _normalizeConfig() {
  70. _normalizeConfig = _asyncToGenerator(function* (givenConfig) {
  71. const config = Object.assign({}, givenConfig);
  72. if (config.username && typeof config.username !== 'string') {
  73. throw new Error('config.username must be a valid string');
  74. }
  75. if (typeof config.host !== 'undefined') {
  76. if (typeof config.host !== 'string' || !config.host) {
  77. throw new Error('config.host must be a valid string');
  78. }
  79. } else if (typeof config.sock !== 'undefined') {
  80. if (!config.sock || typeof config.sock !== 'object') {
  81. throw new Error('config.sock must be a valid object');
  82. }
  83. } else {
  84. throw new Error('config.host or config.sock must be provided');
  85. }
  86. if (config.privateKey) {
  87. const privateKey = config.privateKey;
  88. if (typeof privateKey !== 'string') {
  89. throw new Error('config.privateKey must be a string');
  90. }
  91. if (!(privateKey.includes('BEGIN') && privateKey.includes('KEY'))) {
  92. try {
  93. config.privateKey = yield readFile(privateKey, 'utf8');
  94. } catch (error) {
  95. if (error.code === 'ENOENT') {
  96. throw new Error(`config.privateKey does not exist at ${privateKey}`);
  97. }
  98. throw error;
  99. }
  100. }
  101. } else if (config.password) {
  102. const password = config.password;
  103. if (typeof password !== 'string') {
  104. throw new Error('config.password must be a string');
  105. }
  106. }
  107. config.tryKeyboard = !!config.tryKeyboard;
  108. if (config.tryKeyboard === true) {
  109. if (typeof config.onKeyboardInteractive !== 'function') {
  110. config.onKeyboardInteractive = (name, instructions, instructionsLang, prompts, finish) => {
  111. if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
  112. finish([config.password]);
  113. }
  114. };
  115. }
  116. } else {
  117. config.onKeyboardInteractive = null;
  118. }
  119. return config;
  120. });
  121. return _normalizeConfig.apply(this, arguments);
  122. }
  123. function normalizePutFilesOptions(givenConfig) {
  124. const config = {};
  125. if (givenConfig.sftpOptions && typeof givenConfig.sftpOptions === 'object') {
  126. config.sftpOptions = givenConfig.sftpOptions;
  127. } else config.sftpOptions = {};
  128. if (typeof givenConfig.concurrency === 'number') {
  129. config.concurrency = givenConfig.concurrency;
  130. } else config.concurrency = DEFAULT_CONCURRENCY;
  131. if (typeof givenConfig.sftp === 'object') {
  132. config.sftp = givenConfig.sftp;
  133. } else config.sftp = null;
  134. return config;
  135. }
  136. function normalizePutDirectoryOptions(givenConfig) {
  137. const config = normalizePutFilesOptions(givenConfig);
  138. if (givenConfig.tick) {
  139. if (typeof givenConfig.tick !== 'function') {
  140. throw new Error('config.tick must be a function');
  141. }
  142. config.tick = givenConfig.tick;
  143. } else {
  144. config.tick = function () {};
  145. }
  146. if (givenConfig.validate) {
  147. if (typeof givenConfig.validate !== 'function') {
  148. throw new Error('config.validate must be a function');
  149. }
  150. config.validate = givenConfig.validate;
  151. } else {
  152. config.validate = function (path) {
  153. return _path.default.basename(path).substr(0, 1) !== '.';
  154. };
  155. }
  156. config.recursive = {}.hasOwnProperty.call(givenConfig, 'recursive') ? !!givenConfig.recursive : true;
  157. return config;
  158. }
  159. function generateCallback(resolve, reject) {
  160. return function (error, result) {
  161. if (error) {
  162. reject(error);
  163. } else {
  164. resolve(result);
  165. }
  166. };
  167. }