@@ -37,6 +37,7 @@ import {
3737import { NOTIFY_KIND_PATTERN , NOTIFY_PARAM_NAME , NOTIFY_TEMPLATE_MAX , templatePlaceholders , validateConfig , type Project } from "../config.ts" ;
3838import { ADMIN_TOKEN_VAR , registerSecret } from "../env.ts" ;
3939import { setJsonPath } from "../json.ts" ;
40+ import { DOCS } from "../tui.ts" ;
4041import { bold , dim , emitJson , info , isJson , red , warn , yellow } from "../log.ts" ;
4142import type { OpenCloud } from "../opencloud.ts" ;
4243import { DS_READ_SCOPE } from "../seqstore.ts" ;
@@ -210,10 +211,70 @@ export async function notifyCommand(args: ParsedArgs, deps: NotifyDeps = {}) {
210211 }
211212}
212213
214+ /** Where the docs explain kinds (linked from the "how to start" errors). */
215+ export const NOTIFY_KINDS_DOCS = `${ DOCS } /guides/notifications.md#kinds` ;
216+
217+ /** A kind's minimal declaration: valid as it is, the text to be rewritten. */
218+ export function kindStub ( ) : Record < string , unknown > {
219+ return { text : "Your message with a {placeholder}" , priority : 50 , repeat : "daily" , send : "preferred_hour" } ;
220+ }
221+
222+ /** A minimal ready-to-paste `notifications` block with one kind (valid as it is). */
223+ export function notificationsStub ( kind : string ) : Record < string , unknown > {
224+ return {
225+ dailyCap : 1 ,
226+ quietAfterLeaveMinutes : 120 ,
227+ lapse : [
228+ [ 14 , 7 ] ,
229+ [ 30 , 2 ] ,
230+ [ 60 , 1 ] ,
231+ ] ,
232+ unopenedBackoff : { after : 5 , factor : 0.5 , days : 14 } ,
233+ kinds : { [ kind ] : kindStub ( ) } ,
234+ } ;
235+ }
236+
237+ function indent ( text : string , by = " " ) : string {
238+ return text
239+ . split ( "\n" )
240+ . map ( ( line ) => by + line )
241+ . join ( "\n" ) ;
242+ }
243+
244+ /** The error for a typetorch.json with no `notifications`: how to start, with a block to paste. */
245+ export function noNotificationsError ( kind = "my_kind" ) : Error {
246+ // Pretty, with number pairs kept on one line ("lapse": [[14, 7], ...]).
247+ const pretty = JSON . stringify ( notificationsStub ( kind ) , null , 2 ) . replace ( / \[ \s * ( - ? [ \d . ] + ) , \s * ( - ? [ \d . ] + ) \s * \] / g, "[$1, $2]" ) ;
248+ const block = `"notifications": ${ pretty . replace ( / \[ \s * ( \[ [ ^ \] ] * \] (?: , \s * \[ [ ^ \] ] * \] ) * ) \s * \] / g, ( _ , inner : string ) => `[${ inner . replace ( / , \s * \[ / g, ", [" ) } ]` ) } ` ;
249+ return new Error (
250+ [
251+ `typetorch.json has no "notifications" section. Add one to start (a minimal block, valid as it is; rewrite the text):` ,
252+ "" ,
253+ indent ( block ) ,
254+ "" ,
255+ `Kinds (priority, repeat, send, text and {placeholders}): ${ NOTIFY_KINDS_DOCS } ` ,
256+ ] . join ( "\n" ) ,
257+ ) ;
258+ }
259+
260+ /** The error for a kind that isn't declared: its stub to paste under notifications.kinds. */
261+ export function unknownKindError ( kind : string , declared : string [ ] ) : Error {
262+ const block = `"${ kind } ": ${ JSON . stringify ( kindStub ( ) ) } ` ;
263+ return new Error (
264+ [
265+ `"${ kind } " isn't a kind in typetorch.json (declared: ${ declared . join ( ", " ) || "none" } ). Add it under "notifications" > "kinds" first:` ,
266+ "" ,
267+ indent ( block ) ,
268+ "" ,
269+ `Kinds (priority, repeat, send, text and {placeholders}): ${ NOTIFY_KINDS_DOCS } ` ,
270+ ] . join ( "\n" ) ,
271+ ) ;
272+ }
273+
213274async function notifyPush ( args : ParsedArgs , deps : NotifyDeps ) {
214275 const proj = project ( args ) ;
215276 const config = proj . config . notifications ;
216- if ( ! config ) throw new Error ( `typetorch.json has no "notifications" section: declare the kinds first (docs: guides/notifications.md)` ) ;
277+ if ( ! config ) throw noNotificationsError ( ) ;
217278 const kinds = Object . entries ( config . kinds ) ;
218279 const noTemplate = kinds . filter ( ( [ , kind ] ) => ! kind . template ) . map ( ( [ name ] ) => name ) ;
219280 if ( flagBool ( args , "dry-run" ) ) {
@@ -356,7 +417,7 @@ export function templateRows(proj: Pick<Project, "config">): TemplateRow[] {
356417
357418async function notifyTemplates ( args : ParsedArgs ) {
358419 const proj = project ( args ) ;
359- if ( ! proj . config . notifications ) throw new Error ( `typetorch.json has no "notifications" section: declare the kinds first (docs: guides/notifications.md)` ) ;
420+ if ( ! proj . config . notifications ) throw noNotificationsError ( ) ;
360421 const rows = templateRows ( proj ) ;
361422 const url = dashboardUrl ( proj . config . universeId ) ;
362423 if ( isJson ( ) ) return emitJson ( { dashboard : url , menu : "Engagement > Notifications" , kinds : rows } ) ;
@@ -388,8 +449,8 @@ async function notifyTemplateSet(args: ParsedArgs, kindArg: string | undefined,
388449 if ( id . length > NOTIFY_TEMPLATE_MAX || ! / ^ [ ^ \s { } " ] + $ / . test ( id ) ) throw new UsageError ( `"${ id } " isn't a message id (1-${ NOTIFY_TEMPLATE_MAX } characters, no spaces)` ) ;
389450 const proj = project ( args ) ;
390451 const config = proj . config . notifications ;
391- if ( ! config ) throw new Error ( `typetorch.json has no "notifications" section` ) ;
392- if ( ! ( kind in config . kinds ) ) throw new Error ( `" ${ kind } " isn't a kind in typetorch.json ( ${ Object . keys ( config . kinds ) . join ( ", " ) || "none" } )` ) ;
452+ if ( ! config ) throw noNotificationsError ( kind ) ;
453+ if ( ! ( kind in config . kinds ) ) throw unknownKindError ( kind , Object . keys ( config . kinds ) ) ;
393454 const before = config . kinds [ kind ] . template ;
394455 if ( before === id ) {
395456 if ( isJson ( ) ) return emitJson ( { kind, template : id , changed : false } ) ;
0 commit comments