Skip to content

Commit

Permalink
validate structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sikhote committed Aug 28, 2024
1 parent e81fb03 commit b43adbe
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
18 changes: 18 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"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
}
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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}]}]}
45 changes: 38 additions & 7 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ 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 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 +35,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 +45,34 @@ 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 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, create it
if (!existingData) {
fs.writeFileSync(validationFile, JSON.stringify(currentData));
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();

0 comments on commit b43adbe

Please sign in to comment.