Skip to content

Commit

Permalink
change validate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Viteri committed Sep 6, 2024
1 parent 39a3a0a commit dfa04ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 54 deletions.
4 changes: 0 additions & 4 deletions dist/docsite/js/cdr-tokens.d.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Do not edit directly, this file was auto-generated.
*/

export const CdrColorTextButtonPrimary : string;
export const CdrColorTextButtonPrimaryHover : string;
export const CdrColorTextButtonPrimaryActive : string;
Expand Down
4 changes: 0 additions & 4 deletions dist/rei-dot-com/js/cdr-tokens.d.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* Do not edit directly, this file was auto-generated.
*/

export const CdrColorTextButtonPrimary : string;
export const CdrColorTextButtonPrimaryHover : string;
export const CdrColorTextButtonPrimaryActive : string;
Expand Down
98 changes: 52 additions & 46 deletions validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import glob from 'glob'
import requireJSON5 from 'require-json5'
import _ from 'lodash'
import process from 'process'
import dirToJson from 'dir-to-json';
import fs from 'fs';
import dirToJson from 'dir-to-json'
import fs from 'fs'

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

const addDelimiter = (a, b) => a ? `${a}-${b}` : b
// Utility function to add delimiters
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 = '', toRet = []) => {
const hasValue = _.has(obj, 'value')
Expand All @@ -31,57 +30,64 @@ const validate = (obj, path = '', toRet = []) => {
return toRet
}

// Starts here
files.forEach((file) => {
const response = validate(requireJSON5(file))

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

if (results.length > 0) {
console.log('The following tokens are being skipped:')
console.log(results.join('\r\n'))
process.exitCode = 1
} else {
console.log('All files successfully validated')
}

// Check if file structure is the same
// Validate function for file structure
const validateStructure = async () => {
const isUpdating = args.includes('--update');
const validationFile = 'validate-structure.json';
const newData = await dirToJson('./dist', { sortType: true });
let existingData;
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);
const raw = fs.readFileSync(validationFile, 'utf8')
existingData = JSON.parse(raw)
} catch (err) {
existingData = null;
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;
fs.writeFileSync(validationFile, JSON.stringify(newData))
console.log('Created new validation data')
return
}

if (!_.isEqual(existingData, newData)) {
console.log('Existing data:');
console.log(JSON.stringify(existingData, null, 2));
console.log('New data:');
console.log(JSON.stringify(newData, null, 2));
throw new Error('Structure in dist folder has changed!');
throw new Error('Structure in dist folder has changed!')
}

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

// Main execution flow
const main = async () => {
const files = glob.sync('./tokens/**/*.json5')
const results = []

validateStructure();
// Process each file
files.forEach((file) => {
const response = validate(requireJSON5(file))

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

if (results.length > 0) {
console.log('The following tokens are being skipped:')
console.log(results.join('\r\n'))
process.exitCode = 1
} else {
console.log('All files successfully validated')
}

// Validate structure
try {
await validateStructure()
} catch (error) {
console.error(error.message)
process.exitCode = 1
}
}

export { validate }
main()

0 comments on commit dfa04ff

Please sign in to comment.