@respectify/astro 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ import { createInterface } from 'node:readline/promises';
2
+ import type { ICliLogger, IPrompter, PromptChoice } from './types.js';
3
+
4
+ /** Writes messages to the terminal: information to stdout, warnings and errors to stderr. */
5
+ export class TerminalLogger implements ICliLogger {
6
+ info(message: string): void {
7
+ process.stdout.write(`${message}\n`);
8
+ }
9
+
10
+ warn(message: string): void {
11
+ process.stderr.write(`Warning: ${message}\n`);
12
+ }
13
+
14
+ error(message: string): void {
15
+ process.stderr.write(`Error: ${message}\n`);
16
+ }
17
+ }
18
+
19
+ /** Asks questions on the terminal, when there is one. */
20
+ export class TerminalPrompter implements IPrompter {
21
+ get interactive(): boolean {
22
+ return process.stdin.isTTY === true && process.stdout.isTTY === true;
23
+ }
24
+
25
+ async choose<Value extends string>(question: string, choices: readonly PromptChoice<Value>[]): Promise<Value> {
26
+ const readline = createInterface({ input: process.stdin, output: process.stdout });
27
+ try {
28
+ const menu = choices.map((choice, index) => ` ${index + 1}. ${choice.label}`).join('\n');
29
+ for (;;) {
30
+ const answer = (await readline.question(`${question}\n${menu}\n> `)).trim().toLowerCase();
31
+ const picked = choices[Number(answer) - 1] ?? choices.find((choice) => choice.value === answer);
32
+ if (picked !== undefined) return picked.value;
33
+ process.stdout.write(`Enter a number from 1 to ${choices.length}.\n`);
34
+ }
35
+ } finally {
36
+ readline.close();
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,89 @@
1
+ /** The databases `init` can scaffold a comment store for. */
2
+ export const DATABASE_KINDS = ['postgres', 'sqlite'] as const;
3
+
4
+ /** A database `init` can scaffold a comment store for: Postgres, or SQLite through libSQL/Turso. */
5
+ export type DatabaseKind = (typeof DATABASE_KINDS)[number];
6
+
7
+ /** Where the command writes what it has to say. */
8
+ export interface ICliLogger {
9
+ info(message: string): void;
10
+ warn(message: string): void;
11
+ error(message: string): void;
12
+ }
13
+
14
+ /**
15
+ * The project the command works on. Every path is relative to the project root, and a path
16
+ * that would leave the root is refused.
17
+ */
18
+ export interface IProjectFiles {
19
+ /** True when the path names a file inside the project. */
20
+ exists(relativePath: string): boolean;
21
+
22
+ /** @throws Error when the file is missing or outside the project */
23
+ read(relativePath: string): string;
24
+
25
+ /**
26
+ * Write a new file, creating its folders.
27
+ * @throws Error when the file already exists: nothing is ever overwritten
28
+ */
29
+ create(relativePath: string, content: string): void;
30
+
31
+ /** Add text to the end of an existing file. */
32
+ append(relativePath: string, content: string): void;
33
+ }
34
+
35
+ /** One answer a question accepts. */
36
+ export interface PromptChoice<Value extends string> {
37
+ value: Value;
38
+ label: string;
39
+ }
40
+
41
+ /** Asks the person running the command a question. */
42
+ export interface IPrompter {
43
+ /** True when there is a terminal to ask on. */
44
+ readonly interactive: boolean;
45
+
46
+ choose<Value extends string>(question: string, choices: readonly PromptChoice<Value>[]): Promise<Value>;
47
+ }
48
+
49
+ /** What `init` was asked to do. */
50
+ export interface InitOptions {
51
+ /** The database to scaffold for. When absent it is read from drizzle.config, then asked for. */
52
+ database?: DatabaseKind;
53
+ }
54
+
55
+ /** What happened to one file `init` is responsible for. */
56
+ export type InitStepStatus =
57
+ /** The file did not exist and was written. */
58
+ | 'created'
59
+ /** A line was added to the end of an existing file. */
60
+ | 'updated'
61
+ /** The file already had what it needs, or already exists and was left alone. */
62
+ | 'unchanged'
63
+ /** The command could not safely make the change; `instructions` says what to do by hand. */
64
+ | 'manual';
65
+
66
+ /** The outcome for one file. */
67
+ export interface InitStep {
68
+ status: InitStepStatus;
69
+ /** The file, relative to the project root. */
70
+ path: string;
71
+ /** What the person has to do themselves, for a `manual` step. */
72
+ instructions?: string;
73
+ }
74
+
75
+ /** Everything `init` did and everything left for the person to do. */
76
+ export interface InitResult {
77
+ database: DatabaseKind;
78
+ steps: InitStep[];
79
+ /** The remaining steps, in the order to do them. */
80
+ nextSteps: string[];
81
+ }
82
+
83
+ /** What `init` needs from the outside world. */
84
+ export interface InitDependencies {
85
+ files: IProjectFiles;
86
+ prompter: IPrompter;
87
+ /** The text of the package's default-respectify.config.json. */
88
+ defaultConfigJson: string;
89
+ }
package/src/index.ts CHANGED
@@ -51,9 +51,10 @@ function createVirtualModulesPlugin(modules: Record<string, string>): VirtualMod
51
51
  /**
52
52
  * The Respectify comments integration. Its options are serialized into the server bundle,
53
53
  * so they must be plain data; the comment store and post functions live in the module
54
- * named by `runtime`.
54
+ * named by `runtime`. Every option has a default, so `astro add @respectify/astro` and
55
+ * `respectify-astro init` are enough to set a site up.
55
56
  */
56
- export default function respectify(userOptions: RespectifyIntegrationOptions): AstroIntegration {
57
+ export default function respectify(userOptions: RespectifyIntegrationOptions = {}): AstroIntegration {
57
58
  const parsed = parseIntegrationOptions(userOptions);
58
59
  const resolvedOptions = toResolvedIntegrationOptions(parsed);
59
60
  const routePattern = resolvedOptions.commentsApiPath.replace(/^\//, '');
package/src/options.ts CHANGED
@@ -9,6 +9,9 @@ import type {
9
9
  } from './types';
10
10
 
11
11
  /** The options the server bundle sees when `respectify()` is given only `runtime`. */
12
+ /** Where the runtime module is looked for when `respectify()` is not told. `respectify-astro init` writes it there. */
13
+ export const DEFAULT_RUNTIME_MODULE = './src/respectify.runtime.ts';
14
+
12
15
  export const DEFAULT_INTEGRATION_OPTIONS: ResolvedIntegrationOptions = {
13
16
  configPath: './respectify.config.json',
14
17
  commentsApiPath: '/api/respectify/comments',
@@ -53,7 +56,10 @@ const tolerancesSchema = z.custom<ModerationTolerances>().superRefine((tolerance
53
56
  });
54
57
 
55
58
  const integrationOptionsSchema = z.strictObject({
56
- runtime: z.string().min(1, 'runtime must name the module that calls defineRespectifyRuntime()'),
59
+ runtime: z
60
+ .string()
61
+ .min(1, 'runtime must name the module that calls defineRespectifyRuntime()')
62
+ .default(DEFAULT_RUNTIME_MODULE),
57
63
  configPath: z.string().min(1).default(DEFAULT_INTEGRATION_OPTIONS.configPath),
58
64
  commentsApiPath: z
59
65
  .string()
package/src/types.ts CHANGED
@@ -35,8 +35,9 @@ export interface RespectifyIntegrationOptions {
35
35
  /**
36
36
  * The runtime module: a file whose default export is `defineRespectifyRuntime({ store, ... })`.
37
37
  * A relative path is resolved against the project root; a package specifier is left to Vite.
38
+ * Default: `./src/respectify.runtime.ts`, the file `respectify-astro init` writes.
38
39
  */
39
- runtime: string;
40
+ runtime?: string;
40
41
 
41
42
  /** Path to respectify.config.json (default: ./respectify.config.json) */
42
43
  configPath?: string;