From b43adbeaaf44ff8fe41453b4772e7f080e3cb5c5 Mon Sep 17 00:00:00 2001 From: David Sinclair Date: Wed, 28 Aug 2024 14:47:41 -0700 Subject: [PATCH 1/3] validate structure --- .prettierrc.json | 18 +++++++++++++++++ package-lock.json | 16 +++++++++++++++ package.json | 1 + validate-structure.json | 1 + validate.js | 45 ++++++++++++++++++++++++++++++++++------- 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 .prettierrc.json create mode 100644 validate-structure.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 00000000..51878cea --- /dev/null +++ b/.prettierrc.json @@ -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 +} diff --git a/package-lock.json b/package-lock.json index 615a63e0..1081b2cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,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", @@ -219,6 +220,15 @@ "node": ">=0.10" } }, + "node_modules/dir-to-json": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/dir-to-json/-/dir-to-json-1.0.0.tgz", + "integrity": "sha512-yDfgiqPrKf4Qje1CgsFRB1Dl1TjV9U8YVo6ykIR1gbRf4E/7ErmrhP1FbUfSccs6hojSgEkLWd7xO6XY7ioplg==", + "dev": true, + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/dot-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/dot-case/-/dot-case-3.0.4.tgz", @@ -836,6 +846,12 @@ "integrity": "sha512-FqUYQ+8o158GyGTrMFJms9qh3CqTKvAqgqsTnkLI8sKu0028orqBhxNMFkFen0zGyg6epACD32pjVk58ngIErQ==", "dev": true }, + "dir-to-json": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/dir-to-json/-/dir-to-json-1.0.0.tgz", + "integrity": "sha512-yDfgiqPrKf4Qje1CgsFRB1Dl1TjV9U8YVo6ykIR1gbRf4E/7ErmrhP1FbUfSccs6hojSgEkLWd7xO6XY7ioplg==", + "dev": true + }, "dot-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/dot-case/-/dot-case-3.0.4.tgz", diff --git a/package.json b/package.json index d9926109..013a5524 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/validate-structure.json b/validate-structure.json new file mode 100644 index 00000000..df934fa5 --- /dev/null +++ b/validate-structure.json @@ -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"}]}]}]} \ No newline at end of file diff --git a/validate.js b/validate.js index 6e339830..af372887 100644 --- a/validate.js +++ b/validate.js @@ -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) { @@ -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 ')}`); } }); @@ -43,5 +45,34 @@ if (results.length > 0) { console.log(results.join('\r\n')); process.exitCode = 1; } else { - console.log('All files successfully validated'); -} \ No newline at end of file + 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(); From b4c34a20ccf3d520444b2eb4f4c2a8bd75a609d0 Mon Sep 17 00:00:00 2001 From: David Sinclair Date: Wed, 28 Aug 2024 14:56:59 -0700 Subject: [PATCH 2/3] validate structure --- README.md | 4 ++++ dist/docsite/js/cdr-tokens.d.mts | 2 +- dist/rei-dot-com/js/cdr-tokens.d.mts | 2 +- package.json | 2 +- validate.js | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c11eea56..e2bc65ef 100644 --- a/README.md +++ b/README.md @@ -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). + +### 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, the `validate-structure.json` may be deleted, which will be automatically created on the next run. The `validate-structure.json` file should be commited. diff --git a/dist/docsite/js/cdr-tokens.d.mts b/dist/docsite/js/cdr-tokens.d.mts index 7c403729..0079702b 100644 --- a/dist/docsite/js/cdr-tokens.d.mts +++ b/dist/docsite/js/cdr-tokens.d.mts @@ -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; diff --git a/dist/rei-dot-com/js/cdr-tokens.d.mts b/dist/rei-dot-com/js/cdr-tokens.d.mts index e1c2d217..39a820fc 100644 --- a/dist/rei-dot-com/js/cdr-tokens.d.mts +++ b/dist/rei-dot-com/js/cdr-tokens.d.mts @@ -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; diff --git a/package.json b/package.json index 013a5524..7cda2251 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/validate.js b/validate.js index af372887..c84b641d 100644 --- a/validate.js +++ b/validate.js @@ -63,7 +63,7 @@ const validateStructure = async () => { // If no existing data found, create it if (!existingData) { - fs.writeFileSync(validationFile, JSON.stringify(currentData)); + fs.writeFileSync(validationFile, JSON.stringify(newData)); console.log('Created new validation data'); return; } From 401daae82274993d516ebf552a400df901199db9 Mon Sep 17 00:00:00 2001 From: David Sinclair Date: Wed, 28 Aug 2024 15:37:50 -0700 Subject: [PATCH 3/3] validate structure --- .nvmrc | 1 + README.md | 2 +- validate.js | 7 +++++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 .nvmrc diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..d5a15960 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +20.10.0 diff --git a/README.md b/README.md index e2bc65ef..4c61a354 100644 --- a/README.md +++ b/README.md @@ -291,4 +291,4 @@ See [transform docs](https://amzn.github.io/style-dictionary/#/transforms). ### 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, the `validate-structure.json` may be deleted, which will be automatically created on the next run. The `validate-structure.json` file should be commited. +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. diff --git a/validate.js b/validate.js index c84b641d..21455881 100644 --- a/validate.js +++ b/validate.js @@ -5,6 +5,7 @@ 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 = []; @@ -50,6 +51,7 @@ if (results.length > 0) { // 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; @@ -61,8 +63,9 @@ const validateStructure = async () => { existingData = null; } - // If no existing data found, create it - if (!existingData) { + + // 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;