@tokens-studio/sdk
Version:
The official SDK for Tokens Studio
66 lines • 2.86 kB
JavaScript
/* eslint-disable no-console */
import path from 'node:path';
import fs from 'node:fs/promises';
import { convertTokenData } from 'style-dictionary/utils';
import chalk from 'chalk';
import { error, info, success } from '../utils/messages.js';
import { exportTokens } from '../utils/exportTokens.js';
import { createClient } from '../utils/client.js';
import { configFileName } from '../utils/constants.js';
import { fetchSets } from './fetch-sets.js';
import { fetchConfigs } from './fetch-configs.js';
import { writeFilesToFs } from './write-sets-to-fs.js';
import { fetchRelease } from './fetch-release.js';
import { createExportStructure } from '../utils/create-export-structure.js';
import { fetchThemes } from './fetch-themes.js';
export async function pull(args) {
const resolvedConfigPath = path.resolve(args['--config'] ?? '', configFileName);
let config;
try {
config = JSON.parse(await fs.readFile(resolvedConfigPath, 'utf-8'));
}
catch (e) {
error(`Something wrong parsing your config at: ${chalk.cyan(resolvedConfigPath)}`);
throw e;
}
const client = createClient(args, config);
let tokens = [];
let configs = [];
if (config.release) {
const release = await fetchRelease(client, config);
tokens = release.tokens;
configs = release.configs;
}
else {
// TODO: when moved to Clack, parallelize this. Right now it looks weird in the terminal if we do it,
// because the astro cli kit doesn't handle doing multiple async tasks in parallel well
const sets = await fetchSets(client, config);
const themes = await fetchThemes(client, config);
configs = await fetchConfigs(client, config);
tokens = exportTokens({ tokenSets: sets, themes });
}
console.log('\r');
const filteredTokens = tokens.filter((tokenFile) => !['$themes.json', '$metadata.json'].includes(tokenFile.filename) &&
!tokenFile.filename.endsWith('.tsgraph'));
const tokensCount = filteredTokens.reduce((acc, curr) => acc +
convertTokenData(JSON.parse(curr.contents), {
output: 'array',
usesDtcg: true,
}).length, 0);
success(`Found ${filteredTokens.length} sets with ${tokensCount} tokens in total.`);
filteredTokens.forEach((set) => {
info(`${set.filename}`, '', 1);
});
success(`Found ${configs.length} SD configs in total.`);
configs.forEach((config) => {
info(`${config.filename}`, '', 1);
});
console.log('\r');
const files = createExportStructure(tokens, configs);
if (files.length === 0) {
error(`No files found for project with id: ${config.project}${config.release ? ` and release: ${config.release}` : ''}.`);
return;
}
await writeFilesToFs(files, args, config);
}
//# sourceMappingURL=index.js.map