Check files existing
This commit is contained in:
1 parent
26d19fa2f6
commit
9e6b2452fc
1 file changed
+55
-40
+55
-40
@@ -4,7 +4,6 @@ const addFormats = require('ajv-formats');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const sizeOf = require('image-size');
|
||||
|
||||
const schema = require('./token-schema.json');
|
||||
|
||||
const ajv = new Ajv();
|
||||
@@ -14,23 +13,23 @@ const validate = ajv.compile(schema);
|
||||
const dataDirectoryPath = path.resolve(process.cwd(), 'data');
|
||||
const iconsDirectoryPath = path.resolve(dataDirectoryPath, 'icons');
|
||||
|
||||
function validateJsonFile(filePath, iconUsage) {
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
const valid = validate(data);
|
||||
if (valid) {
|
||||
console.log(`File ${path.basename(filePath)} is valid.`);
|
||||
const iconFileName = data.iconFileName.toLowerCase();
|
||||
if (iconUsage[iconFileName]) {
|
||||
iconUsage[iconFileName]++;
|
||||
} else {
|
||||
iconUsage[iconFileName] = 1;
|
||||
}
|
||||
} else {
|
||||
console.log(`File ${path.basename(filePath)} is invalid:`, validate.errors);
|
||||
async function validateJsonFile(filePath, iconUsage) {
|
||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
const valid = validate(data);
|
||||
if (valid) {
|
||||
console.log(`File ${path.basename(filePath)} is valid.`);
|
||||
const iconFileName = data.iconFileName.toLowerCase();
|
||||
const iconFilePath = path.join(iconsDirectoryPath, iconFileName);
|
||||
if (!fs.existsSync(iconFilePath)) {
|
||||
throw new Error(`File ${path.basename(filePath)} is invalid: Icon file ${iconFileName} does not exist.`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error reading or parsing the file ${path.basename(filePath)}:`, error);
|
||||
if (iconUsage[iconFileName]) {
|
||||
iconUsage[iconFileName]++;
|
||||
} else {
|
||||
iconUsage[iconFileName] = 1;
|
||||
}
|
||||
} else {
|
||||
throw new Error(`File ${path.basename(filePath)} is invalid: ${JSON.stringify(validate.errors)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,37 +52,53 @@ function validatePngFile(filePath, iconUsage) {
|
||||
}
|
||||
|
||||
function validateAllFilesInDirectory(directoryPath, extension, validationFunction, iconUsage) {
|
||||
fs.readdir(directoryPath, (err, files) => {
|
||||
if (err) {
|
||||
console.error('Error reading directory:', err);
|
||||
return;
|
||||
}
|
||||
const files = fs.readdirSync(directoryPath);
|
||||
const fileNames = new Set();
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(directoryPath, file);
|
||||
const fileName = path.basename(file, extension).toLowerCase();
|
||||
|
||||
const fileNames = new Set();
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(directoryPath, file);
|
||||
const fileName = path.basename(file, extension).toLowerCase();
|
||||
|
||||
if (fileNames.has(fileName)) {
|
||||
console.log(`File ${file} is invalid: Duplicate file name (case insensitive).`);
|
||||
} else {
|
||||
fileNames.add(fileName);
|
||||
if (path.extname(file) === extension) {
|
||||
validationFunction(filePath, iconUsage);
|
||||
}
|
||||
if (fileNames.has(fileName)) {
|
||||
throw new Error(`File ${file} is invalid: Duplicate file name (case insensitive).`);
|
||||
} else {
|
||||
fileNames.add(fileName);
|
||||
if (path.extname(file) === extension) {
|
||||
validationFunction(filePath, iconUsage);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function validateJsonFiles(iconUsage) {
|
||||
validateAllFilesInDirectory(dataDirectoryPath, '.json', validateJsonFile, iconUsage);
|
||||
async function validateJsonFiles(iconUsage) {
|
||||
const files = fs.readdirSync(dataDirectoryPath);
|
||||
const fileNames = new Set();
|
||||
for (const file of files) {
|
||||
const filePath = path.join(dataDirectoryPath, file);
|
||||
const fileName = path.basename(file, '.json').toLowerCase();
|
||||
|
||||
if (fileNames.has(fileName)) {
|
||||
throw new Error(`File ${file} is invalid: Duplicate file name (case insensitive).`);
|
||||
} else {
|
||||
fileNames.add(fileName);
|
||||
if (path.extname(file) === '.json') {
|
||||
await validateJsonFile(filePath, iconUsage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validatePngFiles(iconUsage) {
|
||||
validateAllFilesInDirectory(iconsDirectoryPath, '.png', validatePngFile, iconUsage);
|
||||
}
|
||||
|
||||
const iconUsage = {};
|
||||
validateJsonFiles(iconUsage);
|
||||
validatePngFiles(iconUsage);
|
||||
(async () => {
|
||||
const iconUsage = {};
|
||||
try {
|
||||
await validateJsonFiles(iconUsage);
|
||||
validatePngFiles(iconUsage);
|
||||
console.log('\x1b[32m%s\x1b[0m', 'All files are valid.');
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user