Skip to content

Commit

Permalink
Resolve comments and add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
aweell committed Oct 4, 2024
1 parent 2232ff6 commit 3b7e72a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 95 deletions.
20 changes: 20 additions & 0 deletions tokens/figma/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import dotenv from "dotenv";

dotenv.config({ path: "../../.env" });

import { BRANDS } from "./utils/constants.mjs";

export const brands = {
[BRANDS.MOVISTAR]: "ObNHOLPtrIytjy9BH7M9jW",
[BRANDS.O2_NEW]: "CjvgrHEIycSQ6exznxnFXT",
[BRANDS.VIVO_NEW]: "EApRpjaTyUOwW5VQU2ZqgP",
[BRANDS.TELEFONICA]: "m8srmP3eedfvDaqYnbM6PI",
[BRANDS.BLAU]: "czemeClWRGBI8oF7caNa5m",
[BRANDS.TU]: "19IXMaFqdYeC1IIdTwXBgY",
};

export const MIDDLEWARE_TOKEN =
"w7fBxCsEb8WrMVVuxDnCQd";

export const FIGMA_TOKEN =
process.env.FIGMA_TOKEN;
28 changes: 3 additions & 25 deletions tokens/figma/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { fileURLToPath } from "url";

import { updateSkinFiles } from "./update-skins.mjs";
import { updateMiddleware } from "./update-middleware.mjs";
import { BRANDS } from "./utils/constants.mjs";

