UNPKG

@bowtie/sls

Version:

Serverless helpers & utilities

115 lines (90 loc) 3.33 kB
const fs = require('fs-extra') const path = require('path') const yaml = require('js-yaml') const merge = require('deepmerge') const { defaults } = require('../config') const { filterItemsS3, loadObjectsS3, compareS3, syncS3 } = require('./s3'); const { SERVICE_NAME, SERVICE_DIR = 'services' } = process.env const scanRecursive = async (schema, params, prev = null) => { let result = [] if (prev && prev.lastKey) { result = await schema.scan(params).startAt(prev.lastKey).exec() } else { result = await schema.scan(params).exec() } if (result.lastKey) { result = result.concat(await scanRecursive(schema, params, result)) } return result } const parseServiceConfigRaw = () => { const serviceSpec = path.resolve(SERVICE_DIR, SERVICE_NAME + '.yml') if (!fs.existsSync(serviceSpec)) throw new Error(`Missing/invalid services spec: '${serviceSpec}'`) const services = yaml.safeLoad(fs.readFileSync(serviceSpec, 'utf8')) return services[SERVICE_NAME] ? services[SERVICE_NAME] : null } const parseServiceConfigFull = () => { return merge(defaults.service, parseServiceConfigRaw()) } const parseServiceConfig = (opt) => { return opt ? parseServiceConfigFull()[opt] : parseServiceConfigFull() } const findDeployment = (stack) => { const { target, deployments = {} } = parseServiceConfig(); console.log(Object.values(deployments)); // TODO: Better matching? (not [0]...) const matches = Object.values(deployments).find(stacks => stacks.map(s => s.name).includes(stack)); if (!matches || matches.length === 0) { throw new Error(`FAIL: Unable to find deploy for stack: '${stack}'`) } if (matches.length > 1) { console.warn(`Multiple matches found finding host for stack: '${stack}'`) } console.log({ stack, matches }) return matches[0] } const findDeploymentHost = (stack) => { const { target } = parseServiceConfig(); const deployment = findDeployment(stack) if (deployment) { const { hostHeader, aliasConfig } = deployment; console.log('DEBUG:', { hostHeader, aliasConfig, deployment, stack, target }) if (target === 's3' && aliasConfig && aliasConfig.names.length > 0) { console.log('DEBUG:', aliasConfig, aliasConfig.names) return aliasConfig.names[0]; } else if (hostHeader) { return hostHeader; } else { console.warn('WARN: Unable to find deployment URL for stack:', { stack, deployment, target }); } } else { console.warn('WARN: Unable to find deployment for stack:', { stack, deployment, target }); } return null; } const findDeploymentLinks = (stack) => { const { target, deployments = {} } = parseServiceConfig(); console.log(Object.values(deployments)); const deployment = Object.values(deployments).find(stacks => stacks.map(s => s.name).includes(stack))[0]; if (deployment) { const { links = [] } = deployment; const host = findDeploymentHost(stack) if (host) links.push(`https://${host}`) return links } else { console.warn('Unable to find deployment for stack:', { stack, deployment, target, deployments }); } return []; } module.exports = { filterItemsS3, loadObjectsS3, compareS3, syncS3, scanRecursive, parseServiceConfig, parseServiceConfigRaw, parseServiceConfigFull, findDeploymentHost, findDeploymentLinks }