forked from cypress-io/cypress-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
146 lines (118 loc) · 3.63 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* eslint-disable no-console */
const path = require('path')
const chalk = require('chalk')
const Promise = require('bluebird')
const inquirer = require('inquirer')
const minimist = require('minimist')
const debug = require('debug')('deploy')
const questionsRemain = require('@cypress/questions-remain')
const scrape = require('./scrape')
const { shouldDeploy } = require('./should-deploy')
const R = require('ramda')
const la = require('lazy-ass')
const is = require('check-more-types')
const {
warnIfNotCI,
getDeployEnvironment,
checkBranchEnvFolder,
uploadToS3,
getBranch,
} = require('@cypress/deploy-bits')
const distDir = path.resolve('public')
const isValidEnvironment = is.oneOf(['production', 'staging'])
function cliOrAsk (property, ask, minimistOptions) {
// for now isolate the CLI/question logic
const askRemaining = questionsRemain({
[property]: ask,
})
const options = minimist(process.argv.slice(2), minimistOptions)
return askRemaining(options).then(R.prop(property))
}
function prompt (questions) {
return Promise.resolve(inquirer.prompt(questions))
}
function promptToScrape () {
return prompt({
type: 'list',
name: 'scrape',
message: 'Would you like to scrape the docs? (You only need to do this if they have changed on this deployment)',
choices: [
{ name: 'Yes', value: true },
{ name: 'No', value: false },
],
})
.get('scrape')
}
const getScrapeDocs = R.partial(cliOrAsk,
['scrape', promptToScrape, { boolean: 'scrape' }])
function scrapeDocs (env, branch) {
debug('scraping documentation')
console.log('')
// if we aren't on master do nothing
if (branch !== 'master') {
console.log('Skipping doc scraping because you are not on branch:', chalk.cyan('master'))
return
}
// if we arent deploying to production return
if (env !== 'production') {
console.log('Skipping doc scraping because you deployed to:', chalk.cyan(env))
console.log('Only scraping production deploy')
return
}
return getScrapeDocs()
.then((bool) => {
if (bool) {
return scrape()
}
})
}
function deployEnvironmentBranch (env, branch) {
la(is.unemptyString(branch), 'missing branch to deploy', branch)
la(isValidEnvironment(env), 'invalid deploy environment', env)
debug('checking branch %s for environment %s', branch, env)
checkBranchEnvFolder(branch)(env)
debug('uploading to S3 dist folder %s', distDir)
return uploadToS3(distDir, env)
.then(() => scrapeDocs(env, branch))
.then(() => {
console.log(chalk.yellow('\n==============================\n'))
console.log(chalk.bgGreen(chalk.black(' Done Deploying ')))
console.log('')
})
}
function doDeploy (env) {
la(isValidEnvironment(env), 'invalid deploy environment', env)
debug('getting current branch')
return getBranch()
.then((branch) => {
console.log('deploying branch %s to environment %s',
chalk.green(branch), chalk.blue(env))
la(is.unemptyString(branch), 'invalid branch name', branch)
return deployEnvironmentBranch(env, branch)
})
}
function deploy () {
console.log()
console.log(chalk.yellow('Cypress Docs Deploy'))
console.log(chalk.yellow('==============================\n'))
warnIfNotCI()
return getDeployEnvironment()
.then((env) => {
return shouldDeploy(env)
.then((should) => {
if (!should) {
console.log('should NOT deploy to environment %s', env)
return false
}
console.log('should deploy to environment %s', env)
return doDeploy(env)
})
})
}
deploy()
.catch((err) => {
console.error('🔥 deploy failed')
console.error(err)
console.error(err.stack)
process.exit(-1)
})