Skip to content

Commit

Permalink
合并PR,发布1.2.0
Browse files Browse the repository at this point in the history
Release/1.2.0
  • Loading branch information
ossso authored Jan 10, 2024
2 parents 6fa76d6 + 6b2cbf7 commit c2394e9
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 178 deletions.
19 changes: 19 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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
101 changes: 45 additions & 56 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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路径
Expand Down Expand Up @@ -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;
}
}

/**
Expand All @@ -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} 不合法,无法继续扫描`);
Expand All @@ -194,6 +177,7 @@ const scanDir = async ({
path: path.join(dir, i),
// 相对位置
relative: parent,
level,
};
// 判定文件类型 与 判定是否为忽略文件目录
return promises.stat(item.path).then((res) => {
Expand Down Expand Up @@ -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;
};

Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ossso/front-deploy",
"version": "1.1.2",
"version": "1.2.0",
"description": "前端发布工具",
"main": "index.js",
"exports": {
Expand Down Expand Up @@ -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"
}
}
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
75 changes: 29 additions & 46 deletions src/upload/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 4 additions & 2 deletions src/utils/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} 不合法,无法继续扫描`);
Expand All @@ -45,6 +45,7 @@ export const scanDir = async ({
path: join(dir, i),
// 相对位置
relative: parent,
level,
};
// 判定文件类型 与 判定是否为忽略文件目录
return stat(item.path).then((res) => {
Expand Down Expand Up @@ -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;
};

Expand Down
Loading

0 comments on commit c2394e9

Please sign in to comment.