-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
118 lines (95 loc) · 3.34 KB
/
index.js
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
import { rm, readdir, writeFile } from "node:fs/promises";
import he from "he";
import { getApiFiles } from "./src/getApiFiles.js";
import { getCoreVersion } from "./src/getCoreVersion.js";
import { getRawHooks } from "./src/getRawHooks.js";
import { formatHooks } from "./src/formatHooks.js";
import { getElementFiles } from "./src/getElementFiles.js";
import { getRawElements } from "./src/getRawElements.js";
import { formatElements } from "./src/formatElements.js";
import { getServices } from "./src/getServices.js";
import { sortSnippets } from "./src/sortSnippets.js";
import { formatServices } from "./src/formatServices.js";
const SUPPORTED_VERSIONS = [
'11.0.0',
'10.3.0',
];
// 1. Set up final snippets files.
let allHooks = {};
let allElements = {};
let allServices = {};
// 2. Clean tmp directory.
try {
const contents = await readdir('./tmp');
for (const item of contents) {
await rm(`./tmp/${item}`, {
force: true,
recursive: true
});
}
} catch (error) {}
for (const version of SUPPORTED_VERSIONS) {
console.log(`Drupal ${version}`);
// 3. Download & un-archive tarball.
await getCoreVersion(version);
console.log(` - Downloaded`);
/*******************************
* Hooks
*******************************/
// 4. Find all *.api.php files.
const apiFiles = await getApiFiles(version);
console.log(` - Found ${apiFiles.length} files matching *.api.php`);
// 5. Find all hook docblocks.
const rawHooks = await getRawHooks(apiFiles);
console.log(` - Found ${rawHooks.length} defined hooks`);
// 6. Format hook object for VS Code usage.
const formattedHooks = formatHooks(rawHooks);
// 7. Add hook snippet to full set.
formattedHooks.forEach(hook => {
allHooks[hook.prefix] = hook;
});
/*******************************
* Elements
*******************************/
// 8. Find all .php files for element.
const elementFiles = await getElementFiles(version);
console.log(` - Found ${elementFiles.length} files under core/lib/Drupal/Core/Render/Element`);
// 9. Find all Element docblocks.
const rawElements = await getRawElements(elementFiles);
console.log(` - Found ${rawElements.length} defined Elements`);
// 10. Format element objects for VS Code usage.
const formattedElements = formatElements(rawElements);
// 11. Add element snippet to full set.
formattedElements.forEach(element => {
allElements[element.prefix[1]] = element;
});
/*******************************
* Services
*******************************/
// 12. Get all services.
const rawServices = await getServices(version);
console.log(` - Found ${rawServices.length} defined Services`);
// 13. Format services snippets.
const formattedServices = await formatServices(rawServices, version);
// 14. Add snippets to set.
formattedServices.forEach(([name, snippet]) => {
allServices[`${name}`] = snippet;
});
}
// 15. Sort snippets alphabetically.
allHooks = sortSnippets(allHooks);
allElements = sortSnippets(allElements);
allServices = sortSnippets(allServices);
// 16. Write final files.
await writeFile(
'./snippets/hooks.json',
he.unescape(JSON.stringify(allHooks, null, 2)),
);
await writeFile(
'./snippets/elements.json',
he.unescape(JSON.stringify(allElements, null, 2)),
);
await writeFile(
'./snippets/services.json',
he.unescape(JSON.stringify(allServices, null, 2)),
)