Skip to content

Commit

Permalink
fix: delete all hash and date objects
Browse files Browse the repository at this point in the history
  • Loading branch information
wolff453 committed Jul 29, 2023
1 parent f6d935d commit dd7a982
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
53 changes: 30 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ const { LambdaClient, UpdateFunctionCodeCommand, UpdateFunctionConfigurationComm
const { join } = require('path')

class MyPlugin {
constructor (serverless) {
constructor(serverless) {
this.serverless = serverless
this.client = new LambdaClient({ region: this.serverless.configurationInput.provider.region })
this.hooks = {
'package:finalize': () => this.cache()
}
}

async deploy (configuration, stream) {
async deploy(configuration, stream) {
return Promise.all(Object.keys(configuration.functions).map(async (key) => {
console.log(`${configuration.service}-${key}`)
await this.client.send(new UpdateFunctionCodeCommand({
Expand All @@ -24,11 +24,11 @@ class MyPlugin {
}))
}

async getZipStream (config) {
async getZipStream(config) {
return fs.promises.readFile(`${process.cwd()}/.serverless/${config.service}.zip`)
}

async validateCache (hash, relativeUrl) {
async validateCache(hash, relativeUrl) {
try {
const dir = await fs.promises.readdir(join(process.cwd(), relativeUrl))
return dir.some(files => files.includes(hash))
Expand All @@ -37,15 +37,15 @@ class MyPlugin {
}
}

getConfiguration () {
getConfiguration() {
return this.serverless.configurationInput
}

generateHash (config) {
generateHash(config) {
return crypto.createHash('md5').update(config).digest('hex')
}

async createFileCache (filename, relativeUrl) {
async createFileCache(filename, relativeUrl) {
return fs.outputFile(`${join(process.cwd(), relativeUrl)}/${filename}`, filename)
}

Expand All @@ -60,22 +60,29 @@ class MyPlugin {
})
}

deleteDateObjects () {
const copy = JSON.parse(JSON.stringify(this.getConfiguration()))
Object.keys(copy.package).forEach(key => {
if (key.includes('artifact')) {
Reflect.deleteProperty(copy.package, key)
}
})
Object.entries(copy.provider.compiledCloudFormationTemplate.Resources).forEach(([key, value]) => {
if (value?.Type?.includes('AWS::Lambda::Function')) {
delete copy.provider.compiledCloudFormationTemplate.Resources[key].Properties.Code
}
removeVersionLogicalId(config) {
Object.values(config.functions).forEach((value) => {
delete value.versionLogicalId
})
return JSON.stringify(copy)
}

async updateFunctionsConfiguration (layer) {
removeArtifactDirectoryName(config) {
delete config.package.artifactDirectoryName
}

removeCompiledCloudFormationTemplate(config) {
delete config.provider.compiledCloudFormationTemplate
}

deleteDynamicObjects() {
const copyConfig = JSON.parse(JSON.stringify(this.getConfiguration()))
this.removeVersionLogicalId(copyConfig)
this.removeArtifactDirectoryName(copyConfig)
this.removeCompiledCloudFormationTemplate(copyConfig)
return JSON.stringify(copyConfig)
}

async updateFunctionsConfiguration(layer) {
const functions = this.getFunctions()
await Promise.all(Object.entries(functions).map(async functionName => {
await this.client.send(new UpdateFunctionConfigurationCommand({
Expand All @@ -85,15 +92,15 @@ class MyPlugin {
}))
}

getRelativeDir () {
getRelativeDir() {
return this.serverless.configurationInput.custom?.cache?.relativeUrl
}

async cache () {
async cache() {
try {
const config = this.getConfiguration()
const relativeUrl = this.getRelativeDir()
const obj = this.deleteDateObjects()
const obj = this.deleteDynamicObjects()
const hash = this.generateHash(obj)
const cache = await this.validateCache(hash, relativeUrl)
console.log('Generated hash:', hash)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-cache-deploy",
"version": "1.0.3",
"version": "1.0.4",
"description": "![imagem](docs/diagram.jpeg)",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit dd7a982

Please sign in to comment.