-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DRAFT: reordered design tokens structure #3285
Draft
PKulkoRaccoonGang
wants to merge
16
commits into
alpha
Choose a base branch
from
Peter_Kulko/reorder-design-tokens
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+15,240
−6,140
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c4f5466
feat: update style dictionary to v4
PKulkoRaccoonGang 22041ec
feat: converted utils and breakpoints
PKulkoRaccoonGang 9c58f15
feat: DTCG format
PKulkoRaccoonGang f29f538
refactor: added dynamic imports
PKulkoRaccoonGang bb70975
refactor: code refactoring
PKulkoRaccoonGang 0e1ef04
refactor: corrected header
PKulkoRaccoonGang da00992
feat: --output-references CLI arg, registers filters, updates CSS var…
adamstankiewicz 5843459
chore: corrected design tokens types
PKulkoRaccoonGang 1b83468
chore: the fractional value of the spacer has had its comma replaced …
PKulkoRaccoonGang 029325a
refactor: corrected changed values of tokens and CSS variables
PKulkoRaccoonGang a0baaa6
feat: added sd-transforms; refs in source-tokens-only output; fixed r…
adamstankiewicz 70673e4
feat: added expanded tokens
PKulkoRaccoonGang 915d963
fix: fixed typography, display and links sections
PKulkoRaccoonGang 036f28c
chore: renamed --output-token-references
PKulkoRaccoonGang b4d560b
refactor: renamed hasSourceTokensOnly variable
PKulkoRaccoonGang 4b065a6
chore: reordered design tokens structure
PKulkoRaccoonGang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.PHONY: build | ||
build: | ||
rm -rf ./dist | ||
tsc --project tsconfig.build.json | ||
|
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
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,190 @@ | ||
/* eslint-disable no-console */ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
|
||
const tokensDirectory = 'tokens/'; | ||
|
||
const DESIGN_TOKEN_KEY_ORDER = [ | ||
'$type', | ||
'$value', | ||
'$description', | ||
'outputReferences', | ||
'modify', | ||
'source', | ||
]; | ||
|
||
const shouldApplyFix = process.argv.includes('--fix'); | ||
|
||
let mismatchWarningCount = 0; | ||
let totalProcessedFileCount = 0; | ||
|
||
/** | ||
* Reorders the keys in an object based on a specified key order. | ||
* @param {Object} object - 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 a list of mismatched keys. | ||
*/ | ||
function reorderKeysInObject(object, desiredKeyOrder) { | ||
const objectKeys = Object.keys(object); | ||
const objectKeySet = new Set(objectKeys); | ||
let isOrderMismatched = false; | ||
const mismatchedKeyList = []; | ||
|
||
let index = 0; | ||
|
||
const reorderedObject = desiredKeyOrder.reduce((accumulatedObject, key) => { | ||
if (objectKeySet.has(key)) { | ||
accumulatedObject[key] = object[key]; | ||
if (key !== objectKeys[index]) { | ||
isOrderMismatched = true; | ||
mismatchedKeyList.push(objectKeys[index]); | ||
} | ||
index++; | ||
} | ||
return accumulatedObject; | ||
}, {}); | ||
|
||
objectKeys.forEach((key) => { | ||
if (!Object.prototype.hasOwnProperty.call(reorderedObject, key)) { | ||
reorderedObject[key] = object[key]; | ||
if (index < objectKeys.length && key !== objectKeys[index]) { | ||
isOrderMismatched = true; | ||
mismatchedKeyList.push(objectKeys[index]); | ||
} | ||
index++; | ||
} | ||
}); | ||
|
||
return { | ||
reorderedObject, | ||
isOrderMismatched, | ||
mismatchedKeys: mismatchedKeyList, | ||
}; | ||
} | ||
|
||
/** | ||
* Recursively reorders keys in JSON data based on a specified key order for nested objects. | ||
* @param {*} jsonData - The JSON data (object, array, or primitive) to process. | ||
* @param {string} jsonPath - 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 recursivelyReorderKeys(jsonData, jsonPath = '') { | ||
if (Array.isArray(jsonData)) { | ||
return jsonData.map((item, index) => recursivelyReorderKeys(item, `${jsonPath}[${index}]`)); | ||
} | ||
|
||
if (typeof jsonData === 'object' && jsonData !== null) { | ||
const { | ||
reorderedObject: reorderedData, | ||
isOrderMismatched: hasMainMismatch, | ||
mismatchedKeys: mainMismatchedKeys, | ||
} = reorderKeysInObject(jsonData, DESIGN_TOKEN_KEY_ORDER); | ||
|
||
let hasAnyMismatch = hasMainMismatch; | ||
const mismatches = hasMainMismatch ? { [jsonPath]: mainMismatchedKeys } : {}; | ||
|
||
Object.entries(reorderedData).reduce((accumulatedMismatches, [key, value]) => { | ||
if (DESIGN_TOKEN_KEY_ORDER.includes(key)) { | ||
reorderedData[key] = value; | ||
return accumulatedMismatches; | ||
} | ||
|
||
const result = recursivelyReorderKeys(value, `${jsonPath}.${key}`); | ||
reorderedData[key] = result.reorderedData || result; | ||
|
||
if (result.isOrderMismatched) { | ||
Object.assign(accumulatedMismatches, result.mismatches); | ||
hasAnyMismatch = true; | ||
} | ||
return accumulatedMismatches; | ||
}, mismatches); | ||
|
||
return { | ||
reorderedData, | ||
isOrderMismatched: hasAnyMismatch, | ||
mismatches, | ||
}; | ||
} | ||
|
||
return jsonData; | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
async function processJsonFilesInDirectory(directoryPath) { | ||
try { | ||
const directoryEntries = await fs.readdir(directoryPath, { withFileTypes: true }); | ||
|
||
const fileProcessingTasks = directoryEntries.map(async (entry) => { | ||
const entryPath = path.join(directoryPath, entry.name); | ||
|
||
if (entry.isDirectory()) { | ||
return processJsonFilesInDirectory(entryPath); | ||
} | ||
|
||
if (entry.isFile() && path.extname(entry.name) === '.json') { | ||
try { | ||
const fileContent = await fs.readFile(entryPath, 'utf-8'); | ||
const jsonData = JSON.parse(fileContent); | ||
|
||
const { | ||
reorderedData, | ||
isOrderMismatched, | ||
mismatches, | ||
} = recursivelyReorderKeys(jsonData); | ||
|
||
if (isOrderMismatched) { | ||
mismatchWarningCount++; | ||
if (shouldApplyFix) { | ||
await fs.writeFile(entryPath, `${JSON.stringify(reorderedData, null, 2)}\n`, 'utf-8'); | ||
} else { | ||
console.warn(chalk.yellow(`Warning: Key order mismatch in ${entryPath}.`)); | ||
console.warn(chalk.red('Mismatched info:')); | ||
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')) { | ||
await fs.writeFile(entryPath, `${fileContent}\n`, 'utf-8'); | ||
} | ||
|
||
totalProcessedFileCount++; | ||
return null; | ||
} catch (error) { | ||
console.error(chalk.red(`Error processing file ${entryPath}:`), error); | ||
return null; | ||
} | ||
} | ||
|
||
return null; | ||
}); | ||
|
||
await Promise.all(fileProcessingTasks); | ||
} catch (error) { | ||
console.error(chalk.red(`Error reading directory ${directoryPath}:`), error); | ||
} | ||
} | ||
|
||
processJsonFilesInDirectory(tokensDirectory).then(() => { | ||
let statusMessage; | ||
|
||
if (shouldApplyFix) { | ||
statusMessage = chalk.green(`Processed ${totalProcessedFileCount} files. ${mismatchWarningCount} files were updated.`); | ||
} else if (mismatchWarningCount > 0) { | ||
statusMessage = chalk.yellow(`Processed ${totalProcessedFileCount} files. ${mismatchWarningCount} files have key order mismatches.`); | ||
} else { | ||
statusMessage = chalk.green(`Processed ${totalProcessedFileCount} files. All files are in correct order.`); | ||
} | ||
|
||
console.log(statusMessage); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[inform]: If we are satisfied with this functionality, it will be moved to Paragon CLI