Skip to content

Commit 111f672

Browse files
committed
port to new specs
1 parent f622337 commit 111f672

File tree

6 files changed

+148
-88
lines changed

6 files changed

+148
-88
lines changed

cli.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require('path')
33
const fs = require('fs')
44
const semver = require('semver')
5-
const { init } = require('./initer/index')
5+
const { init, initDumb } = require('./initer/index')
66
const { getParsedHyperformJson } = require('./parser/index')
77
const { log } = require('./printers/index')
88
const { maybeShowSurvey, answerSurvey } = require('./surveyor/index')
@@ -21,68 +21,92 @@ if (semver.satisfies(process.version, version) !== true) {
2121
}
2222

2323
if (
24-
(/init|deploy/.test(args[0]) === false)
25-
|| ((args.length === 1) && args[0] !== 'init')
26-
|| (args.length === 3 && args[2] !== '--url')
24+
(/deploy/.test(args[0]) === false)
25+
|| ((args.length === 1))
26+
|| (args.length === 2)
27+
|| (args.length === 3 && args[2] !== '--amazon' && args[2] !== '--google')
2728
|| args.length >= 4) {
2829
log(`Usage:
29-
$ hf init # Creates 'hyperform.json' in current directory
30-
$ hf deploy ./some/file.js # Deploys exports of a Javascript file
30+
$ hf deploy ./some/file.js --amazon # Deploy exports to AWS Lambda
31+
$ hf deploy ./some/file.js --google # Deploy exports to Google Cloud Functions
3132
`)
3233
process.exit(1)
3334
}
3435

3536
// $ hf MODE FPATH [--url]
36-
const mode = args[0]
37+
// const mode = args[0]
3738
const fpath = args[1]
38-
const isPublic = (args[2] === '--url')
39+
// const isPublic = (args[2] === '--url')
3940

4041
const currdir = process.cwd()
4142

42-
// Mode is init
43-
if (mode === 'init') {
44-
init(currdir)
45-
process.exit()
46-
}
43+
let platform
44+
if (args[2] === '--amazon') platform = 'amazon'
45+
if (args[2] === '--google') platform = 'google'
46+
47+
// // Mode is init
48+
// if (mode === 'init') {
49+
// initDumb(currdir)
50+
// process.exit()
51+
// }
4752

4853
// Mode is answer survey
49-
if (mode === 'answer') {
50-
const answer = args.slice(1) // words after $ hf answer
51-
// Send anonymous answer (words and date recorded)
52-
answerSurvey(answer)
53-
.then(process.exit())
54-
}
54+
// if (mode === 'answer') {
55+
// const answer = args.slice(1) // words after $ hf answer
56+
// // Send anonymous answer (words and date recorded)
57+
// answerSurvey(answer)
58+
// .then(process.exit())
59+
// }
5560

5661
// Mode is deploy
5762

