Skip to content

Commit

Permalink
Optimize exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaming743 committed Jul 24, 2020
1 parent a9578df commit a6aa4a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 71 deletions.
49 changes: 19 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
function getList(ftp, src) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
return [2 /*return*/, new Promise(function (resolve, reject) {
ftp.list(src, function (err, list) {
if (err) {
console.error(err);
resolve(false);
reject(err);
}
else {
resolve(list);
Expand All @@ -58,14 +57,13 @@ function rmDir(ftp, src, recusive) {
if (recusive === void 0) { recusive = true; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
return [2 /*return*/, new Promise(function (resolve, reject) {
ftp.rmdir(src, recusive, function (err) {
if (err) {
console.error(err);
resolve(false);
reject(err);
}
else {
resolve(true);
resolve();
}
});
})];
Expand All @@ -76,14 +74,13 @@ exports.rmDir = rmDir;
function deleteFile(ftp, src) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
return [2 /*return*/, new Promise(function (resolve, reject) {
ftp.delete(src, function (err) {
if (err) {
console.error(err);
resolve(false);
reject(err);
}
else {
resolve(true);
resolve();
}
});
})];
Expand All @@ -94,14 +91,12 @@ exports.deleteFile = deleteFile;
function emptyDir(ftp, src, except) {
if (except === void 0) { except = []; }
return __awaiter(this, void 0, void 0, function () {
var list, _loop_1, i, listNum, state_1;
var list, _loop_1, i, listNum;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getList(ftp, src)];
case 1:
list = _a.sent();
if (!list)
return [2 /*return*/, false];
_loop_1 = function (i, listNum) {
var _a, type, name_1, fullSrc;
return __generator(this, function (_b) {
Expand All @@ -116,13 +111,11 @@ function emptyDir(ftp, src, except) {
if (!(type === 'd')) return [3 /*break*/, 2];
return [4 /*yield*/, rmDir(ftp, fullSrc, true)];
case 1:
if (!(_b.sent()))
return [2 /*return*/, { value: false }];
_b.sent();
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, deleteFile(ftp, fullSrc)];
case 3:
if (!(_b.sent()))
return [2 /*return*/, { value: false }];
_b.sent();
_b.label = 4;
case 4: return [2 /*return*/];
}
Expand All @@ -134,14 +127,12 @@ function emptyDir(ftp, src, except) {
if (!(i < listNum)) return [3 /*break*/, 5];
return [5 /*yield**/, _loop_1(i, listNum)];
case 3:
state_1 = _a.sent();
if (typeof state_1 === "object")
return [2 /*return*/, state_1.value];
_a.sent();
_a.label = 4;
case 4:
i++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/, true];
case 5: return [2 /*return*/];
}
});
});
Expand All @@ -150,14 +141,13 @@ exports.emptyDir = emptyDir;
function put(ftp, src, dest) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
return [2 /*return*/, new Promise(function (resolve, reject) {
ftp.put(src, dest, function (err) {
if (err) {
console.error(err);
resolve(false);
reject(err);
}
else {
resolve(true);
resolve();
}
});
})];
Expand All @@ -169,14 +159,13 @@ function mkDir(ftp, src, recusive) {
if (recusive === void 0) { recusive = true; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
return [2 /*return*/, new Promise(function (resolve, reject) {
ftp.mkdir(src, recusive, function (err) {
if (err) {
console.error(err);
resolve(false);
reject(err);
}
else {
resolve(true);
resolve();
}
});
})];
Expand Down
57 changes: 22 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
import Client from 'ftp'

async function getList(ftp: Client, src: string): Promise<Client.ListingElement[] | false> {
return new Promise(resolve => {
async function getList(ftp: Client, src: string): Promise<Client.ListingElement[]> {
return new Promise((resolve, reject) => {
ftp.list(src, (err, list) => {
if (err) {
console.error(err)

resolve(false)
reject(err)
} else {
resolve(list)
}
})
})
}

async function rmDir(ftp: Client, src: string, recusive = true): Promise<boolean> {
return new Promise(resolve => {
async function rmDir(ftp: Client, src: string, recusive = true): Promise<void> {
return new Promise((resolve, reject) => {
ftp.rmdir(src, recusive, err => {
if (err) {
console.error(err)

resolve(false)
reject(err)
} else {
resolve(true)
resolve()
}
})
})
}

async function deleteFile(ftp: Client, src: string): Promise<boolean> {
return new Promise(resolve => {
async function deleteFile(ftp: Client, src: string): Promise<void> {
return new Promise((resolve, reject) => {
ftp.delete(src, err => {
if (err) {
console.error(err)

resolve(false)
reject(err)
} else {
resolve(true)
resolve()
}
})
})
}

async function emptyDir(ftp: Client, src: string, except: string[] = []): Promise<boolean> {
async function emptyDir(ftp: Client, src: string, except: string[] = []): Promise<void> {
const list = await getList(ftp, src)
if (!list) return false

for (let i = 0, listNum = list.length; i < listNum; i++) {
const { type, name } = list[i]
Expand All @@ -55,38 +48,32 @@ async function emptyDir(ftp: Client, src: string, except: string[] = []): Promis
const fullSrc = `./${name}`

if (type === 'd') {
if (!(await rmDir(ftp, fullSrc, true))) return false
await rmDir(ftp, fullSrc, true)
} else {
if (!(await deleteFile(ftp, fullSrc))) return false
await deleteFile(ftp, fullSrc)
}
}

return true
}

async function put(ftp: Client, src: string, dest: string): Promise<boolean> {
return new Promise(resolve => {
async function put(ftp: Client, src: string, dest: string): Promise<void> {
return new Promise((resolve, reject) => {
ftp.put(src, dest, err => {
if (err) {
console.error(err)

resolve(false)
reject(err)
} else {
resolve(true)
resolve()
}
})
})
}

async function mkDir(ftp: Client, src: string, recusive = true): Promise<boolean> {
return new Promise(resolve => {
async function mkDir(ftp: Client, src: string, recusive = true): Promise<void> {
return new Promise((resolve, reject) => {
ftp.mkdir(src, recusive, err => {
if (err) {
console.error(err)

resolve(false)
reject(err)
} else {
resolve(true)
resolve()
}
})
})
Expand Down
12 changes: 6 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Client from 'ftp';
declare function getList(ftp: Client, src: string): Promise<Client.ListingElement[] | false>;
declare function rmDir(ftp: Client, src: string, recusive?: boolean): Promise<boolean>;
declare function deleteFile(ftp: Client, src: string): Promise<boolean>;
declare function emptyDir(ftp: Client, src: string, except?: string[]): Promise<boolean>;
declare function put(ftp: Client, src: string, dest: string): Promise<boolean>;
declare function mkDir(ftp: Client, src: string, recusive?: boolean): Promise<boolean>;
declare function getList(ftp: Client, src: string): Promise<Client.ListingElement[]>;
declare function rmDir(ftp: Client, src: string, recusive?: boolean): Promise<void>;
declare function deleteFile(ftp: Client, src: string): Promise<void>;
declare function emptyDir(ftp: Client, src: string, except?: string[]): Promise<void>;
declare function put(ftp: Client, src: string, dest: string): Promise<void>;
declare function mkDir(ftp: Client, src: string, recusive?: boolean): Promise<void>;
export { put, rmDir, mkDir, getList, emptyDir, deleteFile };

0 comments on commit a6aa4a7

Please sign in to comment.