Skip to content

Commit

Permalink
feat(design): add support of URL as token file input
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Jan 28, 2025
1 parent 5bf474a commit 4bc71f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ describe('Design Token Parser', () => {
});

describe('parseDesignTokenFile', () => {
afterEach(() => jest.resetAllMocks());

test('should read the file according to the reader', async () => {
const readFile = jest.fn().mockResolvedValue('{"test": { "$value": "#000", "$type": "color" }}');
const parseDesignToken = jest.spyOn(parser, 'parseDesignToken').mockImplementation(() => (new Map()));
Expand All @@ -266,6 +268,37 @@ describe('Design Token Parser', () => {
expect(parseDesignToken).toHaveBeenCalledWith({ context: { basePath: '.' }, document: { test: { $value: '#000', $type: 'color' } } });
});

test('should read file from URL', async () => {
jest.spyOn(global, 'fetch').mockImplementation(
jest.fn(
() => Promise.resolve({
text: () => Promise.resolve('{"test": { "$value": "#000", "$type": "color" }}')
})
) as jest.Mock
);
const parseDesignToken = jest.spyOn(parser, 'parseDesignToken').mockImplementation(() => (new Map()));
const url = 'https://www.google.com';
const result = await parser.parseDesignTokenFile(url);

expect(result.size).toBe(0);
expect(parseDesignToken).toHaveBeenCalledTimes(1);
expect(global.fetch).toHaveBeenCalledTimes(1);
});

test('should read file from FS', async () => {
const readFile = jest.fn().mockResolvedValue('{"test": { "$value": "#000", "$type": "color" }}');
jest.mock('node:fs/promises', () => ({
readFile
}));
const parseDesignToken = jest.spyOn(parser, 'parseDesignToken').mockImplementation(() => (new Map()));
const file = '/the/file.json';
const result = await parser.parseDesignTokenFile(file);

expect(result.size).toBe(0);
expect(parseDesignToken).toHaveBeenCalledTimes(1);
expect(readFile).toHaveBeenCalledTimes(1);
});

test('should propagate context object', async () => {
const readFile = jest.fn().mockResolvedValue('{"test": { "$value": "#000", "$type": "color" }}');
const parseDesignToken = jest.spyOn(parser, 'parseDesignToken').mockImplementation(() => (new Map()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,17 @@ interface ParseDesignTokenFileOptions {
* @param options
*/
export const parseDesignTokenFile = async (specificationFilePath: string, options?: ParseDesignTokenFileOptions) => {
const readFile = options?.readFile || (async (filePath: string) => (await import('node:fs/promises')).readFile(filePath, { encoding: 'utf8' }));
let isUrl = false;
try {
const url = new URL(specificationFilePath);
isUrl = url.protocol.startsWith('http');
} catch {
// not an URL
}
const readFile = options?.readFile || (isUrl
? async (filePath: string) => (await fetch(filePath)).text()
: async (filePath: string) => (await import('node:fs/promises')).readFile(filePath, { encoding: 'utf8' })
);
const context = {
basePath: dirname(specificationFilePath),
...options?.specificationContext
Expand Down

0 comments on commit 4bc71f5

Please sign in to comment.