Check files existing
This commit is contained in:
1 parent
26d19fa2f6
commit
9e6b2452fc
1 file changed
+33
-18
+33
-18
@@ -4,7 +4,6 @@ const addFormats = require('ajv-formats');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const sizeOf = require('image-size');
|
const sizeOf = require('image-size');
|
||||||
|
|
||||||
const schema = require('./token-schema.json');
|
const schema = require('./token-schema.json');
|
||||||
|
|
||||||
const ajv = new Ajv();
|
const ajv = new Ajv();
|
||||||
@@ -14,23 +13,23 @@ const validate = ajv.compile(schema);
|
|||||||
const dataDirectoryPath = path.resolve(process.cwd(), 'data');
|
const dataDirectoryPath = path.resolve(process.cwd(), 'data');
|
||||||
const iconsDirectoryPath = path.resolve(dataDirectoryPath, 'icons');
|
const iconsDirectoryPath = path.resolve(dataDirectoryPath, 'icons');
|
||||||
|
|
||||||
function validateJsonFile(filePath, iconUsage) {
|
async function validateJsonFile(filePath, iconUsage) {
|
||||||
try {
|
|
||||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||||
const valid = validate(data);
|
const valid = validate(data);
|
||||||
if (valid) {
|
if (valid) {
|
||||||
console.log(`File ${path.basename(filePath)} is valid.`);
|
console.log(`File ${path.basename(filePath)} is valid.`);
|
||||||
const iconFileName = data.iconFileName.toLowerCase();
|
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.`);
|
||||||
|
}
|
||||||
if (iconUsage[iconFileName]) {
|
if (iconUsage[iconFileName]) {
|
||||||
iconUsage[iconFileName]++;
|
iconUsage[iconFileName]++;
|
||||||
} else {
|
} else {
|
||||||
iconUsage[iconFileName] = 1;
|
iconUsage[iconFileName] = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`File ${path.basename(filePath)} is invalid:`, validate.errors);
|
throw new Error(`File ${path.basename(filePath)} is invalid: ${JSON.stringify(validate.errors)}`);
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error reading or parsing the file ${path.basename(filePath)}:`, error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,19 +52,14 @@ function validatePngFile(filePath, iconUsage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function validateAllFilesInDirectory(directoryPath, extension, validationFunction, iconUsage) {
|
function validateAllFilesInDirectory(directoryPath, extension, validationFunction, iconUsage) {
|
||||||
fs.readdir(directoryPath, (err, files) => {
|
const files = fs.readdirSync(directoryPath);
|
||||||
if (err) {
|
|
||||||
console.error('Error reading directory:', err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileNames = new Set();
|
const fileNames = new Set();
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const filePath = path.join(directoryPath, file);
|
const filePath = path.join(directoryPath, file);
|
||||||
const fileName = path.basename(file, extension).toLowerCase();
|
const fileName = path.basename(file, extension).toLowerCase();
|
||||||
|
|
||||||
if (fileNames.has(fileName)) {
|
if (fileNames.has(fileName)) {
|
||||||
console.log(`File ${file} is invalid: Duplicate file name (case insensitive).`);
|
throw new Error(`File ${file} is invalid: Duplicate file name (case insensitive).`);
|
||||||
} else {
|
} else {
|
||||||
fileNames.add(fileName);
|
fileNames.add(fileName);
|
||||||
if (path.extname(file) === extension) {
|
if (path.extname(file) === extension) {
|
||||||
@@ -73,17 +67,38 @@ function validateAllFilesInDirectory(directoryPath, extension, validationFunctio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateJsonFiles(iconUsage) {
|
async function validateJsonFiles(iconUsage) {
|
||||||
validateAllFilesInDirectory(dataDirectoryPath, '.json', validateJsonFile, 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) {
|
function validatePngFiles(iconUsage) {
|
||||||
validateAllFilesInDirectory(iconsDirectoryPath, '.png', validatePngFile, iconUsage);
|
validateAllFilesInDirectory(iconsDirectoryPath, '.png', validatePngFile, iconUsage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(async () => {
|
||||||
const iconUsage = {};
|
const iconUsage = {};
|
||||||
validateJsonFiles(iconUsage);
|
try {
|
||||||
|
await validateJsonFiles(iconUsage);
|
||||||
validatePngFiles(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