UNPKG

@juangm/samba-client

Version:
117 lines (116 loc) 4.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const util_1 = require("util"); const path_1 = require("path"); const singleSlash = /\//g; const wrap = (str) => `'${str}'`; class SambaClient { constructor(config) { this.configSamba = { address: config.address, username: wrap(config.username || 'guest'), password: config.password ? wrap(config.password) : undefined, domain: config.domain, path: config.path, others: config.others, }; } runCommand(cmd, path, destination) { return __awaiter(this, void 0, void 0, function* () { const workingDir = path_1.dirname(path); const fileName = path_1.basename(path).replace(singleSlash, '\\'); const cmdArgs = util_1.format('%s %s', fileName, destination); return yield this.execute(cmd, cmdArgs, workingDir); }); } execute(cmd, cmdArgs, workingDir) { const fullCmd = wrap(util_1.format('%s %s', cmd, cmdArgs)); const command = ['smbclient', this.getSmbClientArgs(fullCmd).join(' ')].join(' '); const options = { cwd: workingDir }; return new Promise((done, failed) => { child_process_1.exec(command, options, (err, stdout, stderr) => { const allMessage = stdout + stderr; if (err) { console.log('An error occurred: ' + allMessage); failed(err); } else { done(allMessage); } }); }); } getSmbClientArgs(fullCmd) { let args = ['-U', this.configSamba.username]; if (!this.configSamba.password) { args.push('-N'); } args.push('-c', fullCmd, this.configSamba.address); if (this.configSamba.password) { args.push(this.configSamba.password); } if (this.configSamba.domain) { args.push('-W'); args.push(this.configSamba.domain); } if (this.configSamba.path) { args.push('-D'); args.push(this.configSamba.path); } if (this.configSamba.others) { args.push(this.configSamba.others); } return args; } getFile(path, destination) { return __awaiter(this, void 0, void 0, function* () { return yield this.runCommand('get', path, destination); }); } sendFile(path, destination) { return __awaiter(this, void 0, void 0, function* () { const consoleLog = yield this.runCommand('put', path, destination.replace(singleSlash, '\\')); console.log('File was upload successfully!!'); return consoleLog; }); } deleteFile(fileName) { return __awaiter(this, void 0, void 0, function* () { const consoleLog = yield this.execute('del', fileName, ''); console.log('File was delete successfully!!'); return consoleLog; }); } listFiles(fileNamePrefix, fileNameSuffix) { return __awaiter(this, void 0, void 0, function* () { const cmdArgs = util_1.format('%s*%s', fileNamePrefix, fileNameSuffix); return yield this.execute('dir', cmdArgs, ''); }); } mkdir(remotePath) { return __awaiter(this, void 0, void 0, function* () { return yield this.execute('mkdir', remotePath.replace(singleSlash, '\\'), __dirname); }); } dir(remotePath) { return __awaiter(this, void 0, void 0, function* () { return yield this.execute('dir', remotePath.replace(singleSlash, '\\'), __dirname); }); } customCommand(cmd) { return __awaiter(this, void 0, void 0, function* () { return yield this.execute(cmd, '', __dirname); }); } } exports.SambaClient = SambaClient;