-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
184 lines (159 loc) · 5.55 KB
/
index.js
File metadata and controls
184 lines (159 loc) · 5.55 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const request = require('request')
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const AWS = require('aws-sdk')
AWS.config.region = process.env.AWS_REGION
const s3 = new AWS.S3()
const mdbuild = require('woola')
const appconfig = require('./node_modules/woola/lib/appconfig.js')
const ghtoken = process.env.GITHUB_ACCESS_TOKEN
const localStore = process.env.LOCAL_DIR
// const localStore = path.resolve('/tmp')
// const globPattern = '**/*'
let site
const computeContentType = (filename) => {
const parts = filename.split('.')
switch (filename.split('.')[parts.length-1]) {
case 'png':
return 'image/png'
case 'jpg':
return 'image/jpg'
case 'html':
return 'text/html'
case 'js':
return 'application/javascript'
case 'css':
return 'text/css'
}
}
const uploadStream = (file,s3bucket,srcPath='.') => {
const fileStream = fs.createReadStream(path.join(path.resolve(srcPath),file))
fileStream.on('error', function(err) {
console.log('File Error', err)
})
const uploadParams = {
Bucket: s3bucket,
Body: fileStream,
Key: file,
ContentType: computeContentType(file),
CacheControl: 'max-age=60'
}
s3.upload (uploadParams, function (err, data) {
if (err) {
console.log("Error", err)
// reject(err)
} if (data) {
console.log("Upload Success", data.Location)
// resolve(data)
}
})
}
console.log('Loading lambdaBuildBot, current dir: ' + process.cwd())
exports.handler = function (event, context) {
// console.log('Received event:', JSON.stringify(event, null, 2))
const message = event.Records[0].Sns.Message
const mobj = JSON.parse(message)
// console.log(mobj)
const downloadsUrl = mobj.repository.trees_url.replace('{/sha}', '/master?recursive=1');
const repoDirname = localStore
const reqHeaders1 = {
'Authorization': 'token ' + ghtoken,
'User-Agent': 'ghlambdabot'
}
const reqHeaders2 = {
'Authorization': 'token ' + ghtoken,
'User-Agent': 'ghlambdabot',
'Accept': 'application/vnd.github-blob.raw'
}
console.log(`'Requesting: '${downloadsUrl}`)
const stream = request({url: downloadsUrl, headers: reqHeaders1}, (error, response, body) => {
if (error) {
console.log(error)
} else if (response && response.statusCode && !response.statusCode.toString().startsWith('2')) {
console.log(`GitHub API request failed with status ${response.statusCode}`)
} else {
// console.log('message: ' + 'success')
const bodyObj = JSON.parse(body)
console.log(bodyObj)
if (bodyObj.truncated) {
console.log('Too many files for the GitHub tree api, try another portion of api like contents, archive_url, or clone repo')
} else {
const streamPromises = bodyObj.tree.map((fileObject) => {
return new Promise((resolve, reject) => {
if (fileObject.type === 'blob') {
// console.log(fileObject.path)
writeStream(fileObject,resolve,reject)
} else if (fileObject.type === 'tree') {
resolve( fs.mkdir(localStore + '/' + fileObject.path, () => {console.log(`make dir: ${fileObject.path}`)}) )
} else {
console.log('unknown object type returned from GitHub tree api response body')
}
})
})
Promise.all(streamPromises)
.then(() => { console.log('All input streams completed.') })
.then(build)
.catch (err => { console.log(err) })
}
}
})
function writeStream(fileObject,resolve,reject) {
request({url: fileObject.url, headers: reqHeaders2})
.pipe(fs.createWriteStream(localStore + '/' + fileObject.path))
.on('finish', resolve)
.on('error', reject)
}
function getConfig(flags={config: localStore + '/config.js'}, callback) {
site = appconfig(flags)
// TODO: global let bug for site.options.srcPath in appconfig.js upon hot start invocation, tmp fix follows
// site.options.srcPath = path.join(localStore, site.options.srcPath) //source directory
site.options.srcPath = localStore + '/src' //source directory
site.options.dstPath = localStore + '/dist' // destination directory for the distribution
site.options.globPattern = '**/*'
if (typeof callback === 'function') {
callback(site)
} else {
return site
}
}
function readdir (site, callback) {
glob(site.options.globPattern, {cwd: site.options.dstPath, nodir: true}, (err, files) => {
if (err) console.log(err)
else {
// console.log('str: ' + str)
if (typeof callback === 'function') {
callback(site, files)
} else {
console.log(files)
return files
}
}
})
}
function uploadFiles (site, files) {
// TODO: add cache.replace(/[0-9]+/,300) need to change uploadParams location
files.forEach(file => {
// TODO: add other endpoint options with logic testing for S3 vs other services
uploadStream(file, site.s3_bucket, site.options.dstPath)
})
}
const build = function () {
// console.log(`'Saving: '${repoDirname}`)
if (fs.existsSync(localStore)) {
getConfig({config: path.resolve(localStore + '/config.js')}, () => {
mdbuild(site,() => {
readdir(site, uploadFiles)
})
})
} else {
console.log(repoDirname + ' does not exist')
}
}
if (mobj.ref === "refs/heads/master") {
stream
// build() //testing
} else {
console.log(`Exiting without build/deploy... input branch was ${mobj.ref} instead of master`)
}
}