-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypeDefinitions.ts
More file actions
74 lines (62 loc) · 2.64 KB
/
typeDefinitions.ts
File metadata and controls
74 lines (62 loc) · 2.64 KB
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
import fs from 'fs-extra';
import Handlebars from 'handlebars';
import { constantCase } from 'constant-case';
import ora from 'ora';
import { IParsed } from '../interfaces';
import { EPaths } from '../enums';
import { DocsStore } from '../docsStore';
export const generate = async (docsStore: DocsStore) => {
const eventsDefinitionsSpinner = ora('Generating event type definitions...').start();
await generateEventsDefinitions(docsStore);
eventsDefinitionsSpinner.succeed('Events type definitions generated.');
const sampDefinitionsSpinner = ora('Generating samp type definitions...').start();
await generateSampDefinitions(docsStore);
sampDefinitionsSpinner.succeed('Samp type definitions generated.');
};
export const generateEventsDefinitions = async (docsStore: DocsStore) => {
const a_sampEvents = getEventConstants(docsStore.a_samp);
const a_actorEvents = getEventConstants(docsStore.a_actor);
const a_httpEvents = getEventConstants(docsStore.a_http);
// const a_npcEvents = getEventConstants(docsStore.a_npc);
const a_objectsEvents = getEventConstants(docsStore.a_objects);
const a_playersEvents = getEventConstants(docsStore.a_players);
const a_sampdbEvents = getEventConstants(docsStore.a_sampdb);
const a_vehiclesEvents = getEventConstants(docsStore.a_vehicles);
const eventConstants = {
...a_sampEvents,
...a_actorEvents,
...a_httpEvents,
// ...a_npcEvents,
...a_objectsEvents,
...a_playersEvents,
...a_sampdbEvents,
...a_vehiclesEvents
};
const template = Handlebars.compile(await fs.readFile(EPaths.TEMPLATE_EVENTS, 'utf8'));
await fs.outputFile(EPaths.GENERATED_EVENT_TYPES, template({ eventConstants }));
};
export const generateSampDefinitions = async (docsStore: DocsStore) => {
const eventListenerAliases = ['on', 'addListener', 'addEventListener'];
const removeEventListenerAliases = ['removeListener', 'removeEventListener'];
const parsedIncludes = [
docsStore.a_samp,
docsStore.a_actor,
docsStore.a_http,
// docsStore.a_npc,
docsStore.a_objects,
docsStore.a_players,
docsStore.a_sampdb,
docsStore.a_vehicles,
];
const template = Handlebars.compile(await fs.readFile(EPaths.TEMPLATE_SAMP, 'utf8'));
await fs.outputFile(EPaths.GENERATED_SAMP_TYPES, template({ eventListenerAliases, removeEventListenerAliases, parsedIncludes }));
};
export const getEventConstants = (parsed: IParsed) => {
const events = {};
for (const forward of parsed.forward) {
const constant = constantCase(forward.name.replace('On', 'Event'));
events[constant] = forward.name;
}
return events;
};
export const applyFixes = () => {}; // Apply fixes to generated code