Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add convert to DTCG spec utility WIP #1200

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/rare-toys-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'style-dictionary': minor
---

Add a couple of utilities for converting a regular Style Dictionary tokens object/file(s) to DTCG formatted tokens:

- `convertToDTCG`
- `convertJSONToDTCG`
- `convertZIPToDTCG`

[Documentation of these utilities](https://v4.styledictionary.com/reference/utils/dtcg/)
41 changes: 32 additions & 9 deletions __tests__/__setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname } from 'path-unified';
import { fs } from 'style-dictionary/fs';
import { chaiWtrSnapshot } from '../snapshot-plugin/chai-wtr-snapshot.js';
import { fixDate } from './__helpers.js';
import { writeZIP } from '../lib/utils/convertToDTCG.js';

/**
* We have a bunch of files that we use a mock data for our tests
Expand All @@ -17,24 +18,46 @@ import { fixDate } from './__helpers.js';

fixDate();

function ensureDirectoryExistence(filePath) {
let hasInitializedResolve;
export const hasInitialized = new Promise((resolve) => {
hasInitializedResolve = resolve;
});
// in case of Node env, we can resolve it immediately since we don't do this setup stuff
if (typeof window !== 'object') {
hasInitializedResolve();
}

/**
* @param {string} filePath
*/
function ensureDirectoryExists(filePath) {
const dir = dirname(filePath);
if (fs.existsSync(dir)) {
return true;
}
fs.mkdirSync(dir, { recursive: true });
}

function mirrorFile(file, contents) {
ensureDirectoryExistence(file);
fs.writeFileSync(file, contents, 'utf-8');
/**
* @param {string} file
* @param {string | Record<string, string>} contents
*/
async function mirrorFile(file, contents) {
ensureDirectoryExists(file);
// zip files cannot just be written to FS using utf-8 encoding..
if (file.endsWith('.zip')) {
const zipResult = await writeZIP(contents);
contents = new Uint8Array(await zipResult.arrayBuffer());
}
await fs.promises.writeFile(file, contents);
}

export function setup(filesToMirror) {
/**
* @param {[string, string | Record<string, string>][]} filesToMirror
*/
export async function setup(filesToMirror) {
use(chaiAsPromised);
use(chaiWtrSnapshot);

filesToMirror.forEach(([file, contents]) => {
mirrorFile(file, contents);
});
await Promise.all(filesToMirror.map(([file, contents]) => mirrorFile(file, contents)));
hasInitializedResolve();
}
Binary file added __tests__/__tokens/tokens.zip
Binary file not shown.
Loading