5863
// try to read hyperform.json
5964
const hyperformJsonExists = fs.existsSync(path.join(currdir, 'hyperform.json'))
6065
if (hyperformJsonExists === false) {
61-
log(`No hyperform.json found. You can create one with:
62-
$ hf init`)
66+
if (platform === 'amazon') {
67+
log(`No hyperform.json found in current directory. Create it with these fields:
68+
69+
amazon: {
70+
aws_access_key_id: '',
71+
aws_secret_access_key: '',
72+
aws_region: '',
73+
}
74+
75+
`)
76+
}
77+
78+
if (platform === 'google') {
79+
log(`No hyperform.json found in current directory. Create it with these fields:
80+
81+
google: {
82+
gc_project: '',
83+
gc_region: '',
84+
}
85+
86+
`)
87+
}
6388
process.exit(1)
6489
}
6590
// parse and validate hyperform.json
66-
const parsedHyperformJson = getParsedHyperformJson(currdir)
67-
91+
const parsedHyperformJson = getParsedHyperformJson(currdir, platform)
92+
6893
// Dev Note: Do this as early as possible
69-
94+
7095
// Load AWS Credentials from hyperform.json into process.env
7196
// These are identical with variables that Amazon CLI uses, so they may be set
7297
// However, that is fine, hyperform.json should still take precedence
7398
if (parsedHyperformJson.amazon != null) {
7499
process.env.AWS_ACCESS_KEY_ID = parsedHyperformJson.amazon.aws_access_key_id,
75100
process.env.AWS_SECRET_ACCESS_KEY = parsedHyperformJson.amazon.aws_secret_access_key,
76-
process.env.AWS_REGION = parsedHyperformJson.amazon.aws_default_region
101+
process.env.AWS_REGION = parsedHyperformJson.amazon.aws_region
77102
// may, may not be defined.
78103
process.env.AWS_SESSION_TOKEN = parsedHyperformJson.amazon.aws_session_token
79104
}
80-
105+
81106
// Load GC Credentials from hyperform.json into process.env
82107
// These are different from what Google usually occupies (GCLOUD_...)
83108
if (parsedHyperformJson.google != null) {
84-
process.env.GC_CLIENT_EMAIL = parsedHyperformJson.google.gc_client_email,
85-
process.env.GC_PRIVATE_KEY = parsedHyperformJson.google.gc_private_key,
109+
process.env.GC_REGION = parsedHyperformJson.google.gc_private_key,
86110
process.env.GC_PROJECT = parsedHyperformJson.google.gc_project
87111
}
88112