import {
extractSkinJsonData,
Expand All @@ -21,6 +20,7 @@ const files = fs.readdirSync(tokensPath);
const jsonFiles = files.filter((file) =>
file.endsWith(".json")
);

const jsonDataForSkin = extractSkinJsonData(
jsonFiles,
tokensPath
Expand All @@ -32,31 +32,9 @@ const jsonDataForMiddleware =
tokensPath
);

const MIDDLEWARE_TOKEN =
process.env.MIDDLEWARE_KEY;

const brands = {
// Remember to sync these with the workflow file
[BRANDS.MOVISTAR]:
process.env.MOVISTAR_FILE_KEY,
[BRANDS.O2_NEW]: process.env.O2_NEW_FILE_KEY,
[BRANDS.VIVO_NEW]:
process.env.VIVO_NEW_FILE_KEY,
[BRANDS.TELEFONICA]:
process.env.TELEFONICA_FILE_KEY,
[BRANDS.BLAU]: process.env.BLAU_FILE_KEY,
[BRANDS.TU]: process.env.TU_FILE_KEY,
};

const brandNames = Object.keys(brands);

async function processAll() {
await updateSkinFiles(jsonDataForSkin, brands);
await updateMiddleware(
jsonDataForMiddleware,
brandNames,
MIDDLEWARE_TOKEN
);
await updateSkinFiles(jsonDataForSkin);
await updateMiddleware(jsonDataForMiddleware);
}

processAll();
91 changes: 38 additions & 53 deletions tokens/figma/update-middleware.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ import {

import formatBrandName from "./utils/format-brand-name.mjs";

import { brands } from "./config.mjs";
import { MIDDLEWARE_TOKEN } from "./config.mjs";

const brandNames = Object.keys(brands);

async function updateModeCollection(
jsonData,
brand,
FILE_KEY
brand
) {
try {
const figmaData = await getFigmaData(
FILE_KEY
MIDDLEWARE_TOKEN
);
const existingVariables =
figmaData.meta.variables;
Expand Down Expand Up @@ -191,7 +195,10 @@ async function updateModeCollection(
}

// Update the variables and modes in Figma
await postFigmaVariables(FILE_KEY, newData);
await postFigmaVariables(
MIDDLEWARE_TOKEN,
newData
);

return newData;
} catch (error) {
Expand All @@ -200,16 +207,12 @@ async function updateModeCollection(
}
}

async function updateBrandCollection(
jsonData,
brands,
FILE_KEY
) {
async function updateBrandCollection(jsonData) {
try {
// Step 1: Fetch the existing data from Figma

const figmaData = await getFigmaData(
FILE_KEY
MIDDLEWARE_TOKEN
);
const existingCollections =
figmaData.meta.variableCollections;
Expand Down Expand Up @@ -261,7 +264,7 @@ async function updateBrandCollection(

// Step 5: Create or update modes based on the brands

const firstBrand = brands[0];
const firstBrand = brandNames[0];

const firstModeResult =
await updateOrCreateModes({
Expand All @@ -276,7 +279,7 @@ async function updateBrandCollection(

newData.variableModes.push(firstModeResult);

brands.slice(1).forEach(async (brand) => {
brandNames.slice(1).forEach(async (brand) => {
const formattedBrand =
formatBrandName(brand);

Expand Down Expand Up @@ -355,7 +358,7 @@ async function updateBrandCollection(
newData.variables.push(variableData);

// Step 8: Update mode values with the correct aliases for each brand
for (const brand of brands) {
for (const brand of brandNames) {
const formattedBrand =
formatBrandName(brand);

Expand All @@ -372,7 +375,7 @@ async function updateBrandCollection(
hasDefaultMode(
COLLECTION_NAMES.SKIN,
existingCollections
) && brand === brands[0]
) && brand === brandNames[0]
? MODE_NAMES.DEFAULT
: formattedBrand,
targetCollectionName:
Expand All @@ -392,7 +395,7 @@ async function updateBrandCollection(
}

// Loop through each brand to process its specific tokens
for (const brand of brands) {
for (const brand of brandNames) {
const nonColorVariables =
getNonColorVariables(jsonData, brand);

Expand Down Expand Up @@ -444,7 +447,7 @@ async function updateBrandCollection(
hasDefaultMode(
collectionName,
existingCollections
) && brand === brands[0]
) && brand === brandNames[0]
? MODE_NAMES.DEFAULT
: formatBrandName(brand),
targetCollectionName:
Expand All @@ -465,7 +468,10 @@ async function updateBrandCollection(

// Step 9: Send the data to update the Brand collection (POST)

await postFigmaVariables(FILE_KEY, newData);
await postFigmaVariables(
MIDDLEWARE_TOKEN,
newData
);

return newData; // Returning newData for debugging
} catch (error) {
Expand All @@ -474,7 +480,7 @@ async function updateBrandCollection(
}
}

async function postCollections(brand, FILE_KEY) {
async function postCollections(brand) {
const collectionNames = [
COLLECTION_NAMES.SKIN,
COLLECTION_NAMES.COLOR_SCHEME,
Expand All @@ -483,10 +489,13 @@ async function postCollections(brand, FILE_KEY) {
try {
const newData = await updateCollections(
collectionNames,
FILE_KEY
MIDDLEWARE_TOKEN
);

await postFigmaVariables(FILE_KEY, newData);
await postFigmaVariables(
MIDDLEWARE_TOKEN,
newData
);
} catch (error) {
console.error(
`Error creating collections for brand ${brand}:`,
Expand All @@ -495,42 +504,18 @@ async function postCollections(brand, FILE_KEY) {
}
}

async function processBrand(
jsonData,
brand,
FILE_KEY
) {
await postCollections(brand, FILE_KEY);
await updateModeCollection(
jsonData,
brand,
FILE_KEY
);
async function processBrand(jsonData, brand) {
await postCollections(brand);
await updateModeCollection(jsonData, brand);
}

async function processAllBrands(
jsonData,
brands,
FILE_KEY
) {
for (const brand of brands) {
await processBrand(jsonData, brand, FILE_KEY);
async function processAllBrands(jsonData) {
for (const brand of brandNames) {
await processBrand(jsonData, brand);
}
}

export async function updateMiddleware(
jsonData,
brands,
FILE_KEY
) {
await processAllBrands(
jsonData,
brands,
FILE_KEY
);
await updateBrandCollection(
jsonData,
brands,
FILE_KEY
);
export async function updateMiddleware(jsonData) {
await processAllBrands(jsonData);
await updateBrandCollection(jsonData);
}
20 changes: 7 additions & 13 deletions tokens/figma/update-skins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {

import { getPaletteVariables } from "./variables.mjs";

import { brands } from "./config.mjs";

const collectionNames = [
COLLECTION_NAMES.PALETTE,
];
Expand Down Expand Up @@ -144,22 +146,14 @@ async function postPalette(

// Process data for a specific brand

async function processBrand(
jsonData,
brand,
FILE_KEY
) {
async function processBrand(jsonData, brand) {
const FILE_KEY = brands[brand];
await postCollections(brand, FILE_KEY);
await postPalette(jsonData, brand, FILE_KEY);
}

export async function updateSkinFiles(
jsonData,
brands
) {
for (const [brand, FILE_KEY] of Object.entries(
brands
)) {
await processBrand(jsonData, brand, FILE_KEY);
export async function updateSkinFiles(jsonData) {
for (const brand of Object.keys(brands)) {
await processBrand(jsonData, brand);
}
}
5 changes: 1 addition & 4 deletions tokens/figma/utils/api-request.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fetch from "node-fetch";
import dotenv from "dotenv";

dotenv.config({ path: "../../.env" });

const FIGMA_TOKEN = process.env.FIGMA_TOKEN;
import { FIGMA_TOKEN } from "../config.mjs";

async function getFigmaData(FILE_KEY) {
const response = await fetch(
Expand Down

0 comments on commit 3b7e72a

Please sign in to comment.