-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
226 additions
and
175 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const crypto = require('crypto'); | ||
const { publishWithSuffixes } = require('./publish'); | ||
const { runTest } = require('./test-utils/runTest'); | ||
const { createS3Client } = require('./s3client'); | ||
const fs = require('node:fs/promises'); | ||
|
||
// This is a test file. It is intended to be run manually with the proper environment variables set | ||
// | ||
// Run it from the project root using "node tasks/aws-s3-builds-page/publish-test.js" | ||
|
||
const s3Client = createS3Client(); | ||
|
||
runTest(async ({ log }) => { | ||
const suffix1 = `-test-file-` + crypto.randomUUID(); | ||
const suffix2 = `-test-file-` + crypto.randomUUID(); | ||
log(`Publish ${suffix1} and ${suffix2}`); | ||
await publishWithSuffixes([suffix1, suffix2]); | ||
await compareAndDeleteFiles(suffix1, log); | ||
await compareAndDeleteFiles(suffix2, log); | ||
}); | ||
|
||
async function compareAndDeleteFiles(suffix, log) { | ||
const pairs = [ | ||
['dist/handlebars.js', `handlebars${suffix}.js`], | ||
['dist/handlebars.min.js', `handlebars.min${suffix}.js`], | ||
['dist/handlebars.runtime.js', `handlebars.runtime${suffix}.js`], | ||
['dist/handlebars.runtime.min.js', `handlebars.runtime.min${suffix}.js`] | ||
]; | ||
for (const [localFile, remoteFile] of pairs) { | ||
await expectSameContents(localFile, remoteFile, log); | ||
log(`Deleting "${remoteFile}"`); | ||
await s3Client.deleteFile(remoteFile); | ||
} | ||
} | ||
|
||
async function expectSameContents(localFile, remoteFile, log) { | ||
log( | ||
`Checking file contents "${localFile}" vs "${s3Client.fileUrl(remoteFile)}"` | ||
); | ||
const remoteContents = await s3Client.fetchFile(remoteFile); | ||
const localContents = await fs.readFile(localFile, 'utf-8'); | ||
if (remoteContents !== localContents) { | ||
throw new Error( | ||
`Files do not match: ${localFile}" vs "${s3Client.fileUrl(remoteFile)}"` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable no-console */ | ||
const { createS3Client } = require('./s3client'); | ||
|
||
const filenames = [ | ||
'handlebars.js', | ||
'handlebars.min.js', | ||
'handlebars.runtime.js', | ||
'handlebars.runtime.min.js' | ||
]; | ||
|
||
async function publishWithSuffixes(suffixes) { | ||
const s3Client = createS3Client(); | ||
const publishPromises = suffixes.map(suffix => | ||
publishSuffix(s3Client, suffix) | ||
); | ||
return Promise.all(publishPromises); | ||
} | ||
|
||
async function publishSuffix(s3client, suffix) { | ||
const publishPromises = filenames.map(async filename => { | ||
const nameInBucket = getNameInBucket(filename, suffix); | ||
const localFile = getLocalFile(filename); | ||
await s3client.uploadFile(localFile, nameInBucket); | ||
console.log(`Published ${localFile} to build server (${nameInBucket})`); | ||
}); | ||
return Promise.all(publishPromises); | ||
} | ||
|
||
function getNameInBucket(filename, suffix) { | ||
return filename.replace(/\.js$/, suffix + '.js'); | ||
} | ||
|
||
function getLocalFile(filename) { | ||
return 'dist/' + filename; | ||
} | ||
|
||
module.exports = { publishWithSuffixes }; |
This file was deleted.
Oops, something went wrong.
6 changes: 2 additions & 4 deletions
6
tasks/aws-s3-builds-page/deleteFile.js → ...aws-s3-builds-page/s3client/deleteFile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
async function fetchFile(bucket, remoteName) { | ||
return (await fetch(fileUrl(bucket, remoteName))).text(); | ||
} | ||
|
||
function fileUrl(bucket, remoteName) { | ||
const bucketUrl = `https://s3.amazonaws.com/${bucket}`; | ||
return `${bucketUrl}/${remoteName}`; | ||
} | ||
|
||
module.exports = { fetchFile, fileUrl }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { listFiles } = require('./listFiles'); | ||
const { uploadFile } = require('./uploadFile'); | ||
const { deleteFile } = require('./deleteFile'); | ||
const { S3Client } = require('@aws-sdk/client-s3'); | ||
const { requireEnvVar } = require('./requireEnvVar'); | ||
const { fetchFile, fileUrl } = require('./fetchFile'); | ||
|
||
module.exports = { createS3Client }; | ||
|
||
function createS3Client() { | ||
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html | ||
requireEnvVar('AWS_ACCESS_KEY_ID'); | ||
requireEnvVar('AWS_SECRET_ACCESS_KEY'); | ||
|
||
const bucket = requireEnvVar('S3_BUCKET_NAME'); | ||
const s3Client = new S3Client({ | ||
region: 'us-east-1' | ||
}); | ||
|
||
return { | ||
async listFiles() { | ||
return listFiles(s3Client, bucket); | ||
}, | ||
async uploadFile(localName, remoteName) { | ||
await uploadFile(s3Client, bucket, localName, remoteName); | ||
}, | ||
async deleteFile(remoteName) { | ||
await deleteFile(s3Client, bucket, remoteName); | ||
}, | ||
async fetchFile(remoteName) { | ||
return fetchFile(bucket, remoteName); | ||
}, | ||
fileUrl(remoteName) { | ||
return fileUrl(bucket, remoteName); | ||
} | ||
}; | ||
} |
4 changes: 1 addition & 3 deletions
4
tasks/aws-s3-builds-page/listFiles.js → .../aws-s3-builds-page/s3client/listFiles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function requireEnvVar(name) { | ||
if (!process.env[name]) { | ||
throw new Error(`Environment variable "${name}" is required.`); | ||
} | ||
return process.env[name]; | ||
} | ||
|
||
module.exports = { requireEnvVar }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable no-console */ | ||
const { createS3Client } = require('./index'); | ||
const crypto = require('crypto'); | ||
const { runTest } = require('../test-utils/runTest'); | ||
|
||
// This is a test file. It is intended to be run manually | ||
// with the proper environment variables set | ||
// It tests whether the upload/list/delete methods in this directory | ||
// work properly. | ||
// | ||
// Run it from the project root using "node tasks/aws-s3-builds-page/s3client/s3-test.js" | ||
|
||
const client = createS3Client(); | ||
|
||
runTest(async ({ log }) => { | ||
const uuid = crypto.randomUUID(); | ||
const filename = `test-file-${uuid}`; | ||
log(`Starting test with target file "${filename}"`); | ||
|
||
log(`Uploading "${filename}"`); | ||
await client.uploadFile('package.json', filename); | ||
|
||
log(`Check if uploaded "${filename}"`); | ||
const listing = await client.listFiles(); | ||
if (!listing.includes(filename)) { | ||
throw new Error(`File "${filename}" has not been uploaded`); | ||
} | ||
|
||
log(`Check contents of "${filename}"`); | ||
const uploadedContents = await client.fetchFile(filename); | ||
expectStringContains('"name": "handlebars"', uploadedContents); | ||
|
||
log(`Delete "${filename}"`); | ||
await client.deleteFile(filename); | ||
|
||
log(`Check if deleted "${filename}"`); | ||
const listingAfterDelete = await client.listFiles(); | ||
if (listingAfterDelete.includes(filename)) { | ||
throw new Error(`File "${filename}" has not been deleted`); | ||
} | ||
}); | ||
|
||
function expectStringContains(needle, haystack) { | ||
if (!haystack.includes(needle)) { | ||
throw new Error(`Expecting to find "${needle}" in string "${haystack}"`); | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
tasks/aws-s3-builds-page/uploadFile.js → ...aws-s3-builds-page/s3client/uploadFile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-console */ | ||
const { createS3Client } = require('../s3client/index'); | ||
|
||
const s3Client = createS3Client(); | ||
|
||
function runTest(asyncFn) { | ||
asyncFn({ log: console.log.bind(console) }) | ||
.finally(detectSurplusFiles) | ||
.catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
async function detectSurplusFiles() { | ||
const listing = await s3Client.listFiles(); | ||
let surplusFileDetected = false; | ||
const testFilesInBucket = listing.filter(name => name.includes('test-file')); | ||
for (const filename of testFilesInBucket) { | ||
if (process.argv[2] === '--delete-surplus') { | ||
await s3Client.deleteFile(filename); | ||
} else { | ||
console.log(`Detected surplus file "${filename}"`); | ||
surplusFileDetected = true; | ||
} | ||
} | ||
if (surplusFileDetected) { | ||
console.log(`run with --delete-surplus to delete surplus files`); | ||
} | ||
console.log('DONE'); | ||
} | ||
|
||
module.exports = { runTest }; |
Oops, something went wrong.