Skip to content
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

validate structure #205

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.10.0
18 changes: 18 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
sikhote marked this conversation as resolved.
Show resolved Hide resolved
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": false,
"arrowParens": "always",
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "ignore",
"vueIndentScriptAndStyle": false,
"endOfLine": "auto",
"singleAttributePerLine": true
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,7 @@ Found in `style-dictionary/transforms`
See API for [creating a transform](https://amzn.github.io/style-dictionary/#/api?id=registertransform)

See [transform docs](https://amzn.github.io/style-dictionary/#/transforms).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any easy way to pass a bool into the build to validate that changes happened purposely - then automate the rebuild of validate-structure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running npm run validate -- --update will do the trick

### Validation

Because this library has many dependencies, there is a validation script that performs some checks. One of the tests is verifying the file structure has not changed. If a change is intentional, you may execute `npm run validate -- --update` or delete the `validate-structure.json` file and then execute `npm run validate`. The `validate-structure.json` file should be commited.
2 changes: 1 addition & 1 deletion dist/docsite/js/cdr-tokens.d.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Do not edit directly
* Generated on Wed, 28 Feb 2024 20:15:50 GMT
* Generated on Wed, 28 Aug 2024 21:56:24 GMT
*/

export const CdrColorTextButtonPrimary : string;
Expand Down
2 changes: 1 addition & 1 deletion dist/rei-dot-com/js/cdr-tokens.d.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Do not edit directly
* Generated on Wed, 28 Feb 2024 20:15:50 GMT
* Generated on Wed, 28 Aug 2024 21:56:24 GMT
*/

export const CdrColorTextButtonPrimary : string;
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"prerelease": "cross-env NODE_ENV=prerelease npm-publish-prerelease",
"build:tokens": "npm run clean && node ./style-dictionary/build.js",
"clean": "rimraf dist/",
"build": "npm run clean && npm run validate && npm run build:tokens && npm run site-tokens",
"build": "npm run clean && npm run build:tokens && npm run site-tokens && npm run validate",
"build:grid": "node grid.js",
"site-tokens": "node ./siteTokens.js",
"validate": "node ./validate.js"
Expand All @@ -46,6 +46,7 @@
"@divriots/style-dictionary-to-figma": "^0.4.0",
"concat": "^1.0.3",
"cross-env": "^7.0.3",
"dir-to-json": "^1.0.0",
"fs-extra": "^10.1.0",
"glob": "^7.1.6",
"json5": "^2.2.0",
Expand Down
1 change: 1 addition & 0 deletions validate-structure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":"..","path":"","name":"dist","type":"directory","children":[{"parent":"","path":"docsite","name":"docsite","type":"directory","children":[{"parent":"docsite","path":"docsite/android","name":"android","type":"directory","children":[{"parent":"docsite/android","path":"docsite/android/colors.xml","name":"colors.xml","type":"file"},{"parent":"docsite/android","path":"docsite/android/dimens.xml","name":"dimens.xml","type":"file"},{"parent":"docsite/android","path":"docsite/android/font_dimens.xml","name":"font_dimens.xml","type":"file"}]},{"parent":"docsite","path":"docsite/css","name":"css","type":"directory","children":[{"parent":"docsite/css","path":"docsite/css/cdr-tokens.css","name":"cdr-tokens.css","type":"file"}]},{"parent":"docsite","path":"docsite/figma","name":"figma","type":"directory","children":[{"parent":"docsite/figma","path":"docsite/figma/figma.json","name":"figma.json","type":"file"}]},{"parent":"docsite","path":"docsite/ios","name":"ios","type":"directory","children":[{"parent":"docsite/ios","path":"docsite/ios/CdrColor.h","name":"CdrColor.h","type":"file"},{"parent":"docsite/ios","path":"docsite/ios/CdrColor.m","name":"CdrColor.m","type":"file"},{"parent":"docsite/ios","path":"docsite/ios/CdrSize.h","name":"CdrSize.h","type":"file"},{"parent":"docsite/ios","path":"docsite/ios/CdrSize.m","name":"CdrSize.m","type":"file"}]},{"parent":"docsite","path":"docsite/js","name":"js","type":"directory","children":[{"parent":"docsite/js","path":"docsite/js/cdr-tokens.cjs","name":"cdr-tokens.cjs","type":"file"},{"parent":"docsite/js","path":"docsite/js/cdr-tokens.d.mts","name":"cdr-tokens.d.mts","type":"file"},{"parent":"docsite/js","path":"docsite/js/cdr-tokens.mjs","name":"cdr-tokens.mjs","type":"file"}]},{"parent":"docsite","path":"docsite/json","name":"json","type":"directory","children":[{"parent":"docsite/json","path":"docsite/json/android.json","name":"android.json","type":"file"},{"parent":"docsite/json","path":"docsite/json/global.json","name":"global.json","type":"file"},{"parent":"docsite/json","path":"docsite/json/ios.json","name":"ios.json","type":"file"},{"parent":"docsite/json","path":"docsite/json/platform-tokens.json","name":"platform-tokens.json","type":"file"},{"parent":"docsite/json","path":"docsite/json/web.json","name":"web.json","type":"file"}]},{"parent":"docsite","path":"docsite/less","name":"less","type":"directory","children":[{"parent":"docsite/less","path":"docsite/less/cdr-tokens.less","name":"cdr-tokens.less","type":"file"}]},{"parent":"docsite","path":"docsite/scss","name":"scss","type":"directory","children":[{"parent":"docsite/scss","path":"docsite/scss/cdr-tokens.scss","name":"cdr-tokens.scss","type":"file"},{"parent":"docsite/scss","path":"docsite/scss/deprecate.scss","name":"deprecate.scss","type":"file"},{"parent":"docsite/scss","path":"docsite/scss/utility-map.scss","name":"utility-map.scss","type":"file"}]}]},{"parent":"","path":"rei-dot-com","name":"rei-dot-com","type":"directory","children":[{"parent":"rei-dot-com","path":"rei-dot-com/android","name":"android","type":"directory","children":[{"parent":"rei-dot-com/android","path":"rei-dot-com/android/colors.xml","name":"colors.xml","type":"file"},{"parent":"rei-dot-com/android","path":"rei-dot-com/android/dimens.xml","name":"dimens.xml","type":"file"},{"parent":"rei-dot-com/android","path":"rei-dot-com/android/font_dimens.xml","name":"font_dimens.xml","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/css","name":"css","type":"directory","children":[{"parent":"rei-dot-com/css","path":"rei-dot-com/css/cdr-tokens.css","name":"cdr-tokens.css","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/figma","name":"figma","type":"directory","children":[{"parent":"rei-dot-com/figma","path":"rei-dot-com/figma/figma.json","name":"figma.json","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/ios","name":"ios","type":"directory","children":[{"parent":"rei-dot-com/ios","path":"rei-dot-com/ios/CdrColor.h","name":"CdrColor.h","type":"file"},{"parent":"rei-dot-com/ios","path":"rei-dot-com/ios/CdrColor.m","name":"CdrColor.m","type":"file"},{"parent":"rei-dot-com/ios","path":"rei-dot-com/ios/CdrSize.h","name":"CdrSize.h","type":"file"},{"parent":"rei-dot-com/ios","path":"rei-dot-com/ios/CdrSize.m","name":"CdrSize.m","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/js","name":"js","type":"directory","children":[{"parent":"rei-dot-com/js","path":"rei-dot-com/js/cdr-tokens.cjs","name":"cdr-tokens.cjs","type":"file"},{"parent":"rei-dot-com/js","path":"rei-dot-com/js/cdr-tokens.d.mts","name":"cdr-tokens.d.mts","type":"file"},{"parent":"rei-dot-com/js","path":"rei-dot-com/js/cdr-tokens.mjs","name":"cdr-tokens.mjs","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/json","name":"json","type":"directory","children":[{"parent":"rei-dot-com/json","path":"rei-dot-com/json/android.json","name":"android.json","type":"file"},{"parent":"rei-dot-com/json","path":"rei-dot-com/json/global.json","name":"global.json","type":"file"},{"parent":"rei-dot-com/json","path":"rei-dot-com/json/ios.json","name":"ios.json","type":"file"},{"parent":"rei-dot-com/json","path":"rei-dot-com/json/platform-tokens.json","name":"platform-tokens.json","type":"file"},{"parent":"rei-dot-com/json","path":"rei-dot-com/json/web.json","name":"web.json","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/less","name":"less","type":"directory","children":[{"parent":"rei-dot-com/less","path":"rei-dot-com/less/cdr-tokens.less","name":"cdr-tokens.less","type":"file"}]},{"parent":"rei-dot-com","path":"rei-dot-com/scss","name":"scss","type":"directory","children":[{"parent":"rei-dot-com/scss","path":"rei-dot-com/scss/cdr-tokens.scss","name":"cdr-tokens.scss","type":"file"},{"parent":"rei-dot-com/scss","path":"rei-dot-com/scss/deprecate.scss","name":"deprecate.scss","type":"file"},{"parent":"rei-dot-com/scss","path":"rei-dot-com/scss/utility-map.scss","name":"utility-map.scss","type":"file"}]}]}]}
48 changes: 41 additions & 7 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ const glob = require('glob');
const requireJSON5 = require('require-json5');
const _ = require('lodash');
const process = require('process');
const dirToJson = require('dir-to-json');
const fs = require('fs');

const args = process.argv.slice(2);
const files = glob.sync('./tokens/**/*.json5');
const results = [];

const addDelimiter = (a, b) => a ? `${a}-${b}` : b;
const addDelimiter = (a, b) => (a ? `${a}-${b}` : b);

// check if object has a `value` property and if any children also have `value` (style dictionary
// check if object has a `value` property and if any children also have `value` (style dictionary
// will only process the topmost object with `value`)
const validate = (obj, path='', to_ret=[]) => {
const validate = (obj, path = '', to_ret = []) => {
const hasValue = _.has(obj, 'value');

for (const key in obj) {
Expand All @@ -33,8 +36,8 @@ files.forEach((file) => {
const response = validate(requireJSON5(file));

if (response.length > 0) {
results.push(` In ${file}:`)
results.push(` ${response.join('\r\n ')}`)
results.push(` In ${file}:`);
results.push(` ${response.join('\r\n ')}`);
}
});

Expand All @@ -43,5 +46,36 @@ if (results.length > 0) {
console.log(results.join('\r\n'));
process.exitCode = 1;
} else {
console.log('All files successfully validated');
}
console.log('Files successfully validated');
}

// Check if file structure is the same
const validateStructure = async () => {
const isUpdating = args.includes('--update');
const validationFile = 'validate-structure.json';
const newData = await dirToJson('./dist', { sortType: true });
let existingData;

try {
const raw = fs.readFileSync(validationFile, 'utf8');
existingData = JSON.parse(raw);
} catch (err) {
existingData = null;
}


// If no existing data found or is updating, create it
if (!existingData || isUpdating) {
fs.writeFileSync(validationFile, JSON.stringify(newData));
console.log('Created new validation data');
return;
}

if (!_.isEqual(existingData, newData)) {
throw new Error('Structure in dist folder has changed!');
}

console.log('Dist data structure has not changed');
};

validateStructure();
Loading