Subject: [PATCH v2 25/33] gnu: Add tree-sitter-typescript.

From: Pierre Langlois <[email-masked]>
Date: Sun, 29 Aug 2021 11:46:00 +0100

* gnu/packages/tree-sitter.scm (tree-sitter-typescript): New variable.
---
 gnu/packages/tree-sitter.scm | 60 ++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/gnu/packages/tree-sitter.scm b/gnu/packages/tree-sitter.scm
index 8456a15607..001322dd84 100644
--- a/gnu/packages/tree-sitter.scm
+++ b/gnu/packages/tree-sitter.scm
@@ -516,3 +516,63 @@ library.")
     (description
       "This package provides a Rust grammar for the Tree-sitter library.")
     (license license:expat)))
+
+(define-public tree-sitter-typescript
+  (package (inherit tree-sitter-c)
+    (name "tree-sitter-typescript")
+    (version "0.19.0")
+    (source
+      (origin
+        (method git-fetch)
+        (uri (git-reference
+              (url "https://github.com/tree-sitter/tree-sitter-typescript")
+              (commit (string-append "v" version))))
+        (file-name (git-file-name name version))
+        (sha256
+         (base32
+          "01pkmwwmbv6kxda0n6g4cfg72ldmpi9gmp11a5gygn472vhrh2xw"))))
+    (native-inputs
+     `(("tree-sitter-cli" ,tree-sitter-cli)
+       ("tree-sitter-javascript" ,tree-sitter-javascript)
+       ("node-nan" ,node-nan)))
+    (arguments
+     (substitute-keyword-arguments (package-arguments tree-sitter-c)
+      ((#:phases phases)
+       `(modify-phases ,phases
+         ;; The parse-example script tries to clone git repositories.
+         (add-after 'unpack 'remove-parse-example
+           (lambda _
+             (substitute* "package.json"
+               ((" && script\\/parse-examples") ""))))
+         (add-after 'build 'build-gyp
+           (lambda* (#:key inputs #:allow-other-keys)
+             (let ((node (assoc-ref inputs "node")))
+               (invoke (string-append node "/lib/node_modules/npm/node_modules"
+                                           "/node-gyp/bin/node-gyp.js")
+                       "build"))))
+         (replace 'make-files-writable
+           (lambda _
+             (for-each make-file-writable
+                       (find-files "typescript" ".*"))
+             (for-each make-file-writable
+                       (find-files "tsx" ".*"))))
+         (replace 'install-native-lib
+           (lambda* (#:key outputs #:allow-other-keys)
+             (let ((lib (string-append (assoc-ref outputs "out")
+                                       "/lib/tree-sitter")))
+               ;; Invoking `tree-sitter test' loads the grammar which
+               ;; compiles it to a .so binary that we install.
+               (with-directory-excursion "typescript"
+                 (invoke "tree-sitter" "test"))
+               (with-directory-excursion "tsx"
+                 (invoke "tree-sitter" "test"))
+               (mkdir-p lib)
+               (copy-recursively (string-append (getenv "HOME")
+                                                "/.tree-sitter/bin")
+                                 lib))))))))
+    (home-page "https://github.com/tree-sitter/tree-sitter-typescript")
+    (synopsis "Tree-sitter Typescript grammar")
+    (description
+      "This package provides Typescript and TSX grammars for the Tree-sitter
+library.")
+    (license license:expat)))
--
2.33.0