Skip to content

Commit 3bc1ac5

Browse files
author
tcl89
committed
refac main to deployPublishAmazon/Google function
1 parent 3fb085d commit 3bc1ac5

File tree

1 file changed

+172
-102
lines changed

1 file changed

+172
-102
lines changed

index.js

Lines changed: 172 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,146 @@ const { transpile } = require('./transpiler/index')
1515
const { isInTesting } = require('./meta/index')
1616
const schema = require('./schemas/index').hyperformJsonSchema
1717

18+
/**
19+
*
20+
* @param {string} fpath Path to .js file
21+
* @param {{
22+
* needAuth: boolean,
23+
* expectedBearer?: string
24+
* }} publishOptions
25+
*/
26+
async function bundleTranspileZipAmazon(fpath, publishOptions) {
27+
// Bundle
28+
let amazonBundledCode
29+
try {
30+
amazonBundledCode = await bundleAmazon(fpath)
31+
} catch (e) {
32+
log(`Errored bundling ${fpath} for Amazon: ${e}`)
33+
return // just skip that file
34+
}
35+
36+
// Transpile
37+
const amazonTranspiledCode = transpile(amazonBundledCode, publishOptions)
38+
39+
// Zip
40+
try {
41+
const amazonZipPath = await zip(amazonTranspiledCode)
42+
return amazonZipPath
43+
} catch (e) {
44+
// probably underlying issue with the zipping library or OS
45+
// should not happen
46+
log(`Errored zipping ${fpath} for Amazon: ${e}`)
47+
// skip that file
48+
}
49+
}
50+
51+
/**
52+
*
53+
* @param {string} fpath Path to .js file
54+
* @param {{
55+
* needAuth: boolean,
56+
* expectedBearer?: string
57+
* }} publishOptions
58+
*/
59+
async function bundleTranspileZipGoogle(fpath, publishOptions) {
60+
// Bundle
61+
let googleBundledCode
62+
try {
63+
googleBundledCode = await bundleGoogle(fpath)
64+
} catch (e) {
65+
log(`Errored bundling ${fpath} for Google: ${e}`)
66+
return // just skip that file
67+
}
68+
69+
// Transpile
70+
const googleTranspiledCode = transpile(googleBundledCode, publishOptions)
71+
72+
// Zip
73+
try {
74+
const googleZipPath = await zip(googleTranspiledCode)
75+
return googleZipPath
76+
} catch (e) {
77+
// probably underlying issue with the zipping library or OS
78+
// should not happen
79+
log(`Errored zipping ${fpath}: ${e}`)
80+
// skip that file
81+
}
82+
}
83+
84+
/**
85+
* @description Deploys and publishes a given code .zip to AWS Lambda
86+
* @param {string} name
87+
* @param {string} region
88+
* @param {string} zipPath
89+
* @param {{
90+
* needAuth: boolean,
91+
* expectedBearer?: string
92+
* region: string
93+
* }} publishOptions
94+
* @returns {string} URL of the endpoint of the Lambda
95+
*/
96+
async function deployPublishAmazon(name, region, zipPath, publishOptions) {
97+
const amazonSpinnieName = `amazon-main-${name}`
98+
try {
99+
spinnies.add(amazonSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} Deploying ${name}` })
100+
101+
// Deploy it
102+
const amazonDeployOptions = {
103+
name: name,
104+
region: region,
105+
}
106+
const amazonArn = await deployAmazon(zipPath, amazonDeployOptions)
107+
// Publish it
108+
const amazonPublishOptions = {
109+
...publishOptions, // needAuth and expectedBearer
110+
region: region,
111+
}
112+
const amazonUrl = await publishAmazon(amazonArn, amazonPublishOptions)
113+
spinnies.succeed(amazonSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} 🟢 ${name} ${chalk.rgb(255, 255, 255).bgWhite(amazonUrl)}` })
114+
115+
// return url
116+
return amazonUrl
117+
} catch (e) {
118+
spinnies.fail(amazonSpinnieName, {
119+
text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} Error deploying ${name}: ${e.stack}`,
120+
})
121+
logdev(e, e.stack)
122+
return null
123+
}
124+
}
125+
126+
/**
127+
* @description Deploys and publishes a give code .zip to Google Cloud Functions
128+
* @param {string} name
129+
* @param {string} region
130+
* @param {string} project
131+
* @param {string} zipPath
132+
* @returns {string} URL of the Google Cloud Function
133+
*/
134+
async function deployPublishGoogle(name, region, project, zipPath) {
135+
const googleSpinnieName = `google-main-${name}`
136+
try {
137+
spinnies.add(googleSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${name}` })
138+
const googleOptions = {
139+
name: name,
140+
project: project, // process.env.GC_PROJECT,
141+
region: region, // TODO get from parsedhyperfromjson
142+
runtime: 'nodejs12',
143+
}
144+
const googleUrl = await deployGoogle(zipPath, googleOptions)
145+
spinnies.succeed(googleSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${name} ${chalk.rgb(255, 255, 255).bgWhite(googleUrl)}` })
146+
console.log('Google takes another 1 - 2m for changes to take effect')
147+
148+
// return url
149+
return googleUrl
150+
} catch (e) {
151+
spinnies.fail(googleSpinnieName, {
152+
text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${name}: ${e.stack}`,
153+
})
154+
logdev(e, e.stack)
155+
return null
156+
}
157+
}
18158
/**
19159
*
20160
* @param {string} dir
@@ -78,60 +218,22 @@ async function main(dir, fnregex, parsedHyperformJson, needAuth) {
78218
const endpoints = await Promise.all(
79219
// For each file
80220
infos.map(async (info) => {
81-
/// /////////
82-
// Amazon //
83-
/// /////////
221+
const toAmazon = parsedHyperformJson.amazon != null
222+
const toGoogle = parsedHyperformJson.google != null
223+
/// //////////////////////////////////////////////////////////
224+
// Bundle and zip for Amazon //
225+
/// //////////////////////////////////////////////////////////
84226
let amazonZipPath
85-
{
86-
// Bundle
87-
let amazonBundledCode
88-
try {
89-
amazonBundledCode = await bundleAmazon(info.p)
90-
} catch (e) {
91-
log(`Errored bundling ${info.p} for Amazon: ${e}`)
92-
return // just skip that file
93-
}
94-
95-
// Transpile
96-
const amazonTranspiledCode = transpile(amazonBundledCode, publishOptions)
97-
98-
// Zip
99-
try {
100-
amazonZipPath = await zip(amazonTranspiledCode)
101-
} catch (e) {
102-
// probably underlying issue with the zipping library or OS
103-
// should not happen
104-
log(`Errored zipping ${info.p} for Amazon: ${e}`)
105-
return // skip that file
106-
}
107-
}
108-
109-
/// /////////
110-
// Google //
111-
/// /////////
112-
let googleZipPath
113-
{
114-
// Bundle
115-
let googleBundledCode
116-
try {
117-
googleBundledCode = await bundleGoogle(info.p)
118-
} catch (e) {
119-
log(`Errored bundling ${info.p} for Google: ${e}`)
120-
return // just skip that file
121-
}
122-
123-
// Transpile
124-
const googleTranspiledCode = transpile(googleBundledCode, publishOptions)
125-
126-
// Zip
127-
try {
128-
googleZipPath = await zip(googleTranspiledCode)
129-
} catch (e) {
130-
// probably underlying issue with the zipping library or OS
131-
// should not happen
132-
log(`Errored zipping ${info.p}: ${e}`)
133-
return // skip that file
134-
}
227+
if (toAmazon === true) {
228+
amazonZipPath = await bundleTranspileZipAmazon(info.p, publishOptions)
229+
}
230+
231+
/// //////////////////////////////////////////////////////////
232+
// Bundle and zip for Google //
233+
/// //////////////////////////////////////////////////////////
234+
let googleZipPath
235+
if (toGoogle === true) {
236+
googleZipPath = await bundleTranspileZipGoogle(info.p, publishOptions)
135237
}
136238

137239
// NOTE for new functions add allUsers as invoker
@@ -143,65 +245,33 @@ async function main(dir, fnregex, parsedHyperformJson, needAuth) {
143245
/// //////////////////////////////////////////////////////////
144246
/// Deploy to Amazon
145247
/// //////////////////////////////////////////////////////////
146-
let amazonUrl
147-
{
148-
const amazonSpinnieName = `amazon-main-${exp}`
149-
try {
150-
spinnies.add(amazonSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} Deploying ${exp}` })
151-
152-
// Deploy it
153-
const amazonDeployOptions = {
154-
name: exp,
155-
region: parsedHyperformJson.amazon.aws_default_region,
156-
}
157-
const amazonArn = await deployAmazon(amazonZipPath, amazonDeployOptions)
158-
// Publish it
159-
const amazonPublishOptions = {
160-
...publishOptions, // needAuth and expectedBearer
161-
region: parsedHyperformJson.amazon.aws_default_region,
162-
}
163-
amazonUrl = await publishAmazon(amazonArn, amazonPublishOptions) // TODO
164-
165-
spinnies.succeed(amazonSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} 🟢 ${exp} ${chalk.rgb(255, 255, 255).bgWhite(amazonUrl)}` })
166-
} catch (e) {
167-
spinnies.fail(amazonSpinnieName, {
168-
text: `${chalk.rgb(255, 255, 255).bgWhite(' AWS Lambda ')} Error deploying ${exp}: ${e.stack}`,
169-
})
170-
logdev(e, e.stack)
171-
}
248+
let amazonUrl
249+
if (toAmazon === true) {
250+
amazonUrl = await deployPublishAmazon(
251+
exp,
252+
parsedHyperformJson.amazon.aws_default_region,
253+
amazonZipPath,
254+
publishOptions,
255+
)
172256
}
173257

174-
// TODO
175258
/// //////////////////////////////////////////////////////////
176259
/// Deploy to Google
177260
/// //////////////////////////////////////////////////////////
178261
let googleUrl
179-
{
180-
const googleSpinnieName = `google-main-${exp}`
181-
try {
182-
spinnies.add(googleSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${exp}` })
183-
const googleOptions = {
184-
name: exp,
185-
project: 'firstnodefunc', // process.env.GC_PROJECT,
186-
region: 'us-central1', // TODO get from parsedhyperfromjson
187-
runtime: 'nodejs12',
188-
}
189-
googleUrl = await deployGoogle(googleZipPath, googleOptions)
190-
spinnies.succeed(googleSpinnieName, { text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${exp} ${chalk.rgb(255, 255, 255).bgWhite(googleUrl)}` })
191-
} catch (e) {
192-
spinnies.fail(googleSpinnieName, {
193-
text: `${chalk.rgb(255, 255, 255).bgWhite(' Google ')} ${exp}: ${e.stack}`,
194-
})
195-
logdev(e, e.stack)
196-
}
262+
if (toGoogle === true) {
263+
googleUrl = await deployPublishGoogle(
264+
exp,
265+
'us-central1',
266+
'firstnodefunc',
267+
googleZipPath,
268+
)
197269
}
198-
199-
return [amazonUrl] // for tests etc
270+
271+
return [amazonUrl, googleUrl].filter((el) => el) // for tests etc
200272
}),
201273
)
202274

203-
console.log('Google takes another 1 - 2m for changes to take effect')
204-
205275
return [].concat(...endpts)
206276
}),
207277
)

0 commit comments

Comments
 (0)