Skip to content

Commit

Permalink
fix: force paths for images & files extracted on Windows to use forwa…
Browse files Browse the repository at this point in the history
…rd-slashes (#84)
  • Loading branch information
zackbrown committed Feb 25, 2020
1 parent 6ecd3b4 commit 6e5a16d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/extractors/generation/src/utils.ts
Expand Up @@ -149,6 +149,13 @@ const newLine = (writer: CodeBlockWriter) => {
writer.newLine();
};

/**
* Ensures that paths with explicit back-slashes are forced into forward-slashes
*/
const windowsPathSanitize = (path: string) => {
return unescape(escape(path).replace(/%5C/g, '/'));
};

/**
* Returns a valid writable object initializer.
*/
Expand Down Expand Up @@ -291,16 +298,17 @@ export const codegenDesignLanguage = async (spec: CodegenDesignLanguage) => {

for (const [name, asset] of assetsMap) {
const assetName = camelCase(name);
const parsedSrc = parse(asset.src);
const sanitizedSrc = windowsPathSanitize(asset.src);
const parsedSrc = parse(sanitizedSrc);
const sanitizedAssetName = quoteInvalidPropertyName(assetName);

files[sanitizedAssetName] = `new File({src: "${asset.src}"})`;
files[sanitizedAssetName] = `new File({src: "${sanitizedSrc}"})`;
[2, 3, 4].forEach((multiplier) => {
const baseName = join(parsedSrc.dir, parsedSrc.name);
const baseName = windowsPathSanitize(join(parsedSrc.dir, parsedSrc.name));
files[quoteInvalidPropertyName(`${assetName}${multiplier}x`)] = `new File({src: "${baseName}@${multiplier}x${parsedSrc.ext}"})`;
});

images[sanitizedAssetName] = `Image.responsive("${asset.src}", ${asset.width}, ${asset.height})`;
images[sanitizedAssetName] = `Image.responsive("${sanitizedSrc}", ${asset.width}, ${asset.height})`;
}

sourceFile.addVariableStatement({
Expand Down

0 comments on commit 6e5a16d

Please sign in to comment.