Skip to content

Commit

Permalink
fix: #1065 fix loading chunk fail (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
NghiaPham authored and Vu Nguyen committed Apr 29, 2020
1 parent 3ed7546 commit 4d425c2
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions scripts/s3/purge-unused-assets-from-s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const s3 = require('aws-sdk/clients/s3')
const { execSync } = require('child_process')

const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY } = process.env

/* eslint-disable-next-line max-len */
// TODO: precache-manifest file is generated by offline-plugin, There are some work around but complex, and require time:
// E.g: deploy project to folder -> point cloudfront distribution to that folder
const ignorePatterns = [/^config.json/, /^index.html/, /^precache-manifest\..+\.js/, /^service-worker.js/, /sw.js/]

const client = new s3({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
})

const getTwoPreviousCommitsHash = () => {
const stdOutExecSync = execSync('git rev-list --reverse --branches=mater HEAD | tail -2')
const parsedStdOutExecSync = stdOutExecSync
.toString()
.trim()
.split('\n')

return parsedStdOutExecSync
}

const filterS3ObjectKeysNotBelongToHashesOrIgnoreFiles = (s3Objects, hashes) =>
s3Objects
.filter(s3Object => {
const fileName = s3Object.Key
const parsedFileName = fileName.split('.')

const hash = parsedFileName[1]

return !ignorePatterns.some(ignorePattern => ignorePattern.test(fileName)) && !hashes.includes(hash)
})
.map(s3Objects => ({
Key: s3Objects.Key,
}))

const purgeUnusedAssetsFromS3Bucket = async bucket => {
console.log(`Bucket: ${bucket}: Checking for slate assets (assets not belong to 2 previous recent commits)`)

const s3Objects = (await client.listObjects({ Bucket: bucket }).promise()).Contents
const twoPreviousCommitsHash = getTwoPreviousCommitsHash()

const s3ObjectKeysNotBelongToTwoPreviousCommitsHash = filterS3ObjectKeysNotBelongToHashesOrIgnoreFiles(
s3Objects,
twoPreviousCommitsHash,
)

if (s3ObjectKeysNotBelongToTwoPreviousCommitsHash.length > 0) {
console.log(
`Bucket: ${bucket}: Two previous commit hashes are: ${twoPreviousCommitsHash.join(', ')}
Assets of 2 previous recent commits in the master branch have been detected, and will be deleted shortly`,
)

await client
.deleteObjects({
Bucket: bucket,
Delete: {
Objects: s3ObjectKeysNotBelongToTwoPreviousCommitsHash,
},
})
.promise()

console.log(`Bucket: ${bucket}: Delete unused assets successfully`)
return
}

console.log(`Bucket: ${bucket}: No slate assets detected`)
return
}

const main = async () => {
const [, , ...args] = process.argv
if (args.length >= 1 && args[0] && args[1]) {
try {
const bucketDev = args[0]
const bucketProd = args[1]

await Promise.all([purgeUnusedAssetsFromS3Bucket(bucketDev), purgeUnusedAssetsFromS3Bucket(bucketProd)])
} catch (err) {
console.error('One arguments need to be suppled <<development bucket>> and <<production bucket>>')
process.exit(1)
}
}
}

main()

0 comments on commit 4d425c2

Please sign in to comment.