├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── utils ├── file.js └── sftp.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | .idea 3 | *.log 4 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ali-sdk and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##sftp-webpack-plugin 2 | webpack的插件,用于上传文件到指定服务器。 3 | 4 | Installation 5 | ------------ 6 | Install the plugin with npm: 7 | ```shell 8 | $ npm install sftp-webpack-plugin --save-dev 9 | ``` 10 | 11 | Basic Usage 12 | ----------- 13 | 14 | add the plugin to your webpack config as follows: 15 | 16 | ```javascript 17 | var SftpWebpackPlugin = require('sftp-webpack-plugin') 18 | var webpackConfig = { 19 | entry: 'index.js', 20 | output: { 21 | path: 'dist', 22 | filename: 'index_bundle.js' 23 | }, 24 | plugins: [new SftpWebpackPlugin({ 25 | port: 'your port', 26 | host: 'your host', 27 | username: 'your username', 28 | password: 'your password', 29 | from: 'you neeed upload file path ', 30 | to: 'you want to destination' 31 | })] 32 | } 33 | ``` 34 | 35 | Configuration 36 | ------------- 37 | The plugin allowed values are as follows: 38 | 39 | - `port`: 服务器端口。 40 | - `host`: 服务器地址。 41 | - `username`: 服务器登陆用户名。 42 | - `password`: 服务器登陆密码。 43 | - `from`: 你需要上传的文件路径或者文件,其中对于文件,可以是文件路径的数组。 44 | - `to`: 服务器上的目标路径。 -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | var fs = require('fs') 3 | var colors = require('colors') 4 | var fileUtils = require('./utils/file') 5 | var client = require('./utils/sftp') 6 | 7 | function SftpWebpackPlugin(options) { 8 | if (!options || !options.port || !options.host || !options.username || !options.password || !options.from || !options.to) { 9 | console.info('Some parameters of the problem,please set as the follow:') 10 | console.info(" new".red + " SftpWebpackPlugin({"); 11 | console.info(" port:'your port',".yellow); 12 | console.info(" host: 'your host',".yellow); 13 | console.info(" username: 'your username',".yellow); 14 | console.info(" password: 'your password',".yellow); 15 | console.info(" from: 'you neeed upload file path',".yellow); 16 | console.info(" to: 'you want to destination',".yellow); 17 | console.info(" })"); 18 | throw new Error('Some parameters of the problem') 19 | } 20 | this.fileArray = []; 21 | this.options = options; 22 | } 23 | 24 | SftpWebpackPlugin.prototype.apply = function(compiler) { 25 | var _this = this; 26 | compiler.plugin("done", function(compilation) { 27 | _this.sftp(); 28 | }); 29 | }; 30 | 31 | SftpWebpackPlugin.prototype.sftp = function() { 32 | client.sftp(this.options.port, this.options.host, this.options.username, this.options.password, this.options.from, this.options.to) 33 | } 34 | 35 | module.exports = SftpWebpackPlugin; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sftp-webpack-plugin", 3 | "version": "0.2.3", 4 | "main": "index.js", 5 | "description": "it can allow the content of a folder upload to a remote server", 6 | "author": "hades", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:iAmHades/sftp-webpack-plugin.git" 11 | }, 12 | "keywords": [ 13 | "ssh", 14 | "sftp", 15 | "webpack", 16 | "plugin", 17 | "deplay", 18 | "aliyun" 19 | ], 20 | "scripts": {}, 21 | "dependencies": { 22 | "scp2": "0.2.2", 23 | "colors": "1.1.2", 24 | "async-waterfall": "0.1.5" 25 | } 26 | } -------------------------------------------------------------------------------- /utils/file.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | 5 | exports.eachFileSync = function(dir, callback) { 6 | var stats = fs.statSync(dir); 7 | // 遍历子目录 8 | if (stats.isDirectory()) { 9 | var files = exports.fullPath(dir, fs.readdirSync(dir)); 10 | files.forEach(function(f) { 11 | exports.eachFileSync(f, callback); 12 | }); 13 | } else { 14 | callback(dir, stats); 15 | } 16 | } 17 | 18 | exports.fullPath = function(dir, files) { 19 | return files.map(function(f) { 20 | return path.join(dir, f); 21 | }); 22 | } -------------------------------------------------------------------------------- /utils/sftp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var client = require('scp2'); 4 | var waterfall = require('async-waterfall'); 5 | var fileUtils = require('./file'); 6 | var fs = require('fs'); 7 | var colors = require('colors') 8 | 9 | exports.sftp = function(port, host, username, password, from, to) { 10 | client.defaults({ 11 | port: port, 12 | host: host, 13 | username: username, 14 | password: password 15 | }); 16 | var queue = [] 17 | if (from && Array.isArray(from)) { 18 | from.forEach(function(f) { 19 | putQueue(f, to, queue, client); 20 | }) 21 | } else { 22 | putQueue(from, to, queue, client); 23 | } 24 | queue.push( 25 | function(callback) { 26 | client.close() 27 | callback() 28 | } 29 | ) 30 | waterfall(queue, function(err, result) { 31 | if (err) { 32 | console.info(err) 33 | } else { 34 | if (from && Array.isArray(from)) { 35 | from.forEach(function(f) { 36 | console.info("Files ( ".yellow + f + " ) uploaded".yellow) 37 | }) 38 | } else { 39 | console.info("Files ( ".yellow + from + " ) uploaded".yellow) 40 | } 41 | } 42 | }) 43 | 44 | 45 | function putQueue(from, to, queue, client) { 46 | var stats = fs.statSync(from) 47 | if (stats.isDirectory()) { 48 | fileUtils.eachFileSync(from, function(filename, stats) { 49 | queue.push( 50 | function(callback) { 51 | client.upload(filename, to, function(err) { 52 | callback() 53 | }) 54 | } 55 | ) 56 | }); 57 | } else { 58 | queue.push( 59 | function(callback) { 60 | client.upload(from, to, function(err) { 61 | callback() 62 | }) 63 | } 64 | ) 65 | } 66 | } 67 | 68 | } --------------------------------------------------------------------------------