decompose-actions.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. var validate = require('aproba')
  3. var npm = require('../npm.js')
  4. module.exports = function (differences, decomposed, next) {
  5. validate('AAF', arguments)
  6. differences.forEach((action) => {
  7. var cmd = action[0]
  8. var pkg = action[1]
  9. switch (cmd) {
  10. case 'add':
  11. addSteps(decomposed, pkg)
  12. break
  13. case 'update':
  14. updateSteps(decomposed, pkg)
  15. break
  16. case 'move':
  17. moveSteps(decomposed, pkg)
  18. break
  19. case 'remove':
  20. removeSteps(decomposed, pkg)
  21. break
  22. default:
  23. defaultSteps(decomposed, cmd, pkg)
  24. }
  25. })
  26. next()
  27. }
  28. function addAction (decomposed, action, pkg) {
  29. if (decomposed.some((_) => _[0] === action && _[1] === pkg)) return
  30. decomposed.push([action, pkg])
  31. }
  32. function addSteps (decomposed, pkg) {
  33. if (pkg.fromBundle) {
  34. // make sure our source module exists to extract ourselves from
  35. // if we're installing our source module anyway, the duplication
  36. // of these steps will be elided by `addAction` automatically
  37. addAction(decomposed, 'fetch', pkg.fromBundle)
  38. addAction(decomposed, 'extract', pkg.fromBundle)
  39. }
  40. if (!pkg.fromBundle && !pkg.isLink) {
  41. addAction(decomposed, 'fetch', pkg)
  42. addAction(decomposed, 'extract', pkg)
  43. }
  44. if (!pkg.fromBundle || npm.config.get('rebuild-bundle')) {
  45. addAction(decomposed, 'preinstall', pkg)
  46. addAction(decomposed, 'build', pkg)
  47. addAction(decomposed, 'install', pkg)
  48. addAction(decomposed, 'postinstall', pkg)
  49. }
  50. if (!pkg.fromBundle || !pkg.isLink) {
  51. addAction(decomposed, 'finalize', pkg)
  52. }
  53. addAction(decomposed, 'refresh-package-json', pkg)
  54. }
  55. function updateSteps (decomposed, pkg) {
  56. removeSteps(decomposed, pkg.oldPkg)
  57. addSteps(decomposed, pkg)
  58. }
  59. function removeSteps (decomposed, pkg) {
  60. addAction(decomposed, 'unbuild', pkg)
  61. addAction(decomposed, 'remove', pkg)
  62. }
  63. function moveSteps (decomposed, pkg) {
  64. addAction(decomposed, 'move', pkg)
  65. addAction(decomposed, 'build', pkg)
  66. addAction(decomposed, 'install', pkg)
  67. addAction(decomposed, 'postinstall', pkg)
  68. addAction(decomposed, 'refresh-package-json', pkg)
  69. }
  70. function defaultSteps (decomposed, cmd, pkg) {
  71. addAction(decomposed, cmd, pkg)
  72. }