-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathlibrarian.mjs
executable file
·204 lines (169 loc) · 6.52 KB
/
librarian.mjs
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
#!/usr/bin/env node
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/
import { program } from 'commander';
import { readFile, writeFile, access } from 'fs/promises';
import { parseXmlString } from 'libxmljs2';
import path from 'path';
const getLibraryData = async () => {
process.chdir('.moodle');
const moodleDir = process.cwd();
const { fetchComponentData } = await import(`${moodleDir}/.grunt/components.js`);
const exists = async (dir) => {
try {
await access(dir);
return true;
} catch {
return false;
}
};
const thirdPartyLibraries = fetchComponentData().pathList
.map((componentPath) => componentPath.replace(/\\/g, '/'))
.map((componentPath) => `${componentPath}/thirdpartylibs.xml`)
.sort();
const libraryList = [];
const libraryFields = [
'location',
'name',
'description',
'version',
'license',
'licenseversion',
'repository',
];
for (const libraryPath of thirdPartyLibraries) {
if (!await exists(libraryPath)) {
continue;
}
const libraryDir = path.dirname(libraryPath).replace(`${moodleDir}/`, '');
const libraries = parseXmlString(await readFile(libraryPath));
for (const library of libraries.get('//libraries').childNodes()) {
if (library.type() === 'element') {
const libraryData = {
copyrightHolders: [],
};
for (const field of libraryFields) {
libraryData[field] = library.get(field)?.text();
}
libraryData.location = `${libraryDir}/${libraryData.location}`;
libraryData.customised = !!library.get('customised');
for (const copyrightHolder of library.get('copyrights')?.childNodes() || []) {
if (copyrightHolder.type() !== 'element') {
continue;
}
libraryData.copyrightHolders.push(copyrightHolder.text());
}
libraryList.push(libraryData);
}
}
}
return libraryList.sort((a, b) => a.location.localeCompare(b.location));
};
const generateLibraryJSON = async () => {
const startDir = process.cwd();
const outputFile = `${process.cwd()}/data/libraries.json`;
const libraries = await getLibraryData();
process.chdir(startDir);
writeFile(outputFile, JSON.stringify(libraries, null, 4));
};
const renderOutdatedNotice = (isOutdated) => {
if (!isOutdated) {
return '';
}
return `
:::danger Outdated
This library is not currently used in Moodle
:::
`;
};
const renderCustomisedNotice = (isCustomised) => {
if (!isCustomised) {
return '';
}
return ' (with Moodle customisations)';
};
const renderCopyrightHolders = (copyrightHolders) => {
if (copyrightHolders.length === 0) {
return '';
}
return `- **Copyright holders**:
${copyrightHolders.map((copyrightHolder) => ` - ${copyrightHolder}`).join(`
`)}
`
.replaceAll(/</g, '')
.replaceAll(/>/g, '');
};
const renderLibraryUrl = (libraryUrl) => {
if (libraryUrl.length === 0) {
return '';
}
return `- **URL**: [${libraryUrl}](${libraryUrl})`;
};
const renderLibraryData = (libraryData, isOutdated) => {
const libraryName = libraryData.name;
const libraryDescription = libraryData.description || '';
const libraryLocation = libraryData.location;
const libraryVersion = libraryData.version || '';
const libraryLicense = libraryData.license || '';
const libraryLicenseVersion = libraryData.licenseversion || '';
const libraryCustomised = libraryData.customised;
const libraryCopyrightHolders = libraryData.copyrightHolders || [];
const libraryUrl = libraryData.repository || '';
return `
### ${libraryName}
${libraryDescription}
${renderOutdatedNotice(isOutdated)}
- **Location**: ${libraryLocation}
- **Version**: ${libraryVersion}${renderCustomisedNotice(libraryCustomised)}
- **License**: ${libraryLicense} ${libraryLicenseVersion}
${renderLibraryUrl(libraryUrl)}
${renderCopyrightHolders(libraryCopyrightHolders)}
`.replaceAll(/ *$/gm, '');
};
const generateCredits = async () => {
const generateLibraryContentForFile = async (filePath, isOutdated) => JSON.parse(await readFile(filePath, 'utf-8'))
.sort((a, b) => a.location.localeCompare(b.location))
.map((libraryData) => renderLibraryData(libraryData, isOutdated)).join('');
const formattedLibraries = await generateLibraryContentForFile('data/libraries.json', false);
const formattedLegacyLibraries = await generateLibraryContentForFile('data/legacy-libraries.json', true);
const outputFile = 'general/community/credits/thirdpartylibs.md';
const headerContent = await readFile('src/templates/thirdpartylibs/header.md.tpl', 'utf-8');
const otherLibrariesList = await readFile('src/templates/thirdpartylibs/other-libraries.md.tpl', 'utf-8');
const legacyHeaderContent = await readFile('src/templates/thirdpartylibs/outdated-header.md.tpl', 'utf-8');
const footerContent = await readFile('src/templates/thirdpartylibs/footer.md.tpl', 'utf-8');
const content = `${headerContent}
${formattedLibraries}
${otherLibrariesList}
${legacyHeaderContent}
${formattedLegacyLibraries}
${footerContent}`.replaceAll(/\n{3,}/g, '\n\n');
writeFile(outputFile, content);
};
program
.name('Librarian')
.description('CLI Utility to handle Third-Party Library processing')
.version('1.0.0');
program
.command('fetch')
.description('Generate a JSON file containing all third-party libraries currently in Moodle')
.option('--debug')
.action(generateLibraryJSON);
program
.command('generate')
.description('Generate the Third-party libraries Credit page using the generated libraries data')
.action(generateCredits);
program.parse();