dev-engines.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const satisfies = require('semver/functions/satisfies')
  2. const validRange = require('semver/ranges/valid')
  3. const recognizedOnFail = [
  4. 'ignore',
  5. 'warn',
  6. 'error',
  7. 'download',
  8. ]
  9. const recognizedProperties = [
  10. 'name',
  11. 'version',
  12. 'onFail',
  13. ]
  14. const recognizedEngines = [
  15. 'packageManager',
  16. 'runtime',
  17. 'cpu',
  18. 'libc',
  19. 'os',
  20. ]
  21. /** checks a devEngine dependency */
  22. function checkDependency (wanted, current, opts) {
  23. const { engine } = opts
  24. if ((typeof wanted !== 'object' || wanted === null) || Array.isArray(wanted)) {
  25. throw new Error(`Invalid non-object value for "${engine}"`)
  26. }
  27. const properties = Object.keys(wanted)
  28. for (const prop of properties) {
  29. if (!recognizedProperties.includes(prop)) {
  30. throw new Error(`Invalid property "${prop}" for "${engine}"`)
  31. }
  32. }
  33. if (!properties.includes('name')) {
  34. throw new Error(`Missing "name" property for "${engine}"`)
  35. }
  36. if (typeof wanted.name !== 'string') {
  37. throw new Error(`Invalid non-string value for "name" within "${engine}"`)
  38. }
  39. if (typeof current.name !== 'string' || current.name === '') {
  40. throw new Error(`Unable to determine "name" for "${engine}"`)
  41. }
  42. if (properties.includes('onFail')) {
  43. if (typeof wanted.onFail !== 'string') {
  44. throw new Error(`Invalid non-string value for "onFail" within "${engine}"`)
  45. }
  46. if (!recognizedOnFail.includes(wanted.onFail)) {
  47. throw new Error(`Invalid onFail value "${wanted.onFail}" for "${engine}"`)
  48. }
  49. }
  50. if (wanted.name !== current.name) {
  51. return new Error(
  52. `Invalid name "${wanted.name}" does not match "${current.name}" for "${engine}"`
  53. )
  54. }
  55. if (properties.includes('version')) {
  56. if (typeof wanted.version !== 'string') {
  57. throw new Error(`Invalid non-string value for "version" within "${engine}"`)
  58. }
  59. if (typeof current.version !== 'string' || current.version === '') {
  60. throw new Error(`Unable to determine "version" for "${engine}" "${wanted.name}"`)
  61. }
  62. if (validRange(wanted.version)) {
  63. if (!satisfies(current.version, wanted.version, opts.semver)) {
  64. return new Error(
  65. // eslint-disable-next-line max-len
  66. `Invalid semver version "${wanted.version}" does not match "${current.version}" for "${engine}"`
  67. )
  68. }
  69. } else if (wanted.version !== current.version) {
  70. return new Error(
  71. `Invalid version "${wanted.version}" does not match "${current.version}" for "${engine}"`
  72. )
  73. }
  74. }
  75. }
  76. /** checks devEngines package property and returns array of warnings / errors */
  77. function checkDevEngines (wanted, current = {}, opts = {}) {
  78. if ((typeof wanted !== 'object' || wanted === null) || Array.isArray(wanted)) {
  79. throw new Error(`Invalid non-object value for devEngines`)
  80. }
  81. const errors = []
  82. for (const engine of Object.keys(wanted)) {
  83. if (!recognizedEngines.includes(engine)) {
  84. throw new Error(`Invalid property "${engine}"`)
  85. }
  86. const dependencyAsAuthored = wanted[engine]
  87. const dependencies = [dependencyAsAuthored].flat()
  88. const currentEngine = current[engine] || {}
  89. // this accounts for empty array eg { runtime: [] } and ignores it
  90. if (dependencies.length === 0) {
  91. continue
  92. }
  93. const depErrors = []
  94. for (const dep of dependencies) {
  95. const result = checkDependency(dep, currentEngine, { ...opts, engine })
  96. if (result) {
  97. depErrors.push(result)
  98. }
  99. }
  100. const invalid = depErrors.length === dependencies.length
  101. if (invalid) {
  102. const lastDependency = dependencies[dependencies.length - 1]
  103. let onFail = lastDependency.onFail || 'error'
  104. if (onFail === 'download') {
  105. onFail = 'error'
  106. }
  107. const err = Object.assign(new Error(`Invalid engine "${engine}"`), {
  108. errors: depErrors,
  109. engine,
  110. isWarn: onFail === 'warn',
  111. isError: onFail === 'error',
  112. current: currentEngine,
  113. required: dependencyAsAuthored,
  114. })
  115. errors.push(err)
  116. }
  117. }
  118. return errors
  119. }
  120. module.exports = {
  121. checkDevEngines,
  122. }