-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathload.js
More file actions
135 lines (115 loc) · 3.92 KB
/
load.js
File metadata and controls
135 lines (115 loc) · 3.92 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
import isEqual from 'lodash.isequal'
import path from 'path'
import Promise from 'bluebird'
import { readdir, readJSON, exists } from './modules/fs'
import minimatch from 'minimatch'
import { listAliases, isFunctionDeployed } from './aws/lambda'
export async function envs () {
const functionLambdaConfigs = await Promise.map(funcs(), lambdaConfig).map(({ FunctionName }) => FunctionName)
const deployedFunctions = await Promise.filter(functionLambdaConfigs, (name) => isFunctionDeployed(name))
if (deployedFunctions.length <= 0) {
throw new Error('No deployed functions')
}
const allFunctionAliases = await Promise.map(deployedFunctions, async name => {
const aliases = await listAliases(name).map(({ Name }) => Name)
return aliases.sort()
})
const aliases = allFunctionAliases.pop().sort()
if (aliases.length === 0) {
throw new AliasesNotFound()
}
allFunctionAliases.forEach(functionAliases => {
if (!isEqual(aliases, functionAliases)) {
throw new EnvironmentMismatch()
}
})
return aliases
}
export async function events (func, eventName) {
const eventDir = `functions/${func}/events`
let events = await readdir(`functions/${func}/events`)
if (eventName) {
events = events.filter((event) => event === `${eventName}.json`)
if (events.length === 0) {
throw new EventNotFound({ eventDir, eventName })
}
}
return events
}
export async function funcs (pattern = '*') {
const funcs = await readdir('functions').filter(minimatch.filter(pattern))
if (funcs.length === 0) {
throw new FunctionNotFound(pattern)
}
return funcs
}
export async function lambdaConfig (name, env = null) {
const functionConfig = await readJSON(`functions/${name}/lambda.json`)
const functionEnvConfigExists = env ? await exists(`functions/${name}/lambda.${env}.json`) : false
const functionEnvConfig = functionEnvConfigExists ? await readJSON(`functions/${name}/lambda.${env}.json`) : {}
const projectConfig = await readJSON(`lambda.json`)
const projectEnvConfigExists = env ? await exists(`lambda.${env}.json`) : false
const projectEnvConfig = projectEnvConfigExists ? await readJSON(`lambda.${env}.json`) : {}
return Object.assign({}, projectConfig, projectEnvConfig, functionConfig, functionEnvConfig)
}
export async function pkg () {
const pkg = await readJSON('package.json')
if (!pkg || !pkg.shep) { throw new MissingShepConfiguration() }
return pkg
}
export async function distPath (joinedPath) {
const { shep: { dist = 'dist' } } = await pkg()
if (joinedPath) {
return path.join(dist, joinedPath)
}
return dist
}
export async function api () {
try {
const api = await readJSON('api.json')
return api
} catch (e) {
return null
}
}
export function babelrc () {
return readJSON('.babelrc')
}
export class EnvironmentMismatch extends Error {
constructor () {
super()
this.message = 'Mismatched aliases found, please run `shep config sync` to ensure all functions have the same environment'
this.name = 'EnvironmentMismatch'
}
}
export class EventNotFound extends Error {
constructor ({ eventDir, eventName }) {
const message = `No event in '${eventDir}' called ${eventName}`
super(message)
this.message = message
this.name = 'EventNotFound'
}
}
export class FunctionNotFound extends Error {
constructor (pattern) {
const message = `No functions found matching patterns: ${JSON.stringify(pattern)}`
super(message)
this.message = message
this.name = 'FunctionNotFound'
}
}
export class AliasesNotFound extends Error {
constructor () {
super()
this.message = 'Cannot load available aliases, to create an alias use `shep deploy --env beta`'
this.name = 'AliasesNotFound'
}
}
export class MissingShepConfiguration extends Error {
constructor (message) {
const msg = message || 'Missing shep section in package.json'
super(msg)
this.message = msg
this.name = 'MissingShepConfiguration'
}
}