Something went wrong. Try again.
A nicer S3 CLI
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566#!/usr/bin/env node// Vendor ordner.fyi's S3 client, which is not published, into src/vendor.//// node tools/vendor.ts [path to an ordner.fyi checkout] (default: ~/Code/ordner.fyi)//// Each package's sources are copied as they are — tests left out — into// src/vendor/ordner.fyi/<package>/, and the one thing changed is where// `@ordner.fyi/<package>` imports point: at the vendored copy, by a relative// path, so the archive needs no node_modules. `@ordner.fyi/observe` is the// exception: it is a server's event log and metrics, which write to stdout,// so it points at src/observe.ts, which carries the same names for a CLI.// What was copied, from which commit, is written to src/vendor/ordner.fyi/VENDOR.json.import * as FS from 'node:fs';import * as OS from 'node:os';import * as PATH from 'node:path';import { spawnSync } from 'node:child_process';
const ROOT = PATH.resolve(import.meta.dirname, '..');const SOURCE = PATH.resolve(process.argv[2] ?? PATH.join(OS.homedir(), 'Code', 'ordner.fyi'));const TARGET = PATH.join(ROOT, 'src', 'vendor', 'ordner.fyi');
/** The packages, and the module each one's name stands for. */const PACKAGES: Record<string, string> = { 's3': 's3.ts', 'xml': 'index.ts', 'capped-body': 'index.ts', 'http-date': 'http-date.ts',};/** Single dependency-free files taken from packages not vendored whole. */const FILES: Record<string, string[]> = { observe: ['causes.ts'] };/** Where the rest of a package's name points instead, relative to src/. */const SHIMS: Record<string, string> = { observe: 'observe.ts' };
const commit = spawnSync('git', ['-C', SOURCE, 'log', '-1', '--format=%H', '--', ...Object.keys(PACKAGES).map((name) => `packages/${name}`)], { encoding: 'utf-8' }).stdout.trim();const dirty = spawnSync('git', ['-C', SOURCE, 'status', '--porcelain', '--', ...Object.keys(PACKAGES).map((name) => `packages/${name}`)], { encoding: 'utf-8' }).stdout.trim();if (!commit) fail(`${SOURCE} is not an ordner.fyi checkout`);
FS.rmSync(TARGET, { recursive: true, force: true });const copied: Record<string, string[]> = {};for (const [name, only] of [...Object.keys(PACKAGES).map((name) => [name, undefined] as const), ...Object.entries(FILES)]) { const from = PATH.join(SOURCE, 'packages', name, 'src'); const files = (only ?? FS.readdirSync(from).filter((file) => file.endsWith('.ts') && !file.endsWith('.test.ts') && !file.endsWith('.d.ts'))).sort(); const to = PATH.join(TARGET, name); FS.mkdirSync(to, { recursive: true }); for (const file of files) { const text = FS.readFileSync(PATH.join(from, file), 'utf-8').replace(/(from\s+|import\s*\(\s*)(["'])@ordner\.fyi\/([\w-]+)\2/g, (_, lead: string, quote: string, dep: string) => { const target = PACKAGES[dep] ? PATH.join(TARGET, dep, PACKAGES[dep]) : SHIMS[dep] ? PATH.join(ROOT, 'src', SHIMS[dep]) : fail(`${name}/${file} imports @ordner.fyi/${dep}, which is neither vendored nor shimmed`); return `${lead}${quote}${PATH.relative(to, target).split(PATH.sep).join('/').replace(/^(?!\.)/, './')}${quote}`; }); FS.writeFileSync(PATH.join(to, file), text); } copied[name] = files;}FS.writeFileSync(PATH.join(TARGET, 'VENDOR.json'), `${JSON.stringify({ source: 'ordner.fyi (unpublished), packages/*/src — EUPL-1.2, © Philipp Dunkel', commit, ...(dirty ? { uncommitted: dirty.split('\n') } : {}), files: copied,}, null, 2)}\n`);console.error(`* vendored ${Object.values(copied).flat().length} files from ${SOURCE} @ ${commit.slice(0, 12)}${dirty ? ' (with uncommitted changes!)' : ''}`);
function fail(reason: string): never { console.error(`error: ${reason}`); process.exit(1);}