UNPKG

webpack-papa-script

Version:
116 lines (105 loc) 3.07 kB
/** * Created by Nonjene on 2017/3/1. */ const path = require('path'); const chalk = require('chalk'); const { getConf, ftp: FtpConf } = require('./config'); const fileManager = require('./file_manager'); const {webify} = require('./util/webPath'); const Client = require('ftp'); const c = new Client(); module.exports = { uploadToServer(opt) { let target = getConf('target'), duan = getConf('duan'); /* istanbul ignore if */ if (!target || !duan) throw new Error('没有配置活动'); let promiseUpload = this.signin(FtpConf).then( () => new Promise(resolve => fileManager.getAssetsFiles(target, duan, (allFilesInfo, remoteLink) => this.upload(opt, allFilesInfo, () => resolve(remoteLink)) ) ) ); promiseUpload .then(() => this.end()) .catch(e => { /* istanbul ignore next */ console.error(e) }); return promiseUpload; }, uploadStatic(target, opt) { let promiseUpload = this.signin(FtpConf).then( () => new Promise(resolve => fileManager.getStaticFiles(target, (allFilesInfo, remoteLink) => this.upload(opt, allFilesInfo, () => resolve(remoteLink)) ) ) ); promiseUpload .then(() => this.end()) .catch(e => { /* istanbul ignore next */ console.error(e) }); return promiseUpload; }, upload(opt, filesInfo, resolve) { fileManager.upLoadFiles( opt, filesInfo, (remoteFullPath, localFullPath, next) => { const remoteDir = webify(path.dirname(remoteFullPath)); this.mkdir(remoteDir) .then(() => this.putFile(remoteFullPath, localFullPath)) .then(() => next()) .catch(/* istanbul ignore next */ function(e) { console.log(chalk.yellow(e)); return next(e); }); }, resolve ); }, //注意坑 一次运行只能登入一个 signin(Ftp_config) { /* istanbul ignore if */ if (this.promiseSignin) return this.promiseSignin; this.promiseSignin = new Promise((resolve, reject) => { c.on('ready', resolve); c.on('error', reject); c.connect(Ftp_config); }); return this.promiseSignin; }, mkdir(ftpDir) { return new Promise((resolve, reject) => { c.mkdir(ftpDir, true, function(err) { /* istanbul ignore if */ if (err) return reject('创建目录失败:' + ftpDir); resolve(); }); }); }, //note:api没批量功能 putFile(remoteFullPath, localFullPath) { return new Promise((resolve, reject) => { c.put(localFullPath, remoteFullPath, function(err) { /* istanbul ignore if */ if (err) return reject( '上传错误:\n' + localFullPath + ' 无法上传到 ' + remoteFullPath ); resolve(); }); }); }, ls() { /* istanbul ignore next */ c.list(function(err, list) { if (err) throw err; console.dir(list); c.end(); }); }, end() { c.end(); } };