-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-npm.el
69 lines (52 loc) · 2.12 KB
/
build-npm.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;;; npm.el --- Build NPM projects in Emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Justin Andreas Lacoste
;; Author: Justin Andreas Lacoste <[email protected]>
;; URL: https://github.com/27justin/build.el
;; Version: 0.1
;; Keywords: compile, build-system, npm
;;; Commentary:
;; This package implements a transient menu for npm.
;;; Requirements
(require 'build-api)
(require 'json)
;;; Code
(defun build-npm-project-p ()
(build--project-file-exists-p "package.json"))
(defun build--npm-get-package ()
(json-read-file (format "%s/package.json" (project-root (project-current)))))
(defun build--npm-get-targets (callback)
(let* ((package-def (build--npm-get-package))
(scripts-def (cdr (assoc 'scripts package-def)))
(scripts '()))
(seq-do (lambda(tuple)
(push (car tuple) scripts)) scripts-def)
(funcall callback scripts)))
(defun build-npm-run ()
(interactive)
(build--npm-get-targets (lambda(scripts)
(princ scripts)
(let ((choice (funcall build--completing-read "Script: " scripts)))
(funcall build--compile (format "npm run %s" choice))))))
(defun build-npm-install (&optional candidate)
(interactive "sPackage: ")
(funcall build--compile (format "npm install %s %s" (string-join (transient-args 'build-npm-transient) " ") candidate)))
(defun build-npm-uninstall ()
(interactive)
(let* ((package (build--npm-get-package))
(deps (append (cdr (assoc 'dependencies package)) (cdr (assoc 'devDependencies package))))
(candidate (funcall build--completing-read "Uninstall: " deps)))
(funcall build--compile (format "npm uninstall %s" candidate))))
;; NPM transient definition
(transient-define-prefix build-npm-transient ()
"NPM Build Commands"
["NPM Options\n"
["Install"
("-d" "Development dependency" "--save-dev")]]
["NPM Build\n"
["Run"
("r" "Run" build-npm-run)]
["Management"
("i" "Install" build-npm-install)
("u" "Uninstall" build-npm-uninstall)]])
(add-to-list 'build--systems '(build-npm-project-p . build-npm-transient))
(provide 'build-npm)