Something went wrong. Try again.
All-purpose web tiles library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179#!/usr/bin/env nodeimport { cwd, exit } from "node:process";import { readFile } from "node:fs/promises";import { resolve } from "node:path";import { program } from 'commander';import chalk from "chalk";import { addCredentials, deleteCredentials, listCredentials, getDefaultUser, setDefaultUser, unsetDefaultUser, getPassword} from "./credentials.js";import { TilePublisher } from './tile-at.js';import makeRel from './rel.js';
// Events dispatched by TilePublisher carry a few custom string properties.type TileEvent = Event & { message?: string; resource?: string };
const rel = makeRel(import.meta.url);const { version } = JSON.parse(await readFile(rel('../package.json'), 'utf8'));
program .name('atile') .description('Manipulating tiles on the AT protocol') .version(version);
// Credentialsprogram .command('login') .description('log a handle into AT so that you can post') .argument('<handle>', 'your internet handle, as in robin.berjon.com') .argument('<appPassword>', 'an app password for that handle') .action(async (handle: string, appPassword: string) => { await addCredentials(handle, appPassword); await setDefaultUser(handle); });program .command('logout') .description('log a specific handle out') .argument('<handle>', 'your internet handle, as in robin.berjon.com') .action(async (handle: string) => { await deleteCredentials(handle); const du = await getDefaultUser(); if (du === handle) await unsetDefaultUser(); });program .command('default-user') .description('set the default handle to use when unspecified') .argument('<handle>', 'your internet handle, as in robin.berjon.com') .action(async (handle: string) => { const users = (await listCredentials()).map(({ account }) => account); if (!users.find(u => u === handle)) die(`No logged in user "${handle}", cannot default to it.`); await setDefaultUser(handle); });program .command('list-users') .description('list all logged in handles you have') .action(async () => { console.log((await listCredentials()).map(({ account }) => account).sort().join('\n')) });
// Tile publishingprogram .command('publish') .description('publish a tile to the Atmosphere') .argument('<dir>', 'path to a directory that contains a tile and its manifest') .option('-u, --user <handle>', 'the handle to use if not the default') .option('-s, --stable-id', 'save and reuse the tile identifier, updates previous version in place') .option('-t, --tid <tid>', 'specify the TID, will override -s') .action(async (dir: string, options: { user?: string; stableId?: boolean; tid?: string }) => { try { console.warn(chalk.blue(`Publishing tile from "${dir}"`)); const tp = new TilePublisher({ reuseIdentifiers: options.stableId, tid: options.tid }); const identifier = options.user || await getDefaultUser(); if (!identifier) return die('No user specified and no default user set.'); const password = await getPassword(identifier); if (!password) return die(`No password stored for "${identifier}".`); await tp.login(identifier, password); console.warn(chalk.blue(`• Logged in`)); dir = resolve(cwd(), dir); await tp.loadFromDirectory(dir); console.warn(chalk.blue(`• Loaded content from "${dir}"`)); tp.addEventListener('warn', (ev: TileEvent) => console.warn(chalk.yellow.bold(`WARNING: ${ev.message}.`))); tp.addEventListener('start-upload', (ev: TileEvent) => console.warn(chalk.gray(`Uploading ${ev.resource}.`))); tp.addEventListener('fail-upload', (ev: TileEvent) => console.warn(chalk.red(`UPLOAD FAILED for ${ev.resource}.`))); tp.addEventListener('done-upload', (ev: TileEvent) => console.warn(chalk.green.bold(`${ev.resource} uploaded OK.`))); const { uri, success } = await tp.publish(); if (success) { console.warn(chalk.green.bold(`Tile published: ${uri}.`)); } else { console.warn(chalk.red(`FAILED to publish tile.`)); exit(1); } } catch (err) { console.error(chalk.red(err, (err as Error).stack)); exit(1); } });program .command('delete') .description('delete a tile from the Atmosphere') .argument('<dirOrATURL>', 'path to a directory that contains a tile or at: URL of one') .option('-u, --user <handle>', 'the handle to use if not the default') .action(async (dirOrURL: string, options: { user?: string }) => { try { console.warn(chalk.blue(`Deleting tile from "${dirOrURL}"`)); const tp = new TilePublisher(); const identifier = options.user || await getDefaultUser(); if (!identifier) return die('No user specified and no default user set.'); const password = await getPassword(identifier); if (!password) return die(`No password stored for "${identifier}".`); await tp.login(identifier, password); console.warn(chalk.blue(`• Logged in`)); const { success, uri } = await tp.delete(dirOrURL); tp.addEventListener('warn', (ev: TileEvent) => console.warn(chalk.yellow.bold(`WARNING: ${ev.message}.`))); if (success) { console.warn(chalk.green.bold(`Tile deleted: ${uri}.`)); } else { console.warn(chalk.red(`FAILED to delete tile.`)); exit(1); } } catch (err) { console.error(chalk.red(err, (err as Error).stack)); exit(1); } }) ;
// Bundle into a CARprogram .command('bundle') .description('bundle a tile into a .tile carball') .argument('<dir>', 'path to a directory that contains a tile and its manifest') .argument('<out>', 'the carball to write to (will be overwritten if it exists)') .action(async (dir: string, out: string) => { try { console.warn(chalk.blue(`Bundle tile from "${dir}" to "${out}"`)); const tp = new TilePublisher(); dir = resolve(cwd(), dir); await tp.loadFromDirectory(dir); console.warn(chalk.blue(`• Loaded content from "${dir}"`)); tp.addEventListener('warn', (ev: TileEvent) => console.warn(chalk.yellow.bold(`WARNING: ${ev.message}.`))); const success = await tp.bundle(out); if (success) { console.warn(chalk.green.bold(`Tile bundled: ${out}.`)); } else { console.warn(chalk.red(`FAILED to bundle tile.`)); exit(1); } } catch (err) { console.error(chalk.red(err, (err as Error).stack)); exit(1); } });
program.parse();
function die (str: string) { console.error(`Error: ${str}`); exit(1);}