Skip to content

Commit

Permalink
Refactor throwing exceptions. Added writeGltfSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaStorm committed Jun 29, 2016
1 parent 24f1e79 commit eacb0de
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
26 changes: 8 additions & 18 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var path = require('path');
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;
var gltfPipeline = require('../lib/gltfPipeline');
var processFileToDisk = gltfPipeline.processFileToDisk;

Expand All @@ -22,30 +21,21 @@ if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) {
}

var gltfPath = defaultValue(argv._[0], defaultValue(argv.i, argv.input));
var fileExtension = path.extname(gltfPath);
var fileName = path.basename(gltfPath, fileExtension);
var filePath = path.dirname(gltfPath);

var outputPath = defaultValue(argv._[1], defaultValue(argv.o, argv.output));
var outputExtension = path.extname(outputPath);
var binary = defaultValue(defaultValue(argv.b, argv.binary), false);
var separate = defaultValue(defaultValue(argv.s, argv.separate), false);
var separateImage = defaultValue(defaultValue(argv.t, argv.separateImage), false);
var quantize = defaultValue(defaultValue(argv.q, argv.quantize), false);

if (!defined(gltfPath)) {
throw new DeveloperError('Input path is undefined.');
}

if (fileExtension !== '.glb' && fileExtension !== '.gltf') {
throw new DeveloperError('Invalid glTF file.');
}

if (outputExtension !== '.gltf' && outputExtension !== '.glb' || binary && outputExtension !== '.glb') {
throw new DeveloperError('Invalid output path extension.');
}

if (!defined(outputPath)) {
var fileExtension;
if (binary) {
fileExtension = '.glb';
} else {
fileExtension = path.extname(gltfPath);
}
var fileName = path.basename(gltfPath, fileExtension);
var filePath = path.dirname(gltfPath);
// Default output. For example, path/asset.gltf becomes path/asset-optimized.gltf
outputPath = path.join(filePath, fileName + '-optimized' + fileExtension);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/writeBinaryGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var getBinaryGltf = require('./getBinaryGltf');
var Cesium = require('cesium');
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;

module.exports = writeBinaryGltf;

function writeBinaryGltf(gltf, outputPath, createDirectory, callback) {
// Correct output path extension if necessary
if (!defined(outputPath)) {
throw new DeveloperError('Output path is undefined.');
}

var outputExtension = path.extname(outputPath);
if (outputExtension !== '.glb') {
outputPath = path.join(path.dirname(outputPath), path.basename(outputPath, outputExtension) + '.glb');
throw new DeveloperError('Invalid output path extension.');
}
// Create the output directory if specified
if (createDirectory) {
Expand Down
10 changes: 8 additions & 2 deletions lib/writeGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ var async = require('async');
var mkdirp = require('mkdirp');
var writeSource = require('./writeSource');
var removePipelineExtras = require('./removePipelineExtras');
var Cesium = require('cesium');
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;

module.exports = writeGltf;

function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callback) {
// Correct output path extension if necessary
if (!defined(outputPath)) {
throw new DeveloperError('Output path is undefined.');
}

var outputExtension = path.extname(outputPath);
if (outputExtension !== '.gltf') {
outputPath = path.join(path.dirname(outputPath), path.basename(outputPath, outputExtension) + '.gltf');
throw new DeveloperError('Invalid output path extension.');
}
// Create the output directory if specified
if (createDirectory) {
Expand Down
13 changes: 13 additions & 0 deletions specs/lib/writeBinaryGltfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var bufferPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.bin'
var imagePath = './specs/data/boxTexturedUnoptimized/Cesium_Logo_Flat_Binary.png';
var fragmentShaderPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest0FS_Binary.glsl';
var vertexShaderPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest0VS_Binary.glsl';
var invalidPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf';

describe('writeBinaryGltf', function() {
var testData = {
Expand Down Expand Up @@ -90,4 +91,16 @@ describe('writeBinaryGltf', function() {
expect(bufferEqual(binaryBody, body)).toBe(true);
})
});

it('throws an invalid output path error', function() {
expect(function() {
writeBinaryGltf(clone(testData.gltf), undefined, true);
}).toThrowError('Output path is undefined.');
});

it('throws an invalid output extension error', function() {
expect(function() {
writeBinaryGltf(clone(testData.gltf), invalidPath, true);
}).toThrowError('Invalid output path extension.');
});
});
41 changes: 41 additions & 0 deletions specs/lib/writeGltfSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
var fs = require('fs');
var path = require('path');
var writeGltf = require('../../lib/writeGltf');
var readGltf = require('../../lib/readGltf');

var gltfPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest_BinaryInput.gltf';
var outputGltfPath = './output/CesiumTexturedBoxTest.gltf';
var invalidPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.exe';

describe('writeGltf', function() {
it('will write a file to the correct directory', function(done) {
var spy = spyOn(fs, 'writeFile').and.callFake(function(file, data, callback) {
callback();
});

writeGltf(gltfPath, outputGltfPath, true, true, false, function() {
expect(path.normalize(spy.calls.first().args[0])).toEqual(path.normalize(outputGltfPath));
});
done();
});

it('throws an invalid output path error', function() {
var options = {};
readGltf(gltfPath, options, function(gltf) {
expect(function() {
writeGltf(gltf, undefined, true, true, true);
}).toThrowError('Output path is undefined.');
});
});

it('throws an invalid output extension error', function() {
var options = {};
readGltf(gltfPath, options, function(gltf) {
expect(function() {
writeGltf(gltf, invalidPath, true, true, true);
}).toThrowError('Invalid output path extension.');
});
});

});

0 comments on commit eacb0de

Please sign in to comment.