node-build-system.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2016, 2020 Jelle Licht <jlicht@fsfe.org>
  4. ;;; Copyright © 2019, 2021 Timothy Sample <samplet@ngyro.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build node-build-system)
  21. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  22. #:use-module (guix build utils)
  23. #:use-module (guix build json)
  24. #:use-module (ice-9 ftw)
  25. #:use-module (ice-9 match)
  26. #:use-module (srfi srfi-1)
  27. #:export (%standard-phases
  28. node-build))
  29. ;; Commentary:
  30. ;;
  31. ;; Builder-side code of the standard Node/NPM package install procedure.
  32. ;;
  33. ;; Code:
  34. (define (set-home . _)
  35. (with-directory-excursion ".."
  36. (let loop ((i 0))
  37. (let ((dir (string-append "npm-home-" (number->string i))))
  38. (if (directory-exists? dir)
  39. (loop (1+ i))
  40. (begin
  41. (mkdir dir)
  42. (setenv "HOME" (string-append (getcwd) "/" dir))
  43. (format #t "set HOME to ~s~%" (getenv "HOME")))))))
  44. #t)
  45. (define (module-name module)
  46. (let* ((package.json (string-append module "/package.json"))
  47. (package-meta (call-with-input-file package.json read-json)))
  48. (assoc-ref package-meta "name")))
  49. (define (index-modules input-paths)
  50. (define (list-modules directory)
  51. (append-map (lambda (x)
  52. (if (string-prefix? "@" x)
  53. (list-modules (string-append directory "/" x))
  54. (list (string-append directory "/" x))))
  55. (filter (lambda (x)
  56. (not (member x '("." ".."))))
  57. (or (scandir directory) '()))))
  58. (let ((index (make-hash-table (* 2 (length input-paths)))))
  59. (for-each (lambda (dir)
  60. (let ((nm (string-append dir "/lib/node_modules")))
  61. (for-each (lambda (module)
  62. (hash-set! index (module-name module) module))
  63. (list-modules nm))))
  64. input-paths)
  65. index))
  66. (define* (patch-dependencies #:key inputs #:allow-other-keys)
  67. (define index (index-modules (map cdr inputs)))
  68. (define (resolve-dependencies package-meta meta-key)
  69. (fold (lambda (key+value acc)
  70. (match key+value
  71. ('@ acc)
  72. ((key . value) (acons key (hash-ref index key value) acc))))
  73. '()
  74. (or (assoc-ref package-meta meta-key) '())))
  75. (with-atomic-file-replacement "package.json"
  76. (lambda (in out)
  77. (let ((package-meta (read-json in)))
  78. (assoc-set! package-meta "dependencies"
  79. (append
  80. '(@)
  81. (resolve-dependencies package-meta "dependencies")
  82. (resolve-dependencies package-meta "peerDependencies")))
  83. (assoc-set! package-meta "devDependencies"
  84. (append
  85. '(@)
  86. (resolve-dependencies package-meta "devDependencies")))
  87. (write-json package-meta out))))
  88. #t)
  89. (define* (configure #:key outputs inputs #:allow-other-keys)
  90. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  91. (invoke npm "--offline" "--ignore-scripts" "install")
  92. #t))
  93. (define* (build #:key inputs #:allow-other-keys)
  94. (let ((package-meta (call-with-input-file "package.json" read-json)))
  95. (if (and=> (assoc-ref package-meta "scripts")
  96. (lambda (scripts)
  97. (assoc-ref scripts "build")))
  98. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  99. (invoke npm "run" "build"))
  100. (format #t "there is no build script to run~%"))
  101. #t))
  102. (define* (check #:key tests? inputs #:allow-other-keys)
  103. "Run 'npm test' if TESTS?"
  104. (if tests?
  105. (let ((npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  106. (invoke npm "test"))
  107. (format #t "test suite not run~%"))
  108. #t)
  109. (define* (repack #:key inputs #:allow-other-keys)
  110. (invoke "tar" "-czf" "../package.tgz" ".")
  111. #t)
  112. (define* (install #:key outputs inputs #:allow-other-keys)
  113. "Install the node module to the output store item."
  114. (let ((out (assoc-ref outputs "out"))
  115. (npm (string-append (assoc-ref inputs "node") "/bin/npm")))
  116. (invoke npm "--prefix" out
  117. "--global"
  118. "--offline"
  119. "--loglevel" "info"
  120. "--production"
  121. "install" "../package.tgz")
  122. #t))
  123. (define %standard-phases
  124. (modify-phases gnu:%standard-phases
  125. (add-after 'unpack 'set-home set-home)
  126. (add-before 'configure 'patch-dependencies patch-dependencies)
  127. (replace 'configure configure)
  128. (replace 'build build)
  129. (replace 'check check)
  130. (add-before 'install 'repack repack)
  131. (replace 'install install)))
  132. (define* (node-build #:key inputs (phases %standard-phases)
  133. #:allow-other-keys #:rest args)
  134. (apply gnu:gnu-build #:inputs inputs #:phases phases args))