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.

146 lines
5.8 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/create-project ()
  36. "Create a new 'standard' SFDX project."
  37. (interactive)
  38. (let (
  39. (process "sfdx-create-project")
  40. (project-name (read-string "Project Name: "))
  41. (project-dir (read-directory-name "Directory: " "~/Projects"))
  42. )
  43. (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"))
  44. ))
  45. (defun sfdx/create-component ()
  46. "Create a new Lightning Web Component."
  47. (interactive)
  48. (if (locate-dominating-file buffer-file-name "force-app")
  49. (let ((process "sfdx-create-component")
  50. (output-path (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default/lwc/"))
  51. (comp-name (read-string "Component Name: "))
  52. )
  53. (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))
  54. )
  55. (message "You must be in an SFDX project to run that command!")))
  56. (defun sfdx--deploy (component comp-name)
  57. "Internal function to deploy COMP-NAME asyncronously or project if COMPONENT is nil after validations."
  58. (let ((process "sfdx-deploy")
  59. (buffer "*sfdx-output*")
  60. (cd-dir (expand-file-name (locate-dominating-file buffer-file-name "force-app")))
  61. (output-path (concat (locate-dominating-file buffer-file-name "force-app") "force-app/main/default")))
  62. (if component
  63. (progn
  64. (delete-other-windows)
  65. (split-window-below 40)
  66. (other-window 1)
  67. (switch-to-buffer buffer)
  68. (erase-buffer)
  69. (local-set-key (kbd "q") 'delete-window)
  70. (insert (format "Starting deployment of %s...\n" comp-name))
  71. (start-process process buffer "sh" "-c" (concat "cd " cd-dir "; sfdx force:source:deploy --sourcepath ./force-app/main/default/lwc/" comp-name " --loglevel fatal"))
  72. )
  73. (progn
  74. (delete-other-windows)
  75. (split-window-below 40)
  76. (other-window 1)
  77. (switch-to-buffer buffer)
  78. (erase-buffer)
  79. (local-set-key (kbd "q") 'delete-window)
  80. (insert "Starting deployment of project...\n")
  81. (start-process process buffer "sh" "-c" (concat "cd " cd-dir "; sfdx force:source:deploy --sourcepath ./force-app/main/default/ --loglevel fatal"))
  82. )
  83. )
  84. )
  85. )
  86. (defun sfdx/deploy-component-or-project ()
  87. "Deploy the current component or project to target."
  88. (interactive)
  89. (let ((current-folder (file-name-nondirectory
  90. (directory-file-name
  91. (file-name-directory (buffer-file-name))))))
  92. (if (locate-dominating-file buffer-file-name "lwc")
  93. (prog1
  94. ;; Possibly in a component folder, but lets makes sure its not just the LWC folder.
  95. (if (string= current-folder "lwc")
  96. (prog1
  97. ;; Not in a component, deploy project.
  98. ;; (message "Deploying Project...")
  99. (sfdx--deploy nil current-folder))
  100. ;; In a component, deploy component.
  101. ;; (message "Deploying Component...")
  102. (sfdx--deploy t current-folder)))
  103. (prog1
  104. ;; Are we in a project?
  105. (if (locate-dominating-file buffer-file-name "force-app")
  106. (prog1
  107. ;; In project, deploy project.
  108. ;; (message "Deploying Project...")
  109. (sfdx--deploy nil current-folder))
  110. (prog1
  111. ;; Not in an SFDX project.
  112. (message "You are not in a component folder or an SFDX project!"))
  113. )
  114. )
  115. )
  116. )
  117. )
  118. (defun sfdx/retrieve-component ()
  119. "Retrieve the source for the current component (destructively overwrites)."
  120. (interactive)
  121. (message "can't do that yet, i'm still learning"))
  122. (define-transient-command sfdx/transient-action ()
  123. "SFDX CLI Actions"
  124. ["Project Specific"
  125. ("P" "Create New Project" sfdx/create-project)]
  126. ["Component Level"
  127. ("c" "create new" sfdx/create-component)
  128. ("d" "deploy" sfdx/deploy-component-or-project)
  129. ("r" "retrieve" sfdx/retrieve-component)])
  130. (provide 'sfdx)
  131. ;;; sfdx.el ends here