This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcreate-service.py
executable file
·174 lines (153 loc) · 7.56 KB
/
create-service.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
#!/usr/bin/env python3
# What this file does:
# + It creates all required files for a new nodecg-io service
# + It copies version and dependency information (e.g., for typescript) from noodecg-io-template
# + It'll also create a sample and the files for the docs
#
# Requirements:
# + python3 in your PATH
# + executed from root of nodecg-io repo
import json
import os
import shutil
if __name__ == '__main__':
service_name = input('Service name: ').lower()
description = input('Short description: ')
author_name = input('Author name: ')
author_url = input('Author url: ')
sample_name = input('Sample name: ').lower()
service_name_c = service_name.replace("-", " ").title().replace(" ", "")
service_name_cc = service_name_c[0].lower() + service_name_c[1:]
with open('services/nodecg-io-template/package.json') as file:
package = json.loads(file.read())
# Replace some entries from the template package.json
package['name'] = f'nodecg-io-{service_name}'
package['description'] = description
package['homepage'] = f'https://codeoverflow-org.github.io/nodecg-io-docs/RELEASE/samples/{sample_name}'
package['author'] = {
'name': author_name,
'url': author_url
}
package['repository']['directory'] = f'services/nodecg-io-{service_name}'
os.mkdir(f'services/nodecg-io-{service_name}')
with open(f'services/nodecg-io-{service_name}/package.json', mode='w') as file:
file.write(json.dumps(package, indent=4))
shutil.copy('services/nodecg-io-template/schema.json',
f'services/nodecg-io-{service_name}/schema.json')
shutil.copy('services/nodecg-io-template/tsconfig.json',
f'services/nodecg-io-{service_name}/tsconfig.json')
os.mkdir(f'services/nodecg-io-{service_name}/extension')
with open(f'services/nodecg-io-{service_name}/extension/index.ts', mode='w') as file:
file.writelines([
'import NodeCG from "@nodecg/types";\n',
'import { Result, emptySuccess, success, ServiceBundle } from "nodecg-io-core";\n',
f'import {{ {service_name_c}Client }} from "./{service_name_cc}Client";\n',
'\n',
f'export interface {service_name_c}Config {{\n',
' placeholder: string;\n',
'}\n',
'\n',
f'export {{ {service_name_c}Client }} from "./{service_name_cc}Client";\n',
'\n',
'module.exports = (nodecg: NodeCG.ServerAPI) => {\n',
f' new {service_name_c}Service(nodecg, "{service_name}", __dirname, "../schema.json").register();\n',
'};\n',
'\n',
f'class {service_name_c}Service extends ServiceBundle<{service_name_c}Config, {service_name_c}Client> {{\n',
f' async validateConfig(_: {service_name_c}Config): Promise<Result<void>> {{\n',
' // TODO: Implement\n',
' return emptySuccess();\n',
' }\n',
'\n',
f' async createClient(config: {service_name_c}Config): Promise<Result<{service_name_c}Client>> {{\n',
' // TODO: Implement\n',
f' const client = {service_name_c}Client.createClient(config);\n',
f' this.nodecg.log.info("Successfully created {service_name} client.");\n',
' return success(client);\n',
' }\n',
'\n',
f' stopClient(_: {service_name_c}Client): void {{\n',
' // TODO: Implement\n',
f' this.nodecg.log.info("Successfully stopped {service_name} client.");\n',
' }\n',
'\n',
f' removeHandlers(_: {service_name_c}Client): void {{\n',
' // TODO: Implement (optional)\n',
' }\n',
'}\n'
])
with open(f'services/nodecg-io-{service_name}/extension/{service_name_cc}Client.ts', mode='w') as file:
file.writelines([
f'import {{ {service_name_c}Config }} from "./index";\n',
'\n',
f'export class {service_name_c}Client {{\n',
f' constructor(_: {service_name_c}Config) {{\n',
' // TODO: Implement\n',
' }\n',
'\n',
f' static createClient(config: {service_name_c}Config): {service_name_c}Client {{\n',
' // TODO: Implement\n',
f' return new {service_name_c}Client(config);\n',
' }\n',
'}\n'
])
with open('samples/template/package.json') as file:
sample_package = json.loads(file.read())
sample_dependencies = {}
for dependency in sample_package['dependencies']:
if dependency != 'nodecg-io-template':
sample_dependencies[dependency] = sample_package['dependencies'][dependency]
sample_dependencies[f'nodecg-io-{service_name}'] = package['version']
# Replace some entries from the template package.json
sample_package['name'] = sample_name
sample_package['dependencies'] = sample_dependencies
sample_package['nodecg']['bundleDependencies'] = {
f'nodecg-io-{service_name}': package['version']
}
os.mkdir(f'samples/{sample_name}')
with open(f'samples/{sample_name}/package.json', mode='w') as file:
file.write(json.dumps(sample_package, indent=4))
with open('samples/template/tsconfig.json') as file:
sample_tsconfig = json.loads(file.read())
# Replace reference in template tsconfig.json
sample_tsconfig['references'][1]["path"] = sample_tsconfig['references'][1]["path"].replace(
'template', service_name)
with open(f'samples/{sample_name}/tsconfig.json', mode='w') as file:
file.write(json.dumps(sample_tsconfig, indent=4))
os.mkdir(f'samples/{sample_name}/extension')
with open(f'samples/{sample_name}/extension/index.ts', mode='w') as file:
file.writelines([
'import NodeCG from "@nodecg/types";\n',
f'import {{ {service_name_c}Client }} from "nodecg-io-{service_name}";\n',
'import { requireService } from "nodecg-io-core";\n',
'\n',
'module.exports = function (nodecg: NodeCG.ServerAPI) {\n',
f' nodecg.log.info("Sample bundle for {service_name_c} started.");\n',
'\n',
f' const {service_name_cc} = requireService<{service_name_c}Client>(nodecg, "{service_name}");\n',
'\n',
f' {service_name_cc}?.onAvailable((_) => {{\n',
f' nodecg.log.info("{service_name_c} service available.");\n',
' // TODO: Implement\n',
' });\n',
'\n',
f' {service_name_cc}?.onUnavailable(() => {{\n',
f' nodecg.log.info("{service_name_c} service unavailable.");\n',
' // TODO: Implement\n',
' });\n',
'};\n'
])
if not os.path.isdir('docs'):
print('Could not create documentation files. You should clone nodecg-io-docs in the directory "docs".')
else:
with open(f'docs/docs/samples/{sample_name}.md', mode='w') as file:
file.writelines([
'<!-- Marker for build.py that there\'s no sample bundle. Remove this if you created one -->\n',
'\n',
f'# Documentation for {service_name_c}-service missing.\n',
'\n',
'You can help us [create it](../contribute/sample_documentation.md).'
])
os.system('npm install')
os.system('npm run rebuild')
print(f"\n\nService {service_name_c} created. Please add it to mkdocs.yml")