Files
jackwener__opencli/clis/lesswrong/comments.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

65 lines
2.3 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { EmptyResultError } from '@jackwener/opencli/errors';
import { DOMAIN, SITE, gqlEscape, gqlRequest, parsePostId, stripHtml, } from './_helpers.js';
cli({
site: SITE,
name: 'comments',
description: 'Top comments on a post',
domain: DOMAIN,
strategy: Strategy.PUBLIC,
browser: false,
args: [
{
name: 'url-or-id',
type: 'string',
required: true,
positional: true,
help: 'Post URL or LessWrong post ID',
},
{ name: 'limit', type: 'int', default: 5, help: 'Number of comments' },
],
columns: ['rank', 'score', 'author', 'text'],
func: async (_page, kwargs) => {
const postId = gqlEscape(parsePostId(String(kwargs['url-or-id'])));
const limit = Number(kwargs.limit ?? 5);
// Fetch post title and comments in parallel
const [postData, commentsData] = await Promise.all([
gqlRequest(`query PostTitle {
post(input: {selector: {documentId: "${postId}"}}) {
result { _id title slug }
}
}`),
gqlRequest(`query Comments {
comments(input: {terms: {view: "postCommentsTop", postId: "${postId}", limit: ${limit}}}) {
results { _id user { displayName } baseScore htmlBody postedAt }
}
}`),
]);
const post = postData?.post?.result;
if (!post?._id) {
throw new EmptyResultError('lesswrong comments', `Post "${postId}" not found`);
}
const comments = (commentsData?.comments?.results ?? []);
const rows = [];
// First row: post context
rows.push({
rank: '',
score: '',
author: '',
text: `Comments on: ${post.title ?? 'Untitled'} (https://${DOMAIN}/posts/${post._id}/${post.slug})`,
});
for (let i = 0; i < comments.length; i++) {
const item = comments[i];
const user = item.user;
const raw = stripHtml(item.htmlBody ?? '');
rows.push({
rank: i + 1,
score: item.baseScore ?? 0,
author: user?.displayName ?? 'Unknown',
text: raw.length > 500 ? `${raw.slice(0, 500)}...` : raw,
});
}
return rows;
},
});