This repository was archived by the owner on Apr 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-generate.py
65 lines (51 loc) · 1.84 KB
/
vue-generate.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
"""
Easily create new components in a vue project
Just execute the script with create-component.py <component name>
to create a new component (it will be added in the components folder
and imported in App.vue).
For faster use, for example, you could rename the script to something shorter,
like vue-c.py
Created by: regi18
Version: 1.2.0
Github: https://github.com/regi18/
"""
import argparse
import os
import re
parser = argparse.ArgumentParser()
parser.add_argument("name", help="The component's name (please pass it with PascalCase)", nargs="?", type=str)
args = parser.parse_args()
# Converts the name to kebab-case for the tag's name
def pacal_case2kebab_case(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
kebab_case_component_name = pacal_case2kebab_case(args.name)
# How the component will be created
component_template = """<template>
</template>
<script>
export default {{
name: '{}'
}}
</script>
<style scoped>
</style>
""".format(kebab_case_component_name)
# Create the new component
if os.path.isdir(".\\src\\components\\"):
with open(".\\src\\components\\{}.vue".format(args.name), "x") as f:
f.write(component_template)
else:
print('[!] Components folder not found (.\\src\\components\\)')
quit()
import_string = "import " + args.name + " from './components/" + args.name + ".vue';\n"
# Imports and adds the newly created component to the App.vue file
with open(".\\src\\App.vue","r") as f:
file = f.readlines()
for i,line in enumerate(file):
if "<script>" in line:
file.insert(i+1,import_string)
if "components: {" in line:
file.insert(i+1," " + args.name + ",\n")
with open(".\\src\\App.vue","w") as w:
w.writelines(file)