forked from serverless-components/website
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverless.js
More file actions
162 lines (128 loc) · 4.74 KB
/
serverless.js
File metadata and controls
162 lines (128 loc) · 4.74 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const aws = require('aws-sdk')
const path = require('path')
const util = require('util')
const types = require('./serverless.types.js')
const exec = util.promisify(require('child_process').exec)
const { Component, utils } = require('@serverless/core')
const {
configureBucketForHosting,
configureDomainForBucket,
configureBucketForRedirect
} = require('./utils')
/*
* Website
*/
class Website extends Component {
/**
* Types
*/
types() { return types }
/*
* Default
*/
async default(inputs = {}) {
this.context.status('Deploying')
this.context.debug(`Starting Website Component.`)
// Default to current working directory
inputs.code = inputs.code || {}
inputs.code.root = inputs.code.root ? path.resolve(inputs.code.root) : process.cwd()
if (inputs.code.src) {
inputs.code.src = path.join(inputs.code.root, inputs.code.src)
}
inputs.region = inputs.region || 'us-east-1'
inputs.bucketName = this.state.bucketName || inputs.bucketName || this.context.resourceId()
this.context.status(`Preparing AWS S3 Bucket`)
this.context.debug(`Preparing website AWS S3 bucket ${inputs.bucketName}.`)
const websiteBucket = await this.load('@serverless/aws-s3', 'websiteBucket')
const bucketOutputs = await websiteBucket({
name: inputs.bucketName,
accelerated: false,
region: inputs.region
})
this.state.bucketName = inputs.bucketName
await this.save()
const s3 = new aws.S3({ region: inputs.region, credentials: this.context.credentials.aws })
this.context.debug(`Configuring bucket ${inputs.bucketName} for website hosting.`)
await configureBucketForHosting(s3, inputs.bucketName)
// Build environment variables
if (inputs.env && Object.keys(inputs.env).length && inputs.code.root) {
this.context.status(`Bundling environment variables`)
this.context.debug(`Bundling website environment variables.`)
let script = 'window.env = {};\n'
inputs.env = inputs.env || {}
for (const e in inputs.env) {
// eslint-disable-line
script += `window.env.${e} = ${JSON.stringify(inputs.env[e])};\n` // eslint-disable-line
}
const envFilePath = path.join(inputs.code.root, 'env.js')
await utils.writeFile(envFilePath, script)
this.context.debug(`Website env written to file ${envFilePath}.`)
}
// If a hook is provided, build the website
if (inputs.code.hook) {
this.context.status('Building assets')
this.context.debug(`Running ${inputs.code.hook} in ${inputs.code.root}.`)
const options = { cwd: inputs.code.root }
try {
await exec(inputs.code.hook, options)
} catch (err) {
console.error(err.stderr) // eslint-disable-line
throw new Error(
`Failed building website via "${inputs.code.hook}" due to the following error: "${err.stderr}"`
)
}
}
this.context.status('Uploading')
const dirToUploadPath = inputs.code.src || inputs.code.root
this.context.debug(
`Uploading website files from ${dirToUploadPath} to bucket ${bucketOutputs.name}.`
)
await websiteBucket.upload({ dir: dirToUploadPath })
this.state.bucketName = inputs.bucketName
this.state.region = inputs.region
this.state.url = `http://${bucketOutputs.name}.s3-website-${inputs.region}.amazonaws.com`
await this.save()
const outputs = {
url: this.state.url,
env: inputs.env || {}
}
// Configure custom domain, if specified
if (inputs.domain) {
const domain = await this.load('@serverless/domain')
const subdomain = inputs.domain.split('.')[0]
const secondLevelDomain = inputs.domain.replace(`${subdomain}.`, '')
const domainInputs = {
domain: secondLevelDomain,
subdomains: {}
}
domainInputs.subdomains[subdomain] = { url: this.state.url }
const domainOutputs = await domain(domainInputs)
outputs.domain = domainOutputs.domains[0]
this.state.domain = outputs.domain
await this.save()
}
this.context.debug(`Website deployed successfully to URL: ${this.state.url}.`)
return outputs
}
/**
* Remove
*/
async remove() {
this.context.status(`Removing`)
this.context.debug(`Starting Website Removal.`)
this.context.debug(`Removing Website bucket.`)
const websiteBucket = await this.load('@serverless/aws-s3', 'websiteBucket')
await websiteBucket.remove()
// Remove custom domain, if specified
if (this.state.domain) {
this.context.debug(`Removing custom domain.`)
const domain = await this.load('@serverless/domain')
await domain.remove()
}
this.state = {}
await this.save()
this.context.debug(`Finished Website Removal.`)
return {}
}
}
module.exports = Website