Skip to content

Commit

Permalink
fixup! 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 7, 2024
1 parent 1bc17e5 commit 8a63bbf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
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;
};
22 changes: 1 addition & 21 deletions exporters/variables-scss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@supernovaio/sdk-exporters';
import { ExporterConfiguration } from '../config';
import { generateOutputFilesByThemes } from './generators/fileGenerator';
import { safeStringify } from './helpers/safeStringify';

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

Expand Down Expand Up @@ -50,27 +51,6 @@ Pulsar.export(async (sdk: Supernova, context: PulsarContext): Promise<Array<AnyO

// Export the original data only if 'generateOriginalDataFiles' is set to true in config.local.json
if (exportConfiguration.generateOriginalDataFiles) {
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;
};

textFiles.push(
createTextFile('./original-data/', '_original-tokens.json', safeStringify(tokens)),
createTextFile('./original-data/', '_original-groups.json', JSON.stringify(tokenGroups, null, 2)),
Expand Down

0 comments on commit 8a63bbf

Please sign in to comment.