1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const md5 = require('md5')
|
5 | const Vinyl = require('vinyl')
|
6 | const chalk = require('chalk')
|
7 | const axios = require('axios')
|
8 | const path = require('path')
|
9 | const execa = require('execa')
|
10 | const config = require('../../config')
|
11 | const { getFile, uploadVinylFile } = require('../ftp')
|
12 | const { rootPath, buffer2String } = require('../utils')
|
13 | const maraConf = require(config.paths.marauder)
|
14 | const CONF_DIR = '/wap_front/hybrid/config/'
|
15 | const CONF_NAME = getHbConfName(maraConf)
|
16 | const CONF_PATH = `${CONF_DIR}/${CONF_NAME}`
|
17 | const CONF_URL = `http://wap_front.dev.sina.cn/hybrid/config/${CONF_NAME}`
|
18 |
|
19 | const publishStep = [
|
20 | `${chalk.blue('🐝 [1/4]')} Fetching config...`,
|
21 |
|
22 | `${chalk.blue('✏️ [2/4]')} Updating config...`,
|
23 | `${chalk.blue('🚀 [3/4]')} Pushing config...`,
|
24 | `${chalk.blue('🎉 [4/4]')} ${chalk.green('Success')}\n`
|
25 | ]
|
26 |
|
27 | function getHbConfName(config) {
|
28 | const confName =
|
29 | (config && config.ciConfig && config.ciConfig.zip_config_name) ||
|
30 | 'sina_news'
|
31 | return `${confName}.json`
|
32 | }
|
33 |
|
34 | async function updateRemoteHbConf(hbConf) {
|
35 |
|
36 | const confFile = new Vinyl({
|
37 | path: rootPath(CONF_NAME),
|
38 | contents: Buffer.from(JSON.stringify(hbConf))
|
39 | })
|
40 |
|
41 | try {
|
42 | await uploadVinylFile(confFile, CONF_DIR)
|
43 | } catch (e) {
|
44 | console.log('Hybrid config 上传失败')
|
45 | throw new Error(e)
|
46 | }
|
47 | }
|
48 |
|
49 | async function getGitRepoName() {
|
50 | try {
|
51 | const { stdout: remoteUrl } = await execa('git', [
|
52 | 'config',
|
53 | '--get',
|
54 | 'remote.origin.url'
|
55 | ])
|
56 |
|
57 | return path.basename(remoteUrl, '.git')
|
58 | } catch (e) {
|
59 | error(e)
|
60 | }
|
61 |
|
62 | function error(e) {
|
63 | console.log('获取git工程名失败,请检查是否设置远程git仓库')
|
64 | throw new Error(e)
|
65 | }
|
66 | }
|
67 |
|
68 | async function getHbConf(confPath) {
|
69 | try {
|
70 | const hbConf = await axios(confPath)
|
71 | const initConf = {
|
72 | status: 0,
|
73 | reqTime: Date.now(),
|
74 | data: {
|
75 | modules: []
|
76 | }
|
77 | }
|
78 |
|
79 | return hbConf.data || initConf
|
80 | } catch (e) {
|
81 | console.log(`请检查网络或联系管理员`)
|
82 | throw new Error(e)
|
83 | }
|
84 | }
|
85 |
|
86 | function logResult(hbMod) {
|
87 | console.log(hbMod)
|
88 | console.log(`\n${chalk.bgYellow(' CONF ')} ${chalk.yellow(CONF_URL)}\n`)
|
89 | }
|
90 |
|
91 | module.exports = async function(entry, remotePath) {
|
92 | console.log('----------- Hybrid Publish Dev -----------\n')
|
93 | console.log(publishStep[0])
|
94 |
|
95 | const hbConf = await getHbConf(CONF_URL)
|
96 | const repoName = await getGitRepoName()
|
97 | const moduleName = `${repoName}/${entry}`
|
98 | const localPkgPath = rootPath(`dist/${entry}/${entry}.php`)
|
99 | const manifest = rootPath(`dist/${entry}/${entry}.php`)
|
100 | const moduleIdx = hbConf.data.modules.findIndex(
|
101 | item => item.name === moduleName
|
102 | )
|
103 | let gkList = []
|
104 | let qeList = []
|
105 | let rank = 5
|
106 |
|
107 | try {
|
108 | const manifest = require(rootPath(`src/view/${entry}/public/manifest.json`))
|
109 |
|
110 | gkList = manifest.display.gkTestIds || []
|
111 | qeList = manifest.display.qeTestIds || []
|
112 | rank = manifest.rank || 5
|
113 | } catch (e) {}
|
114 |
|
115 | const hbMod = {
|
116 | name: moduleName,
|
117 | version: process.env.npm_package_version,
|
118 | pkg_url: `${remotePath + entry}.php`,
|
119 | hybrid: true,
|
120 | md5: md5(fs.readFileSync(localPkgPath)),
|
121 | gkList,
|
122 | qeList,
|
123 | rank
|
124 | }
|
125 |
|
126 | console.log(publishStep[1])
|
127 | if (moduleIdx > -1) {
|
128 | hbConf.data.modules[moduleIdx] = hbMod
|
129 | } else {
|
130 | hbConf.data.modules.push(hbMod)
|
131 | }
|
132 |
|
133 | console.log(publishStep[2])
|
134 | await updateRemoteHbConf(hbConf)
|
135 | console.log(publishStep[3])
|
136 |
|
137 | logResult(hbMod)
|
138 | }
|