Skip to content

Commit

Permalink
feat: Support background, watch, and system metadata with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
John Lindquist committed Feb 4, 2025
1 parent b3135c2 commit 321e917
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
114 changes: 114 additions & 0 deletions src/core/scriptlets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,3 +1566,117 @@ say('poop')
t.is(scripts[0].name, "Silly")
t.is(scripts[0].schedule, "*/5 * * * * *")
})

ava("parseMarkdownAsScriptlets parses background metadata", async (t) => {
const markdown = `
## Background Task
<!--
background: true
-->
\`\`\`ts
console.log('Running in background')
\`\`\`
`.trim()
const scripts = await parseMarkdownAsScriptlets(markdown)
t.is(scripts.length, 1)
t.is(scripts[0].name, "Background Task")
t.is(scripts[0].background, "true")

Check failure on line 1585 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-windows

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.\r

Check failure on line 1585 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-mac-and-ubuntu (macos-latest)

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.

Check failure on line 1585 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-mac-and-ubuntu (ubuntu-latest)

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.
})

ava("parseMarkdownAsScriptlets validates background metadata", async (t) => {
const markdown = `
## Invalid Background
<!--
background: invalid
-->
\`\`\`ts
console.log('test')
\`\`\`
`.trim()

await t.throwsAsync(async () => {
await parseMarkdownAsScriptlets(markdown)
}, { message: "Invalid background value: invalid. Must be 'true' or 'false'" })
})

ava("parseMarkdownAsScriptlets parses watch metadata", async (t) => {
const markdown = `
## Watch Files
<!--
watch: src/**/*.ts
-->
\`\`\`ts
console.log('Watching files')
\`\`\`
`.trim()
const scripts = await parseMarkdownAsScriptlets(markdown)
t.is(scripts.length, 1)
t.is(scripts[0].name, "Watch Files")
t.is(scripts[0].watch, "src/**/*.ts")
})

ava("parseMarkdownAsScriptlets validates watch metadata", async (t) => {
const markdown = `
## Invalid Watch
<!--
watch:
-->
\`\`\`ts
console.log('test')
\`\`\`
`.trim()

await t.throwsAsync(async () => {
await parseMarkdownAsScriptlets(markdown)
}, { message: "Watch metadata must have a value" })
})

ava("parseMarkdownAsScriptlets parses system metadata", async (t) => {
const markdown = `
## System Task
<!--
system: darwin
-->
\`\`\`ts
console.log('Running on Mac')
\`\`\`
`.trim()
const scripts = await parseMarkdownAsScriptlets(markdown)
t.is(scripts.length, 1)
t.is(scripts[0].name, "System Task")
t.is(scripts[0].system, "darwin")
})

ava("parseMarkdownAsScriptlets parses multiple metadata", async (t) => {
const markdown = `
## Multi Metadata
<!--
background: true
watch: src/**/*.ts
system: darwin
schedule: */5 * * * *
-->
\`\`\`ts
console.log('Multiple metadata test')
\`\`\`
`.trim()
const scripts = await parseMarkdownAsScriptlets(markdown)
t.is(scripts.length, 1)
t.is(scripts[0].name, "Multi Metadata")
t.is(scripts[0].background, "true")

Check failure on line 1678 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-windows

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.

Check failure on line 1678 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-mac-and-ubuntu (macos-latest)

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.

Check failure on line 1678 in src/core/scriptlets.test.ts

View workflow job for this annotation

GitHub Actions / test-mac-and-ubuntu (ubuntu-latest)

Argument of type '"true"' is not assignable to parameter of type 'boolean | "auto"'.
t.is(scripts[0].watch, "src/**/*.ts")
t.is(scripts[0].system, "darwin")
t.is(scripts[0].schedule, "*/5 * * * *")
})
22 changes: 18 additions & 4 deletions src/core/scriptlets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,30 @@ export let parseMarkdownAsScriptlets = async (markdown: string): Promise<Scriptl
let key = line.slice(0, indexOfColon).trim()
let value = line.slice(indexOfColon + 1).trim()
let lowerCaseKey = key.toLowerCase()
let ignore = ['background', 'watch', 'system'].includes(lowerCaseKey)
if (ignore) {
continue
}

// Validate schedule metadata if present
if (lowerCaseKey === 'schedule') {
const cronRegex = /^(\S+\s+){4,5}\S+$/
if (!cronRegex.test(value)) {
throw new Error(`Invalid cron syntax in schedule metadata: ${value}`)
}
}

// Validate background metadata if present
if (lowerCaseKey === 'background') {
const validValues = ['true', 'false']
if (!validValues.includes(value.toLowerCase())) {
throw new Error(`Invalid background value: ${value}. Must be 'true' or 'false'`)
}
}

// Validate watch metadata if present
if (lowerCaseKey === 'watch') {
if (!value.trim()) {
throw new Error('Watch metadata must have a value')
}
}

if (parsingMarkdownMetadata) {
markdownMetadata[lowerCaseKey] = value
} else {
Expand Down

0 comments on commit 321e917

Please sign in to comment.