update-scripts.js 664 B

1234567891011121314151617181920212223242526272829
  1. const updateScripts = ({ content, originalContent = {} }) => {
  2. const newScripts = content.scripts
  3. if (!newScripts) {
  4. return originalContent
  5. }
  6. // validate scripts content being appended
  7. const hasInvalidScripts = () =>
  8. Object.entries(newScripts)
  9. .some(([key, value]) =>
  10. typeof key !== 'string' || typeof value !== 'string')
  11. if (hasInvalidScripts()) {
  12. throw Object.assign(
  13. new TypeError(
  14. 'package.json scripts should be a key-value pair of strings.'),
  15. { code: 'ESCRIPTSINVALID' }
  16. )
  17. }
  18. return {
  19. ...originalContent,
  20. scripts: {
  21. ...newScripts,
  22. },
  23. }
  24. }
  25. module.exports = updateScripts