Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
refactor: extract prepareResources
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 9, 2020
1 parent 6a43faa commit 6a24d19
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
49 changes: 2 additions & 47 deletions cdk/cloudformation.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
import { TestApp } from './TestApp'
import * as path from 'path'
import { promises as fs } from 'fs'
import { getLambdaSourceCodeBucketName } from './getLambdaSourceCodeBucketName'
import { TestStackLambdas } from './TestStack'
import { packBaseLayer, packLayeredLambdas, WebpackMode } from '../src'
import { prepareResources } from './prepareResources'

const stackName = process.env.STACK_NAME

if (!stackName) {
if (stackName === undefined) {
console.error(`STACK_NAME not set!`)
process.exit(1)
}

const prepareResources = async ({
stackName,
rootDir,
}: {
stackName: string
rootDir: string
}) => {
// Pack the lambdas
const outDir = path.resolve(rootDir, 'dist', 'lambdas')
try {
await fs.stat(outDir)
} catch (_) {
await fs.mkdir(outDir)
}
const sourceCodeBucketName = await getLambdaSourceCodeBucketName({
stackName,
})
const baseLayerZipFileName = await packBaseLayer({
srcDir: rootDir,
outDir,
Bucket: sourceCodeBucketName,
})
const lambdas = await packLayeredLambdas<TestStackLambdas>({
id: 'test-lambdas',
mode: WebpackMode.production,
srcDir: rootDir,
outDir,
Bucket: sourceCodeBucketName,
lambdas: {
uuid: path.resolve(rootDir, 'test', 'uuidLambda.ts'),
},
tsConfig: path.resolve(rootDir, 'tsconfig.json'),
})

return {
sourceCodeBucketName,
baseLayerZipFileName,
lambdas,
}
}

prepareResources({
stackName,
rootDir: process.cwd(),
Expand Down
55 changes: 55 additions & 0 deletions cdk/prepareResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as path from 'path'
import { promises as fs } from 'fs'
import { getLambdaSourceCodeBucketName } from './getLambdaSourceCodeBucketName'
import { TestStackLambdas } from './TestStack'
import {
packBaseLayer,
packLayeredLambdas,
WebpackMode,
LayeredLambdas,
} from '../src'

export const prepareResources = async ({
stackName,
rootDir,
}: {
stackName: string
rootDir: string
}): Promise<{
sourceCodeBucketName: string
baseLayerZipFileName: string
lambdas: LayeredLambdas<TestStackLambdas>
}> => {
// Pack the lambdas
const outDir = path.resolve(rootDir, 'dist', 'lambdas')
try {
await fs.stat(outDir)
} catch (_) {
await fs.mkdir(outDir)
}
const sourceCodeBucketName = await getLambdaSourceCodeBucketName({
stackName,
})
const baseLayerZipFileName = await packBaseLayer({
srcDir: rootDir,
outDir,
Bucket: sourceCodeBucketName,
})
const lambdas = await packLayeredLambdas<TestStackLambdas>({
id: 'test-lambdas',
mode: WebpackMode.production,
srcDir: rootDir,
outDir,
Bucket: sourceCodeBucketName,
lambdas: {
uuid: path.resolve(rootDir, 'test', 'uuidLambda.ts'),
},
tsConfig: path.resolve(rootDir, 'tsconfig.json'),
})

return {
sourceCodeBucketName,
baseLayerZipFileName,
lambdas,
}
}

0 comments on commit 6a24d19

Please sign in to comment.