forked from AdguardTeam/HostlistsRegistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (63 loc) · 3.22 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
const path = require('path');
const builder = require('adguard-hostlists-builder');
const fs = require('fs/promises');
const { getYmlSourcesBlockedServices, getJsonBlockedServices } = require('./scripts/services/get-services-content');
const { mergeServicesData, groupServicesData } = require('./scripts/services/merge-services-data');
const { getDifferences, restoreRemovedSourceFiles } = require('./scripts/services/restore-removed-services');
const { validateSvgIcons } = require('./scripts/services/validate-svg-icons');
const { logger } = require('./scripts/helpers/logger');
const filtersDir = path.join(__dirname, './filters');
const assetsDir = path.join(__dirname, './assets');
const tagsDir = path.join(__dirname, './tags');
const localesDir = path.join(__dirname, './locales');
const inputServicesDir = path.join(__dirname, './services');
const outputServicesFile = path.join(assetsDir, 'services.json');
/**
* Build services data by reading and processing content from a destination JSON file
* and source YAML files. Differences are handled, and the final grouped data is written back
* to the destination JSON file.
*
* @param {string} sourceDirPath - The directory path containing source YAML files.
* @param {string} distFilePath - The file path to the destination JSON file.
* @returns {Promise<void>} - A Promise resolving once the build process is complete.
*
* @throws {Error} - Throws an error if there's an issue during the build process.
*/
const buildServices = async (sourceDirPath, distFilePath) => {
try {
// Read content from the JSON file
const distBlockedServices = await getJsonBlockedServices(distFilePath);
// Read content from the source YML files
const sourceBlockedServices = await getYmlSourcesBlockedServices(sourceDirPath);
// Get the differences between the destination and source data
const differences = getDifferences(distBlockedServices, sourceBlockedServices);
// If there are differences, restore removed source files
if (differences) {
await restoreRemovedSourceFiles(differences, sourceDirPath);
}
// Merge data from the destination and source files
const mergedServicesData = mergeServicesData(distBlockedServices, sourceBlockedServices);
// Validate SVG icons in merged data. Throws an error if any SVG icon is not valid.
validateSvgIcons(mergedServicesData);
// Groups data by keys
const groupedServicesData = groupServicesData(mergedServicesData);
// Write the grouped service data to the destination JSON file
await fs.writeFile(distFilePath, JSON.stringify(groupedServicesData, null, 2));
// Add localizations for service groups
logger.success(`Successfully finished building ${distFilePath}`);
process.exit(0);
} catch (error) {
logger.error(`Error occurred while building ${distFilePath}`, error.message);
process.exit(1);
}
};
// Compile hostlists.
(async () => {
try {
await builder.build(filtersDir, tagsDir, localesDir, assetsDir);
await buildServices(inputServicesDir, outputServicesFile);
} catch (error) {
logger.error('Failed to compile hostlists');
process.exit(1);
}
})();