-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathlwc.py
209 lines (171 loc) · 7.99 KB
/
lwc.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sublime
import sublime_plugin
import os
import re
from . import context
from . import util
from . import processor
from .salesforce.lib.panel import Printer
class CreateLightningWebComponent(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateLightningWebComponent, self).__init__(*args, **kwargs)
def run(self, *args):
if self._settings["api_version"] < 45:
message = "Lightning Web Component only available for API Version >= 45, current is %s"
sublime.error_message(message % self._settings["api_version"])
return
self.window.show_input_panel("Please Input Lightning Web Component Name: ",
"", self.on_input, None, None)
def on_input(self, lwc_name):
# Create component to local according to user input
if not re.match('^[a-z]+\\w*[A-Za-z0-9]$', lwc_name) or re.match('__+', lwc_name):
message = 'Invalid format, do you want to try again?'
if not sublime.ok_cancel_dialog(message):
return
self.window.show_input_panel("Please Input Lightning Web Component Name: ",
"", self.on_input, None, None)
return
# Build dir for new Lightning web component
self._workspace = self._settings["workspace"]
component_dir = os.path.join(self._workspace, "src", "lwc", lwc_name)
if not os.path.exists(component_dir):
os.makedirs(component_dir)
else:
message = "%s is already exist, do you want to try again?" % lwc_name
if not sublime.ok_cancel_dialog(message, "Try Again?"):
return
self.window.show_input_panel("Input Lightning Web Component Name: ",
"", self.on_input, None, None)
return
# Get template attribute
templates = util.load_templates()
template_bundle = templates.get("Lwc")
for tpl_name in template_bundle:
template = template_bundle.get(tpl_name)
with open(os.path.join(self._workspace, ".templates", template["directory"])) as fp:
body = fp.read()
# Insert lwc name and api version into files
body = body.replace('{class_name__c}', lwc_name)
body = body.replace('{api_version}', str(self._settings["api_version"]))
lwc_file = os.path.join(component_dir, lwc_name + template["extension"])
# Create Aura Lightning file
with open(lwc_file, "w") as fp:
fp.write(body)
# If created succeed, just open it and refresh project
window = sublime.active_window()
window.open_file(lwc_file)
window.run_command("refresh_folder_list")
# Deploy Lightning Web Component to server
self.window.run_command("deploy_lwc_to_server", {
"dirs": [component_dir],
"switch_project": False,
"update_meta": True
})
def is_enabled(self):
return util.check_action_enabled()
def is_visible(self):
self._settings = context.get_settings()
if self._settings["api_version"] < 45:
return False
return True
class DeployLwcToServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DeployLwcToServer, self).__init__(*args, **kwargs)
def run(self, dirs, switch_project=True, source_org=None, update_meta=False):
if switch_project:
return self.window.run_command("switch_project", {
"callback_options": {
"callback_command": "deploy_lwc_to_server",
"args": {
"switch_project": False,
"source_org": self.settings["default_project_name"],
"dirs": dirs,
"update_meta": update_meta
}
}
})
base64_package = util.build_lightning_package(dirs, 'LightningComponentBundle')
processor.handle_deploy_thread(base64_package, source_org=source_org, update_meta=update_meta)
def is_visible(self, dirs, switch_project=True):
self.settings = context.get_settings()
visible = True
if not dirs or len(dirs) == 0:
visible = False
for _dir in dirs:
attributes = util.get_file_attributes(_dir)
metadata_folder = attributes["metadata_folder"]
if metadata_folder != "lwc":
visible = False
if self.settings["default_project_name"] not in _dir:
visible = False
return visible
class CreateLwcElement(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateLwcElement, self).__init__(*args, **kwargs)
def run(self, dirs, element=""):
""" element: Component, Controller, Helper, Style, Documentation, Render
"""
self.resource_type = element
# for adding additional JS file, user has to input name first:
if self.resource_type == "AdditionalJS":
self.window.show_input_panel("Please Input Additional JavaScript File Name: ",
"", self.on_input, None, None)
else:
self.create_resource()
def on_input(self, js_file_name):
# Create component to local according to user input
if not re.match('^[a-z]+\\w*[A-Za-z0-9]$', js_file_name) or re.match('__+', js_file_name):
message = 'Invalid format, do you want to try again?'
if not sublime.ok_cancel_dialog(message):
return
self.window.show_input_panel("Please Input Lightning Web Component Name: ",
"", self.on_input, None, None)
return
# Create new additional JS file
self.create_resource(js_file_name)
def create_resource(self, js_file_name=None):
# Get template attribute, generate lwc resource file path
templates = util.load_templates()
template = templates.get("LwcElement").get(self.resource_type)
templates_path = os.path.join(self.settings["workspace"],
".templates", template["directory"])
extension = template["extension"]
element_name = self.lwc_name if js_file_name is None else js_file_name
element_name += extension
# Combine lwc element component name
element_file = os.path.join(self._dir, element_name)
# If element file is already exist, just alert
if os.path.isfile(element_file):
return self.window.open_file(element_file)
# Open template file and copy to the body, replace class name if necessary:
with open(templates_path) as fp:
body = fp.read()
if js_file_name is not None:
body = body.replace('{class_name}', js_file_name)
# Create lwc Element file
with open(element_file, "w") as fp:
fp.write(body)
# If created succeed, just open it and refresh project
self.window.open_file(element_file)
self.window.run_command("refresh_folder_list")
# Deploy Aura to server
self.window.run_command("deploy_lightning_to_server", {
"dirs": [self._dir],
"switch_project": False,
"element": self.resource_type,
"update_meta": True
})
def is_visible(self, dirs, element=""):
if not dirs or len(dirs) != 1:
return False
self._dir = dirs[0]
self.settings = context.get_settings()
# Check whether project is the active one
if self.settings["default_project_name"] not in self._dir:
return False
# Check metadata folder
attributes = util.get_file_attributes(self._dir)
if attributes["metadata_folder"] != "lwc":
return False
self.lwc_name = attributes["name"]
return True