1 | 'use strict'
|
2 |
|
3 | const vfs = require('vinyl-fs')
|
4 | const Ftp = require('vinyl-ftp')
|
5 | const openBrowser = require('react-dev-utils/openBrowser')
|
6 | const path = require('path')
|
7 | const chalk = require('chalk')
|
8 | const config = require('../config')
|
9 | const C = require('../config/const')
|
10 | const { rootPath } = require('./utils')
|
11 |
|
12 | const isInteractive = process.stdout.isTTY
|
13 | const ftpConf = config.ftp
|
14 | const 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 |
|
20 | async 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 |
|
37 | function 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 |
|
51 | module.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 |
|
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 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | module.exports.uploadDir = async function(options) {
|
79 | const HOST = 'http://wap_front.dev.sina.cn'
|
80 | const view = `${options.view}/` || ''
|
81 |
|
82 |
|
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 |
|
106 | module.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 | }
|