My attempt to optimize my emacs load time <1 second
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
8.6 KiB

  1. ;;; sfdx --- Emacs wrapper for basic sfdx cli commands
  2. ;;; Commentary:
  3. ;;; Code:
  4. (require 'transient)
  5. (defvar sfdx-create-css)
  6. (setq-default sfdx-create-css t)
  7. (defun sfdx/next-component-file ()
  8. "Find next file with the same name, but different file extension."
  9. (interactive)
  10. (let (
  11. (current-file-name (file-name-sans-extension (buffer-file-name)))
  12. (current-ext (file-name-extension (buffer-file-name)))
  13. )
  14. (when (string= current-ext "js")
  15. (find-file (concat current-file-name ".html")))
  16. (when (string= current-ext "html")
  17. (if (file-exists-p (concat current-file-name ".css"))
  18. (find-file (concat current-file-name ".css"))
  19. (if (file-exists-p (concat current-file-name ".scss"))
  20. (find-file (concat current-file-name ".scss"))
  21. (if (and sfdx-create-css (yes-or-no-p "Do you want to create a CSS file?"))
  22. (find-file (concat current-file-name ".css"))
  23. (setq-local sfdx-create-css nil)
  24. (find-file (concat current-file-name ".js"))))))
  25. (when (string= current-ext "css")
  26. (find-file (concat current-file-name ".js")))
  27. (when (string= current-ext "scss")
  28. (find-file (concat current-file-name ".js")))
  29. ))
  30. (defun sfdx--goto-project (project-path)
  31. "Internal function to load the PROJECT-PATH in current window."
  32. ;; DEBUG - this isn't working to auto-open the folder.
  33. ;; (find-file project-path)
  34. (message project-path))
  35. (defun sfdx/exec-process (cmd name &optional comint)
  36. "Execute CMD as a process in a buffer NAME, optionally passing COMINT as non-nil to put buffer in `comint-mode'."
  37. (let ((compilation-buffer-name-function
  38. (lambda (mode)
  39. (format "*%s*" name))))
  40. (message (concat "Running " cmd))
  41. (compile cmd comint)))
  42. (defun sfdx/create-project ()
  43. "Create a new 'standard' SFDX project."
  44. (interactive)
  45. (let (
  46. (process "sfdx-create-project")
  47. (project-name (read-string "Project Name: "))
  48. (project-dir (read-directory-name "Directory: " "~/Projects"))
  49. )
  50. (async-start-process process "sh" `(lambda (result) (sfdx--goto-project (concat (expand-file-name ',project-dir) ',project-name))) "-c" (concat "sfdx force:project:create --projectname " project-name " --outputdir " (expand-file-name project-dir) " --template standard"))
  51. ))
  52. (defun sfdx/create-component ()
  53. "Create a new Lightning Web Component."
  54. (interactive)
  55. (if (locate-dominating-file buffer-file-name "force-app")
  56. (let ((process "sfdx-create-component")
  57. (output-path (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default/lwc/"))
  58. (comp-name (read-string "Component Name: "))
  59. )
  60. (async-start-process process "sh" (lambda (result) (message "Component Created")) "-c" (concat "sfdx force:lightning:component:create --type lwc --componentname " comp-name " --outputdir " output-path))
  61. )
  62. (message "You must be in an SFDX project to run that command!")))
  63. (defun sfdx/fetch-component ()
  64. "Fetch a Lightning Web Component from Org."
  65. (interactive)
  66. (if (locate-dominating-file buffer-file-name "force-app")
  67. (let ((process "sfdx-fetch-component")
  68. (cd-dir (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default/lwc/"))
  69. (comp-name (read-string "Component Name: "))
  70. )
  71. (sfdx/exec-process (format "sh -c \"cd %s; sfdx force:source:retrieve -m LightningComponentBundle:%s\"" cd-dir comp-name) "sfdx:retrieve_component" t)
  72. ;; (async-start-process process "sh" (lambda (result) (message (concat "Component Retrieved: \"" comp-name "\""))) "-c" (concat "sfdx force:source:retrieve -m LightningComponentBundle:" comp-name))
  73. )
  74. (message "You must be in an SFDX project to run that command!")))
  75. (defun sfdx--deploy (component comp-name)
  76. "Internal function to deploy COMP-NAME asyncronously or whole project if COMPONENT is nil after validations."
  77. (let ((process "sfdx-deploy")
  78. (buffer "*sfdx-output*")
  79. (cd-dir (expand-file-name (locate-dominating-file buffer-file-name "force-app")))
  80. (output-path (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default")))
  81. (if component
  82. (sfdx/exec-process (format "sh -c \"cd %s; sfdx force:source:deploy --sourcepath ./force-app/main/default/lwc/%s --loglevel fatal\"" cd-dir comp-name) "sfdx:deploy_component" t)
  83. (sfdx/exec-process (format "sh -c \"cd %s; sfdx force:source:deploy --sourcepath ./force-app/main/default/ --loglevel fatal\"" cd-dir) "sfdx:deploy_project" t))))
  84. (defun sfdx--retrieve (component comp-name)
  85. "Internal function to retrieve COMP-NAME asyncronously or whole project if COMPONENT is nil after validations."
  86. (let ((process "sfdx-deploy")
  87. (buffer "*sfdx-output*")
  88. (cd-dir (expand-file-name (locate-dominating-file buffer-file-name "force-app")))
  89. (output-path (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default")))
  90. (if component
  91. (sfdx/exec-process (format "sh -c \"cd %s; sfdx force:source:retrieve --sourcepath ./force-app/main/default/lwc/%s --loglevel fatal\"" cd-dir comp-name) "sfdx:retrieve_component" t)
  92. (progn
  93. (if (yes-or-no-p "Are you sure? This will completely overwrite any local changes! ")
  94. (sfdx/exec-process (format "sh -c \"cd %s; sfdx force:source:retrieve --sourcepath ./force-app/main/default/ --loglevel fatal\"" cd-dir) "sfdx:retrieve_project" t)
  95. (message "Cancelled")
  96. )))))
  97. (defun sfdx/deploy-component-or-project ()
  98. "Deploy the current component or project to target."
  99. (interactive)
  100. (let ((current-folder (file-name-nondirectory
  101. (directory-file-name
  102. (file-name-directory (buffer-file-name))))))
  103. (if (locate-dominating-file buffer-file-name "lwc")
  104. (prog1
  105. ;; Possibly in a component folder, but lets makes sure its not just the LWC folder.
  106. (if (string= current-folder "lwc")
  107. (prog1
  108. ;; Not in a component, deploy project.
  109. ;; (message "Deploying Project...")
  110. (sfdx--deploy nil current-folder))
  111. ;; In a component, deploy component.
  112. ;; (message "Deploying Component...")
  113. (sfdx--deploy t current-folder)))
  114. (prog1
  115. ;; Are we in a project?
  116. (if (locate-dominating-file buffer-file-name "force-app")
  117. (prog1
  118. ;; In project, deploy project.
  119. ;; (message "Deploying Project...")
  120. (sfdx--deploy nil current-folder))
  121. (prog1
  122. ;; Not in an SFDX project.
  123. (message "You are not in a component folder or an SFDX project!"))
  124. )
  125. )
  126. )
  127. )
  128. )
  129. (defun sfdx/retrieve-component ()
  130. "Retrieve the source for the current component (destructively overwrites)."
  131. (interactive)
  132. (let ((current-folder (file-name-nondirectory
  133. (directory-file-name
  134. (file-name-directory (buffer-file-name))))))
  135. (if (locate-dominating-file buffer-file-name "lwc")
  136. (prog1
  137. ;; Possibly in a component folder, but lets makes sure its not just the LWC folder.
  138. (if (string= current-folder "lwc")
  139. (prog1
  140. ;; Not in a component, retrieve project.
  141. ;; (message "Retrieving Project...")
  142. (sfdx--retrieve nil current-folder))
  143. ;; In a component, retrieve component.
  144. ;; (message "Retrieving Component...")
  145. (sfdx--retrieve t current-folder)))
  146. (prog1
  147. ;; Are we in a project?
  148. (if (locate-dominating-file buffer-file-name "force-app")
  149. (prog1
  150. ;; In project, retrieve project.
  151. ;; (message "Retrieving Project...")
  152. (sfdx--retrieve nil current-folder))
  153. (prog1
  154. ;; Not in an SFDX project.
  155. (message "You are not in a component folder or an SFDX project!"))
  156. )
  157. )
  158. )
  159. )
  160. )
  161. (define-transient-command sfdx/transient-action ()
  162. "SFDX CLI Actions"
  163. ["Project Specific"
  164. ("P" "Create New Project" sfdx/create-project)]
  165. ["Create Component"
  166. ("n" "create new" sfdx/create-component)]
  167. ["Actions for this Component"
  168. ("d" "deploy" sfdx/deploy-component-or-project)
  169. ("r" "retrieve" sfdx/retrieve-component)]
  170. ["Download Component"
  171. ("f" "fetch by component name" sfdx/fetch-component)])
  172. (provide 'sfdx)
  173. ;;; sfdx.el ends here