Files
inkreach-teable-cli/dist/lib/ctx.js
T
yeuimu 3810c33d69 refactor: rename CLI command 'inkreach' -> 'inkreach-teable'
Rename the command (and its config namespace) to match the project name:

- bin:          inkreach        -> inkreach-teable
- program name: inkreach        -> inkreach-teable
- config dir:   ~/.inkreach/    -> ~/.inkreach-teable/
- env vars:     INKREACH_*      -> INKREACH_TEABLE_*
- all docs (SKILL.md, guides, api-reference, AGENTS.md, design doc, install.sh)

Unchanged (intentional):
- npm package name: @inkreach/teable-cli
- package directory: packages/inkreach-cli/
- skill name: inkreach-teable-ops
- organization/authors: 'inkreach' (npm scope @inkreach)

Existing config auto-migrated (~/.inkreach -> ~/.inkreach-teable). Verified
end-to-end: auth status, workflow list (636), build clean.
2026-08-05 10:14:26 +08:00

73 lines
2.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requireBaseId = exports.run = exports.makeCtx = void 0;
const config_1 = require("./config");
const client_1 = require("./client");
const request_1 = require("./request");
/**
* Build a Ctx. `command` is the subcommand being executed (so .opts() returns
* its local options); `program` is the top-level program (for global options).
*/
function makeCtx(program, command) {
const g = program.opts();
const cfg = (0, config_1.resolveConfig)({
endpoint: g.endpoint,
token: g.token,
baseId: g.baseId,
});
return {
client: new client_1.Client(cfg),
json: !!g.json,
baseId: cfg.baseId,
command,
program,
};
}
exports.makeCtx = makeCtx;
/**
* Wrap an async command action with normalized error handling.
*
* The wrapped fn receives (ctx, positionalArgs[], options). commander passes
* positional args before the trailing Command; we detect the trailing Command
* and forward everything else as positional, plus parsed options.
*/
function run(fn) {
return async function (...cmdArgs) {
// commander convention: last arg is the Command instance (the subcommand).
const command = cmdArgs[cmdArgs.length - 1];
const program = command?.parent ? findRoot(command) : command;
const positional = cmdArgs.slice(0, -1);
const options = (command?.opts() || {});
const ctx = makeCtx(program, command);
try {
await fn(ctx, positional, options);
}
catch (err) {
const { status, code, message } = (0, request_1.describeError)(err);
if (ctx.json) {
process.stdout.write(JSON.stringify({ error: { status, code, message } }) + '\n');
}
else {
process.stderr.write(`✗ ${code}${status ? ` (${status})` : ''}: ${message}\n`);
}
process.exitCode = 1;
}
};
}
exports.run = run;
function findRoot(cmd) {
let c = cmd;
while (c.parent)
c = c.parent;
return c;
}
/** Require a baseId: from flag, else ctx default, else throw with guidance. */
function requireBaseId(ctx, flagVal) {
const baseId = flagVal || ctx.baseId;
if (!baseId) {
throw new client_1.ApiError(0, 'MISSING_BASE_ID', 'No base id. Pass --base-id <id>, set a default with `inkreach-teable auth login --base-id <id>`, or export INKREACH_TEABLE_BASE_ID.');
}
return baseId;
}
exports.requireBaseId = requireBaseId;