@@ -18,7 +18,7 @@ const binaries = {
18
18
/**
19
19
* Converts an FBX to a GTLF or GLB file.
20
20
* @param string srcFile path to the source file.
21
- * @param string destFile path to the destination file.
21
+ * @param string destFile path to the destination file or destination path .
22
22
* This must end in `.glb` or `.gltf` (case matters).
23
23
* @param string[] [opts] options to pass to the converter tool.
24
24
* @return Promise<string> a promise that yields the full path to the converted
@@ -33,19 +33,31 @@ function convert(srcFile, destFile, opts = []) {
33
33
throw new Error ( `Unsupported OS: ${ os . type ( ) } ` ) ;
34
34
}
35
35
36
- let destExt ;
37
- if ( destFile . endsWith ( '.glb' ) ) {
38
- destExt = '.glb' ;
39
- opts . includes ( '--binary' ) || opts . push ( '--binary' ) ;
40
- } else if ( destFile . endsWith ( '.gltf' ) ) {
41
- destExt = '.gltf' ;
42
- } else {
36
+ let destExt = path . extname ( destFile ) . toLowerCase ( ) ;
37
+
38
+ if ( ! destExt ) {
39
+ destExt = '.gltf'
40
+
41
+ const srcFilename = path . basename ( srcFile , path . extname ( srcFile ) )
42
+ destFile = path . join ( destFile , srcFilename + destExt )
43
+ }
44
+
45
+ if ( destExt !== '.glb' && destExt !== '.gltf' ) {
43
46
throw new Error ( `Unsupported file extension: ${ destFile } ` ) ;
44
47
}
45
48
49
+ const binary = opts . includes ( '--binary' ) || opts . includes ( '-b' ) ;
50
+
51
+ if ( binary && destExt !== '.glb' ) {
52
+ destExt = '.glb' ;
53
+ } else if ( ! binary && destExt === 'glb' ) {
54
+ opts . push ( '--binary' ) ;
55
+ }
56
+
46
57
let srcPath = fs . realpathSync ( srcFile ) ;
47
58
let destDir = fs . realpathSync ( path . dirname ( destFile ) ) ;
48
- let destPath = path . join ( destDir , path . basename ( destFile , destExt ) ) ;
59
+ let destFilename = path . basename ( destFile , path . extname ( destFile ) ) + destExt ;
60
+ let destPath = path . join ( destDir , destFilename ) ;
49
61
50
62
let args = opts . slice ( 0 ) ;
51
63
args . push ( '--input' , srcPath , '--output' , destPath ) ;
@@ -72,7 +84,7 @@ function convert(srcFile, destFile, opts = []) {
72
84
reject ( new Error ( `Converter output:\n` +
73
85
( output . length ? output : "<none>" ) ) ) ;
74
86
} else {
75
- resolve ( destPath + destExt ) ;
87
+ resolve ( destPath ) ;
76
88
}
77
89
} ) ;
78
90
0 commit comments