|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('path'); |
| 4 | +var join = path.join; |
| 5 | +var dirname = path.dirname; |
| 6 | +var Client = require('scp2').Client; |
| 7 | +var through = require('through2'); |
| 8 | +var debug = require('debug')('gulp-scp2'); |
| 9 | +var PluginError = require('gulp-util').PluginError; |
| 10 | + |
| 11 | +module.exports = function(options) { |
| 12 | + options || (options = {}); |
| 13 | + options.host = options.host || 'localhost'; |
| 14 | + options.username = options.username || 'admin'; |
| 15 | + options.dest = options.dest || '/home/' + options.username; |
| 16 | + |
| 17 | + var client = createClient(options); |
| 18 | + |
| 19 | + return through.obj(function transform(file, enc, callback) { |
| 20 | + if (file.isStream()) return callback(new PluginError('gulp-scp2', 'Streaming not supported.')); |
| 21 | + |
| 22 | + var path = join(options.dest, file.relative); |
| 23 | + client.mkdir(dirname(path), function() { |
| 24 | + client.write({ |
| 25 | + destination: path, |
| 26 | + content: file.contents |
| 27 | + }, callback); |
| 28 | + }); |
| 29 | + }, function flush(callback) { |
| 30 | + client.close(); |
| 31 | + callback(); |
| 32 | + }); |
| 33 | +}; |
| 34 | + |
| 35 | +function createClient(options) { |
| 36 | + var client = new Client(options); |
| 37 | + client.on('connect', function() { |
| 38 | + debug('ssh connect %s', options.host); |
| 39 | + }); |
| 40 | + client.on('close', function() { |
| 41 | + debug('ssh connect %s', options.host); |
| 42 | + }); |
| 43 | + client.on('mkdir', function(dir) { |
| 44 | + debug('mkdir %s', dir); |
| 45 | + }); |
| 46 | + client.on('write', function(o) { |
| 47 | + debug('write %s', o.destination); |
| 48 | + }); |
| 49 | + client.on('error', function(err) { |
| 50 | + debug('error %s', err); |
| 51 | + }); |
| 52 | + return client; |
| 53 | +} |
0 commit comments