Download Script
The download.js
script initiates the entire pipeline by retrieving AWS's latest architecture icon set. It ensures we only process new files, conserves bandwidth, and anchors the toolkit with a reliable input set.
Overview of Capabilities
- Locates and parses AWS’s architecture asset page using Cheerio.
- Downloads ZIP file to a temp path using HTTPS streams.
- Generates a SHA-256 hash to verify whether the download is new.
- Extracts contents to a clean working directory.
- Stores version integrity via reusable checksum paths.
- Cleans up temp files and reports success or failure clearly.
Script Entry and Configuration
At the top of the script, constants are defined to control paths and reusable settings.
const TEMP_ZIP_PATH = path.resolve(".tmp", "aws-icons.zip");
const TARGET_DIR = path.resolve("raw-aws-icons");
const CHECKSUM_PATHS = [
path.join(TARGET_DIR, ".checksum"),
path.join(".checksums", "icons.sha256")
];
Function: getLatestZipUrl()
Retrieves the latest downloadable ZIP file containing AWS icons from the official site.
async function getLatestZipUrl() {
const res = await fetch(AWS_ICON_LISTING_URL);
const html = await res.text();
const $ = cheerio.load(html);
const href = $("a")
.map((i, el) => $(el).attr("href"))
.get()
.find(href => href.endsWith(".zip"));
return new URL(href, AWS_ICON_LISTING_URL).href;
}
Function: downloadFile()
Downloads the ZIP file using stream pipelines for performance.
async function downloadFile(url, dest) {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
await streamPipeline(res.body, fs.createWriteStream(dest));
}
Function: extractZip()
Extracts only the icons directory from the ZIP file into the working path.
async function extractZip(source, hash) {
const zip = new AdmZip(source);
const tempPath = path.resolve(".tmp", hash);
zip.extractAllTo(tempPath);
await fs.ensureDir(TARGET_DIR);
await fs.copy(path.join(tempPath, "Icons"), TARGET_DIR);
await writeChecksum(hash);
await fs.remove(tempPath);
}
Error Handling and Exits
All core logic is wrapped in `try/catch` blocks. Any thrown exception will halt the process and log a clear message.
// Example of wrapper logic
try {
await main();
} catch (err) {
console.error(err);
process.exit(1);
}