Skip to content

Commit

Permalink
perf: convert window path to unix standard
Browse files Browse the repository at this point in the history
  • Loading branch information
filipemeneses committed Sep 9, 2023
1 parent 3f8cd03 commit 963ffad
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/unity-to-json/convertUnityProjectToJson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('convertUnityProjectToJson', () => {

expect(result.version).toEqual('unity-to-json@v2');
expect(result.files[0]).toEqual({
filepath: 'Assets\\Scenes\\SampleScene.unity',
filepath: 'Assets/Scenes/SampleScene.unity',
data: [{ fileId: '1.1', type: '%YAML' }, {
fileId: '1',
type: '29',
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('convertUnityProjectToJson', () => {
}],
});
expect(result.files[1]).toEqual({
filepath: 'Assets\\blasterB.fbx.meta',
filepath: 'Assets/blasterB.fbx.meta',
data: [{
fileId: '2',
type: 'fileFormatVersion:',
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('convertUnityProjectToJson', () => {
}],
});
expect(result.files[2]).toEqual({
filepath: 'Assets\\Scenes\\SampleScene.unity.meta',
filepath: 'Assets/Scenes/SampleScene.unity.meta',
data: [{
fileId: '2',
type: 'fileFormatVersion:',
Expand All @@ -337,7 +337,7 @@ describe('convertUnityProjectToJson', () => {
}],
});

expect(result.files[3].filepath).toEqual('Assets\\blasterB.fbx');
expect(result.files[3].filepath).toEqual('Assets/blasterB.fbx');
expect(result.files[3].data).toStartWith('Z2xURgIAAADsdAAAOBUAAEpTT057CiJhc3Nl');
});
});
5 changes: 3 additions & 2 deletions src/unity-to-json/createUnityProjectToJsonWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import { getUnityProjectGlobs } from './getUnityProjectGlobs';
import { convertUnityProjectToJson } from './convertUnityProjectToJson';
import { loadAndParseFiles } from './loadAndParseFiles';
import { toUnixPath } from './toUnixPath';

export const createUnityProjectToJsonWatcher = async ({
unityProjectRootFolderPath,
Expand Down Expand Up @@ -50,7 +51,7 @@ export const createUnityProjectToJsonWatcher = async ({
});

const onFileUnlink = async (filepath: string) => {
context.files = context.files.filter((f) => filepath.endsWith(f.filepath));
context.files = context.files.filter((f) => toUnixPath(filepath).endsWith(f.filepath));

await onSceneChange(context);
};
Expand Down Expand Up @@ -79,7 +80,7 @@ export const createUnityProjectToJsonWatcher = async ({
filepath,
]);

const localFile = context.files.find((f) => filepath.endsWith(f.filepath));
const localFile = context.files.find((f) => toUnixPath(filepath).endsWith(f.filepath));
localFile.data = file.data;

if (path.basename(filepath) !== sceneFilename) {
Expand Down
3 changes: 2 additions & 1 deletion src/unity-to-json/loadAndParseFiles.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import path from 'path';
import { UnityOrMetaFile } from '../unity/parseUnityFile';
import { readAndParseFile } from './readAndParseFile';
import { toUnixPath } from './toUnixPath';

export const loadAndParseFiles = (
unityProjectRootFolderPath: string,
files: string[],
): Promise<any | UnityOrMetaFile> => Promise.all(
files.map(async (filepath: string) => ({
filepath: path.relative(unityProjectRootFolderPath, filepath),
filepath: toUnixPath(path.relative(unityProjectRootFolderPath, filepath)),
data: await readAndParseFile(filepath),
})),
);
3 changes: 3 additions & 0 deletions src/unity-to-json/toUnixPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const toUnixPath = (path:string) => (
path.replace(/[\\/]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '')
);

0 comments on commit 963ffad

Please sign in to comment.