diff --git a/lib/index.js b/lib/index.js index 703ba76..355825c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); @@ -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(); } }); })]; @@ -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(); } }); })]; @@ -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) { @@ -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*/]; } @@ -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*/]; } }); }); @@ -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(); } }); })]; @@ -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(); } }); })]; diff --git a/src/index.ts b/src/index.ts index 2c1dc75..af5816e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,10 @@ import Client from 'ftp' -async function getList(ftp: Client, src: string): Promise { - return new Promise(resolve => { +async function getList(ftp: Client, src: string): Promise { + return new Promise((resolve, reject) => { ftp.list(src, (err, list) => { if (err) { - console.error(err) - - resolve(false) + reject(err) } else { resolve(list) } @@ -14,37 +12,32 @@ async function getList(ftp: Client, src: string): Promise { - return new Promise(resolve => { +async function rmDir(ftp: Client, src: string, recusive = true): Promise { + 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 { - return new Promise(resolve => { +async function deleteFile(ftp: Client, src: string): Promise { + 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 { +async function emptyDir(ftp: Client, src: string, except: string[] = []): Promise { 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] @@ -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 { - return new Promise(resolve => { +async function put(ftp: Client, src: string, dest: string): Promise { + 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 { - return new Promise(resolve => { +async function mkDir(ftp: Client, src: string, recusive = true): Promise { + return new Promise((resolve, reject) => { ftp.mkdir(src, recusive, err => { if (err) { - console.error(err) - - resolve(false) + reject(err) } else { - resolve(true) + resolve() } }) }) diff --git a/types/index.d.ts b/types/index.d.ts index 4a1b5ca..a36a8cf 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,8 +1,8 @@ import Client from 'ftp'; -declare function getList(ftp: Client, src: string): Promise; -declare function rmDir(ftp: Client, src: string, recusive?: boolean): Promise; -declare function deleteFile(ftp: Client, src: string): Promise; -declare function emptyDir(ftp: Client, src: string, except?: string[]): Promise; -declare function put(ftp: Client, src: string, dest: string): Promise; -declare function mkDir(ftp: Client, src: string, recusive?: boolean): Promise; +declare function getList(ftp: Client, src: string): Promise; +declare function rmDir(ftp: Client, src: string, recusive?: boolean): Promise; +declare function deleteFile(ftp: Client, src: string): Promise; +declare function emptyDir(ftp: Client, src: string, except?: string[]): Promise; +declare function put(ftp: Client, src: string, dest: string): Promise; +declare function mkDir(ftp: Client, src: string, recusive?: boolean): Promise; export { put, rmDir, mkDir, getList, emptyDir, deleteFile };