Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
feat: brandColors updated to figma variable json
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettbear committed Apr 23, 2024
1 parent c6f7d80 commit e9068a9
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 559 deletions.
65 changes: 65 additions & 0 deletions brandColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fs = require('fs').promises;

/**
* Generate TypeScript types and JavaScript object mapping for brand colors based on a JSON file.
* @param inputFilePath - The path to the JSON file containing color definitions.

Check failure on line 5 in brandColor.js

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Missing JSDoc @param "inputFilePath" type
* @param typesOutputFilePath - The path where the TypeScript types file should be written.

Check failure on line 6 in brandColor.js

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Missing JSDoc @param "typesOutputFilePath" type
* @param colorsOutputFilePath - The path where the JavaScript colors object file should be written.

Check failure on line 7 in brandColor.js

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Missing JSDoc @param "colorsOutputFilePath" type
*/
async function generateBrandColorFiles(
inputFilePath,
typesOutputFilePath,
colorsOutputFilePath,
) {
try {
const data = await fs.readFile(inputFilePath, 'utf8');
const colorData = JSON.parse(data);
let tsOutput = 'export type BrandColor = {\n';
let jsOutput =
"import type { BrandColor } from './brandColor.types';\n\nexport const brandColor: BrandColor = {\n";

Object.entries(colorData).forEach(([colorFamily, shades]) => {
Object.entries(shades).forEach(([shade, details]) => {
// Skip shades with a dash followed by numbers and a percent sign (e.g., "500-10%")
if (!/-\d+%/u.test(shade)) {
const comment = `/** ${colorFamily}/${colorFamily}${shade}: ${details.value} */`;
const tsLine = ` ${colorFamily}${shade}: string;`;
tsOutput += ` ${comment}\n ${tsLine}\n`;

// Add color definition to jsOutput
const jsComment = ` // ${
colorFamily.charAt(0).toUpperCase() + colorFamily.slice(1)
}\n`;
const jsLine = ` ${colorFamily}${shade}: '${details.value}',\n`;
jsOutput += jsComment + jsLine;
}
});
});

tsOutput += '};\n';
jsOutput += '};\n';

// Write the TypeScript types file
await fs.writeFile(typesOutputFilePath, tsOutput);
console.log(
`TypeScript types file has been created successfully at ${typesOutputFilePath}.`,
);

// Write the JavaScript colors file
await fs.writeFile(colorsOutputFilePath, jsOutput);
console.log(
`JavaScript colors file has been created successfully at ${colorsOutputFilePath}.`,
);
} catch (error) {
console.error('Failed to read or write files:', error);
}
}

const inputFilePath = './src/figma/brandColorBrandEvolution.json';
const typesOutputFilePath = './src/js/brandColor/brandColor.types.ts';
const colorsOutputFilePath = './src/js/brandColor/brandColor.ts';
generateBrandColorFiles(
inputFilePath,
typesOutputFilePath,
colorsOutputFilePath,
);
2 changes: 1 addition & 1 deletion src/figma/brandColor.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
"description": ""
}
},
"purpe": {
"purple": {
"100": {
"value": "#efd2ff",
"type": "color",
Expand Down
2 changes: 1 addition & 1 deletion src/figma/brandColorBrandEvolution.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
"description": ""
}
},
"purpe": {
"purple": {
"100": {
"value": "#efd2ff",
"type": "color",
Expand Down
Loading

0 comments on commit e9068a9

Please sign in to comment.