UNPKG

3.13 kBJavaScriptView Raw
1'use strict'
2
3const vfs = require('vinyl-fs')
4const Ftp = require('vinyl-ftp')
5const openBrowser = require('react-dev-utils/openBrowser')
6const path = require('path')
7const chalk = require('chalk')
8const config = require('../config')
9const C = require('../config/const')
10const { rootPath } = require('./utils')
11
12const isInteractive = process.stdout.isTTY
13const ftpConf = config.ftp
14const uploadStep = [
15 `${chalk.blue('🌝 [1/2]')} Connecting ${chalk.yellow(config.ftp.host)}...`,
16 `${chalk.blue('🚀 [2/2]')} Uploading package...`,
17 `🎉 ${chalk.green('Success!')}\n`
18]
19
20async function upload(filePath, remotePath) {
21 console.log('------------- Ftp uploading -------------\n')
22 console.log(uploadStep[0])
23 const conn = new Ftp(ftpConf)
24
25 return new Promise((resolve, reject) => {
26 console.log(uploadStep[1])
27
28 vfs.src([filePath], { buffer: false }).pipe(
29 conn
30 .dest(remotePath)
31 .on('end', resolve)
32 .on('error', reject)
33 )
34 })
35}
36
37function getRemotePath({ project, view, namespace, target, version }) {
38 namespace = namespace ? `branch_${namespace}` : ''
39
40 return path.posix.join(
41 '/wap_front/marauder',
42 project,
43 ftpConf.remotePath.version ? version : '',
44 namespace,
45 // 添加构建类型标识,隔离环境
46 target || '',
47 view
48 )
49}
50
51module.exports.uploadVinylFile = async function(vinylFile, remoteFolder) {
52 const conn = new Ftp(ftpConf)
53 const remotePath = path
54 .join('/', remoteFolder, vinylFile.relative)
55 .replace(/\\/g, '/')
56
57 return new Promise((resolve, reject) => {
58 // vinyl-ftp 私有方法,接受一个 Vinyl 文件
59 conn.upload(vinylFile, remotePath, (err, file) => {
60 if (err) {
61 reject(err)
62 } else {
63 resolve(file)
64 }
65 })
66 })
67}
68
69/**
70 * 文件夹上传
71 * @param {object} options
72 * @param {string} options.project 项目名
73 * @param {string} options.view 页面名
74 * @param {string} [options.namespace] 命名空间
75 * @param {string} [options.target] 页面类型
76 * @return {Promise}
77 */
78module.exports.uploadDir = async function(options) {
79 const HOST = 'http://wap_front.dev.sina.cn'
80 const view = `${options.view}/` || ''
81
82 // /wap_front/marauder/hdphoto/1.1.0/wensen/index
83 const remotePath = getRemotePath(options)
84 const localPath = rootPath(`${C.DIST_DIR}/${view}`) + '/**'
85
86 try {
87 await upload(localPath, remotePath)
88 console.log(uploadStep[2])
89
90 const url = HOST + remotePath.replace('/wap_front', '')
91 console.log(`${chalk.yellow.inverse(' URL ')} ${chalk.yellow(url)}\n`)
92
93 ftpConf.openBrowser && isInteractive && openBrowser(url)
94
95 return url
96 } catch (err) {
97 const errMsg =
98 `🌚 ${err}\n` +
99 ' 1) 请检查网络和 VPN 连接\n' +
100 ' 2) 请检查 marauder.config ftp 配置'
101
102 throw new Error(chalk.red(errMsg))
103 }
104}
105
106module.exports.getFile = async function(remotePath) {
107 const conn = new Ftp(ftpConf)
108
109 return new Promise((resolve, reject) => {
110 conn
111 .src(remotePath, { buffer: true })
112 .on('data', file => resolve(file.contents))
113 .on('error', reject)
114 })
115}