Skip to content

Deploy service

Course: Code along - Vercel - chapter 2

Initialise an empty typescript project.
npm init -y
Configure the `tsconfig.json`.
npx tsc --init
Create an infinitely running for loop that pulls values from the redis queue.
import { createClient, commandOptions } from "redis";
import { copyFinalDist, downloadS3Folder } from "./aws";
import { buildProject } from "./utils";
async function main() {
while(1) {
const res = await subscriber.brPop(
commandOptions({ isolated: true }),
'build-queue',
0
);
console.log(res.element)
}
}
main();
Write a function called `downloadS3Folder` that downloads all the files from a given location in S3.
$1b
Run `npm run build` to convert the `React code` into `HTML/CSS` files. (Bonus if this is containerized).
import { exec, spawn } from "child_process";
import path from "path";
export function buildProject(id: string) {
return new Promise((resolve) => {
const child = exec(`cd ${path.join(__dirname, `output/${id}`)} && npm install && npm run build`)
child.stdout?.on('data', function(data) {
console.log('stdout: ' + data);
});
child.stderr?.on('data', function(data) {
console.log('stderr: ' + data);
});
child.on('close', function(code) {
resolve("")
});
})
}
Write a function that uploads a directory to S3 (you can copy it from the last module).
export function copyFinalDist(id: string) {
const folderPath = path.join(__dirname, `output/${id}/dist`);
const allFiles = getAllFiles(folderPath);
allFiles.forEach(file => {
uploadFile(`dist/${id}/` + file.slice(folderPath.length + 1), file);
})
}
const getAllFiles = (folderPath: string) => {
let response: string[] = [];
const allFilesAndFolders = fs.readdirSync(folderPath);allFilesAndFolders.forEach(file => {
const fullFilePath = path.join(folderPath, file);
if (fs.statSync(fullFilePath).isDirectory()) {
response = response.concat(getAllFiles(fullFilePath))
} else {
response.push(fullFilePath);
}
});
return response;
}
const uploadFile = async (fileName: string, localFilePath: string) => {
const fileContent = fs.readFileSync(localFilePath);
const response = await s3.upload({
Body: fileContent,
Bucket: "vercel",
Key: fileName,
}).promise();
console.log(response);
}
Store in the `redis database` that this specific upload has been processed.
publisher.hSet("status", id, "deployed")