@@ -92,7 +116,7 @@ try {
92116
// Do not import earlier, it needs to absorb process.env set above
93117
// TODO: make less sloppy
94118
const { main } = require('./index')
95-
main(currdir, fpath, parsedHyperformJson, isPublic)
119+
main(currdir, fpath, platform, parsedHyperformJson)
96120
// show anonymous survey question with 1/30 probability
97121
// .then(() => maybeShowSurvey())
98122
} catch (e) {

deployer/google/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const fetch = require('node-fetch')
44
const { logdev } = require('../../printers/index')
55

66
let gcOptions
7+
if (process.env.GC_PROJECT) {
8+
gcOptions = {
9+
projectId: process.env.GC_PROJECT,
10+
}
11+
}
712
// Don't consult hyperform.json yet for Google credentials
813

914
// if (process.env.GC_CLIENT_EMAIL && process.env.GC_PRIVATE_KEY && process.env.GC_PROJECT) {

index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const { spinnies, log, logdev } = require('./printers/index')
1515
const { zip } = require('./zipper/index')
1616
const { deployGoogle, publishGoogle } = require('./deployer/google/index')
1717
const { transpile } = require('./transpiler/index')
18-
const schema = require('./schemas/index').hyperformJsonSchema
1918
const packagejson = require('./package.json')
2019
const { createCopy } = require('./copier/index')
2120
const { zipDir } = require('./zipper/google/index')
2221
const { kindle } = require('./kindler/index')
22+
const { amazonSchema, googleSchema } = require('./schemas/index')
2323
/**
2424
*
2525
* @param {string} fpath Path to .js file
@@ -164,7 +164,7 @@ async function deployPublishAmazon(name, region, zipPath, isPublic) {
164164
if (isPublic === true) {
165165
amazonUrl = await publishAmazon(amazonArn, region)
166166
}
167-
spinnies.succ(amazonSpinnieName, { text: `🟢 ${name} ${chalk.rgb(255, 255, 255).bgWhite(amazonUrl || '(no URL. Add --url if you want one)')}` })
167+
spinnies.succ(amazonSpinnieName, { text: `🟢 Deployed ${name} to AWS Lambda` })
168168

169169
// (return url)
170170
return amazonUrl
@@ -207,7 +207,7 @@ async function deployPublishGoogle(name, region, project, zipPath, isPublic) {
207207
// enables anyone with the URL to call the function
208208
await publishGoogle(name, project, region)
209209
}
210-
spinnies.succ(googleSpinnieName, { text: `🟢 ${name} ${chalk.rgb(255, 255, 255).bgWhite(googleUrl || '(no URL. Specify --url if you want one)')}` })
210+
spinnies.succ(googleSpinnieName, { text: `🟢 Deployed ${name} to Google Cloud Functions` })
211211
console.log('Google takes another 1 - 2m for changes to take effect')
212212

213213
// return url
@@ -221,14 +221,12 @@ async function deployPublishGoogle(name, region, project, zipPath, isPublic) {
221221
}
222222
}
223223
/**
224-
*
225224
* @param {string} dir
226225
* @param {Regex} fpath the path to the .js file whose exports should be deployed
227-
* @param {*} parsedHyperformJson
228-
* @param {boolean} isPublic Controls whether (Amazon) to create URL endpoint and (Google) whether to remove IAM protection on the URL
229-
* @returns {{ urls: string[] }} urls: Mixed Array of endpoint URLs.
226+
* @param {amazon|google} platform
227+
* @param {{amazon: {aws_access_key_id:string, aws_secret_access_key: string, aws_region: string}}} parsedHyperformJson
230228
*/
231-
async function main(dir, fpath, parsedHyperformJson, isPublic) {
229+
async function main(dir, fpath, platform, parsedHyperformJson) {
232230
// Check node version (again)
233231
const version = packagejson.engines.node
234232
if (semver.satisfies(process.version, version) !== true) {
@@ -237,6 +235,9 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
237235
}
238236

239237
// verify parsedHyperformJson (again)
238+
let schema
239+
if (platform === 'amazon') schema = amazonSchema
240+
if (platform === 'google') schema = googleSchema
240241
const { error, value } = schema.validate(parsedHyperformJson)
241242
if (error) {
242243
throw new Error(`${error} ${value}`)
@@ -252,8 +253,8 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
252253
return [] // no endpoint URLs created
253254
}
254255

255-
const isToAmazon = parsedHyperformJson.amazon != null
256-
const isToGoogle = parsedHyperformJson.google != null
256+
const isToAmazon = platform === 'amazon'
257+
const isToGoogle = platform === 'google'
257258

258259
let amazonZipPath
259260
let googleZipPath
@@ -269,6 +270,7 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
269270
/// ///////////////////////////////////////////////////
270271
/// Each export, deploy as function & publish. Obtain URL.
271272
/// ///////////////////////////////////////////////////
273+
const isPublic = false
272274

273275
let endpoints = await Promise.all(
274276
// For each export
@@ -280,7 +282,7 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
280282
if (isToAmazon === true) {
281283
amazonUrl = await deployPublishAmazon(
282284
exp,
283-
parsedHyperformJson.amazon.aws_default_region,
285+
parsedHyperformJson.amazon.aws_region,
284286
amazonZipPath,
285287
isPublic,
286288
)
@@ -293,8 +295,9 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
293295
if (isToGoogle === true) {
294296
googleUrl = await deployPublishGoogle(
295297
exp,
296-
'us-central1',
297-
'hyperform-7fd42', // TODO
298+
// TODO lol
299+
parsedHyperformJson.google.gc_region,
300+
parsedHyperformJson.google.gc_project, // TODO
298301
googleZipPath,
299302
isPublic,
300303
)
@@ -359,7 +362,7 @@ async function main(dir, fpath, parsedHyperformJson, isPublic) {
359362
// if (toAmazon === true) {
360363
// amazonUrl = await deployPublishAmazon(
361364
// exp,
362-
// parsedHyperformJson.amazon.aws_default_region,
365+
// parsedHyperformJson.amazon.aws_region,
363366
// amazonZipPath,
364367
// isPublic,
365368
// )

initer/index.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getDefaultSectionString(filecontents) {
3333
* default: {
3434
* aws_access_key_id?: string,
3535
* aws_secret_access_key?: string,
36-
* aws_default_region?: string
36+
* aws_region?: string
3737
* }}
3838
*/
3939
function parseAwsCredentialsOrConfigFile(filecontents) {
@@ -103,6 +103,47 @@ function parseAwsCredentialsOrConfigFile(filecontents) {
103103
}
104104
}
105105

106+
/**
107+
* Just creates an empty hyperform.json
108+
* @param {string} absdir
109+
*/
110+
function initDumb(absdir, platform) {
111+
let json
112+
if (platform === 'amazon') {
113+
json = {
114+
amazon: {
115+
aws_access_key_id: '',
116+
aws_secret_access_key: '',
117+
aws_region: '',
118+
},
119+
}
120+
} else if (platform === 'google') {
121+
json = {
122+
google: {
123+
gc_project: '',
124+
gc_region: '',
125+
},
126+
}
127+
} else {
128+
throw new Error(`platform must be google or amazon but is ${platform}`)
129+
}
130+
131+
// append 'hyperform.json' to .gitignore
132+
// (or create .gitignore if it does not exist yet)
133+
fs.appendFileSync(
134+
path.join(absdir, '.gitignore'),
135+
`${EOL}hyperform.json`,
136+
)
137+
138+
// write results to hyperform.json
139+
fs.writeFileSync(
140+
path.join(absdir, 'hyperform.json'),
141+
JSON.stringify(json, null, 2),
142+
)
143+
log('✓ Created `hyperform.json` ') //
144+
log('✓ Added `hyperform.json` to `.gitignore`') //
145+
}
146+
106147
// TODO shorten
107148
/**
108149
* @description Tries to infer AWS credentials and config, and creates a hyperform.json in "absdir" with what it could infer. If hyperform.json already exists in "absdir" it just prints a message.
@@ -111,7 +152,7 @@ function parseAwsCredentialsOrConfigFile(filecontents) {
111152
* amazon: {
112153
* aws_access_key_id: string?,
113154
* aws_secret_access_key: string?,
114-
* aws_default_region: string?
155+
* aws_region: string?
115156
* }
116157
* }}
117158
*/
@@ -120,13 +161,12 @@ function init(absdir) {
120161
amazon: {
121162
aws_access_key_id: '',
122163
aws_secret_access_key: '',
123-
aws_default_region: '', // TODO
164+
aws_region: '',
165+
},
166+
google: {
167+
gc_project: '',
168+
gc_region: '',
124169
},
125-
// google: {
126-
// gc_project: '',
127-
// gc_client_email: '',
128-
// gc_private_key: '',
129-
// },
130170
}
131171

132172
const filedest = path.join(absdir, 'hyperform.json')
@@ -171,7 +211,7 @@ function init(absdir) {
171211
const configFileContents = fs.readFileSync(possibleConfigPath, { encoding: 'utf-8' })
172212

173213
const parsedConfig = parseAwsCredentialsOrConfigFile(configFileContents)
174-
hyperformJsonContents.amazon.aws_default_region = parsedConfig.default.region
214+
hyperformJsonContents.amazon.aws_region = parsedConfig.default.region
175215
logdev(`Inferred AWS region from ${possibleConfigPath}`)
176216
} else {
177217
logdev(`Could not guess AWS region. No AWS config file found in ${possibleConfigPath}`) // TODO region will not be a single region, but smartly multiple ones (or?)
@@ -191,7 +231,7 @@ function init(absdir) {
191231
}
192232

193233
if (typeof process.env.AWS_REGION === 'string' && process.env.AWS_REGION.trim().length > 0) {
194-
hyperformJsonContents.amazon.aws_default_region = process.env.AWS_REGION.trim()
234+
hyperformJsonContents.amazon.aws_region = process.env.AWS_REGION.trim()
195235
logdev('Environment variable AWS_REGION set, overriding value from config file')
196236
}
197237

@@ -214,6 +254,7 @@ function init(absdir) {
214254

215255
module.exports = {
216256
init,
257+
initDumb,
217258
_only_for_testing_getDefaultSectionString: getDefaultSectionString,
218259
_only_for_testing_parseAwsCredentialsOrConfigFile: parseAwsCredentialsOrConfigFile,
219260
}

0 commit comments

Comments
 (0)