@tokens-studio/sdk
Version:
The official SDK for Tokens Studio
60 lines • 2.17 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 { createClient } from '../utils/client.js';
import { configFileName } from '../utils/constants.js';
import { fetchSets } from './fetch-sets.js';
import { writeFilesToFs } from './write-sets-to-fs.js';
import { fetchRelease } from './fetch-release.js';
export async function pull(args) {
const client = createClient(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 { project, org, release } = config;
let files = [];
if (release) {
const { tokens, configs } = await fetchRelease(client, {
project,
org,
config,
});
files = [
// put token files inside a wrapping tokens folder
...tokens.map(({ name, contents }) => ({
name: name === '$themes.json' ? name : `tokens/${name}`,
contents,
})),
...configs,
];
}
else {
files = await fetchSets(client, config);
console.log('\r');
const tokensCount = files.reduce((acc, curr) => acc +
convertTokenData(JSON.parse(curr.contents), {
output: 'array',
usesDtcg: true,
}).length, 0);
success(`Found ${files.length} sets with ${tokensCount} tokens in total.`);
files.forEach((set) => {
info(`${set.name}`, '', 1);
});
}
console.log('\r');
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