mirror of https://github.com/CesiumGS/obj2gltf.git
Handle file path errors and better promise handling
This commit is contained in:
parent
72c20eb6ee
commit
965402c535
|
@ -130,4 +130,7 @@ console.time('Total');
|
||||||
convert(objPath, gltfPath, options)
|
convert(objPath, gltfPath, options)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
console.timeEnd('Total');
|
console.timeEnd('Total');
|
||||||
|
})
|
||||||
|
.catch(function(error) {
|
||||||
|
console.log(error.message);
|
||||||
});
|
});
|
||||||
|
|
|
@ -48,6 +48,7 @@ var defaultLogger = function(message) {
|
||||||
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
||||||
*/
|
*/
|
||||||
function convert(objPath, gltfPath, options) {
|
function convert(objPath, gltfPath, options) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
options = defaultValue(options, {});
|
options = defaultValue(options, {});
|
||||||
var binary = defaultValue(options.binary, false);
|
var binary = defaultValue(options.binary, false);
|
||||||
var separate = defaultValue(options.separate, false);
|
var separate = defaultValue(options.separate, false);
|
||||||
|
@ -73,13 +74,22 @@ function convert(objPath, gltfPath, options) {
|
||||||
throw new DeveloperError('gltfPath is required');
|
throw new DeveloperError('gltfPath is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var objExtension = path.extname(objPath).toLowerCase();
|
||||||
|
if (objExtension !== '.obj') {
|
||||||
|
throw new DeveloperError('Invalid obj path "' + objPath + '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = path.extname(gltfPath).toLowerCase();
|
||||||
|
if (extension !== '.gltf' && extension !== '.glb') {
|
||||||
|
throw new DeveloperError('Invalid gltf path "' + gltfPath + '"');
|
||||||
|
}
|
||||||
|
|
||||||
var basePath = path.dirname(gltfPath);
|
var basePath = path.dirname(gltfPath);
|
||||||
var modelName = path.basename(gltfPath, path.extname(gltfPath));
|
var modelName = path.basename(gltfPath, path.extname(gltfPath));
|
||||||
var extension = path.extname(gltfPath);
|
|
||||||
if (extension === '.glb') {
|
if (extension === '.glb') {
|
||||||
binary = true;
|
binary = true;
|
||||||
if (bypassPipeline) {
|
if (bypassPipeline) {
|
||||||
options.logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
||||||
extension = '.gltf';
|
extension = '.gltf';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,6 +130,9 @@ function convert(objPath, gltfPath, options) {
|
||||||
} else {
|
} else {
|
||||||
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
|
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
var Cesium = require('cesium');
|
||||||
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var convert = require('../../lib/convert');
|
var convert = require('../../lib/convert');
|
||||||
var writeUris = require('../../lib/writeUris');
|
var writeUris = require('../../lib/writeUris');
|
||||||
|
|
||||||
|
var DeveloperError = Cesium.DeveloperError;
|
||||||
|
|
||||||
var objPath = 'specs/data/box-textured/box-textured.obj';
|
var objPath = 'specs/data/box-textured/box-textured.obj';
|
||||||
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
||||||
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
||||||
|
var objPathInvalid = 'invalid/';
|
||||||
|
var gltfPathInvalid = 'invalid/model.invalid';
|
||||||
|
var objPathNonExistent = 'specs/data/non-existent.obj';
|
||||||
|
var gltfPathNonExistent = 'specs/data/non-existent.gltf';
|
||||||
|
|
||||||
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
||||||
|
|
||||||
|
@ -116,7 +123,7 @@ describe('convert', function() {
|
||||||
}), done).toResolve();
|
}), done).toResolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Uses a custom logger', function(done) {
|
it('uses a custom logger', function(done) {
|
||||||
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
||||||
var logCount = 0;
|
var logCount = 0;
|
||||||
var options = {
|
var options = {
|
||||||
|
@ -131,15 +138,23 @@ describe('convert', function() {
|
||||||
}), done).toResolve();
|
}), done).toResolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if objPath is undefined', function() {
|
it('rejects if objPath is undefined', function(done) {
|
||||||
expect(function() {
|
expect(convert(undefined, gltfPath), done).toRejectWith(DeveloperError);
|
||||||
convert(undefined, gltfPath);
|
|
||||||
}).toThrowDeveloperError();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if gltfPath is undefined', function() {
|
it('rejects if gltfPath is undefined', function(done) {
|
||||||
expect(function() {
|
expect(convert(objPath, undefined), done).toRejectWith(DeveloperError);
|
||||||
convert(objPath, undefined);
|
});
|
||||||
}).toThrowDeveloperError();
|
|
||||||
|
it('rejects if obj path is invalid', function(done) {
|
||||||
|
expect(convert(objPathInvalid, gltfPath), done).toRejectWith(DeveloperError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if gltf path is invalid', function(done) {
|
||||||
|
expect(convert(objPath, gltfPathInvalid), done).toRejectWith(DeveloperError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if obj path does not exist', function(done) {
|
||||||
|
expect(convert(objPathNonExistent, gltfPath), done).toRejectWith(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue