This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (42 loc) · 1.65 KB
/
index.js
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
const debug = require('debug')('macro-custom-pragma-runner')
const path = require('path')
const resolvePragma = name => {
const validPaths = [
path.resolve(process.cwd(), 'src', 'pragmas', name),
path.resolve(process.cwd(), 'src', 'pragmas', name.replace(/^_/, '')),
path.resolve(process.cwd(), 'node_modules', name),
path.resolve(process.cwd(), 'node_modules', `@${name}`),
path.resolve(process.cwd(), 'node_modules', name.replace(/^_/, '')),
path.resolve(process.cwd(), 'node_modules', `@${name.replace(/^_/, '')}`)
]
for (const p of validPaths) {
try {
require.resolve(p)
debug(`Custom pragma found in ${p}.`)
return require(p)
} catch (err) {
debug(`Custom pragma not found in ${p}.`)
continue
}
}
throw new Error(`Custom pragama not found in valid paths: ${validPaths.join(', ')}`)
}
module.exports = async function customPragmaRunner (arc, cloudformation, stage) {
debug('invoked')
const customPragmas = Object.keys(arc).filter(n => n.startsWith('_')).map(name => name.replace(/^_/, '')) // grab custom pragma keys from arc
debug(`Custom pragmas found ${customPragmas.join(', ')}`)
for await (const pragmaName of customPragmas) {
debug(`Running custom pragma: ${pragmaName}`)
const args = arc['_' + pragmaName]
let output = null
try {
output = await resolvePragma('_' + pragmaName)({ arc, cloudformation, stage, args }) || {}
} catch (err) {
console.error(`Error executing custom pragma _${pragmaName}: ${err.message}`)
throw err
}
const { cloudformation: cftMods } = output
Object.assign(cloudformation, cftMods)
}
return cloudformation
}