cordis-yaml.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Cordis YAML parsing and Loader-entry classification shared by repository checks.
  3. * @module scripts/cordis-yaml
  4. */
  5. import * as yaml from 'js-yaml'
  6. /** A Loader `!!js` expression preserved as data instead of executed. */
  7. export interface JsExpr {
  8. __jsExpr: string
  9. }
  10. const jsExprType = new yaml.Type('tag:yaml.org,2002:js', {
  11. kind: 'scalar',
  12. resolve: data => typeof data === 'string',
  13. construct: (data: unknown): JsExpr => {
  14. if (typeof data !== 'string') throw new TypeError('!!js requires a scalar string')
  15. return { __jsExpr: data }
  16. },
  17. })
  18. const schema = yaml.JSON_SCHEMA.extend(jsExprType)
  19. /**
  20. * Parse a Cordis config while preserving Loader `!!js` expressions as data.
  21. * @param source - Cordis YAML source text.
  22. * @returns the parsed YAML value.
  23. */
  24. export function loadCordisYaml(source: string): unknown {
  25. return yaml.load(source, { schema })
  26. }
  27. /**
  28. * Test whether a value is a preserved Loader `!!js` expression.
  29. * @param value - parsed YAML value.
  30. * @returns whether the value contains one preserved expression.
  31. */
  32. export function isJsExpr(value: unknown): value is JsExpr {
  33. return typeof value === 'object'
  34. && value !== null
  35. && typeof (value as Record<string, unknown>).__jsExpr === 'string'
  36. }
  37. /**
  38. * Test whether a Loader entry owns nested entries in its `config` array.
  39. * @param value - parsed Loader entry.
  40. * @returns whether the entry is an explicit or package-named Cordis group.
  41. */
  42. export function isCordisGroupEntry(value: unknown): value is Record<string, unknown> & { config: unknown[] } {
  43. return typeof value === 'object'
  44. && value !== null
  45. && Array.isArray((value as Record<string, unknown>).config)
  46. && ((value as Record<string, unknown>).group === true
  47. || (value as Record<string, unknown>).name === '@deepseek-ai/cordis-plugin-group')
  48. }