1 | const fs = require('fs-extra')
|
2 | const { join } = require('path')
|
3 | const inquirer = require('inquirer')
|
4 | const { shell, shellSync } = require('execa')
|
5 | const getSpinner = require('../utils/getSpinner')
|
6 | const { success, warning } = require('../utils/tip')
|
7 | const { isFireProject, checkFireVersion } = require('../utils/fire')
|
8 | const { __noble, __root } = require('../utils/paths')
|
9 | const { page } = require('../utils/getFireConfig')()
|
10 | const install = require('../utils/install')
|
11 | const opn = require('opn')
|
12 | const glob = require('glob')
|
13 | function getProjectName() {
|
14 | let projectName = (
|
15 | require(join(process.cwd(), 'package.json')).name || ''
|
16 | ).trim()
|
17 | if (projectName.indexOf('static-') === 0) {
|
18 | projectName = projectName.substring(7)
|
19 | }
|
20 | return projectName
|
21 | }
|
22 | function getIndexPage() {
|
23 | return (
|
24 | (typeof page === 'string' && page.trim() === '*'
|
25 | ? glob
|
26 | .sync('./page/*.html', { cwd: __root })
|
27 | .map(name => name.slice(7, name.length - 5))
|
28 | : page || []
|
29 | ).filter(item => item !== '__BASE__')[0] || 'example'
|
30 | )
|
31 | }
|
32 | function isIp(ip) {
|
33 | if (typeof ip !== 'string') {
|
34 | return false
|
35 | }
|
36 | const ipArr = ip.split('.').map(i => {
|
37 | if (+i === 0) {
|
38 | return 0
|
39 | }
|
40 | return +i || -1
|
41 | })
|
42 | if (ipArr.length !== 4 || ipArr[0] < 1 || ipArr[0] > 255) {
|
43 | return false
|
44 | }
|
45 | return !ipArr.slice(1).some(item => item < 0 || item > 255)
|
46 | }
|
47 |
|
48 | function canDeployAtCurrBranch() {
|
49 | const { stdout } = shellSync(
|
50 | `cd ${__root} && git symbolic-ref --short -q HEAD`
|
51 | )
|
52 | return stdout !== 'deploy' && stdout !== 'master'
|
53 | }
|
54 | module.exports = async function() {
|
55 | const nobleSpinner = getSpinner('deploying...')
|
56 | try {
|
57 | if (!isFireProject()) {
|
58 | return warning('This is not a fire project')
|
59 | }
|
60 | if (!canDeployAtCurrBranch()) {
|
61 | return warning('你不能在master或者deploy分支上进行此操作')
|
62 | }
|
63 | let { hasMerge = 'yes' } = await inquirer.prompt([
|
64 | {
|
65 | type: 'input',
|
66 | message: '部署前请先确认是否已经将最新的master代码合并过来了?',
|
67 | name: 'hasMerge',
|
68 | default: 'yes'
|
69 | }
|
70 | ])
|
71 | hasMerge = hasMerge.trim().toLowerCase()
|
72 | if (hasMerge !== 'y' && hasMerge !== 'yes') {
|
73 | return warning('请先合并代码,再执行此操作')
|
74 | }
|
75 | let serverIp = process.argv[3] || ''
|
76 | if (!serverIp.trim()) {
|
77 | let { stdout = '' } = await shell('git config user.name')
|
78 | if (stdout.indexOf('@2dfire.com') > 0) {
|
79 | stdout = stdout.substring(0, stdout.indexOf('@2dfire.com'))
|
80 | }
|
81 | const ipPath = join(__noble, stdout + '.js')
|
82 | if (fs.pathExistsSync(ipPath)) {
|
83 | serverIp = (require(ipPath) || {}).serverIp
|
84 | }
|
85 | if (!serverIp) {
|
86 | const { ip } = await inquirer.prompt([
|
87 | {
|
88 | type: 'input',
|
89 | message: 'You server ip:',
|
90 | name: 'ip',
|
91 | validate(val) {
|
92 | return val.trim() ? true : 'You must provide a server ip'
|
93 | }
|
94 | }
|
95 | ])
|
96 | serverIp = ip
|
97 | }
|
98 | }
|
99 | if (!isIp(serverIp)) {
|
100 | return warning('the ip is invalid')
|
101 | }
|
102 | nobleSpinner.start()
|
103 | const fireVer = checkFireVersion()
|
104 | if (fireVer) {
|
105 | const { latestVer } = fireVer || {}
|
106 | return warning(
|
107 | `The latest version is ${latestVer}, you must update the project config : 'fire update'`,
|
108 | true
|
109 | )
|
110 | }
|
111 |
|
112 | const { stdout } = await shell('git status --short')
|
113 | await shell('git add .')
|
114 | if (stdout) {
|
115 | await shell('git commit -m noble && git pull && git push origin')
|
116 | }
|
117 | await shell('node node_modules/fire-scripts/bin/index build NODE_ENV=dev')
|
118 | nobleSpinner.stop()
|
119 | const projectName = getProjectName()
|
120 | await shell(join(__dirname, `noble.sh ${serverIp} ${projectName}`))
|
121 | success(`\n√ deploy completed!`)
|
122 | opn(`http://${serverIp}/nginx/${projectName}/page/${getIndexPage()}.html`)
|
123 | } catch (e) {
|
124 | nobleSpinner.stop()
|
125 | console.log(e)
|
126 | }
|
127 | }
|