-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4d560b
commit 40ff25a
Showing
120 changed files
with
10,967 additions
and
5,381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
|
||
const folderPath = 'tokens/'; | ||
|
||
const KEY_ORDERS = { | ||
main: ['$type', '$value', '$description', 'outputReferences', 'modify', 'source'], | ||
value: ['behavior', 'delay', 'duration', 'property', 'timing-function'], | ||
modify: ['type', 'toReplace', 'replaceWith'], | ||
}; | ||
|
||
const shouldFix = process.argv.includes('--fix'); | ||
let warningsCount = 0; | ||
let processedFileCount = 0; | ||
|
||
/** | ||
* Reorders the keys in an object based on a specified key order. | ||
* @param {Object} obj - The object to reorder. | ||
* @param {string[]} desiredKeyOrder - The desired order for the keys. | ||
* @returns {Object} - An object containing the reordered object and | ||
* a flag indicating if the key order was mismatched and mismatched keys. | ||
*/ | ||
function reorderObjectKeys(obj, desiredKeyOrder) { | ||
const reorderedObject = {}; | ||
const originalKeyList = Object.keys(obj); | ||
let isKeyOrderMismatched = false; | ||
const mismatchedKeysList = []; | ||
|
||
desiredKeyOrder.forEach((key) => { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
reorderedObject[key] = obj[key]; | ||
} | ||
}); | ||
|
||
originalKeyList.forEach((key) => { | ||
if (!Object.prototype.hasOwnProperty.call(reorderedObject, key)) { | ||
reorderedObject[key] = obj[key]; | ||
} | ||
}); | ||
|
||
if (JSON.stringify(originalKeyList) !== JSON.stringify(Object.keys(reorderedObject))) { | ||
isKeyOrderMismatched = true; | ||
mismatchedKeysList.push(...originalKeyList.filter((key, index) => key !== Object.keys(reorderedObject)[index])); | ||
} | ||
|
||
return { reorderedObject, isKeyOrderMismatched, mismatchedKeys: mismatchedKeysList }; | ||
} | ||
|
||
/** | ||
* Recursively reorders keys in JSON data based on specified key orders for nested objects. | ||
* @param {*} data - The JSON data (object, array, or primitive) to process. | ||
* @param {string} currentPath - The path to the current data within the JSON structure. | ||
* @returns {Object} - An object containing the reordered data and | ||
* a flag indicating if any key order mismatches were found. | ||
*/ | ||
function reorderKeysInJson(data, currentPath = '') { | ||
if (Array.isArray(data)) { | ||
return data.map((item, index) => reorderKeysInJson(item, `${currentPath}[${index}]`)); | ||
} | ||
if (typeof data === 'object' && data !== null) { | ||
const { | ||
reorderedObject: reorderedData, isKeyOrderMismatched: mainMismatch, mismatchedKeys: mainMismatchedKeys, | ||
} = reorderObjectKeys(data, KEY_ORDERS.main); | ||
let hasAnyKeyOrderMismatch = mainMismatch; | ||
const mismatches = mainMismatch ? { [currentPath]: mainMismatchedKeys } : {}; | ||
|
||
if ( | ||
Object.prototype.hasOwnProperty.call(reorderedData, '$value') | ||
&& typeof reorderedData.$value === 'object' | ||
&& reorderedData.$value !== null | ||
) { | ||
if (Object.values(reorderedData.$value).every((item) => typeof item === 'object' && item !== null)) { | ||
Object.keys(reorderedData.$value).forEach((key) => { | ||
const { | ||
reorderedObject, isKeyOrderMismatched, mismatchedKeys: valueMismatchedKeys, | ||
} = reorderObjectKeys(reorderedData.$value[key], KEY_ORDERS.value); | ||
reorderedData.$value[key] = reorderedObject; | ||
if (isKeyOrderMismatched) { | ||
hasAnyKeyOrderMismatch = true; | ||
mismatches[`${currentPath}.$value.${key}`] = valueMismatchedKeys; | ||
} | ||
}); | ||
} else { | ||
const { | ||
reordered: reorderedValue, isKeyOrderMismatched, mismatchedKeys: valueMismatchedKeys, | ||
} = reorderObjectKeys(reorderedData.$value, KEY_ORDERS.value); | ||
reorderedData.$value = reorderedValue; | ||
if (isKeyOrderMismatched) { | ||
hasAnyKeyOrderMismatch = true; | ||
mismatches[`${currentPath}.$value`] = valueMismatchedKeys; | ||
} | ||
} | ||
} | ||
|
||
if (Array.isArray(reorderedData.modify)) { | ||
reorderedData.modify = reorderedData.modify.map((item, index) => { | ||
if (typeof item === 'object' && item !== null) { | ||
const { | ||
reorderedObject, | ||
isKeyOrderMismatched, mismatchedKeys: modifyMismatchedKeys, | ||
} = reorderObjectKeys(item, KEY_ORDERS.modify); | ||
if (isKeyOrderMismatched) { | ||
hasAnyKeyOrderMismatch = true; | ||
mismatches[`${currentPath}.modify[${index}]`] = modifyMismatchedKeys; | ||
} | ||
return reorderedObject; | ||
} | ||
return item; | ||
}); | ||
} | ||
|
||
Object.keys(reorderedData).forEach((key) => { | ||
const result = reorderKeysInJson(reorderedData[key], `${currentPath}.${key}`); | ||
reorderedData[key] = result.reorderedData || result; | ||
if (result.isKeyOrderMismatched) { | ||
Object.assign(mismatches, result.mismatches); | ||
hasAnyKeyOrderMismatch = true; | ||
} | ||
}); | ||
|
||
return { | ||
reorderedData, | ||
isKeyOrderMismatched: hasAnyKeyOrderMismatch, | ||
mismatches, | ||
}; | ||
} | ||
return { | ||
reorderedData: data, | ||
isKeyOrderMismatched: false, | ||
mismatches: {}, | ||
}; | ||
} | ||
|
||
/** | ||
* Processes all JSON files in a given directory path, reordering keys in each file based on predefined key orders. | ||
* @param {string} directoryPath - The path of the directory containing JSON files. | ||
*/ | ||
function processJsonFilesInDirectory(directoryPath) { | ||
fs.readdirSync(directoryPath).forEach((fileName) => { | ||
const filePath = path.join(directoryPath, fileName); | ||
const fileStats = fs.statSync(filePath); | ||
|
||
if (fileStats.isDirectory()) { | ||
processJsonFilesInDirectory(filePath); | ||
} else if (fileStats.isFile() && path.extname(fileName) === '.json') { | ||
try { | ||
let fileContent = fs.readFileSync(filePath, 'utf-8'); | ||
const jsonData = JSON.parse(fileContent); | ||
|
||
const { reorderedData, isKeyOrderMismatched, mismatches } = reorderKeysInJson(jsonData); | ||
|
||
if (isKeyOrderMismatched) { | ||
warningsCount++; | ||
if (shouldFix) { | ||
fs.writeFileSync(filePath, JSON.stringify(reorderedData, null, 2), 'utf-8'); | ||
} else { | ||
console.warn(chalk.yellow(`Warning: Key order mismatch in ${filePath}.`)); | ||
console.warn(chalk.red('Mismatched keys by path:')); | ||
Object.entries(mismatches).forEach(([keyPath, keys]) => { | ||
console.warn(chalk.cyan(` Path: ${keyPath.slice(1)}`)); | ||
console.warn(chalk.magenta(` Mismatched keys: ${keys.join(', ')}`)); | ||
console.warn(); | ||
}); | ||
} | ||
} | ||
|
||
if (!fileContent.endsWith('\n')) { | ||
fileContent += '\n'; | ||
fs.writeFileSync(filePath, fileContent, 'utf-8'); | ||
} | ||
|
||
processedFileCount++; | ||
} catch (error) { | ||
console.error(chalk.red(`Error processing file ${filePath}:`), error); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
processJsonFilesInDirectory(folderPath); | ||
|
||
let statusMessage; | ||
|
||
if (shouldFix) { | ||
statusMessage = chalk.green(`Processed ${processedFileCount} files. ${warningsCount} files were updated.`); | ||
} else if (warningsCount > 0) { | ||
statusMessage = chalk.yellow(`Processed ${processedFileCount} files. ${warningsCount} files have key order mismatches.`); | ||
} else { | ||
statusMessage = chalk.green(`Processed ${processedFileCount} files. All files are in correct order.`); | ||
} | ||
|
||
console.log(statusMessage); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.