From 49a8b118b873bfeba55636fb188d92e8edddea38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E8=89=B2=E9=98=B3=E5=85=89?= <1122@hizhou.cn> Date: Wed, 10 Jan 2024 22:57:33 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20=E7=A7=BB=E9=99=A4scp2=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0ssh2-sftp-client=EF=BC=8C=E4=BF=AE=E5=A4=8Dse?= =?UTF-8?q?rver=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- src/index.js | 18 ++++-- src/upload/server.js | 75 +++++++++------------ src/utils/scan.js | 6 +- yarn.lock | 151 +++++++++++++++++++++++++------------------ 5 files changed, 133 insertions(+), 121 deletions(-) diff --git a/package.json b/package.json index 6834629..5273161 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "cos-nodejs-sdk-v5": "^2.11.6", "ignore": "^5.2.0", "progress": "^2.0.3", - "scp2": "^0.5.0", - "single-line-log": "^1.1.2" + "single-line-log": "^1.1.2", + "ssh2-sftp-client": "^9.1.0" } } diff --git a/src/index.js b/src/index.js index 21ba76d..02e6f40 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ const deploy = async ({ config, }) => { const startTime = Date.now(); - console.log(chalk.white(` 正则扫描目录[${dir}] `)); + console.log(chalk.white(` 正在扫描目录[${dir}] `)); const { deployType = 'server', alioss, @@ -73,13 +73,17 @@ const deploy = async ({ try { // OSS上传 - await Promise.all( - tasks.oss.map((i) => ossUpload(i, alioss).then(() => bar.tick())), - ); + if (tasks.oss.length) { + await Promise.all( + tasks.oss.map((i) => ossUpload(i, alioss).then(() => bar.tick())), + ); + } // COS上传 - await Promise.all( - tasks.cos.map((i) => cosUpload(i, cos).then(() => bar.tick())), - ); + if (tasks.cos.length) { + await Promise.all( + tasks.cos.map((i) => cosUpload(i, cos).then(() => bar.tick())), + ); + } // 服务器上传 let scp2 = null; for (let i = 0; i < tasks.server.length; i += 1) { diff --git a/src/upload/server.js b/src/upload/server.js index 9a4ef1c..e58068b 100644 --- a/src/upload/server.js +++ b/src/upload/server.js @@ -2,64 +2,47 @@ * 上传文件到服务器 */ -import scp2 from 'scp2'; +import path from 'path'; +import SftpClient from 'ssh2-sftp-client'; import { toLinux, } from '../utils/path-win-2-linux'; -const sshField = '__ssh'; - async function serverUpload( item, - config = {}, + sshConfig, ) { - const { - port, - host, - username, - password, - privateKey, - } = config; - /** - * SSH配置 - */ - const ssh = { - port, - host, - username, - }; + const localPath = item.path; + const remotePath = toLinux(item.remotePath); + const remoteDir = path.dirname(remotePath); - if (privateKey) { - ssh.privateKey = privateKey; - } else { - ssh.password = password; - } - - /** - * 注入配置 - */ - if (!scp2[sshField]) { - scp2.defaults(ssh); - } + const sftp = new SftpClient(); /** * 执行上传 */ - return new Promise((resolve, reject) => { - scp2.upload( - item.path, - // 远程保存路径 - toLinux(item.remotePath), - (err) => { - if (err) { - console.error(err); - reject(err); - } else { - resolve(scp2); - } - }, - ); - }); + try { + await sftp.connect(sshConfig); + // 检查远程目录是否存在,如果不存在则创建 + try { + await sftp.stat(remoteDir); + } catch (err) { + if (err.code === 'ENOENT') { + // 远程目录不存在,创建目录 + await sftp.mkdir(remoteDir, true); // 参数 true 表示递归创建目录 + } else { + throw err; + } + } + // 上传文件 + await sftp.put(localPath, remotePath); + // 关闭SFTP连接 + await sftp.end(); + } catch (err) { + // 关闭SFTP连接 + await sftp.end(); + throw err; + } } export default serverUpload; diff --git a/src/utils/scan.js b/src/utils/scan.js index 2a48d50..f6066d0 100644 --- a/src/utils/scan.js +++ b/src/utils/scan.js @@ -25,7 +25,7 @@ export const scanDir = async ({ ig = null, // 忽略规则 ignoreRule = null, -}) => { +}, level = 0) => { const dirStat = await stat(dir); if (!dirStat || !dirStat.isDirectory()) { throw Error(`扫描目录 ${dir} 不合法,无法继续扫描`); @@ -45,6 +45,7 @@ export const scanDir = async ({ path: join(dir, i), // 相对位置 relative: parent, + level, }; // 判定文件类型 与 判定是否为忽略文件目录 return stat(item.path).then((res) => { @@ -79,9 +80,10 @@ export const scanDir = async ({ ].filter((o) => o).join('/'), list, ig: scanDirIg, - }); + }, level + 1); })); list.push(...files); + list.sort((a, b) => a.level - b.level); return list; }; diff --git a/yarn.lock b/yarn.lock index 07b19dd..6ed6cb3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1147,7 +1147,7 @@ array.prototype.flat@^1.2.5: define-properties "^1.1.3" es-abstract "^1.19.0" -asn1@~0.2.0, asn1@~0.2.3: +asn1@^0.2.6, asn1@~0.2.3: version "0.2.6" resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.6.tgz#0d3a7bb6e64e02a90c0303b31f292868ea09a08d" integrity sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ== @@ -1171,11 +1171,6 @@ astral-regex@^2.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ== -async@~0.9.0: - version "0.9.2" - resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d" - integrity sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0= - asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" @@ -1232,7 +1227,7 @@ balanced-match@^1.0.0: resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== -bcrypt-pbkdf@^1.0.0: +bcrypt-pbkdf@^1.0.0, bcrypt-pbkdf@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz#a4301d389b6a43f9b67ff3ca11a3f6637e360e9e" integrity sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4= @@ -1268,6 +1263,11 @@ buffer-from@^1.0.0: resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== +buildcheck@~0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/buildcheck/-/buildcheck-0.0.6.tgz#89aa6e417cfd1e2196e3f8fe915eb709d2fe4238" + integrity sha512-8f9ZJCUXyT1M35Jx7MkBgmBMo3oHTTBIPLiY9xyL0pl3T5RwcPEY8cUHr5LBNfu/fk6c2T4DJZuVM/8ZZT2D2A== + builtin-status-codes@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8" @@ -1373,6 +1373,16 @@ concat-map@0.0.1: resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= +concat-stream@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-2.0.0.tgz#414cf5af790a48c60ab9be4527d56d5e41133cb1" + integrity sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A== + dependencies: + buffer-from "^1.0.0" + inherits "^2.0.3" + readable-stream "^3.0.2" + typedarray "^0.0.6" + conf@^9.0.0: version "9.0.2" resolved "https://registry.yarnpkg.com/conf/-/conf-9.0.2.tgz#943589602b1ce274d9234265314336a698972bc5" @@ -1441,6 +1451,14 @@ cos-nodejs-sdk-v5@^2.11.6: request "^2.88.2" xml2js "^0.4.19" +cpu-features@~0.0.9: + version "0.0.9" + resolved "https://registry.yarnpkg.com/cpu-features/-/cpu-features-0.0.9.tgz#5226b92f0f1c63122b0a3eb84cb8335a4de499fc" + integrity sha512-AKjgn2rP2yJyfbepsmLfiYcmtNn/2eUvocUyM/09yB0YDiz39HteK/5/T4Onf0pmdYDMgkBoGvRLvEguzyL7wQ== + dependencies: + buildcheck "~0.0.6" + nan "^2.17.0" + cross-env@^7.0.3: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf" @@ -1626,6 +1644,11 @@ env-paths@^2.2.0: resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2" integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A== +err-code@^2.0.2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9" + integrity sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA== + es-abstract@^1.19.0, es-abstract@^1.19.1: version "1.19.1" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.19.1.tgz#d4885796876916959de78edaa0df456627115ec3" @@ -2061,18 +2084,6 @@ glob@^7.1.2, glob@^7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" -glob@~7.0.3: - version "7.0.6" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.6.tgz#211bafaf49e525b8cd93260d14ab136152b3f57a" - integrity sha1-IRuvr0nlJbjNkyYNFKsTYVKz9Xo= - dependencies: - fs.realpath "^1.0.0" - inflight "^1.0.4" - inherits "2" - minimatch "^3.0.2" - once "^1.3.0" - path-is-absolute "^1.0.0" - globals@^11.1.0: version "11.12.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e" @@ -2219,7 +2230,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@~2.0.1, inherits@~2.0.3: +inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3: version "2.0.4" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -2561,11 +2572,6 @@ lodash.truncate@^4.4.2: resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193" integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM= -lodash@~4.11.1: - version "4.11.2" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.11.2.tgz#d6b4338b110a58e21dae5cebcfdbbfd2bc4cdb3b" - integrity sha1-1rQzixEKWOIdrlzrz9u/0rxM2zs= - lru-cache@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920" @@ -2627,7 +2633,7 @@ mimic-fn@^3.0.0: resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-3.1.0.tgz#65755145bbf3e36954b949c16450427451d5ca74" integrity sha512-Ysbi9uYW9hFyfrThdDEQuykN4Ey6BuwPD2kpI5ES/nFTDn/98yxYNLZJcgUAKPT/mcrLLKaGzJR9YVxJrIdASQ== -minimatch@^3.0.2, minimatch@^3.0.4: +minimatch@^3.0.4: version "3.1.2" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw== @@ -2681,6 +2687,11 @@ mz@^2.7.0: object-assign "^4.0.1" thenify-all "^1.0.0" +nan@^2.17.0, nan@^2.18.0: + version "2.18.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.18.0.tgz#26a6faae7ffbeb293a39660e88a76b82e30b7554" + integrity sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w== + natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" @@ -2957,6 +2968,14 @@ progress@^2.0.0, progress@^2.0.3: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== +promise-retry@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-2.0.1.tgz#ff747a13620ab57ba688f5fc67855410c370da22" + integrity sha512-y+WKFlBR8BGXnsNlIHFGPZmyDf3DFMoLhaflAnyZgV6rG6xu+JwesTo2Q9R6XwYmtmwAFCkAk3e35jEdoeh/3g== + dependencies: + err-code "^2.0.2" + retry "^0.12.0" + proxy-agent@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/proxy-agent/-/proxy-agent-5.0.0.tgz#d31405c10d6e8431fde96cba7a0c027ce01d633b" @@ -3039,15 +3058,14 @@ readable-stream@^2.3.6: string_decoder "~1.1.1" util-deprecate "~1.0.1" -readable-stream@~1.0.0: - version "1.0.34" - resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" - integrity sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw= +readable-stream@^3.0.2: + version "3.6.2" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967" + integrity sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA== dependencies: - core-util-is "~1.0.0" - inherits "~2.0.1" - isarray "0.0.1" - string_decoder "~0.10.x" + inherits "^2.0.3" + string_decoder "^1.1.1" + util-deprecate "^1.0.1" regenerate-unicode-properties@^10.0.1: version "10.0.1" @@ -3147,6 +3165,11 @@ resolve@^1.14.2, resolve@^1.20.0: path-parse "^1.0.7" supports-preserve-symlinks-flag "^1.0.0" +retry@^0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" + integrity sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow== + rimraf@^2.6.1: version "2.7.1" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec" @@ -3168,7 +3191,7 @@ rollup@^2.70.1: optionalDependencies: fsevents "~2.3.2" -safe-buffer@^5.0.1, safe-buffer@^5.1.2: +safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@~5.2.0: version "5.2.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ== @@ -3188,16 +3211,6 @@ sax@>=0.6.0: resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== -scp2@^0.5.0: - version "0.5.0" - resolved "https://registry.yarnpkg.com/scp2/-/scp2-0.5.0.tgz#64ee74bc3685f3a4c6290f2da8c1e3b4eef92e8d" - integrity sha1-ZO50vDaF86TGKQ8tqMHjtO75Lo0= - dependencies: - async "~0.9.0" - glob "~7.0.3" - lodash "~4.11.1" - ssh2 "~0.4.10" - sdk-base@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/sdk-base/-/sdk-base-2.0.1.tgz#ba40289e8bdf272ed11dd9ea97eaf98e036d24c6" @@ -3321,22 +3334,25 @@ sprintf-js@~1.0.2: resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= -ssh2-streams@~0.0.22: - version "0.0.23" - resolved "https://registry.yarnpkg.com/ssh2-streams/-/ssh2-streams-0.0.23.tgz#aeef30831bb5fc4af6aa3f6d0a261a413531612b" - integrity sha1-ru8wgxu1/Er2qj9tCiYaQTUxYSs= +ssh2-sftp-client@^9.1.0: + version "9.1.0" + resolved "https://registry.yarnpkg.com/ssh2-sftp-client/-/ssh2-sftp-client-9.1.0.tgz#bef1d5352c3dc214836cb6dec9427f3988d9ff2b" + integrity sha512-Hzdr9OE6GxZjcmyM9tgBSIFVyrHAp9c6U2Y4yBkmYOHoQvZ7pIm27dmltvcmRfxcWiIcg8HBvG5iAikDf+ZuzQ== dependencies: - asn1 "~0.2.0" - readable-stream "~1.0.0" - streamsearch "~0.1.2" + concat-stream "^2.0.0" + promise-retry "^2.0.1" + ssh2 "^1.12.0" -ssh2@~0.4.10: - version "0.4.15" - resolved "https://registry.yarnpkg.com/ssh2/-/ssh2-0.4.15.tgz#07c6f4106d9f7b6ea6e4df636c6c53f1f9817ff8" - integrity sha1-B8b0EG2fe26m5N9jbGxT8fmBf/g= +ssh2@^1.12.0: + version "1.15.0" + resolved "https://registry.yarnpkg.com/ssh2/-/ssh2-1.15.0.tgz#2f998455036a7f89e0df5847efb5421748d9871b" + integrity sha512-C0PHgX4h6lBxYx7hcXwu3QWdh4tg6tZZsTfXcdvc5caW/EMxaB4H9dWsl7qk+F7LAW762hp8VbXOX7x4xUYvEw== dependencies: - readable-stream "~1.0.0" - ssh2-streams "~0.0.22" + asn1 "^0.2.6" + bcrypt-pbkdf "^1.0.2" + optionalDependencies: + cpu-features "~0.0.9" + nan "^2.18.0" sshpk@^1.7.0: version "1.17.0" @@ -3379,11 +3395,6 @@ stream-wormhole@^1.0.4: resolved "https://registry.yarnpkg.com/stream-wormhole/-/stream-wormhole-1.1.0.tgz#300aff46ced553cfec642a05251885417693c33d" integrity sha512-gHFfL3px0Kctd6Po0M8TzEvt3De/xu6cnRrjlfYNhwbhLPLwigI2t1nc6jrzNuaYg5C4YF78PPFuQPzRiqn9ew== -streamsearch@~0.1.2: - version "0.1.2" - resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a" - integrity sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo= - string-width@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" @@ -3418,6 +3429,13 @@ string.prototype.trimstart@^1.0.4: call-bind "^1.0.2" define-properties "^1.1.3" +string_decoder@^1.1.1: + version "1.3.0" + resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e" + integrity sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA== + dependencies: + safe-buffer "~5.2.0" + string_decoder@~0.10.x: version "0.10.31" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94" @@ -3577,6 +3595,11 @@ type-fest@^0.20.2: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4" integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ== +typedarray@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" + integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== + unbox-primitive@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.1.tgz#085e215625ec3162574dc8859abee78a59b14471" @@ -3655,7 +3678,7 @@ urllib@^2.33.1: statuses "^1.3.1" utility "^1.16.1" -util-deprecate@~1.0.1: +util-deprecate@^1.0.1, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8= From 531fa1631d502df221f96b3ff45708a918c5363f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E8=89=B2=E9=98=B3=E5=85=89?= <1122@hizhou.cn> Date: Wed, 10 Jan 2024 22:59:59 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=94=A7=20=E6=B7=BB=E5=8A=A0npm?= =?UTF-8?q?=E5=BF=BD=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .npmignore | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .npmignore diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..532d7ee --- /dev/null +++ b/.npmignore @@ -0,0 +1,19 @@ +.DS_Store +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* +package-lock.json +tests/**/coverage/ +*.local + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln + +demo/ +demo.js From fd736e935e6cd8338e8efc3fc52b0e1de1a92be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E8=89=B2=E9=98=B3=E5=85=89?= <1122@hizhou.cn> Date: Wed, 10 Jan 2024 23:00:12 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A7=20version:=201.2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6834629..bd5d2bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ossso/front-deploy", - "version": "1.1.2", + "version": "1.2.0", "description": "前端发布工具", "main": "index.js", "exports": { From 6b2cbf725cbdc046e540f529f5852db07b853115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E8=89=B2=E9=98=B3=E5=85=89?= <1122@hizhou.cn> Date: Wed, 10 Jan 2024 23:08:09 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A8=20=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/index.cjs | 101 ++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/dist/index.cjs b/dist/index.cjs index 7145140..c3ca816 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -6,17 +6,18 @@ var chalk = require('chalk'); var ProgressBar = require('progress'); var COS = require('cos-nodejs-sdk-v5'); var OSS = require('ali-oss'); -var scp2 = require('scp2'); +var SftpClient = require('ssh2-sftp-client'); var promises = require('fs/promises'); var ignore = require('ignore'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } +var path__default = /*#__PURE__*/_interopDefaultLegacy(path); var chalk__default = /*#__PURE__*/_interopDefaultLegacy(chalk); var ProgressBar__default = /*#__PURE__*/_interopDefaultLegacy(ProgressBar); var COS__default = /*#__PURE__*/_interopDefaultLegacy(COS); var OSS__default = /*#__PURE__*/_interopDefaultLegacy(OSS); -var scp2__default = /*#__PURE__*/_interopDefaultLegacy(scp2); +var SftpClient__default = /*#__PURE__*/_interopDefaultLegacy(SftpClient); var ignore__default = /*#__PURE__*/_interopDefaultLegacy(ignore); // 转换Windows路径为Linux路径 @@ -104,59 +105,41 @@ async function ossUpload( * 上传文件到服务器 */ -const sshField = '__ssh'; - async function serverUpload( item, - config = {}, + sshConfig, ) { - const { - port, - host, - username, - password, - privateKey, - } = config; - /** - * SSH配置 - */ - const ssh = { - port, - host, - username, - }; + const localPath = item.path; + const remotePath = toLinux(item.remotePath); + const remoteDir = path__default["default"].dirname(remotePath); - if (privateKey) { - ssh.privateKey = privateKey; - } else { - ssh.password = password; - } - - /** - * 注入配置 - */ - if (!scp2__default["default"][sshField]) { - scp2__default["default"].defaults(ssh); - } + const sftp = new SftpClient__default["default"](); /** * 执行上传 */ - return new Promise((resolve, reject) => { - scp2__default["default"].upload( - item.path, - // 远程保存路径 - toLinux(item.remotePath), - (err) => { - if (err) { - console.error(err); - reject(err); - } else { - resolve(scp2__default["default"]); - } - }, - ); - }); + try { + await sftp.connect(sshConfig); + // 检查远程目录是否存在,如果不存在则创建 + try { + await sftp.stat(remoteDir); + } catch (err) { + if (err.code === 'ENOENT') { + // 远程目录不存在,创建目录 + await sftp.mkdir(remoteDir, true); // 参数 true 表示递归创建目录 + } else { + throw err; + } + } + // 上传文件 + await sftp.put(localPath, remotePath); + // 关闭SFTP连接 + await sftp.end(); + } catch (err) { + // 关闭SFTP连接 + await sftp.end(); + throw err; + } } /** @@ -174,7 +157,7 @@ const scanDir = async ({ ig = null, // 忽略规则 ignoreRule = null, -}) => { +}, level = 0) => { const dirStat = await promises.stat(dir); if (!dirStat || !dirStat.isDirectory()) { throw Error(`扫描目录 ${dir} 不合法,无法继续扫描`); @@ -194,6 +177,7 @@ const scanDir = async ({ path: path.join(dir, i), // 相对位置 relative: parent, + level, }; // 判定文件类型 与 判定是否为忽略文件目录 return promises.stat(item.path).then((res) => { @@ -228,9 +212,10 @@ const scanDir = async ({ ].filter((o) => o).join('/'), list, ig: scanDirIg, - }); + }, level + 1); })); list.push(...files); + list.sort((a, b) => a.level - b.level); return list; }; @@ -247,7 +232,7 @@ const deploy = async ({ config, }) => { const startTime = Date.now(); - console.log(chalk__default["default"].white(` 正则扫描目录[${dir}] `)); + console.log(chalk__default["default"].white(` 正在扫描目录[${dir}] `)); const { deployType = 'server', alioss, @@ -303,13 +288,17 @@ const deploy = async ({ try { // OSS上传 - await Promise.all( - tasks.oss.map((i) => ossUpload(i, alioss).then(() => bar.tick())), - ); + if (tasks.oss.length) { + await Promise.all( + tasks.oss.map((i) => ossUpload(i, alioss).then(() => bar.tick())), + ); + } // COS上传 - await Promise.all( - tasks.cos.map((i) => cosUpload(i, cos).then(() => bar.tick())), - ); + if (tasks.cos.length) { + await Promise.all( + tasks.cos.map((i) => cosUpload(i, cos).then(() => bar.tick())), + ); + } // 服务器上传 let scp2 = null; for (let i = 0; i < tasks.server.length; i += 1) {