This code generates a JSON object summarizing information about source...

August 23, 2025 at 06:17 PM

const fs = require("fs"); const crypto = require("crypto"); const cp = require("child_process"); const SITES_DIR = "src/sites"; const DISABLED_SOURCES = ["Sankaku", "Tumblr"]; function mkPath(source, filename) { return SITES_DIR + "/" + source + "/" + filename; } function parseSites(path) { if (!fs.existsSync(path)) { return []; } return fs.readFileSync(path, 'utf-8') .split('\n') .map((line) => line.trim()) .filter((line) => line.length > 0); } function getLastCommit(path) { const cmd = `git log -n 1 --pretty=format:%H/%an/%aI/%s -- "${path}"`; const out = cp.execSync(cmd).toString(); if (!out) { return undefined; } const parts = out.split("/"); return { hash: parts[0], author: parts[1], date: parts[2], subject: parts[3], }; } const args = process.argv.slice(2); const branch = args.length > 0 ? args[0] : "master"; const isNightly = branch === "develop"; const output = { name: "Official Grabber sources" + (isNightly ? " (nightly)" : ""), home: "https://github.com/Bionus/imgbrd-grabber", url: "https://github.com/Bionus/imgbrd-grabber/releases/download/sources-" + branch + "/", sources: [], }; const sources = fs .readdirSync(SITES_DIR) .filter((f) => fs.statSync(SITES_DIR + "/" + f).isDirectory()); for (const source of sources) { // Skip disabled sources if (DISABLED_SOURCES.includes(source)) { continue; } // Skip directories without a "model.ts" file const modelFile = mkPath(source, "model.ts"); if (!fs.existsSync(modelFile)) { continue; } const contents = fs.readFileSync(modelFile, "utf-8"); const name = /name:\s*"([^"]+)"/.exec(contents); const version = /version:\s*"([^"]+)"/.exec(contents); const defaultSites = parseSites(mkPath(source, "sites.txt")); const supportedSites = parseSites(mkPath(source, "supported.txt")); const lastCommit = getLastCommit(modelFile); output.sources.push({ slug: source.replace(/[^a-z0-9_.-]+/gi, ".").replace(/^\.+|\.+$/g, ""), name: name ? name[1] : source, version: version ? version[1] : undefined, hash: crypto.createHash("sha256").update(fs.readFileSync(mkPath(source, "model.js"), "utf-8")).digest("hex"), lastCommit, defaultSites, supportedSites: supportedSites.length ? supportedSites : defaultSites, }); } console.log(JSON.stringify(output));

This code generates a JSON object summarizing information about source directories for an application. Here's a breakdown of what this code does:

  1. Import Necessary Modules:

    • fs: For file system operations like reading files and directories.
    • crypto: To compute a SHA-256 hash for a file.
    • child_process (cp): To execute shell commands (e.g., running Git commands).
  2. Constants:

    • SITES_DIR: The parent directory where all site directories are stored (src/sites).
    • DISABLED_SOURCES: A list of directories (sources) to exclude from processing (Sankaku, Tumblr).
  3. Helper Functions:

    • mkPath(source, filename): Constructs the path to a file within a source directory.
    • parseSites(path): Reads and parses a file (e.g., sites.txt or supported.txt) into an array of lines, filtering out empty or whitespace-only lines.
    • getLastCommit(path): Runs a Git command (git log) to retrieve the last commit for a given file. It parses and returns details such as commit hash, author, date, and subject.
  4. Processing Logic:

    • The main script retrieves arguments passed to the Node.js process.
    • It determines if the build is "nightly" based on whether the actively checked branch is develop.
    • It initializes the output object, which will store metadata about the sources.
  5. Iterating Over Source Directories:

    • Retrieves a list of all directories within SITES_DIR.
    • For each directory:
      • Skips directories if they are in the DISABLED_SOURCES list.
      • Checks if the directory contains a model.ts file (a required file for source processing).
    • Reads and extracts information from model.ts:
      • Finds a name and version property using regular expressions.
      • Parses the sites.txt and supported.txt files within the directory.
    • Computes a unique SHA-256 hash of the model.js file contents (if it exists).
    • Retrieves the last commit for model.ts using the getLastCommit function.
    • Compiles this metadata into an object and adds it to the sources array in the output object.
  6. Final Output:

    • The process ends by converting the output object to a JSON string (using JSON.stringify(output)) and printing it to the console.

Purpose

This script is part of an automated system (likely for a web app or software project). It gathers metadata about sources (e.g., libraries, plugins, or services) contained in a specific directory structure (src/sites). It includes details like:

  • Each source's name, version, and SHA-256 hash of a JavaScript file.
  • Supported or default "sites" (parsed from sites.txt and supported.txt files).
  • The last Git commit affecting the source (model.ts).

The final JSON output can be used for release management, source listing on a website, or other purposes such as identifying available plugins in this application.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node