Skip to content

Commit

Permalink
fix: resolved filepath, async parser await (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jun 28, 2024
1 parent a265057 commit 4e10493
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-countries-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix parsers async support, use resolved filePath instead of raw.
21 changes: 10 additions & 11 deletions lib/utils/combineJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,31 @@ export default async function combineJSON(

for (let i = 0; i < files.length; i++) {
const filePath = files[i];
const resolvedPath = resolve(filePath);
let file_content = null;
try {
// Iterate over custom parsers, if the file path matches the parser's
// pattern regex, use it's parse function to generate the object
parsers.forEach(({ pattern, parse }) => {
for (const { pattern, parse } of parsers) {
if (filePath.match(pattern)) {
file_content = parse({
contents: /** @type {string} */ (fs.readFileSync(resolve(filePath), 'utf-8')),
filePath,
file_content = await parse({
contents: /** @type {string} */ (fs.readFileSync(resolvedPath, 'utf-8')),
filePath: resolvedPath,
});
}
});
}

// If there is no file_content then no custom parser ran on that file
if (!file_content) {
if (['.js', '.mjs'].includes(extname(filePath))) {
let _filePath = resolve(filePath);
let resolvedPath = resolve(filePath);
// eslint-disable-next-line no-undef
if (typeof window !== 'object' && process?.platform === 'win32') {
// Windows FS compatibility. If in browser, we use an FS shim which doesn't require this Windows workaround
_filePath = new URL(`file:///${_filePath}`).href;
resolvedPath = new URL(`file:///${resolvedPath}`).href;
}
file_content = (await import(/* webpackIgnore: true */ _filePath)).default;
file_content = (await import(/* webpackIgnore: true */ resolvedPath)).default;
} else {
file_content = JSON5.parse(
/** @type {string} */ (fs.readFileSync(resolve(filePath), 'utf-8')),
/** @type {string} */ (fs.readFileSync(resolvedPath, 'utf-8')),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e10493

Please sign in to comment.