UNPKG

oss-upload-tool

Version:
1 lines 16.5 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _path=require("path"),_stanUtils=require("stan-utils"),_utils=require("@planjs/utils"),_cos=_interopRequireDefault(require("./client/cos")),_ali=_interopRequireDefault(require("./client/ali")),_s2=_interopRequireDefault(require("./client/s3")),_getUploadList=_interopRequireDefault(require("./get-upload-list")),_consts=require("./consts"),_utils2=require("./utils"),_excluded=["dest","rename","transform","src"];function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _typeof(t){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},_typeof(t)}function ownKeys(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(r),!0).forEach((function(e){_defineProperty(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function _defineProperty(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function _objectWithoutProperties(t,e){if(null==t)return{};var r,n,o=_objectWithoutPropertiesLoose(t,e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(t);for(n=0;n<a.length;n++)r=a[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(o[r]=t[r])}return o}function _objectWithoutPropertiesLoose(t,e){if(null==t)return{};var r,n,o={},a=Object.keys(t);for(n=0;n<a.length;n++)r=a[n],e.indexOf(r)>=0||(o[r]=t[r]);return o}function _regeneratorRuntime(){/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */_regeneratorRuntime=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var o=e&&e.prototype instanceof f?e:f,a=Object.create(o.prototype),i=new S(n||[]);return a._invoke=function(t,e,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return E()}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var u=g(i,r);if(u){if(u===l)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=s(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===l)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(t,r,i),a}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var l={};function f(){}function p(){}function y(){}var h={};u(h,o,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(O([])));v&&v!==e&&r.call(v,o)&&(h=v);var _=y.prototype=f.prototype=Object.create(h);function m(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function b(t,e){function n(o,a,i,u){var c=s(t[o],t,a);if("throw"!==c.type){var l=c.arg,f=l.value;return f&&"object"==_typeof(f)&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):e.resolve(f).then((function(t){l.value=t,i(l)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}var o;this._invoke=function(t,r){function a(){return new e((function(e,o){n(t,r,e,o)}))}return o=o?o.then(a,a):a()}}function g(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,g(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function x(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function O(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return a.next=a}}return{next:E}}function E(){return{value:void 0,done:!0}}return p.prototype=y,u(_,"constructor",y),u(y,"constructor",p),p.displayName=u(y,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===p||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,y):(t.__proto__=y,u(t,i,"GeneratorFunction")),t.prototype=Object.create(_),t},t.awrap=function(t){return{__await:t}},m(b.prototype),u(b.prototype,a,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new b(c(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},m(_),u(_,i,"Generator"),u(_,o,(function(){return this})),u(_,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=O,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(x),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return i.type="throw",i.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,l):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),x(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;x(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:O(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}function _createForOfIteratorHelper(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,u=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return i=t.done,t},e:function(t){u=!0,a=t},f:function(){try{i||null==r.return||r.return()}finally{if(u)throw a}}}}function asyncGeneratorStep(t,e,r,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function _asyncToGenerator(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function i(t){asyncGeneratorStep(a,n,o,i,u,"next",t)}function u(t){asyncGeneratorStep(a,n,o,i,u,"throw",t)}i(void 0)}))}}function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_unsupportedIterableToArray(t)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArray(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}function _arrayWithoutHoles(t){if(Array.isArray(t))return _arrayLikeToArray(t)}function _slicedToArray(t,e){return _arrayWithHoles(t)||_iterableToArrayLimit(t,e)||_unsupportedIterableToArray(t,e)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function _iterableToArrayLimit(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,a=[],i=!0,u=!1;try{for(r=r.call(t);!(i=(n=r.next()).done)&&(a.push(n.value),!e||a.length!==e);i=!0);}catch(t){u=!0,o=t}finally{try{i||null==r.return||r.return()}finally{if(u)throw o}}return a}}function _arrayWithHoles(t){if(Array.isArray(t))return t}function getUploadType(t){if(null!=t&&t.type)return t.type;for(var e=0,r=[[null==t?void 0:t.COSOptions,[_consts.COS_SECRET_ID,_consts.COS_SECRET_KEY],"COS"],[null==t?void 0:t.ALIOptions,[_consts.ALIOSS_ENDPOINT_KEY,_consts.ALIOSS_SECRET_ID,_consts.ALIOSS_SECRET_KEY],"ALI"],[null==t?void 0:t.S3Options,[_consts.S3_SECRET_ID,_consts.S3_SECRET_KEY],"S3"]];e<r.length;e++){var n=_slicedToArray(r[e],3),o=n[0],a=n[1],i=n[2];if(void 0!==o)return i;if(_utils2.getGlobalValue.apply(void 0,_toConsumableArray(a)))return i}return"COS"}function ossUpload(t){return _ossUpload.apply(this,arguments)}function _ossUpload(){return _ossUpload=_asyncToGenerator(_regeneratorRuntime().mark((function t(e){var r,n,o,a,i,u,c,s,l,f,p,y,h,d;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=e.targets,n=e.cwd,o=void 0===n?process.cwd():n,a=e.parallelLimit,i=void 0===a?5:a,u=e.uploadParams,c=e.maxAttempts,s=void 0===c?0:c,l=e.verbose,f=e.existCheck,p=void 0!==f&&f,y=getUploadType(e),!(h=Array.isArray(r)?r:(0,_utils.isPlanObject)(r)?[r]:[]).length){t.next=8;break}return t.delegateYield(_regeneratorRuntime().mark((function t(){var r,n,a,c,f,d,v,_,m;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:r=[],n=_createForOfIteratorHelper(h),t.prev=2,c=_regeneratorRuntime().mark((function t(){var e,n,i,u,c,s,l;return _regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=a.value,n=e.dest,i=e.rename,u=e.transform,c=e.src,s=_objectWithoutProperties(e,_excluded),t.next=4,(0,_stanUtils.globby)(c,_objectSpread({expandDirectories:!1,onlyFiles:!1,cwd:o},s));case 4:l=t.sent,(Array.isArray(n)?n:[n]).forEach((function(t){r.push.apply(r,_toConsumableArray((0,_getUploadList.default)(l,t,{flatten:e.flatten,rename:i,transform:u,cwd:o})))}));case 6:case"end":return t.stop()}}),t)})),n.s();case 5:if((a=n.n()).done){t.next=9;break}return t.delegateYield(c(),"t0",7);case 7:t.next=5;break;case 9:t.next=14;break;case 11:t.prev=11,t.t1=t.catch(2),n.e(t.t1);case 14:return t.prev=14,n.f(),t.finish(14);case 17:t.t2=y,t.next="COS"===t.t2?20:"ALI"===t.t2||"AOSS"===t.t2?22:"S3"===t.t2?24:26;break;case 20:return f=new _cos.default(e),t.abrupt("break",28);case 22:return f=new _ali.default(e),t.abrupt("break",28);case 24:return f=new _s2.default(e),t.abrupt("break",28);case 26:return console.log(_stanUtils.chalk.red("".concat(y," is not support."))),t.abrupt("return",{v:Promise.reject(new Error("oss type is not support"))});case 28:if(r.length){t.next=31;break}return console.log(_stanUtils.chalk.yellow("No items to upload")),t.abrupt("return",{v:Promise.reject(new Error("No items to upload"))});case 31:return d=new _stanUtils.Listr({concurrent:i,renderer:l?"verbose":"default"}),v=(0,_utils.asyncPool)({maxConcurrency:i}),r.forEach((function(t){v.executor((function(){return new Promise((function(e,r){var n,a="Uploading ".concat(_stanUtils.chalk.yellow((0,_stanUtils.slash)((0,_path.relative)(o,t.filePath)))," to ").concat(_stanUtils.chalk.green(t.path));d.add({title:a,task:(n=_asyncToGenerator(_regeneratorRuntime().mark((function n(o,i){var c,l,y,h,d;return _regeneratorRuntime().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(c=Date.now(),!("boolean"==typeof p&&p||("string"==typeof p||Array.isArray(p))&&(0,_stanUtils.multimatch)(t.path,p).length)){n.next=20;break}return n.next=4,f.getUploadedUrl(t,u);case 4:return l=n.sent,n.prev=5,n.next=8,(0,_utils2.getRemoteFileInfo)(l.url);case 8:if(y=n.sent,(h=y.etag)&&!h.includes((0,_stanUtils.getFileEtag)(t.filePath))){n.next=16;break}return d=l.url,i.title=a.replace("Uploading","File exists")+" ".concat((0,_stanUtils.pms)(Date.now()-c)," -> ").concat(_stanUtils.chalk.greenBright(d)),o.push(l),e(),n.abrupt("return");case 16:n.next=20;break;case 18:n.prev=18,n.t0=n.catch(5);case 20:return n.abrupt("return",(0,_utils.retry)(f.upload,{maxAttempts:s+1})(t,u,{onProgress:function(t,e){i.output="".concat((t/e*100).toFixed(2),"%")}}).then((function(t){var r=t.url;i.title=a.replace("Uploading","Uploaded")+" ".concat((0,_stanUtils.pms)(Date.now()-c)," -> ").concat(_stanUtils.chalk.greenBright(r)),o.push(t),e()}),(function(t){return r(),Promise.reject(t)})));case 21:case"end":return n.stop()}}),n,null,[[5,18]])}))),function(t,e){return n.apply(this,arguments)})})}))}))})),t.prev=34,_=Date.now(),t.next=38,d.run([]);case 38:return m=t.sent,console.log(_stanUtils.chalk.green(" Upload successfully ".concat((0,_stanUtils.pms)(Date.now()-_),"."))),t.abrupt("return",{v:m});case 43:return t.prev=43,t.t3=t.catch(34),console.log(_stanUtils.chalk.red(" Upload failed")),t.abrupt("return",{v:Promise.reject(t.t3)});case 47:case"end":return t.stop()}}),t,null,[[2,11,14,17],[34,43]])}))(),"t0",5);case 5:if("object"!==_typeof(d=t.t0)){t.next=8;break}return t.abrupt("return",d.v);case 8:return console.log(_stanUtils.chalk.yellow("No items to upload")),t.abrupt("return",Promise.reject(new Error("No items to upload")));case 10:case"end":return t.stop()}}),t)}))),_ossUpload.apply(this,arguments)}var _default=ossUpload;exports.default=_default;