Skip to content

Commit

Permalink
Feat(variables-scss): Add debug option for exporter #DS-1505
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelklibani committed Oct 8, 2024
1 parent e8b7bea commit 35824e2
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 61 deletions.
8 changes: 8 additions & 0 deletions exporters/variables-scss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ Tokens are sorted alphabetically by origin (Figma) name or by name (Supernova).

The index file contains SCSS forwards of all other outputs.

## Development

To generate the original unformatted data from Supernova, enable the `generateOriginalDataFiles` option in the `config.local.json` file.
Once activated, the original data will be generated alongside other outputs and stored in the `original-data` folder.

- \_original-tokens.json
- \_original-groups.json

[supernova-studio]: https://github.com/Supernova-Studio
[alma-career]: https://github.com/lmc-eu
6 changes: 3 additions & 3 deletions exporters/variables-scss/config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"key": "generateDisclaimer",
"key": "generateOriginalDataFiles",
"type": "boolean",
"default": false,
"title": "Show Generated File Disclaimer",
"description": "When enabled, a disclaimer showing the fact that the file was generated automatically and should not be changed manually will appear in all style styles"
"title": "Generate Original Data Files",
"description": "When enabled, the original data files will be generated in the output directory"
}
]
2 changes: 1 addition & 1 deletion exporters/variables-scss/config.local.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"generateDisclaimer": true
"generateOriginalDataFiles": false
}
2 changes: 1 addition & 1 deletion exporters/variables-scss/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export type ExporterConfiguration = {
* that the file was generated automatically
* and should not be changed manually will appear in all files
*/
generateDisclaimer: boolean;
generateOriginalDataFiles: boolean;
};
54 changes: 27 additions & 27 deletions exporters/variables-scss/generated/exporter.cjs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { safeStringify } from '../safeStringify';

type CircularObject = { [key: string]: CircularObject | unknown };

describe('safeStringify', () => {
it('should stringify a simple object', () => {
const obj = { key: 'value', number: 42 };
const result = safeStringify(obj);

expect(result).toBe(JSON.stringify(obj, null, 2));
});

it('should handle circular references in objects', () => {
const obj: CircularObject = { key: 'value' };
obj.self = obj; // Introduce circular reference

const result = safeStringify(obj);

expect(result).toContain('"self": "CIRCULAR_REFERENCE"');
});

it('should handle nested objects with no circular references', () => {
const obj = { outer: { inner: { key: 'value' } } };
const result = safeStringify(obj);

expect(result).toBe(JSON.stringify(obj, null, 2));
});

it('should handle arrays within objects', () => {
const obj = { list: [1, 2, 3] };
const result = safeStringify(obj);

expect(result).toBe(JSON.stringify(obj, null, 2));
});

it('should handle null values', () => {
const obj = { key: null };
const result = safeStringify(obj);

expect(result).toBe(JSON.stringify(obj, null, 2));
});
});
20 changes: 20 additions & 0 deletions exporters/variables-scss/src/helpers/safeStringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const safeStringify = (obj: object) => {
let cache: string[] | null = [];
const str = JSON.stringify(
obj,
(key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache?.includes(value)) {
return 'CIRCULAR_REFERENCE';
}
cache?.push(value);
}

return value;
},
2,
);
cache = null;

return str;
};
39 changes: 10 additions & 29 deletions exporters/variables-scss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { FileHelper } from '@supernovaio/export-helpers';
import {
AnyOutputFile,
OutputTextFile,
PulsarContext,
RemoteVersionIdentifier,
Supernova,
OutputTextFile,
} from '@supernovaio/sdk-exporters';
import { ExporterConfiguration } from '../config';
import { generateOutputFilesByThemes } from './generators/fileGenerator';
import { safeStringify } from './helpers/safeStringify';

export const exportConfiguration = Pulsar.exportConfig<ExporterConfiguration>();

// https://github.com/Supernova-Studio/exporters/issues/4
// @ts-ignore-next-line
Expand Down Expand Up @@ -46,35 +49,13 @@ Pulsar.export(async (sdk: Supernova, context: PulsarContext): Promise<Array<AnyO
return createTextFile(file.path, file.fileName, file.content);
});

// TODO: Only for debugging purposes, remove for production!
const safeStringify = (obj: object) => {
let cache: string[] | null = [];
const str = JSON.stringify(
obj,
(key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache?.includes(value)) {
return 'CIRCULAR_REFERENCE';
}
cache?.push(value);
}

return value;
},
2,
// Export the original data only if 'generateOriginalDataFiles' is set to true in config.local.json
if (exportConfiguration.generateOriginalDataFiles) {
textFiles.push(
createTextFile('./original-data/', '_original-tokens.json', safeStringify(tokens)),
createTextFile('./original-data/', '_original-groups.json', JSON.stringify(tokenGroups, null, 2)),
);
cache = null;

return str;
};

// TODO: Only for debugging purposes - remove for production!
textFiles.push(
createTextFile('./original-data/', '_original-tokens.json', safeStringify(tokens)),
createTextFile('./original-data/', '_original-groups.json', JSON.stringify(tokenGroups, null, 2)),
);
}

return textFiles;
});

export const exportConfiguration = Pulsar.exportConfig<ExporterConfiguration>();

0 comments on commit 35824e2

Please sign in to comment.